1
1
import pytest
2
2
import uuid
3
- from faker import Faker
3
+
4
+ import sqlalchemy
4
5
5
6
import time_entries ._domain as domain
6
7
import time_entries ._infrastructure as infrastructure
7
- import sqlalchemy
8
8
9
9
@pytest .fixture (name = 'create_fake_database' )
10
10
def _create_fake_database (with_data : bool ) -> domain .ActivitiesDao :
11
11
db_fake = infrastructure .DB ('sqlite:///:memory:' )
12
12
demo_data = [
13
13
{
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' ,
18
18
'status' : 'active' ,
19
- 'tenant_id ' : Faker (). uuid4 () ,
19
+ 'deleted ' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
20
20
},
21
21
{
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' ,
26
26
'status' : 'active' ,
27
- 'tenant_id ' : Faker (). uuid4 () ,
27
+ 'deleted ' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
28
28
},
29
29
]
30
30
if with_data :
@@ -39,99 +39,130 @@ def _create_fake_database(with_data: bool) -> domain.ActivitiesDao:
39
39
)
40
40
query = activity .insert ()
41
41
db_fake .get_session ().execute (query , demo_data )
42
- return db_fake , demo_data
42
+ return db_fake
43
43
44
44
45
45
@pytest .mark .parametrize ('with_data' ,[False ])
46
46
def 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
+
52
58
53
59
@pytest .mark .parametrize ('with_data' ,[True ])
54
60
def test_update__returns_an_activity_updated__when_an_activity_matching_its_id_is_found_with_sql_database (create_fake_database ):
55
- body = {
61
+ activity = {
56
62
'name' : 'Activity Demo create 3' ,
57
63
'tenant_id' : 'cc925a5d-9644-4a4f-8d99-0bee49aadd05' ,
58
64
'description' : 'test demo create an new activity' ,
59
65
'status' : 'active' ,
60
66
'deleted' : 'b4327ba6-9f96-49ee-a9ac-3c1edf525172' ,
61
67
}
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 )
68
78
assert results == expected_result
69
79
70
80
71
81
@pytest .mark .parametrize ('with_data' ,[False ])
72
82
def 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 )
77
92
assert results == None
78
93
79
-
94
+
80
95
@pytest .mark .parametrize ('with_data' ,[True ])
81
96
def 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
85
97
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
+ }),
88
114
]
89
- dao = infrastructure .ActivitiesSQLDao (database )
115
+ dao = infrastructure .ActivitiesSQLDao (create_fake_database )
90
116
results = dao .get_all ()
91
117
assert results == expected_result
92
118
93
119
94
120
@pytest .mark .parametrize ('with_data' ,[True ])
95
121
def 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' )
101
132
assert results == expected_result
102
133
103
134
104
135
@pytest .mark .parametrize ('with_data' ,[False ])
105
136
def 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' )
110
139
assert results == None
111
140
112
141
113
142
@pytest .mark .parametrize ('with_data' ,[False ])
114
143
def 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 )
117
145
results = dao .get_all ()
118
146
assert results == []
119
147
120
148
121
149
@pytest .mark .parametrize ('with_data' ,[True ])
122
150
def 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' )
128
161
assert results == expected_result
129
162
130
-
163
+
131
164
@pytest .mark .parametrize ('with_data' ,[False ])
132
165
def 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' )
137
168
assert results == None
0 commit comments