def finish_handle(self):

        filepath = os.path.join(settings.FILEPATH, "corehq", "pillows", "mappings", "reportcase_mapping.py")
        casepillow = ReportCasePillow(create_index=False)

        # current index
        # check current index
        aliased_indices = casepillow.check_alias()

        current_index = casepillow.es_index

        sys.stderr.write("current index:\n")
        sys.stderr.write('REPORT_CASE_INDEX="%s"\n' % current_index)

        # regenerate the mapping dict
        mapping = reportcase_mapping.REPORT_CASE_MAPPING
        casepillow.default_mapping = mapping
        delattr(casepillow, "_calc_meta_cache")
        calc_index = "%s_%s" % (casepillow.es_index_prefix, casepillow.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(
                "REPORT_CASE_INDEX hash has changed, please update \n\t%s\n\tREPORT_CASE_INDEX property with the line below:\n"
                % filepath
            )
            sys.stdout.write('REPORT_CASE_INDEX="%s"\n' % calc_index)
        else:
            sys.stderr.write("REPORT_CASE_INDEX unchanged\n")
Exemplo n.º 2
0
    def testReportCaseTransform(self):
        case = EXAMPLE_CASE
        case["domain"] = settings.ES_CASE_FULL_INDEX_DOMAINS[0]
        report_pillow = ReportCasePillow(online=False)
        processed_case = report_pillow.change_transform(case)
        mapping = report_pillow.default_mapping

        # known properties, not #value'd
        self.assertEqual(processed_case["user_id"], case["user_id"])
        self.assertEqual(processed_case["actions"][0]["doc_type"], case["actions"][0]["doc_type"])

        # dynamic case properties #valued
        self.assertEqual(processed_case["last_poop"].get("#value"), case["last_poop"])
        self.assertEqual(processed_case["diaper_type"].get("#value"), case["diaper_type"])
        self.assertTrue(isinstance(processed_case["prior_diapers"].get("#value", None), list))
        self.assertEqual(processed_case["prior_diapers"].get("#value", None), case["prior_diapers"])
    def testReportCaseTransform(self):
        case = EXAMPLE_CASE
        case['domain'] = settings.ES_CASE_FULL_INDEX_DOMAINS[0]
        report_pillow = ReportCasePillow(online=False)
        processed_case = report_pillow.change_transform(case)
        mapping = report_pillow.default_mapping

        #known properties, not #value'd
        self.assertEqual(processed_case['user_id'], case['user_id'])
        self.assertEqual(processed_case['actions'][0]['doc_type'], case['actions'][0]['doc_type'])

        #dynamic case properties #valued
        self.assertEqual(processed_case['last_poop'].get(VALUE_TAG), case['last_poop'])
        self.assertEqual(processed_case['diaper_type'].get(VALUE_TAG), case['diaper_type'])
        self.assertTrue(isinstance(processed_case['prior_diapers'], list))
        for diaper in processed_case['prior_diapers']:
            self.assertTrue(VALUE_TAG in diaper)

        self.assertEqual(case['prior_diapers'], [x[VALUE_TAG] for x in processed_case['prior_diapers']])
    def finish_handle(self):

        filepath = os.path.join(settings.FILEPATH, 'corehq','pillows','mappings','reportcase_mapping.py')
        casepillow = ReportCasePillow(create_index=False)

        #current index
        #check current index
        aliased_indices = casepillow.check_alias()

#        current_index = '%s_%s' % (casepillow.es_index_prefix, casepillow.calc_meta())
        current_index = casepillow.es_index

        #regenerate the mapping dict
        m = DEFAULT_MAPPING_WRAPPER
        m['dynamic'] = True
        m['ignore_malformed'] = True
        m['properties'] = dynamic.set_properties(self.doc_class, custom_types=case_special_types, nested_types=case_nested_types, dynamic=True)
        m['dynamic_templates'] = [dynamic.everything_is_dict_template]

        m['_meta']['comment'] = "Autogenerated [%s] mapping from ptop_generate_mapping %s" % (self.doc_class_str, datetime.utcnow().strftime('%m/%d/%Y'))

        casepillow.default_mapping = m
        delattr(casepillow, '_calc_meta_cache')
        output = []
        output.append('REPORT_CASE_INDEX="%s_%s"' % (casepillow.es_index_prefix, casepillow.calc_meta()))
        output.append('REPORT_CASE_MAPPING=%s' % pprint.pformat(m))
        newcalc_index = "%s_%s" % (casepillow.es_index_prefix, casepillow.calc_meta())
        print "Writing new case_index and mapping: %s" % output[0]
        with open(filepath, 'w') as outfile:
            outfile.write('\n'.join(output))

        if newcalc_index not in aliased_indices and newcalc_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)))

        sys.stderr.write("File written to %s\n" % filepath)
Exemplo n.º 5
0
    def finish_handle(self):

        filepath = os.path.join(settings.FILEPATH, 'corehq', 'pillows',
                                'mappings', 'reportcase_mapping.py')
        casepillow = ReportCasePillow(create_index=False)

        #current index
        #check current index
        aliased_indices = casepillow.check_alias()

        current_index = casepillow.es_index

        sys.stderr.write("current index:\n")
        sys.stderr.write('REPORT_CASE_INDEX="%s"\n' % current_index)

        #regenerate the mapping dict
        mapping = reportcase_mapping.REPORT_CASE_MAPPING
        casepillow.default_mapping = mapping
        delattr(casepillow, '_calc_meta_cache')
        calc_index = "%s_%s" % (casepillow.es_index_prefix,
                                casepillow.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(
                "REPORT_CASE_INDEX hash has changed, please update \n\t%s\n\tREPORT_CASE_INDEX property with the line below:\n"
                % filepath)
            sys.stdout.write('REPORT_CASE_INDEX="%s"\n' % calc_index)
        else:
            sys.stderr.write("REPORT_CASE_INDEX unchanged\n")
class testReportCaseProcessing(TestCase):
    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 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 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 testOwnerIDSetOnTransform(self):
        """
        Test that the owner_id gets set to the case when the pillow calls change transform
        """
        case_owner_id = CASE_WITH_OWNER_ID
        case_no_owner_id = CASE_NO_OWNER_ID

        pillow = CasePillow(create_index=False, online=False)
        changed_with_owner_id = pillow.change_transform(case_owner_id)
        changed_with_no_owner_id = pillow.change_transform(case_no_owner_id)

        self.assertEqual(changed_with_owner_id.get("owner_id"), "testuser")
        self.assertEqual(changed_with_no_owner_id.get("owner_id"), "testuser")

    def testReportXFormTransform(self):
        form = XFORM_SINGLE_CASE
        report_pillow = ReportXFormPillow(online=False)
        form['domain'] = settings.ES_XFORM_FULL_INDEX_DOMAINS[0]
        processed_form = report_pillow.change_transform(form)
        mapping = report_pillow.default_mapping

        #root level
        for k, v in processed_form['form'].items():
            if k in mapping['properties']['form']['properties']:
                if isinstance(v, dict):
                    self.assertFalse(
                        '#value' in v,
                        msg=
                        "Error, processed dict contains a #value dict for key [%s] when it shouldn't"
                        % k)
                else:
                    #it's not a dict, so that means it's ok
                    if k in form:
                        self.assertEqual(form[k], v)
            else:
                self.assertTrue(isinstance(v, dict))
                if isinstance(form['form'][k], dict):
                    #if the original is a dict, then, make sure the keys are the same
                    self.assertFalse('#value' in v)
                    self.assertEqual(sorted(form['form'][k].keys()),
                                     sorted(v.keys()))
                else:
                    self.assertTrue('#value' in v)
                    self.assertEqual(form['form'][k], v['#value'])

    def testReportCaseTransform(self):
        case = EXAMPLE_CASE
        case['domain'] = settings.ES_CASE_FULL_INDEX_DOMAINS[0]
        report_pillow = ReportCasePillow(online=False)
        processed_case = report_pillow.change_transform(case)
        mapping = report_pillow.default_mapping

        #known properties, not #value'd
        self.assertEqual(processed_case['user_id'], case['user_id'])
        self.assertEqual(processed_case['actions'][0]['doc_type'],
                         case['actions'][0]['doc_type'])

        #dynamic case properties #valued
        self.assertEqual(processed_case['last_poop'].get(VALUE_TAG),
                         case['last_poop'])
        self.assertEqual(processed_case['diaper_type'].get(VALUE_TAG),
                         case['diaper_type'])
        self.assertTrue(isinstance(processed_case['prior_diapers'], list))
        for diaper in processed_case['prior_diapers']:
            self.assertTrue(VALUE_TAG in diaper)

        self.assertEqual(
            case['prior_diapers'],
            [x[VALUE_TAG] for x in processed_case['prior_diapers']])