-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_endpoints.py
More file actions
130 lines (119 loc) · 4.2 KB
/
test_endpoints.py
File metadata and controls
130 lines (119 loc) · 4.2 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import unittest
import json
from api.views.red_flag_routes import app, my_red_flags
""" Module for running my tests """
class BaseTest(unittest.TestCase):
""" Class for test data"""
def setUp(self):
self.client = app.test_client()
self.sample_record_data = {
"createdBy": "anthony",
"type": "red-flag",
"location": "kitalanga",
"status": "resolved",
"Images": "fed.png",
"Videos": "gfgg.mp4",
"comment": "This is good"
}
class TestRedFlag(BaseTest):
""" Test for creating a red-flag record"""
def test_create_red_flag(self):
response = self.client.post(
"/api/v1/red-flags",
data = json.dumps(self.sample_record_data),
content_type = "application/json"
)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.content_type, "application/json")
self.assertIn(b"Created red-flag record", response.data)
def test_home(self):
""" Testing for getting the data at my home route """
response = self.client.get('/')
assert b'Welcome to ernest\'s iReporter app.' in response.data
assert response.status_code == 200
def test_get_all_redflags(self):
""" Test for getting all red-flag records"""
self.client.post(
"/api/v1/red-flags",
data = json.dumps(self.sample_record_data),
content_type = "application/json"
)
response = self.client.get(
"/api/v1/red-flags",
content_type = "application/json"
)
self.assertEqual(response.status_code, 200)
self.assertIn("createdBy", str(response.data))
self.assertEqual(response.content_type, "application/json")
""" missing value field = bad """
def test_post(self):
record = {
"Images": "fed.png",
"Videos": "gfgg.mp4",
"comment": ""
}
response = self.client.post(
"/api/v1/red-flags",
data=json.dumps(record),
content_type='application/json'
)
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 400)
self.assertEqual("Required field is missing", response_data['Error'])
def test_fetch_specific_redflag(self):
""" Test for getting a specific red-flag record """
response = self.client.get(
'/api/v1/red-flags/1',
content_type = "application/json")
response_data = json.loads(response.data.decode())
self.assertEqual(response.content_type, "application/json")
self.assertEqual(response.status_code, 200)
self.assertEqual(response_data["status"], 200)
response = self.client.get(
'/api/v1/red-flags/100',
content_type = "application/json")
assert response.status_code == 404
def test_edit_location_and_comment(self):
""" Test to edit location and comment of a red-flag record"""
self.client.post(
"/api/v1/red-flags",
data = json.dumps(self.sample_record_data),
content_type = "application/json"
)
new_location = {"location": "Kampala"}
response = self.client.patch(
"/api/v1/red-flags/1/location",
content_type="application/json",
data=json.dumps(new_location)
)
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertEqual(my_red_flags[0]["location"], "Kampala")
self.assertEqual(response_data["status"], 200)
self.assertIn("Updated red-flag's record location", response_data["message"])
self.assertIsInstance(response_data, dict)
new_comment = {"comment": "Bad reports"}
response = self.client.patch(
"/api/v1/red-flags/1/comment",
content_type="application/json",
data=json.dumps(new_comment)
)
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertEqual(my_red_flags[0]["comment"], "Bad reports")
self.assertEqual(response_data["status"], 200)
self.assertIn("Updated red-flag's record comment", response_data["message"])
self.assertIsInstance(response_data, dict)
def test_delete_record(self):
""" Test to delete a specific red-flag record """
self.client.post(
"/api/v1/red-flags",
data = json.dumps(self.sample_record_data),
content_type = "application/json"
)
response = self.client.delete(
"/api/v1/red-flags/1"
)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(my_red_flags), 0)
assert response.status_code == 200