|
22 | 22 |
|
23 | 23 | """Unit tests for 'adabot/lib/common_funcs.py'""" |
24 | 24 |
|
| 25 | +import datetime |
25 | 26 | import re |
26 | 27 |
|
27 | 28 | import pytest # pylint: disable=unused-import |
| 29 | +import requests |
| 30 | + |
28 | 31 | from adabot.lib import common_funcs |
| 32 | +from adabot import github_requests |
29 | 33 |
|
30 | 34 |
|
31 | 35 | def test_list_repos(): |
@@ -88,3 +92,63 @@ def test_get_docs_link(links, tmp_path): |
88 | 92 | readme.write_text(links["content"]) |
89 | 93 |
|
90 | 94 | assert links["expects"] == common_funcs.get_docs_link(tmp_path, mock_submod_path) |
| 95 | + |
| 96 | + |
| 97 | +def published_date(subtract_days=0): |
| 98 | + """Utility to return a formatted date""" |
| 99 | + pub_date = datetime.datetime.today() |
| 100 | + pub_date = pub_date - datetime.timedelta(days=subtract_days) |
| 101 | + return datetime.datetime.strftime(pub_date, "%Y-%m-%dT%H:%M:%SZ") |
| 102 | + |
| 103 | + |
| 104 | +mock_repo_results = [ |
| 105 | + { |
| 106 | + "id": "new library", |
| 107 | + "name": "new_library", |
| 108 | + "latest": f'{{"published_at":"{published_date()}"}}', |
| 109 | + "releases": f'[{{"published_at":"{published_date()}"}}]', |
| 110 | + "expects": "new", |
| 111 | + }, |
| 112 | + { |
| 113 | + "id": "updated library", |
| 114 | + "name": "updated_library", |
| 115 | + "latest": f'{{"published_at":"{published_date(subtract_days=6)}"}}', |
| 116 | + "releases": ( |
| 117 | + "[" |
| 118 | + f'{{"published_at":"{published_date(subtract_days=7)}"}},' |
| 119 | + f'{{"published_at":"{published_date(subtract_days=10)}"}}' |
| 120 | + "]" |
| 121 | + ), |
| 122 | + "expects": "updated", |
| 123 | + }, |
| 124 | + { |
| 125 | + "id": "non-updated library", |
| 126 | + "name": "non_updated_library", |
| 127 | + "latest": f'{{"published_at":"{published_date(subtract_days=16)}"}}', |
| 128 | + "releases": f'[{{"published_at":"{published_date(subtract_days=16)}"}}]', |
| 129 | + "expects": None, |
| 130 | + }, |
| 131 | +] |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize( |
| 135 | + "repos", mock_repo_results, ids=[repo["id"] for repo in mock_repo_results] |
| 136 | +) |
| 137 | +# pylint: disable=protected-access |
| 138 | +def test_is_new_or_updated(monkeypatch, repos): |
| 139 | + """Test 'is_new_or_updated'""" |
| 140 | + |
| 141 | + def mock_github_get(url): |
| 142 | + """Mock 'github_requests.get()' for testing""" |
| 143 | + mock_repo_key = url.split("/")[-1] |
| 144 | + |
| 145 | + result = requests.Response() |
| 146 | + result.status_code = 200 |
| 147 | + result.encoding = "utf-8" |
| 148 | + result._content = repos[mock_repo_key].encode() |
| 149 | + |
| 150 | + return result |
| 151 | + |
| 152 | + monkeypatch.setattr(github_requests, "get", mock_github_get) |
| 153 | + |
| 154 | + assert repos["expects"] == common_funcs.is_new_or_updated(repos) |
0 commit comments