-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
311 lines (281 loc) · 9.02 KB
/
tests.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
This file contains unit tests for the Animal, Food, and Shelter models
and their corresponding API endpoints.
"""
import unittest
from testing_doggos import app, db
import json
class AnimalTestCase(unittest.TestCase):
"""
Test case for the Animal model and API endpoints.
"""
def setUp(self):
"""
Set up the test environment before each test case.
"""
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test_animal_shelter.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.app = app.test_client()
self.ctx = app.app_context()
self.ctx.push()
db.create_all()
def tearDown(self):
"""
Clean up the test environment after each test case.
"""
db.drop_all()
self.ctx.pop()
def test_add_animal(self):
"""
Test adding a new animal to the database.
"""
response = self.app.post('/animals', json={
'name': 'Hector',
'race': 'Dog',
'food_id': 1,
'shelter_id': 1,
'age': 3,
'weight': 10.5,
'status': 'Adoptable',
'sex': 'Female'
})
self.assertEqual(response.status_code, 201)
def test_get_animals(self):
"""
Test retrieving all animals from the database.
"""
response = self.app.get('/animals')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json), 0)
def test_get_animal(self):
"""
Test retrieving a specific animal from the database.
"""
testing_doggo = {
'name': 'Hector',
'race': 'Dog',
'food_id': 1,
'shelter_id': 1,
'age': 3,
'weight': 10.5,
'status': 'Adoptable',
'sex': 'Female'
}
response = self.app.post('/animals', json=testing_doggo)
animal_id = response.json['id']
response = self.app.get(f'/animals/{animal_id}')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json['name'], testing_doggo['name'])
self.assertEqual(response.json['race'], testing_doggo['race'])
def test_update_animal(self):
"""
Test updating an existing animal in the database.
"""
response = self.app.post('/animals', json={
'name': 'Doggo',
'race': 'Dog',
'food_id': 1,
'shelter_id': 1,
'age': 3,
'weight': 10.5,
'status': 'Adoptable',
'sex': 'Female'
})
animal_id = response.json['id']
update_data = {
'name': 'Whiskas',
'race': 'Cat',
'food_id': 2,
'shelter_id': 2,
'age': 5,
'weight': 4.5,
'status': 'Adopted',
'sex': 'Female'
}
response = self.app.put(
f'/animals/{animal_id}',
json=json.dumps(update_data))
def test_delete_animal(self):
"""
Test deleting an animal from the database.
"""
response = self.app.post('/animals', json={
'name': 'Hector',
'race': 'Dog',
'food_id': 3,
'shelter_id': 5,
'age': 12,
'weight': 32,
'status': 'Adoptable',
'sex': 'Male'
})
animal_id = response.json['id']
response = self.app.delete(f'/animals/{animal_id}')
self.assertEqual(response.status_code, 204)
class FoodTestCase(unittest.TestCase):
"""
Test case for the Food model and API endpoints.
"""
def setUp(self):
"""
Set up the test environment before each test case.
"""
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test_animal_shelter.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.app = app.test_client()
self.ctx = app.app_context()
self.ctx.push()
db.create_all()
def tearDown(self):
"""
Clean up the test environment after each test case.
"""
db.drop_all()
self.ctx.pop()
def test_add_food(self):
"""
Test adding a new food item to the database.
"""
response = self.app.post('/foods', json={
'name': 'Dog Food',
'amount': 10,
'weight': 5.0
})
self.assertEqual(response.status_code, 201)
def test_get_foods(self):
"""
Test retrieving all food items from the database.
"""
response = self.app.get('/foods')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json), 0)
def test_get_food(self):
"""
Test retrieving a specific food item from the database.
"""
response = self.app.post('/foods', json={
'name': 'Dog Food',
'amount': 10,
'weight': 5.0
})
food_id = response.json['id']
response = self.app.get(f'/foods/{food_id}')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json['name'], 'Dog Food')
def test_update_food(self):
"""
Test updating an existing food item in the database.
"""
response = self.app.post('/foods', json={
'name': 'Dog Food',
'amount': 10,
'weight': 5.0
})
food_id = response.json['id']
updated = {
'name': 'Updated Dog Food',
'amount': 15,
'weight': 7.5
}
response = self.app.put(f'/foods/{food_id}', json=json.dumps(updated))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json['name'], 'Updated Dog Food')
def test_delete_food(self):
"""
Test deleting a food item from the database.
"""
response = self.app.post('/foods', json={
'name': 'Dog Food',
'amount': 10,
'weight': 5.0
})
food_id = response.json['id']
response = self.app.delete(f'/foods/{food_id}')
self.assertEqual(response.status_code, 204)
class ShelterTestCase(unittest.TestCase):
"""
Test case for the Shelter model and API endpoints.
"""
def setUp(self):
"""
Set up the test environment before each test case.
"""
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test_animal_shelter.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.app = app.test_client()
self.ctx = app.app_context()
self.ctx.push()
db.create_all()
def tearDown(self):
"""
Clean up the test environment after each test case.
"""
db.drop_all()
self.ctx.pop()
def test_add_shelter(self):
"""
Test adding a new shelter to the database.
"""
response = self.app.post('/shelters', json={
'name': 'Dog Shelter',
'amount': 5,
'capacity': 10
})
self.assertEqual(response.status_code, 201)
def test_get_shelters(self):
"""
Test retrieving all shelters from the database.
"""
response = self.app.get('/shelters')
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.json), 0)
def test_get_shelter(self):
"""
Test retrieving a specific shelter from the database.
"""
response = self.app.post('/shelters', json={
'name': 'Dog Shelter',
'amount': 5,
'capacity': 10
})
shelter_id = response.json['id']
response = self.app.get(f'/shelters/{shelter_id}')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json['name'], 'Dog Shelter')
def test_update_shelter(self):
"""
Test updating an existing shelter in the database.
"""
response = self.app.post('/shelters', json={
'name': 'Dog Shelter',
'amount': 5,
'capacity': 10
})
shelter_id = response.json['id']
updated = {
'name': 'Updated Dog Shelter',
'amount': 7,
'capacity': 12
}
response = self.app.put(
f'/shelters/{shelter_id}',
json=json.dumps(updated))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json['name'], 'Updated Dog Shelter')
def test_delete_shelter(self):
"""
Test deleting a shelter from the database.
"""
response = self.app.post('/shelters', json={
'name': 'Dog Shelter',
'amount': 5,
'capacity': 10
})
shelter_id = response.json['id']
response = self.app.delete(f'/shelters/{shelter_id}')
self.assertEqual(response.status_code, 204)
if __name__ == '__main__':
unittest.main()