-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_routes.py
More file actions
110 lines (73 loc) · 3.85 KB
/
test_routes.py
File metadata and controls
110 lines (73 loc) · 3.85 KB
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
def test_read_item(self):
# Assuming you're using Flask or Django, make a GET request to read an item
response = self.client.get("/items/1") # '1' is the ID of the item you're reading
# Check if the response status code is 200 (OK)
self.assertEqual(response.status_code, 200)
# Validate the contents of the response
data = response.get_json()
self.assertEqual(data['id'], 1)
self.assertEqual(data['name'], 'Sample Item') # Adjust according to your expected item data
# Adjust according to your expected item data
def test_read_model(self):
model = YourModel.objects.create(field1='value1', field2='value2')
retrieved_model = YourModel.objects.get(id=model.id)
self.assertEqual(retrieved_model.field1, 'value1')
self.assertEqual(retrieved_model.field2, 'value2')
def test_update_model(self):
model = YourModel.objects.create(field1='initial_value', field2='value2')
# Update the model field
model.field1 = 'updated_value'
model.save()
# Retrieve and check if the update was successful
updated_model = YourModel.objects.get(id=model.id)
self.assertEqual(updated_model.field1, 'updated_value')
self.assertEqual(updated_model.field2, 'value2')
def test_delete_model(self):
model = YourModel.objects.create(field1='value1', field2='value2')
model_id = model.id
# Delete the model
model.delete()
# Try to retrieve the model, should raise DoesNotExist exception
with self.assertRaises(YourModel.DoesNotExist):
YourModel.objects.get(id=model_id)
def test_list_all_models(self):
# Create multiple instances of the model
model1 = YourModel.objects.create(field1='value1', field2='value2')
model2 = YourModel.objects.create(field1='value3', field2='value4')
# Get all instances of the model
all_models = YourModel.objects.all()
# Check if the total count matches the number of created models
self.assertEqual(all_models.count(), 2)
# Check if the retrieved models are correct
self.assertIn(model1, all_models)
self.assertIn(model2, all_models)
def test_find_by_name(self):
# Create instances with different names
model1 = YourModel.objects.create(name='John Doe', field1='value1')
model2 = YourModel.objects.create(name='Jane Smith', field1='value2')
# Retrieve the model by name
found_model = YourModel.objects.get(name='John Doe')
# Check if the found model is the correct one
self.assertEqual(found_model, model1)
self.assertNotEqual(found_model, model2)
def test_find_by_category(self):
# Create instances with different categories
model1 = YourModel.objects.create(name='Product1', category='Electronics', field1='value1')
model2 = YourModel.objects.create(name='Product2', category='Books', field1='value2')
# Retrieve models by category
electronics_models = YourModel.objects.filter(category='Electronics')
books_models = YourModel.objects.filter(category='Books')
# Check if the models belong to the correct category
self.assertIn(model1, electronics_models)
self.assertNotIn(model1, books_models)
self.assertIn(model2, books_models)
self.assertNotIn(model2, electronics_models)
def test_find_by_availability(self):
# Create instances with different availability statuses
available_model = YourModel.objects.create(name='Product1', is_available=True, field1='value1')
unavailable_model = YourModel.objects.create(name='Product2', is_available=False, field1='value2')
# Retrieve models that are available
available_products = YourModel.objects.filter(is_available=True)
# Check if only available products are retrieved
self.assertIn(available_model, available_products)
self.assertNotIn(unavailable_model, available_products)