def finish_handle(self): filepath = os.path.join(settings.FILEPATH, 'corehq', 'pillows', 'mappings', 'xform_mapping.py') xform_pillow = XFormPillow(create_index=False) #check current index aliased_indices = xform_pillow.check_alias() current_index = xform_pillow.es_index sys.stderr.write("current index:\n") sys.stderr.write('XFORM_INDEX="%s"\n' % current_index) #regenerate the mapping dict mapping = xform_mapping.XFORM_MAPPING xform_pillow.default_mapping = mapping delattr(xform_pillow, '_calc_meta_cache') calc_index = "%s_%s" % (xform_pillow.es_index_prefix, xform_pillow.calc_meta()) if calc_index not in aliased_indices and calc_index != current_index: sys.stderr.write( "\n\tWarning, current index %s is not aliased at the moment\n" % current_index) sys.stderr.write("\tCurrent live aliased index: %s\n\n" % (','.join(aliased_indices))) if calc_index != current_index: sys.stderr.write( "XFORM_INDEX hash has changed, please update \n\t%s\n\tXFORM_INDEX property with the line below:\n" % filepath) sys.stdout.write('XFORM_INDEX="%s"\n' % calc_index) else: sys.stderr.write("XFORM_INDEX unchanged\n")
def test_xformPillowTransform(self): """ Test to make sure report xform and reportxform pillows strip the appVersion dict to match the mappings """ pillows = [ReportXFormPillow(online=False),XFormPillow(online=False)] bad_appVersion = { "_id": "foo", "domain": settings.ES_XFORM_FULL_INDEX_DOMAINS[0], "form": { "meta": { "@xmlns": "http://openrosa.org/jr/xforms", "username": "******", "instanceID": "foo", "userID": "some_user_id", "timeEnd": "2013-09-20T01:33:12Z", "appVersion": { "@xmlns": "http://commcarehq.org/xforms", "#text": "CCODK:\"2.5.1\"(11126). v236 CC2.5b[11126] on April-15-2013" }, "timeStart": "2013-09-19T01:13:20Z", "deviceID": "somedevice" } } } for pillow in pillows: cleaned = pillow.change_transform(bad_appVersion) self.assertFalse(isinstance(cleaned['form']['meta']['appVersion'], dict)) self.assertTrue(isinstance(cleaned['form']['meta']['appVersion'], str)) self.assertTrue(cleaned['form']['meta']['appVersion'], "CCODK:\"2.5.1\"(11126). v236 CC2.5b[11126] on April-15-2013")
def testXFormPillowSingleCaseProcess(self): """ Test that xform pillow can process and cleanup a single xform with a case submission """ xform = XFORM_SINGLE_CASE pillow = XFormPillow(create_index=False, online=False) changed = pillow.change_transform(xform) self.assertIsNone(changed['form']['case'].get('@date_modified')) self.assertIsNotNone(xform['form']['case']['@date_modified'])
def testXFormMapping(self): """ Verify that a simple case doc will yield the basic mapping """ pillow = XFormPillow(create_index=False, online=False) t1 = pillow.get_mapping_from_type(XFORM_SINGLE_CASE) t2 = pillow.get_mapping_from_type(XFORM_MULTI_CASES) self.assertEqual(t1, t2)
def testXFormPillowListCaseProcess(self): """ Test that xform pillow can process and cleanup a single xform with a list of cases in it """ xform = XFORM_MULTI_CASES pillow = XFormPillow(create_index=False, online=False) changed = pillow.change_transform(xform) changed_cases = extract_case_blocks(changed) orig_cases = extract_case_blocks(xform) [self.assertIsNotNone(x['@date_modified']) for x in orig_cases] [self.assertIsNone(x.get('@date_modified')) for x in changed_cases]
def test_get_list(self): """ Any form in the appropriate domain should be in the list from the API. """ # The actual infrastructure involves saving to CouchDB, having PillowTop # read the changes and write it to ElasticSearch. # In order to test just the API code, we set up a fake XFormES (this should # really be a parameter to the XFormInstanceResource constructor) # and write the translated form directly; we are not trying to test # the ptop infrastructure. #the pillow is set to offline mode - elasticsearch not needed to validate pillow = XFormPillow(online=False) fake_xform_es = FakeXFormES() v0_4.MOCK_XFORM_ES = fake_xform_es backend_form = XFormInstance(xmlns='fake-xmlns', domain=self.domain.name, received_on=datetime.utcnow(), form={ '#type': 'fake-type', '@xmlns': 'fake-xmlns' }) backend_form.save() translated_doc = pillow.change_transform(backend_form.to_json()) fake_xform_es.add_doc(translated_doc['_id'], translated_doc) self.client.login(username=self.username, password=self.password) response = self.client.get(self.list_endpoint) self.assertEqual(response.status_code, 200) api_forms = simplejson.loads(response.content)['objects'] self.assertEqual(len(api_forms), 1) api_form = api_forms[0] self.assertEqual(api_form['form']['@xmlns'], backend_form.xmlns) self.assertEqual(api_form['received_on'], backend_form.received_on.isoformat()) backend_form.delete()