-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_management.py
216 lines (162 loc) · 5.94 KB
/
test_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# test_management.py
# Import modules
import pytest
import management
from storage import Database
class TestUser:
def test_username(self):
test_user = management.User("System", "root")
# Tests that the username is the same as the assigned
# username.
assert (
test_user.username == "System"
), """Username does not
match expected result."""
del test_user
def test_logged_in(self):
# This creates a database to test on.
# which will not have any effect on the
# real database.
test_db = Database("loggedIn", delete_on_close=True)
management.setup(test_db)
# Creates a user instance which should be
# logged out.
test_user = management.User("testing", "account", 1)
assert test_user.logged_in is False, "User should be logged out."
del test_user
# Creates a user instance which is logged in.
logged_in_user = management.User("System", "root", login=True)
assert (
logged_in_user.logged_in is True
), """System Account should be
logged in."""
del logged_in_user
del test_db
def test_super_admin(self):
# This creates a database to test on.
# which will not have any effect on the
# real database.
test_db = Database("superAdmin", delete_on_close=True)
management.setup(test_db)
# This test is used to see if a user will
# be set to super admin if they are not
# logged in.
test_user = management.User("System", "root")
super_admin = test_user.super_admin
assert (
super_admin is False
), """Expected System to not be a super
admin till the user is logged in."""
del test_user
# This tests if a super user will
# be classed as a super user by the
# method.
test_user = management.User("System", "root", login=True)
assert (
test_user.super_admin is True
), """System is expected to be a
super admin."""
del test_user
del test_db
def test_admin(self):
# This creates a database to test on.
# which will not have any effect on the
# real database.
test_db = Database("admin", delete_on_close=True)
management.setup(test_db)
# This test is used to see if a user will
# be set to admin if they are not
# logged in.
test_user = management.User("System", "root")
super_admin = test_user.super_admin
assert (
super_admin is False
), """Expected System to not be a admin till
the user is logged in."""
del test_user
# This tests if a admin user will
# be classed as a admin user by the
# method.
test_user = management.User("System", "root", login=True)
assert test_user.admin is True, "System is expected to be admin."
del test_user
del test_db
class TestUserManager:
def test_remove_user(self):
# This creates a database to test on.
# which will not have any effect on the
# real database.
test_db = Database("remove", delete_on_close=True)
management.setup(test_db)
admin_user = management.User("System", "root", login=True)
guest_user = management.User("Guest", "root", login=True)
result = management.UserManager.remove_user(admin_user, guest_user)
assert result is True, "Failed to delete guest user."
with pytest.raises(management.PermissionDenied):
assert management.UserManager.remove_user(
admin_user, admin_user
), "The admin should not be able to delete them selfs."
del test_db
def test_us_date_to_uk():
# This tests the function to verify that the result is the expected result.
# Sets default month for test.
month = "04"
# Sets default year for test.
year = "2020"
# Creates a loop for the
# days.
for day in range(1, 30):
# Converts the day to a string.
day = str(day)
# Creates a US Style date for
# the function to convert.
us_date = "{}/{}/{}".format(year, month, day)
# Creates a UK Style date to assert against.
uk_date = "{}/{}/{}".format(day, month, year)
# Converts the US Style date to UK style date.
converted_date = management.us_date_to_uk(us_date)
# Checks if the date is correct.
assert uk_date == converted_date, "Expected: {}, result: {}".format(
converted_date, uk_date
)
def test__get_dates():
test_db = Database("admin", delete_on_close=True)
management.setup(test_db)
data = (1, "J", "J", "J", "0", "0", "0", "27/04/2022", "28/04/2022")
test_db.con.execute(
"""
INSERT INTO bookings( ID,
first_name,
last_name,
mobile_number,
email_address,
postcode,
pets,
start_time,
end_time)
VALUES(
?,
?,
?,
?,
?,
?,
?,
?,
?
)
""",
data,
)
test_db.save()
dates = management.get_dates()
for i in dates.index:
start_date = dates.loc[i, "start_date"]
end_date = dates.loc[i, "end_date"]
assert (
start_date.strftime("%d/%m/%Y") == "27/04/2022"
), "The start date should be 27/04/2022"
assert (
end_date.strftime("%d/%m/%Y") == "27/04/2022"
), "The end date should be 27/04/2022"
del test_db