def test_migration(self): xform = self.get_xml("form") form_bad_tz = self.get_json("form") case_bad_tz = self.get_json("case") form_good_tz = self.get_json("form-tz") case_good_tz = self.get_json("case-tz") with override_settings(PHONE_TIMEZONES_HAVE_BEEN_PROCESSED=False, PHONE_TIMEZONES_SHOULD_BE_PROCESSED=False): submit_form_locally(xform, self.domain) # Form before xform_instance, = get_forms_by_type(self.domain, "XFormInstance", limit=10) xform_json = xform_instance.to_json() self._compare_forms(xform_json, form_bad_tz, "Form before migration does not match") # Case before case, = get_cases_in_domain(self.domain) self._compare_cases(case.to_json(), case_bad_tz, "Case before migration does not match") run_timezone_migration_for_domain(self.domain) # Form after xform_instance, = get_forms_by_type(self.domain, "XFormInstance", limit=10) xform_json = xform_instance.to_json() self._compare_forms(xform_json, form_good_tz, "Form after migration does not match") # Case after case, = get_cases_in_domain(self.domain) self._compare_cases(case.to_json(), case_good_tz, "Case after migration does not match")
def test_migration(self): xform = self.get_xml('form') form_bad_tz = self.get_json('form') case_bad_tz = self.get_json('case') form_good_tz = self.get_json('form-tz') case_good_tz = self.get_json('case-tz') with override_settings(PHONE_TIMEZONES_HAVE_BEEN_PROCESSED=False, PHONE_TIMEZONES_SHOULD_BE_PROCESSED=False): submit_form_locally(xform, self.domain) # Form before xform_instance, = get_forms_by_type(self.domain, 'XFormInstance', limit=10) xform_json = xform_instance.to_json() self._compare_forms(xform_json, form_bad_tz, "Form before migration does not match") # Case before case, = get_cases_in_domain(self.domain) self._compare_cases(case.to_json(), case_bad_tz, "Case before migration does not match") run_timezone_migration_for_domain(self.domain) # Form after xform_instance, = get_forms_by_type(self.domain, 'XFormInstance', limit=10) xform_json = xform_instance.to_json() self._compare_forms(xform_json, form_good_tz, "Form after migration does not match") # Case after case, = get_cases_in_domain(self.domain) self._compare_cases(case.to_json(), case_good_tz, "Case after migration does not match")
def test_get_forms_by_type_xformerror(self): forms = get_forms_by_type(self.domain, 'XFormError', limit=10) self.assertEqual(len(forms), len(self.xform_errors)) self.assertEqual({form._id for form in forms}, {form._id for form in self.xform_errors}) for form in forms: self.assertIsInstance(form, XFormError)
def testSubmissionError(self): evil_laugh = "mwa ha ha!" def fail(sender, xform, **kwargs): raise Exception(evil_laugh) successful_form_received.connect(fail) try: file = os.path.join(os.path.dirname(__file__), "data", "simple_form.xml") with open(file) as f: res = self.client.post(self.url, { "xml_submission_file": f }) self.assertEqual(201, res.status_code) self.assertIn(evil_laugh, res.content) # make sure we logged it [log] = get_forms_by_type(self.domain.name, 'XFormError', limit=1) self.assertIsNotNone(log) self.assertIn(evil_laugh, log.problem) with open(file) as f: self.assertEqual(f.read(), log.get_xml()) finally: successful_form_received.disconnect(fail)
def get(self, count): # this is a hack, but override the doc type because there is an # equivalent doc type in the view def _update_doc_type(form): form.doc_type = self.doc_type return form forms = get_forms_by_type(self.domain, self.doc_type, recent_first=True, limit=count) return [_update_doc_type(form) for form in forms]
def testSubmitBadXML(self): f, path = tmpfile() with f: f.write("this isn't even close to xml") with open(path) as f: res = self.client.post(self.url, {"xml_submission_file": f}) self.assertEqual(500, res.status_code) self.assertIn("Invalid XML", res.content) # make sure we logged it [log] = get_forms_by_type(self.domain.name, "SubmissionErrorLog", limit=1) self.assertIsNotNone(log) self.assertIn("Invalid XML", log.problem) self.assertEqual("this isn't even close to xml", log.get_xml())
def testSubmitDuplicate(self): file = os.path.join(os.path.dirname(__file__), "data", "simple_form.xml") with open(file) as f: res = self.client.post(self.url, {"xml_submission_file": f}) self.assertEqual(201, res.status_code) self.assertIn(u" √ ".encode("utf-8"), res.content) with open(file) as f: res = self.client.post(self.url, {"xml_submission_file": f}) self.assertEqual(201, res.status_code) self.assertIn("Form is a duplicate", res.content) # make sure we logged it [log] = get_forms_by_type(self.domain.name, "XFormDuplicate", limit=1) self.assertIsNotNone(log) self.assertIn("Form is a duplicate", log.problem) with open(file) as f: self.assertEqual(f.read(), log.get_xml())
def get_forms_by_type(domain, type_, limit, recent_first=False): return get_forms_by_type(domain, type_, recent_first, limit)
def get_last_form_submission(self): result = get_forms_by_type(self.domain, 'XFormInstance', recent_first=True, limit=1) return result[0] if len(result) > 0 else None
def get_last_form_submission(self): [form] = get_forms_by_type(self.domain, 'XFormInstance', recent_first=True, limit=1) return form