def testArchive(self): xform = FormProcessorInterface.create_from_generic( GenericXFormInstance(form={'foo': 'bar'}), GenericFormAttachment(name='form.xml', content='<data/>') ) self.assertEqual("XFormInstance", xform.doc_type) self.assertEqual(0, len(xform.history)) lower_bound = datetime.utcnow() - timedelta(seconds=1) FormProcessorInterface.archive_xform(xform, user='******') upper_bound = datetime.utcnow() + timedelta(seconds=1) xform = FormProcessorInterface.get_xform(xform.id) self.assertEqual('XFormArchived', xform.doc_type) [archival] = xform.history self.assertTrue(lower_bound <= archival.date <= upper_bound) self.assertEqual('archive', archival.operation) self.assertEqual('mr. librarian', archival.user) lower_bound = datetime.utcnow() - timedelta(seconds=1) FormProcessorInterface.unarchive_xform(xform, user='******') upper_bound = datetime.utcnow() + timedelta(seconds=1) xform = FormProcessorInterface.get_xform(xform.id) self.assertEqual('XFormInstance', xform.doc_type) [archival, restoration] = xform.history self.assertTrue(lower_bound <= restoration.date <= upper_bound) self.assertEqual('unarchive', restoration.operation) self.assertEqual('mr. researcher', restoration.user)
def test_broken_save(self): """ Test that if the second form submission terminates unexpectedly and the main form isn't saved, then there are no side effects such as the original having been marked as deprecated. """ class BorkDB(object): """context manager for making a db's bulk_save temporarily fail""" def __init__(self, db): self.old = {} self.db = db def __enter__(self): self.old['bulk_save'] = self.db.bulk_save self.db.bulk_save = MagicMock(name='bulk_save', side_effect=RequestFailed()) def __exit__(self, exc_type, exc_val, exc_tb): self.db.bulk_save = self.old['bulk_save'] xforms = FormProcessorInterface.get_by_doc_type(self.domain, 'XFormInstance') self.assertEqual(len(xforms), 0) xml_data1, xml_data2 = self._get_files() submit_form_locally(xml_data1, self.domain) xform = FormProcessorInterface.get_xform(self.ID) self.assertEqual(self.ID, xform.id) self.assertEqual("XFormInstance", xform.doc_type) self.assertEqual(self.domain, xform.domain) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 0 ) # This seems like a couch specific test util. Will likely need postgres test utils with BorkDB(XFormInstance.get_db()): with self.assertRaises(RequestFailed): submit_form_locally(xml_data2, self.domain) # it didn't go through, so make sure there are no edits still xforms = FormProcessorInterface.get_by_doc_type(self.domain, 'XFormDeprecated') self.assertEqual(len(xforms), 0) xform = FormProcessorInterface.get_xform(self.ID) self.assertIsNotNone(xform) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID, saved=False).count(), 1 ) self.assertEqual( UnfinishedSubmissionStub.objects.filter(xform_id=self.ID).count(), 1 )
def test_simple_delete(self): factory = CaseFactory() case = factory.create_case() [case] = factory.create_or_update_case(CaseStructure(case_id=case._id, attrs={'update': {'foo': 'bar'}})) self.assertIsNotNone(FormProcessorInterface.get_case(case.id)) self.assertEqual(2, len(case.xform_ids)) for form_id in case.xform_ids: self.assertIsNotNone(FormProcessorInterface.get_xform(form_id)) FormProcessorInterface.hard_delete_case(case) with self.assertRaises(CaseNotFound): FormProcessorInterface.get_case(case.id) for form_id in case.xform_ids: with self.assertRaises(XFormNotFound): FormProcessorInterface.get_xform(form_id)
def test_second_edit_fails(self): form_id = uuid.uuid4().hex case_id = uuid.uuid4().hex case_block = CaseBlock( create=True, case_id=case_id, case_type='person', ).as_string() submit_case_blocks(case_block, domain=self.domain, form_id=form_id) # submit an edit form with a bad case update (for example a bad ID) case_block = CaseBlock( create=True, case_id='', case_type='person', ).as_string() submit_case_blocks(case_block, domain=self.domain, form_id=form_id) xform = FormProcessorInterface.get_xform(form_id) self.assertEqual('XFormError', xform.doc_type) deprecated_xform = FormProcessorInterface.get_xform(xform.deprecated_form_id) self.assertEqual('XFormDeprecated', deprecated_xform.doc_type)