1818This module provides supporting test fixtures for the unit test
1919"""
2020from typing import Optional
21+ from datetime import timedelta
2122
2223import pytest
2324from faker import Faker
24- from faker .providers import company
25+ from faker .providers import company , lorem , date_time
2526
2627from 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
4272class 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-
0 commit comments