Skip to content

Commit 01cfe24

Browse files
committed
removing PopResearch specific things...
1 parent 2084525 commit 01cfe24

File tree

2 files changed

+16
-74
lines changed

2 files changed

+16
-74
lines changed

src/api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,6 @@ def update_project(self, projectId, status=None):
330330
strings_and_keys = generate_survey_xml_strings_and_secondary_keys(survey, wave_number)
331331
cmix_responses = []
332332
for secondary_key, xml_string in strings_and_keys:
333-
334-
# Give up after 10 tries
335-
if survey.failed_creation_attempts >= 10:
336-
continue
337-
338333
url = '{}/surveys/data'.format(CMIX_SERVICES['file']['BASE_URL'])
339334
payload = {"data": xml_string}
340335
response = requests.post(url, payload, headers=self._authentication_headers)

tests/test_api.py

Lines changed: 16 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
from src.api import CmixAPI, default_cmix_api, CMIX_SERVICES
1515
from src.error import CmixError
1616

17-
from popresearch.models import SURVEY_TYPE_COMPARISON, SURVEY_TYPE_CREATIVE, \
18-
CmixDataArchive, CmixSurvey, Survey
17+
from popresearch.models import CmixDataArchive, Survey
1918

2019
from .factories import UserFactory
2120

@@ -26,7 +25,7 @@ def setUp(self):
2625
self.cmix_api._authentication_headers = {'Authorization': 'Bearer test'}
2726
self.user = UserFactory.create_safe()
2827
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
3029

3130
def test_cmix_authentication_check(self):
3231
with self.assertRaises(CmixError):
@@ -73,7 +72,7 @@ def test_create_export_archive(self):
7372
'name': 'Default'
7473
}]
7574
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')
7776
self.assertEqual(CmixDataArchive.objects.all().count(), 1)
7877

7978
def test_create_export_archive_errors_handled(self):
@@ -95,7 +94,7 @@ def test_create_export_archive_errors_handled(self):
9594
}]
9695
mock_get.return_value = mock_response
9796
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')
9998

10099
# Remove error from POST response.
101100
mock_post_response.json.return_value = {'response': 1}
@@ -110,7 +109,7 @@ def test_create_export_archive_errors_handled(self):
110109
}]
111110
mock_get.return_value = mock_response
112111
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')
114113

115114
# Check CmixError is raised if no default layout is returned.
116115
mock_response.status_code = 200
@@ -119,7 +118,7 @@ def test_create_export_archive_errors_handled(self):
119118
'name': 'Not Default',
120119
}]
121120
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')
123122

124123
def test_get_survey_status(self):
125124
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
@@ -130,10 +129,10 @@ def test_get_survey_status(self):
130129
mock_get.json.return_value = {'status': 'LIVE'}
131130
mock_request.get.return_value = mock_get
132131

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')
134133

135134
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)
137136
mock_request.get.assert_any_call(surveys_url, headers=self.cmix_api._authentication_headers)
138137

139138
def test_get_survey_status_error_handled(self):
@@ -146,20 +145,20 @@ def test_get_survey_status_error_handled(self):
146145
mock_request.get.return_value = mock_get
147146

148147
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)
150149

151150
def test_get_survey_test_url(self):
152151
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
153152
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')
155154

156155
with mock.patch('popresearch.cmix.api.requests') as mock_request:
157156
mock_get = mock.Mock()
158157
mock_get.status_code = 200
159158
mock_get.json.return_value = {'testToken': 'test'}
160159
mock_request.get.return_value = mock_get
161160

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)
163162

164163
def test_get_survey_test_url_no_token_handled(self):
165164
self.cmix_api._authentication_headers = {'Authentication': 'Bearer test'}
@@ -170,7 +169,7 @@ def test_get_survey_test_url_no_token_handled(self):
170169
mock_request.get.return_value = mock_get
171170

172171
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)
174173

175174
def test_get_survey_completes(self):
176175
with mock.patch('popresearch.cmix.api.requests') as mock_request:
@@ -186,7 +185,7 @@ def test_get_survey_completes(self):
186185
mock_request.get.side_effect = [
187186
mock.Mock(json=lambda: mock_respondents),
188187
]
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)
190189

191190
def test_get_surveys(self):
192191
with mock.patch('popresearch.cmix.api.requests') as mock_request:
@@ -229,9 +228,9 @@ def test_fetch_banner_filter(self):
229228
question_a = 123
230229
question_b = 124
231230
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)
233232
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)
235234
expected_payload = {
236235
'testYN': 'LIVE',
237236
'status': 'COMPLETE',
@@ -248,7 +247,7 @@ def test_fetch_banner_filter(self):
248247

249248
def test_update_archive_status(self):
250249
cda = CmixDataArchive.objects.create(
251-
cmix_survey_id=self.cmix_survey.id,
250+
cmix_survey_id=self.survey_id,
252251
json={'dataLayoutId': 1, 'id': 2}
253252
)
254253
with mock.patch('popresearch.cmix.api.requests.get') as mock_request:
@@ -295,55 +294,3 @@ def test_add_extra_url_params(self):
295294
extra_params_formatted = '&{}&{}'.format(extra_params[0], extra_params[1])
296295
expected = '{}{}'.format(url, extra_params_formatted)
297296
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

Comments
 (0)