-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
206 lines (185 loc) · 6.51 KB
/
test_app.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
#pytest -W ignore -s
import pytest, random
from app import app as flask_app
from models import Tag
from crud import CRUD, db
from utils import generate_random_string
print('\n=> Starting testing')
@pytest.fixture(scope="session")
def app():
flask_app.app_context().push()
return flask_app
#============HTTP testing=================
@pytest.fixture(scope="session")
def client(app):
return app.test_client()
def test_index_page(client):
print('\n=> Testing the index page returns a 200 status code')
response = client.get('/')
assert response.status_code == 200
#============CRUD testing=================
# Mocking db.sessions.commit() to avoid commiting
# test objects by mistake
@pytest.fixture(scope="function")
def mock_crud(mocker):
return mocker.patch(
"crud.db.session.commit",
return_value=True
)
@pytest.fixture(scope="function")
def context_tags(mock_crud):
tags_list = [generate_random_string() for _ in range(2)]
tags = ' '.join(tags_list)
metatags = CRUD.addTags(tags, commit=False)
context = {'metatags': metatags, 'tags': tags}
return context
def test_adding_multiple_tags(mock_crud, context_tags):
print('\n=> Testing adding multiple tags to the database')
assert len(context_tags.get('tags')) > 1
@pytest.fixture(scope="function")
def context_api(mock_crud, context_tags):
context = context_tags
label = generate_random_string()
url = generate_random_string()
description = ' '.join([generate_random_string() for _ in range(5)])
tags = context.get('tags')
api_id = CRUD.addApi(label, url, description=description, tags=tags, commit=False)
context['api_id'] = api_id
return context
def test_adding_api(mock_crud, context_api):
print('\n=> Testing adding api to the database')
api_id = context_api.get('api_id')
assert api_id
api = CRUD.getApi(api_id)
assert api
def test_edit_api_by_id(mock_crud, context_api):
print('\n=> Testing editing api by id')
id = context_api.get('api_id')
label = generate_random_string()
url = generate_random_string()
description = ' '.join([generate_random_string() for _ in range(5)])
tags = ' '.join([generate_random_string() for _ in range(5)])
code = CRUD.editApi(
id,
label=label,
url=url,
description=description,
tags=tags
)
assert code>0
api = CRUD.getApi(id)
assert api.label == label
assert api.url == url
assert api.description == description
assert set(tags.split()) == set([t.text for t in api.tags])
def test_filter_apis_by_tags(mock_crud, context_api):
print('\n=> Testing filtering apis by tags')
apis = CRUD.getApis(context_api.get('tags'))
assert apis
@pytest.fixture(scope="function")
def context_endpoint(mock_crud, context_api):
context = context_api
api_id = context.get('api_id')
label = generate_random_string()
url = generate_random_string()
description = ' '.join([generate_random_string() for _ in range(5)])
tags = context.get('tags')
id = CRUD.addEndpoint(
api_id,
label,
url,
description,
tags=tags,
commit=False
)
context['endpoint_id'] = id
return context
def test_adding_endpoint(mock_crud, context_endpoint):
print('\n=> Testing adding endpoint to the database')
assert context_endpoint.get('endpoint_id')
def test_edit_endpoint_by_id(mock_crud, context_endpoint):
print('\n=> Testing editing endpoint by id')
endpoint_id = context_endpoint.get('endpoint_id')
label = generate_random_string()
url = generate_random_string()
description = ' '.join([generate_random_string() for _ in range(5)])
tags = ' '.join([generate_random_string() for _ in range(5)])
code = CRUD.editEndpoint(
endpoint_id,
label=label,
url=url,
description=description,
tags=tags
)
assert code>0
endpoint = CRUD.getEndpoint(endpoint_id)
assert endpoint.label == label
assert endpoint.url == url
assert endpoint.description == description
assert set(tags.split()) == set([t.text for t in endpoint.tags])
def test_delete_endpoint_by_id(mock_crud, context_endpoint):
print('\n=> Testing deleting endpoint by id')
code = CRUD.deleteEndpoint(context_endpoint.get('endpoint_id'))
assert code>0
@pytest.fixture(scope="function")
def context_field(mock_crud, context_endpoint):
context = context_endpoint
endpoint_id = context.get('endpoint_id')
label = generate_random_string()
type_field = generate_random_string()
description = ' '.join([generate_random_string() for _ in range(5)])
default = generate_random_string()
required = random.choice([True, False])
id = CRUD.addEndpointField(
endpoint_id,
label,
type_field,
description=description,
default=default,
required=required,
commit=False
)
context['field_id'] = id
return context
def test_adding_field(mock_crud, context_field):
print('\n=> Testing adding a field to an endpoint')
assert context_field.get('field_id')
def test_edit_field_by_id(mock_crud, context_field):
print('\n=> Testing editing field by id')
field_id = context_field.get('field_id')
label = generate_random_string()
type_field = generate_random_string()
description = ' '.join([generate_random_string() for _ in range(5)])
default = generate_random_string()
required = random.choice([True, False])
code = CRUD.editField(
field_id,
label=label,
type_field=type_field,
description=description,
default=default,
required=required,
commit=False
)
assert code>0
field = CRUD.getField(field_id)
assert field.label == label
assert field.type == type_field
assert field.description == description
assert field.default == default
assert field.required == required
def test_deleting_field(mock_crud, context_field):
print('\n=> Testing deleting field by id')
code = CRUD.deleteField(context_field.get('field_id'))
assert code>0
def test_cascade_delete_api_by_id(mock_crud, context_field):
print('\n=> Testing cascade deleting api by id')
api_id = context_field.get('api_id')
endpoint_id = context_field.get('endpoint_id')
field_id = context_field.get('field_id')
code = CRUD.deleteApi(api_id)
assert code>0
existing_endpoint_id = CRUD.getEndpoint(endpoint_id)
assert existing_endpoint_id is None
existing_field = CRUD.getField(field_id)
assert existing_field is None