17
17
'''
18
18
19
19
import json
20
- import pytest
21
20
import os .path
22
21
from flask import url_for
23
22
from flask import current_app
@@ -36,25 +35,21 @@ def get_sample_json(jsonfile, model_type):
36
35
# open file
37
36
json_dataset = None
38
37
39
- try :
40
- with open (
41
- os .path .join (
42
- root ,
43
- 'interface' ,
44
- 'static' ,
45
- 'data' ,
46
- 'json' ,
47
- 'programmatic_interface' ,
48
- model_type ,
49
- 'dataset_url' ,
50
- jsonfile
51
- ),
52
- 'r'
53
- ) as json_file :
54
- json_dataset = json .load (json_file )
55
-
56
- except Exception as error :
57
- pytest .fail (error )
38
+ with open (
39
+ os .path .join (
40
+ root ,
41
+ 'interface' ,
42
+ 'static' ,
43
+ 'data' ,
44
+ 'json' ,
45
+ 'programmatic_interface' ,
46
+ model_type ,
47
+ 'dataset_url' ,
48
+ jsonfile
49
+ ),
50
+ 'r'
51
+ ) as json_file :
52
+ json_dataset = json .load (json_file )
58
53
59
54
return json .dumps (json_dataset )
60
55
@@ -78,7 +73,9 @@ def get_endpoint():
78
73
data = get_sample_json ('svm-data-new.json' , 'svm' )
79
74
)
80
75
76
+ # assertion checks
81
77
assert res .status_code == 200
78
+ assert res .json ['status' ] == 0
82
79
83
80
84
81
def test_data_append (client , live_server ):
@@ -100,7 +97,9 @@ def get_endpoint():
100
97
data = get_sample_json ('svm-data-append.json' , 'svm' )
101
98
)
102
99
100
+ # assertion checks
103
101
assert res .status_code == 200
102
+ assert res .json ['status' ] == 0
104
103
105
104
106
105
def test_model_generate (client , live_server ):
@@ -122,14 +121,21 @@ def get_endpoint():
122
121
data = get_sample_json ('svm-model-generate.json' , 'svm' )
123
122
)
124
123
124
+ # assertion checks
125
125
assert res .status_code == 200
126
+ assert res .json ['status' ] == 0
126
127
127
128
128
129
def test_model_predict (client , live_server ):
129
130
'''@test_model_predict
130
131
131
132
This method tests the 'model_predict' session.
132
133
134
+ Note: for debugging, the following syntax will output the corresponding
135
+ json values, nested within 'json.loads()', to the travis ci:
136
+
137
+ raise ValueError(res.json['result']['key1'])
138
+
133
139
'''
134
140
135
141
@live_server .app .route ('/load-data' )
@@ -144,4 +150,44 @@ def get_endpoint():
144
150
data = get_sample_json ('svm-model-predict.json' , 'svm' )
145
151
)
146
152
153
+ # check each probability is within acceptable margin
154
+ fixed_prob = [
155
+ 0.14075033321086294 ,
156
+ 0.14500955005546354 ,
157
+ 0.14156072707544004 ,
158
+ 0.19249135186767916 ,
159
+ 0.38018803779055466
160
+ ]
161
+ cp = res .json ['result' ]['confidence' ]['probability' ]
162
+ margin_prob = 0.005
163
+ check_prob = [
164
+ i for i in fixed_prob if any (abs (i - j ) > margin_prob for j in cp )
165
+ ]
166
+
167
+ # assertion checks
147
168
assert res .status_code == 200
169
+ assert res .json ['status' ] == 0
170
+ assert res .json ['result' ]
171
+ assert res .json ['result' ]['confidence' ]
172
+ assert res .json ['result' ]['confidence' ]['classes' ] == [
173
+ 'dep-variable-1' ,
174
+ 'dep-variable-2' ,
175
+ 'dep-variable-3' ,
176
+ 'dep-variable-4' ,
177
+ 'dep-variable-5'
178
+ ]
179
+ assert res .json ['result' ]['confidence' ]['decision_function' ] == [
180
+ 0.1221379769127864 ,
181
+ 0.0 ,
182
+ - 0.2201467913263242 ,
183
+ - 0.22014661657537662 ,
184
+ - 0.12213797691278638 ,
185
+ - 0.33333297925570843 ,
186
+ - 0.33333281615328886 ,
187
+ - 0.2201467913263242 ,
188
+ - 0.22014661657537662 ,
189
+ 1.8353514974478458e-07
190
+ ]
191
+ assert check_prob
192
+ assert res .json ['result' ]['model' ] == 'svm'
193
+ assert res .json ['result' ]['result' ] == 'dep-variable-4'
0 commit comments