Skip to content

Commit 1c8305f

Browse files
committed
Added testing for the event retrieval
1 parent 9dfd314 commit 1c8305f

File tree

10 files changed

+135
-23
lines changed

10 files changed

+135
-23
lines changed

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ dev = [
5353
"pylint",
5454
"coverage",
5555
"types-requests",
56-
"pytest"
56+
"pytest",
5757
]
5858
unit_tests = [
5959
"pytest",
6060
"pytest-mock",
61-
"requests-mock"
61+
"requests-mock",
62+
"Faker"
6263
]
6364
integration_tests = [
6465
"pytest",

src/pytito/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
1818
Variables that describes the Package
1919
"""
20-
__version__ = "0.0.2"
20+
__version__ = "0.0.3"

src/pytito/admin/_base_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ def _populate_json(self) -> None:
7979

8080
def _get_response(self, endpoint: str) -> dict[str, Any]:
8181

82-
full_end_point = self._end_point + '/' + endpoint
82+
if endpoint == '':
83+
full_end_point = self._end_point
84+
else:
85+
full_end_point = self._end_point + '/' + endpoint
8386

8487
response = requests.get(
8588
url=full_end_point,

src/pytito/admin/account.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ class Account(AdminAPIBase):
3030
One of the accounts available through the Tito IO AdminAPI
3131
"""
3232

33-
def __init__(self, account_slug:str, json_content:Optional[dict[str, Any]]=None):
34-
super().__init__(json_content=json_content, allow_automatic_json_retrieval=True)
33+
def __init__(self, account_slug: str, json_content: Optional[dict[str, Any]] = None,
34+
api_key: Optional[str] = None):
35+
super().__init__(json_content=json_content,
36+
allow_automatic_json_retrieval=True,
37+
api_key=api_key)
3538
self.__account_slug = account_slug
39+
self.__api_key_internal = api_key
3640

3741
@property
3842
def _end_point(self) -> str:
@@ -51,6 +55,7 @@ def __event_getter(self, end_point: str) -> dict[str, Event]:
5155
raise RuntimeError('Account Slug inconsistency')
5256
slug = event['slug']
5357
return_dict[slug] = Event(event_slug=slug, account_slug=self.__account_slug,
58+
api_key=self.__api_key_internal,
5459
json_content=event)
5560
return return_dict
5661

src/pytito/admin/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AdminAPI(AdminAPIBase):
3232
def __init__(self, api_key:Optional[str]=None) -> None:
3333
super().__init__(json_content=None, api_key=api_key)
3434
self._populate_json()
35-
self.accounts = {account_slug:Account(account_slug=account_slug)
35+
self.accounts = {account_slug:Account(account_slug=account_slug, api_key=api_key)
3636
for account_slug in self.__account_slugs}
3737

3838
@property

src/pytito/admin/event.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ class Event(AdminAPIBase):
3131
"""
3232

3333
def __init__(self, account_slug:str, event_slug:str,
34-
json_content:Optional[dict[str, Any]]=None) -> None:
35-
super().__init__(json_content=json_content)
34+
json_content:Optional[dict[str, Any]]=None,
35+
api_key: Optional[str] = None) -> None:
36+
super().__init__(json_content=json_content, api_key=api_key)
3637
self.__account_slug = account_slug
3738
self.__event_slug = event_slug
3839

tests/integration_tests/conftest.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1+
"""
2+
pytito is a python wrapper for the tito.io API
3+
Copyright (C) 2024
14
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
This module provides supporting test fixtures for the integration tests
19+
"""
220
import pytest
321
from pytito import AdminAPI
422

@@ -18,4 +36,3 @@ def pytito_account_implementation(admin_api):
1836
A test fixture that provides an mocked AdminAPI with mocked data
1937
"""
2038
yield admin_api.accounts['pytito']
21-

tests/integration_tests/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ def test_pytito_connection(pytito_account):
3838
correctly
3939
"""
4040
assert isinstance(pytito_account, Account)
41-
assert pytito_account.name == 'pytito'
41+
assert pytito_account.name == 'pytito'

tests/unit_tests/conftest.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
This module provides supporting test fixtures for the unit test
1919
"""
2020
from typing import Optional
21+
from datetime import timedelta
2122

2223
import pytest
2324
from faker import Faker
24-
from faker.providers import company
25+
from faker.providers import company, lorem, date_time
2526

2627
from pytito import AdminAPI
2728

@@ -39,24 +40,66 @@ def mocked_environment_api_key_implementation(mocker):
3940
yield key
4041

4142

43+
# pylint:disable-next=too-few-public-methods
44+
class Event:
45+
"""
46+
Event within the data model of the mocked data
47+
"""
48+
faker: Optional[Faker] = None
49+
50+
def __init__(self, date_range_start, date_range_end):
51+
if self.faker is None:
52+
self.faker = Faker()
53+
self.faker.add_provider(lorem)
54+
self.faker.add_provider(date_time)
55+
56+
self.title = ' '.join(self.faker.words(3))
57+
self.description = self.faker.paragraph(nb_sentences=3,
58+
variable_nb_sentences=True)
59+
self.start_at = self.faker.date_time_between_dates(
60+
datetime_start=date_range_start,
61+
datetime_end=date_range_end).astimezone()
62+
63+
@property
64+
def slug(self) -> str:
65+
"""
66+
slug used to form the end_point of the api
67+
"""
68+
return self.title.replace(' ', '-')
69+
70+
71+
# pylint:disable-next=too-few-public-methods
4272
class Account:
43-
FAKER:Optional[Faker] = None
73+
"""
74+
Account with in the data model of the mocked data
75+
"""
76+
faker: Optional[Faker] = None
4477

4578
def __init__(self):
46-
if self.FAKER is None:
47-
self.FAKER = Faker()
48-
self.FAKER.add_provider(company)
79+
if self.faker is None:
80+
self.faker = Faker()
81+
self.faker.add_provider(company)
4982

50-
self.name = self.FAKER.bs()
51-
self.description = self.FAKER.catch_phrase()
83+
self.name = self.faker.bs()
84+
self.description = self.faker.catch_phrase()
85+
86+
# future events
87+
self.events: list[Event] = [Event(date_range_start=timedelta(days=1),
88+
date_range_end=timedelta(days=365)) for _ in range(5)]
5289

5390
@property
5491
def slug(self) -> str:
92+
"""
93+
slug used to form the end_point of the api
94+
"""
5595
return self.name.replace(' ', '-')
5696

5797

5898
@pytest.fixture(scope='function', name='mocked_data')
59-
def mocked_data_implementation(requests_mock):
99+
def mocked_data_implementation():
100+
"""
101+
Test fixture to generate a set of mocked data for the use in various tests
102+
"""
60103
yield [Account() for _ in range(2)]
61104

62105

@@ -65,12 +108,22 @@ def mocked_admin_api_implementation(requests_mock, mocked_data):
65108
"""
66109
A test fixture that provides an mocked AdminAPI with mocked data
67110
"""
111+
112+
# pylint:disable-next=unused-argument
68113
def hello_json_content(request, context):
69-
return {'accounts':[item.slug for item in mocked_data] }
114+
return {'accounts': [item.slug for item in mocked_data]}
70115

71116
requests_mock.get("https://api.tito.io/v3/hello", status_code=200,
72117
json=hello_json_content)
118+
for account in mocked_data:
119+
requests_mock.get(f"https://api.tito.io/v3/{account.slug}", status_code=200,
120+
json={'account': {'name': account.name, 'slug': account.slug}})
121+
requests_mock.get(f"https://api.tito.io/v3/{account.slug}/events", status_code=200,
122+
json={'events': [
123+
{'title': event.title,
124+
'slug': event.slug,
125+
'start_at': event.start_at.isoformat(timespec='milliseconds'),
126+
'account_slug': account.slug}
127+
for event in account.events]})
73128

74129
yield AdminAPI(api_key='fake_api_key')
75-
76-

tests/unit_tests/test_account.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,43 @@
1919
"""
2020
from pytito.admin import Account
2121

22+
2223
def test_accounts(mocked_data, mocked_admin_api):
2324
"""
2425
Check the accounts are instantiated correctly
2526
"""
26-
for data_model_account, (admin_api_account_slug, admin_api_account) in zip(mocked_data, mocked_admin_api.accounts.items()):
27+
for data_model_account, (admin_api_account_slug, admin_api_account) in \
28+
zip(mocked_data, mocked_admin_api.accounts.items()):
2729
assert isinstance(admin_api_account, Account)
2830
assert data_model_account.slug == admin_api_account_slug
2931

32+
33+
def test_account_attributes(mocked_data, mocked_admin_api):
34+
"""
35+
check that the data is properly populated with in the account when requested
36+
"""
37+
for data_model_account, admin_api_account in \
38+
zip(mocked_data, mocked_admin_api.accounts.values()):
39+
assert data_model_account.name == admin_api_account.name
40+
41+
42+
def test_account_events(mocked_data, mocked_admin_api):
43+
"""
44+
test that the list of upcoming events is served up correctly
45+
"""
46+
for data_model_account, admin_api_account in \
47+
zip(mocked_data, mocked_admin_api.accounts.values()):
48+
for data_model_event, (event_slug, event) in \
49+
zip(data_model_account.events, admin_api_account.events.items()):
50+
assert data_model_event.title == event.title
51+
assert data_model_event.slug == event_slug
52+
53+
54+
def test_account_next_event(mocked_data, mocked_admin_api):
55+
"""
56+
Test that the next event is report correctly
57+
"""
58+
for data_model_account, admin_api_account in \
59+
zip(mocked_data, mocked_admin_api.accounts.values()):
60+
data_model_next_event = sorted(data_model_account.events, key=lambda item: item.start_at)[0]
61+
assert admin_api_account.next_event.title == data_model_next_event.title

0 commit comments

Comments
 (0)