Skip to content

Commit 8c40dfb

Browse files
committed
add tests for 'common_funcs.is_new_or_updated'
1 parent 3a48294 commit 8c40dfb

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/unit/test_common_funcs.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222

2323
"""Unit tests for 'adabot/lib/common_funcs.py'"""
2424

25+
import datetime
2526
import re
2627

2728
import pytest # pylint: disable=unused-import
29+
import requests
30+
2831
from adabot.lib import common_funcs
32+
from adabot import github_requests
2933

3034

3135
def test_list_repos():
@@ -88,3 +92,63 @@ def test_get_docs_link(links, tmp_path):
8892
readme.write_text(links["content"])
8993

9094
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

Comments
 (0)