11import pytest
22import uuid
3- from faker import Faker
3+
4+ import sqlalchemy
45
56import time_entries ._domain as domain
67import time_entries ._infrastructure as infrastructure
7- import sqlalchemy
88
99@pytest .fixture (name = 'create_fake_database' )
1010def _create_fake_database (with_data : bool ) -> domain .ActivitiesDao :
1111 db_fake = infrastructure .DB ('sqlite:///:memory:' )
1212 demo_data = [
1313 {
14- 'id' : Faker (). uuid4 ( ),
15- 'name' : Faker (). user_name () ,
16- 'description ' : Faker (). sentence () ,
17- 'deleted ' : Faker (). uuid4 () ,
14+ 'id' : uuid . UUID ( 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ),
15+ 'name' : 'Activity Demo create' ,
16+ 'tenant_id ' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
17+ 'description ' : 'test demo create an new activity' ,
1818 'status' : 'active' ,
19- 'tenant_id ' : Faker (). uuid4 () ,
19+ 'deleted ' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
2020 },
2121 {
22- 'id' : Faker (). uuid4 ( ),
23- 'name' : Faker (). user_name () ,
24- 'description ' : Faker (). sentence () ,
25- 'deleted ' : Faker (). uuid4 () ,
22+ 'id' : uuid . UUID ( 'c61a4a49-3364-49a3-a7f7-0c5f2d15072b' ),
23+ 'name' : 'Activity Demo create' ,
24+ 'tenant_id ' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
25+ 'description ' : 'test demo create an new activity' ,
2626 'status' : 'active' ,
27- 'tenant_id ' : Faker (). uuid4 () ,
27+ 'deleted ' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
2828 },
2929 ]
3030 if with_data :
@@ -39,99 +39,130 @@ def _create_fake_database(with_data: bool) -> domain.ActivitiesDao:
3939 )
4040 query = activity .insert ()
4141 db_fake .get_session ().execute (query , demo_data )
42- return db_fake , demo_data
42+ return db_fake
4343
4444
4545@pytest .mark .parametrize ('with_data' ,[False ])
4646def test__create_activity__returns_a_activity_dto__when_saves_correctly_with_sql_database (create_fake_database ):
47- database , activity_data = create_fake_database
48- dao = infrastructure .ActivitiesSQLDao (database )
49- results = dao .create_activity (activity_data [0 ])
50- assert results .id != None
51-
47+ activity = {
48+ 'name' : 'Activity Demo create' ,
49+ 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
50+ 'description' : 'test demo create an new activity' ,
51+ 'status' : 'active' ,
52+ 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
53+ }
54+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
55+ results = dao .create_activity (activity )
56+ assert results .id != None \
57+
5258
5359@pytest .mark .parametrize ('with_data' ,[True ])
5460def test_update__returns_an_activity_updated__when_an_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
55- body = {
61+ activity = {
5662 'name' : 'Activity Demo create 3' ,
5763 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
5864 'description' : 'test demo create an new activity' ,
5965 'status' : 'active' ,
6066 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
6167 }
62- database , activity_data = create_fake_database
63- activity_data [0 ].update (body )
64- activity_data [0 ]["id" ] = uuid .UUID (activity_data [0 ]["id" ]).hex
65- expected_result = domain .Activity (** activity_data [0 ])
66- dao = infrastructure .ActivitiesSQLDao (database )
67- results = dao .update (activity_data [0 ]["id" ],body )
68+ expected_result = domain .Activity (** {
69+ 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
70+ 'name' : 'Activity Demo create 3' ,
71+ 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
72+ 'description' : 'test demo create an new activity' ,
73+ 'status' : 'active' ,
74+ 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
75+ })
76+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
77+ results = dao .update ('b4327ba69f9649eea9ac3c1edf525172' ,activity )
6878 assert results == expected_result
6979
7080
7181@pytest .mark .parametrize ('with_data' ,[False ])
7282def test_update__returns_none__when_no_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
73- database , activity_data = create_fake_database
74- activity_data [0 ]["id" ] = uuid .UUID (activity_data [0 ]["id" ]).hex
75- dao = infrastructure .ActivitiesSQLDao (database )
76- results = dao .update (activity_data [0 ]["id" ],activity_data [0 ])
83+ activity = {
84+ 'name' : 'Activity Demo create 3' ,
85+ 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
86+ 'description' : 'test demo create an new activity' ,
87+ 'status' : 'active' ,
88+ 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
89+ }
90+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
91+ results = dao .update ('b4327ba69f9649eea9ac3c1edf525172' ,activity )
7792 assert results == None
7893
79-
94+
8095@pytest .mark .parametrize ('with_data' ,[True ])
8196def test__get_all__returns_a_list_of_activity_dto_objects__when_one_or_more_activities_are_found_with_sql_database (create_fake_database ):
82- database , activity_data = create_fake_database
83- for activity in activity_data :
84- activity ["id" ] = uuid .UUID (activity ["id" ]).hex
8597 expected_result = [
86- domain .Activity (** activity_data [0 ]),
87- domain .Activity (** activity_data [1 ]),
98+ domain .Activity (** {
99+ 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
100+ 'name' : 'Activity Demo create' ,
101+ 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
102+ 'description' : 'test demo create an new activity' ,
103+ 'status' : 'active' ,
104+ 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
105+ }),
106+ domain .Activity (** {
107+ 'id' : 'c61a4a49336449a3a7f70c5f2d15072b' ,
108+ 'name' : 'Activity Demo create' ,
109+ 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
110+ 'description' : 'test demo create an new activity' ,
111+ 'status' : 'active' ,
112+ 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
113+ }),
88114 ]
89- dao = infrastructure .ActivitiesSQLDao (database )
115+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
90116 results = dao .get_all ()
91117 assert results == expected_result
92118
93119
94120@pytest .mark .parametrize ('with_data' ,[True ])
95121def test_get_by_id__returns_an_activity_dto__when_found_one_activity_that_matches_its_id_with_sql_database (create_fake_database ):
96- database , activity_data = create_fake_database
97- activity_data [0 ]["id" ] = uuid .UUID (activity_data [0 ]["id" ]).hex
98- expected_result = domain .Activity (** activity_data [0 ])
99- dao = infrastructure .ActivitiesSQLDao (database )
100- results = dao .get_by_id (activity_data [0 ]["id" ])
122+ expected_result = domain .Activity (** {
123+ 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
124+ 'name' : 'Activity Demo create' ,
125+ 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
126+ 'description' : 'test demo create an new activity' ,
127+ 'status' : 'active' ,
128+ 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
129+ })
130+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
131+ results = dao .get_by_id ('b4327ba69f9649eea9ac3c1edf525172' )
101132 assert results == expected_result
102133
103134
104135@pytest .mark .parametrize ('with_data' ,[False ])
105136def test__get_by_id__returns_none__when_no_activity_matches_its_id_with_sql_database (create_fake_database ):
106- database , activity_data = create_fake_database
107- activity_data [0 ]["id" ] = uuid .UUID (activity_data [0 ]["id" ]).hex
108- dao = infrastructure .ActivitiesSQLDao (database )
109- results = dao .get_by_id (activity_data [0 ]["id" ])
137+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
138+ results = dao .get_by_id ('b4327ba69f9649eea9ac3c1edf525172' )
110139 assert results == None
111140
112141
113142@pytest .mark .parametrize ('with_data' ,[False ])
114143def test_get_all__returns_an_empty_list__when_doesnt_found_any_activities_with_sql_database (create_fake_database ):
115- database , activity_data = create_fake_database
116- dao = infrastructure .ActivitiesSQLDao (database )
144+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
117145 results = dao .get_all ()
118146 assert results == []
119147
120148
121149@pytest .mark .parametrize ('with_data' ,[True ])
122150def test_delete__returns_an_activity_with_inactive_status__when_an_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
123- database , activity_data = create_fake_database
124- activity_data [0 ].update ({"id" :uuid .UUID (activity_data [0 ]["id" ]).hex ,"status" :"inactive" })
125- expected_result = domain .Activity (** activity_data [0 ])
126- dao = infrastructure .ActivitiesSQLDao (database )
127- results = dao .delete (activity_data [0 ]["id" ])
151+ expected_result = domain .Activity (** {
152+ 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
153+ 'name' : 'Activity Demo create' ,
154+ 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
155+ 'description' : 'test demo create an new activity' ,
156+ 'status' : 'inactive' ,
157+ 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
158+ })
159+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
160+ results = dao .delete ('b4327ba69f9649eea9ac3c1edf525172' )
128161 assert results == expected_result
129162
130-
163+
131164@pytest .mark .parametrize ('with_data' ,[False ])
132165def test_delete__returns_none__when_no_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
133- database , activity_data = create_fake_database
134- activity_data [0 ]["id" ] = uuid .UUID (activity_data [0 ]["id" ]).hex
135- dao = infrastructure .ActivitiesSQLDao (database )
136- results = dao .delete (activity_data [0 ]["id" ])
166+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
167+ results = dao .delete ('b4327ba69f9649eea9ac3c1edf525172' )
137168 assert results == None
0 commit comments