14
14
from src .api import CmixAPI , default_cmix_api , CMIX_SERVICES
15
15
from src .error import CmixError
16
16
17
- from popresearch .models import SURVEY_TYPE_COMPARISON , SURVEY_TYPE_CREATIVE , \
18
- CmixDataArchive , CmixSurvey , Survey
17
+ from popresearch .models import CmixDataArchive , Survey
19
18
20
19
from .factories import UserFactory
21
20
@@ -26,7 +25,7 @@ def setUp(self):
26
25
self .cmix_api ._authentication_headers = {'Authorization' : 'Bearer test' }
27
26
self .user = UserFactory .create_safe ()
28
27
self .survey = Survey .objects .create (user = self .user , name = "Test Survey" )
29
- self .cmix_survey = CmixSurvey . objects . create ( survey = self . survey , cmix_id = 42 )
28
+ self .survey_id = 1337
30
29
31
30
def test_cmix_authentication_check (self ):
32
31
with self .assertRaises (CmixError ):
@@ -73,7 +72,7 @@ def test_create_export_archive(self):
73
72
'name' : 'Default'
74
73
}]
75
74
mock_get .return_value = mock_response
76
- self .cmix_api .create_export_archive (self .cmix_survey , 'XLSX_READABLE' )
75
+ self .cmix_api .create_export_archive (self .survey_id , 'XLSX_READABLE' )
77
76
self .assertEqual (CmixDataArchive .objects .all ().count (), 1 )
78
77
79
78
def test_create_export_archive_errors_handled (self ):
@@ -95,7 +94,7 @@ def test_create_export_archive_errors_handled(self):
95
94
}]
96
95
mock_get .return_value = mock_response
97
96
with self .assertRaises (CmixError ):
98
- self .cmix_api .create_export_archive (self .cmix_survey , 'XLSX_READABLE' )
97
+ self .cmix_api .create_export_archive (self .survey_id , 'XLSX_READABLE' )
99
98
100
99
# Remove error from POST response.
101
100
mock_post_response .json .return_value = {'response' : 1 }
@@ -110,7 +109,7 @@ def test_create_export_archive_errors_handled(self):
110
109
}]
111
110
mock_get .return_value = mock_response
112
111
with self .assertRaises (CmixError ):
113
- self .cmix_api .create_export_archive (self .cmix_survey , 'XLSX_READABLE' )
112
+ self .cmix_api .create_export_archive (self .survey_id , 'XLSX_READABLE' )
114
113
115
114
# Check CmixError is raised if no default layout is returned.
116
115
mock_response .status_code = 200
@@ -119,7 +118,7 @@ def test_create_export_archive_errors_handled(self):
119
118
'name' : 'Not Default' ,
120
119
}]
121
120
with self .assertRaises (CmixError ):
122
- self .cmix_api .create_export_archive (self .cmix_survey , 'XLSX_READABLE' )
121
+ self .cmix_api .create_export_archive (self .survey_id , 'XLSX_READABLE' )
123
122
124
123
def test_get_survey_status (self ):
125
124
self .cmix_api ._authentication_headers = {'Authentication' : 'Bearer test' }
@@ -130,10 +129,10 @@ def test_get_survey_status(self):
130
129
mock_get .json .return_value = {'status' : 'LIVE' }
131
130
mock_request .get .return_value = mock_get
132
131
133
- self .assertEqual (self .cmix_api .get_survey_status (self .cmix_survey . id ), 'live' )
132
+ self .assertEqual (self .cmix_api .get_survey_status (self .survey_id ), 'live' )
134
133
135
134
base_url = CMIX_SERVICES ['survey' ]['BASE_URL' ]
136
- surveys_url = '{}/surveys/{}' .format (base_url , self .cmix_survey . id )
135
+ surveys_url = '{}/surveys/{}' .format (base_url , self .survey_id )
137
136
mock_request .get .assert_any_call (surveys_url , headers = self .cmix_api ._authentication_headers )
138
137
139
138
def test_get_survey_status_error_handled (self ):
@@ -146,20 +145,20 @@ def test_get_survey_status_error_handled(self):
146
145
mock_request .get .return_value = mock_get
147
146
148
147
with self .assertRaises (CmixError ):
149
- self .cmix_api .get_survey_status (self .cmix_survey . id )
148
+ self .cmix_api .get_survey_status (self .survey_id )
150
149
151
150
def test_get_survey_test_url (self ):
152
151
self .cmix_api ._authentication_headers = {'Authentication' : 'Bearer test' }
153
152
correct_test_link = '{}/#/?cmixSvy={}&cmixTest={}' .format (
154
- CMIX_SERVICES ['test' ]['BASE_URL' ], self .cmix_survey . id , 'test' )
153
+ CMIX_SERVICES ['test' ]['BASE_URL' ], self .survey_id , 'test' )
155
154
156
155
with mock .patch ('popresearch.cmix.api.requests' ) as mock_request :
157
156
mock_get = mock .Mock ()
158
157
mock_get .status_code = 200
159
158
mock_get .json .return_value = {'testToken' : 'test' }
160
159
mock_request .get .return_value = mock_get
161
160
162
- self .assertEqual (self .cmix_api .get_survey_test_url (self .cmix_survey . id ), correct_test_link )
161
+ self .assertEqual (self .cmix_api .get_survey_test_url (self .survey_id ), correct_test_link )
163
162
164
163
def test_get_survey_test_url_no_token_handled (self ):
165
164
self .cmix_api ._authentication_headers = {'Authentication' : 'Bearer test' }
@@ -170,7 +169,7 @@ def test_get_survey_test_url_no_token_handled(self):
170
169
mock_request .get .return_value = mock_get
171
170
172
171
with self .assertRaises (CmixError ):
173
- self .cmix_api .get_survey_test_url (self .cmix_survey . id )
172
+ self .cmix_api .get_survey_test_url (self .survey_id )
174
173
175
174
def test_get_survey_completes (self ):
176
175
with mock .patch ('popresearch.cmix.api.requests' ) as mock_request :
@@ -186,7 +185,7 @@ def test_get_survey_completes(self):
186
185
mock_request .get .side_effect = [
187
186
mock .Mock (json = lambda : mock_respondents ),
188
187
]
189
- self .assertEqual (self .cmix_api .get_survey_completes (self .cmix_survey . id ), mock_respondents )
188
+ self .assertEqual (self .cmix_api .get_survey_completes (self .survey_id ), mock_respondents )
190
189
191
190
def test_get_surveys (self ):
192
191
with mock .patch ('popresearch.cmix.api.requests' ) as mock_request :
@@ -229,9 +228,9 @@ def test_fetch_banner_filter(self):
229
228
question_a = 123
230
229
question_b = 124
231
230
response_id = 125
232
- self .cmix_api .fetch_banner_filter (self .cmix_survey . id , question_a , question_b , response_id )
231
+ self .cmix_api .fetch_banner_filter (self .survey_id , question_a , question_b , response_id )
233
232
expected_url = '{}/surveys/{}/response-counts' .format (
234
- CMIX_SERVICES ['reporting' ]['BASE_URL' ], self .cmix_survey . id )
233
+ CMIX_SERVICES ['reporting' ]['BASE_URL' ], self .survey_id )
235
234
expected_payload = {
236
235
'testYN' : 'LIVE' ,
237
236
'status' : 'COMPLETE' ,
@@ -248,7 +247,7 @@ def test_fetch_banner_filter(self):
248
247
249
248
def test_update_archive_status (self ):
250
249
cda = CmixDataArchive .objects .create (
251
- cmix_survey_id = self .cmix_survey . id ,
250
+ cmix_survey_id = self .survey_id ,
252
251
json = {'dataLayoutId' : 1 , 'id' : 2 }
253
252
)
254
253
with mock .patch ('popresearch.cmix.api.requests.get' ) as mock_request :
@@ -295,55 +294,3 @@ def test_add_extra_url_params(self):
295
294
extra_params_formatted = '&{}&{}' .format (extra_params [0 ], extra_params [1 ])
296
295
expected = '{}{}' .format (url , extra_params_formatted )
297
296
self .assertEqual (result , expected )
298
-
299
- def test_comparisonpop_survey_creation (self ):
300
- self .maxDiff = None
301
- with open ('backend/apps/popresearch/tests/test_files/cmix/json/survey_comparison.json' , "r" ) as json_data_file :
302
- survey_json = json .load (json_data_file )
303
- comparison_survey = Survey .objects .create (
304
- user = self .user , name = "ComparisonPop with Creative United States No Creative" ,
305
- survey_type = SURVEY_TYPE_COMPARISON , json = survey_json )
306
- with mock .patch ('popresearch.cmix.api.requests' ) as mock_request :
307
- mock_post = mock .Mock ()
308
- mock_post .status_code = 200
309
- mock_post .json .return_value = {
310
- 'id' : '100' ,
311
- 'response' : '1' ,
312
- 'token_type' : 'test' ,
313
- 'access_token' : 'test' ,
314
- }
315
- mock_request .post .return_value = mock_post
316
- self .cmix_api .create_survey (comparison_survey )
317
- # (2 Test Cells + 1 Control Cell) * (2 Geographies) = 6
318
-
319
- self .assertEqual (CmixSurvey .objects .filter (survey = comparison_survey ).count (), 6 )
320
-
321
- def test_fail_survey_creation_quits_after_10_fails (self ):
322
- self .maxDiff = None
323
- with open (
324
- 'backend/apps/popresearch/tests/test_files/cmix/json/survey_creativepop_no_ameritest.json' ,
325
- "r" ) as json_data_file :
326
- survey_json = json .load (json_data_file )
327
- creativepop_survey = Survey .objects .create (
328
- user = self .user , name = "CreativePop to mock fail" , survey_type = SURVEY_TYPE_CREATIVE , json = survey_json )
329
- with mock .patch ('popresearch.cmix.api.requests' ) as mock_request :
330
- mock_post = mock .Mock ()
331
- mock_post .status_code = 500
332
- mock_post .json .return_value = {
333
- 'id' : '100' ,
334
- 'response' : '1' ,
335
- 'token_type' : 'test' ,
336
- 'access_token' : 'test' ,
337
- }
338
- mock_request .post .return_value = mock_post
339
- for i in range (1 , 11 ):
340
- try :
341
- self .cmix_api .create_survey (creativepop_survey )
342
- except Exception :
343
- "Failure throws an exception we're ignoring here."
344
- self .assertEqual (CmixSurvey .objects .filter (survey = creativepop_survey ).count (), 0 )
345
- self .assertEqual (Survey .objects .filter (id = creativepop_survey .id )[0 ].failed_creation_attempts , i )
346
-
347
- # After 10 tries it won't try try, and thus won't fail and won't throw an error
348
- self .cmix_api .create_survey (creativepop_survey )
349
- self .assertEqual (Survey .objects .filter (id = creativepop_survey .id )[0 ].failed_creation_attempts , 10 )
0 commit comments