11import pytest
22import uuid
3+ from faker import Faker
34
45import time_entries ._domain as domain
56import time_entries ._infrastructure as infrastructure
6-
7+ import sqlalchemy
78
89@pytest .fixture (name = 'create_fake_database' )
910def _create_fake_database (with_data : bool ) -> domain .ActivitiesDao :
1011 db_fake = infrastructure .DB ('sqlite:///:memory:' )
1112 demo_data = [
1213 {
13- 'id' : uuid . UUID ( 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ),
14- 'name' : 'Activity Demo create' ,
15- 'tenant_id ' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
16- 'description ' : 'test demo create an new activity' ,
14+ 'id' : Faker (). uuid4 ( ),
15+ 'name' : Faker (). user_name () ,
16+ 'description ' : Faker (). sentence () ,
17+ 'deleted ' : Faker (). uuid4 () ,
1718 'status' : 'active' ,
18- 'deleted ' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
19+ 'tenant_id ' : Faker (). uuid4 () ,
1920 },
2021 {
21- 'id' : uuid . UUID ( 'c61a4a49-3364-49a3-a7f7-0c5f2d15072b' ),
22- 'name' : 'Activity Demo create' ,
23- 'tenant_id ' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
24- 'description ' : 'test demo create an new activity' ,
22+ 'id' : Faker (). uuid4 ( ),
23+ 'name' : Faker (). user_name () ,
24+ 'description ' : Faker (). sentence () ,
25+ 'deleted ' : Faker (). uuid4 () ,
2526 'status' : 'active' ,
26- 'deleted ' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
27+ 'tenant_id ' : Faker (). uuid4 () ,
2728 },
2829 ]
2930 if with_data :
30- query = db_fake .activity .insert ()
31+ activity = sqlalchemy .Table ('activity' , db_fake .metadata ,
32+ sqlalchemy .Column ('name' , sqlalchemy .String ),
33+ sqlalchemy .Column ('id' , infrastructure .GUID (), primary_key = True , default = uuid .uuid4 ),
34+ sqlalchemy .Column ('description' , sqlalchemy .String ),
35+ sqlalchemy .Column ('deleted' , sqlalchemy .String ),
36+ sqlalchemy .Column ('status' , sqlalchemy .String ),
37+ sqlalchemy .Column ('tenant_id' , sqlalchemy .String ),
38+ extend_existing = True ,
39+ )
40+ query = activity .insert ()
3141 db_fake .get_session ().execute (query , demo_data )
32- return db_fake
42+ return db_fake , demo_data
3343
3444
3545@pytest .mark .parametrize ('with_data' ,[False ])
3646def test__create_activity__returns_a_activity_dto__when_saves_correctly_with_sql_database (create_fake_database ):
37- activity = {
38- 'name' : 'Activity Demo create' ,
39- 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
40- 'description' : 'test demo create an new activity' ,
41- 'status' : 'active' ,
42- 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
43- }
44- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
45- results = dao .create_activity (activity )
47+ database , activity_data = create_fake_database
48+ dao = infrastructure .ActivitiesSQLDao (database )
49+ results = dao .create_activity (activity_data [0 ])
4650 assert results .id != None
4751
4852
4953@pytest .mark .parametrize ('with_data' ,[True ])
5054def test_update__returns_an_activity_updated__when_an_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
51- activity = {
55+ body = {
5256 'name' : 'Activity Demo create 3' ,
5357 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
5458 'description' : 'test demo create an new activity' ,
5559 'status' : 'active' ,
5660 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
5761 }
58- expected_result = domain .Activity (** {
59- 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
60- 'name' : 'Activity Demo create 3' ,
61- 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
62- 'description' : 'test demo create an new activity' ,
63- 'status' : 'active' ,
64- 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
65- })
66- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
67- results = dao .update ('b4327ba69f9649eea9ac3c1edf525172' ,activity )
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 )
6868 assert results == expected_result
6969
7070
7171@pytest .mark .parametrize ('with_data' ,[False ])
7272def test_update__returns_none__when_no_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
73- activity = {
74- 'name' : 'Activity Demo create 3' ,
75- 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
76- 'description' : 'test demo create an new activity' ,
77- 'status' : 'active' ,
78- 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
79- }
80- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
81- results = dao .update ('b4327ba69f9649eea9ac3c1edf525172' ,activity )
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 ])
8277 assert results == None
8378
8479
8580@pytest .mark .parametrize ('with_data' ,[True ])
8681def 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
8785 expected_result = [
88- domain .Activity (** {
89- 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
90- 'name' : 'Activity Demo create' ,
91- 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
92- 'description' : 'test demo create an new activity' ,
93- 'status' : 'active' ,
94- 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
95- }),
96- domain .Activity (** {
97- 'id' : 'c61a4a49336449a3a7f70c5f2d15072b' ,
98- 'name' : 'Activity Demo create' ,
99- 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
100- 'description' : 'test demo create an new activity' ,
101- 'status' : 'active' ,
102- 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
103- }),
86+ domain .Activity (** activity_data [0 ]),
87+ domain .Activity (** activity_data [1 ]),
10488 ]
105- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
89+ dao = infrastructure .ActivitiesSQLDao (database )
10690 results = dao .get_all ()
10791 assert results == expected_result
10892
10993
11094@pytest .mark .parametrize ('with_data' ,[True ])
11195def test_get_by_id__returns_an_activity_dto__when_found_one_activity_that_matches_its_id_with_sql_database (create_fake_database ):
112- expected_result = domain .Activity (** {
113- 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
114- 'name' : 'Activity Demo create' ,
115- 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
116- 'description' : 'test demo create an new activity' ,
117- 'status' : 'active' ,
118- 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
119- })
120- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
121- results = dao .get_by_id ('b4327ba69f9649eea9ac3c1edf525172' )
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" ])
122101 assert results == expected_result
123102
124103
125104@pytest .mark .parametrize ('with_data' ,[False ])
126105def test__get_by_id__returns_none__when_no_activity_matches_its_id_with_sql_database (create_fake_database ):
127- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
128- results = dao .get_by_id ('b4327ba69f9649eea9ac3c1edf525172' )
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" ])
129110 assert results == None
130111
131112
132113@pytest .mark .parametrize ('with_data' ,[False ])
133114def test_get_all__returns_an_empty_list__when_doesnt_found_any_activities_with_sql_database (create_fake_database ):
134- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
115+ database , activity_data = create_fake_database
116+ dao = infrastructure .ActivitiesSQLDao (database )
135117 results = dao .get_all ()
136118 assert results == []
137119
138120
139121@pytest .mark .parametrize ('with_data' ,[True ])
140122def test_delete__returns_an_activity_with_inactive_status__when_an_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
141- expected_result = domain .Activity (** {
142- 'id' : 'b4327ba69f9649eea9ac3c1edf525172' ,
143- 'name' : 'Activity Demo create' ,
144- 'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
145- 'description' : 'test demo create an new activity' ,
146- 'status' : 'inactive' ,
147- 'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
148- })
149- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
150- results = dao .delete ('b4327ba69f9649eea9ac3c1edf525172' )
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" ])
151128 assert results == expected_result
152129
153130
154131@pytest .mark .parametrize ('with_data' ,[False ])
155132def test_delete__returns_none__when_no_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
156- dao = infrastructure .ActivitiesSQLDao (create_fake_database )
157- results = dao .delete ('b4327ba69f9649eea9ac3c1edf525172' )
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" ])
158137 assert results == None
0 commit comments