Example #1
0
    def handle(self, *args, **options):

        self.stdout.write("Pulling stuff\n")
        beneficiaries = CommCareCase.get_all_cases('opm', include_docs=True)
        users = CommCareUser.by_domain('opm')
        fixtures = FixtureDataItem.get_item_list('opm', 'condition_amounts')
        # you won't be able to get the fixtures without the FixtureDataType
        fixtures.append(FixtureDataType.by_domain_tag('opm',
            'condition_amounts').one())
        forms = []

        for b in beneficiaries:
            forms += b.get_forms()

        test_data = []

        month, year = test_month_year
        for report_class in [IncentivePaymentReport, BeneficiaryPaymentReport]:
            self.stdout.write("Running %s\n" % report_class.__name__)
            report = get_report(report_class, month, year)
            snapshot = OpmReportSnapshot(
                domain=DOMAIN,
                month=month,
                year=year,
                report_class=report.report_class.__name__,
                headers=report.headers,
                slugs=report.slugs,
                rows=report.rows,
            )
            test_data.append(snapshot.to_json())

        self.stdout.write("Saving raw data\n")
        test_data += [form.to_json() for form in forms]
        test_data += [u.to_json() for u in users]
        test_data += [b.to_json() for b in beneficiaries]
        test_data += [f.to_json() for f in fixtures]

        doc_ids = set()
        docs = []
        for doc in test_data:
            if doc.get('_id') not in doc_ids:
                for attrib in ['_rev', '_attachments']:
                    try:
                        del doc[attrib]
                    except KeyError:
                        pass                
                doc_ids.add(doc.get('_id', 'null'))
                docs.append(doc)
            else:
                self.stdout.write("Ignoring duplicates\n")

        with open(test_data_location, 'w') as f:
            f.write(json.dumps(docs, indent=2))
            
        self.stdout.write("Pulled stuff, let's hope it worked\n")
Example #2
0
    def test_all_results(self):
        print "%s Tests:" % self.ReportClass.__name__
        month, year = test_month_year
        # report produced from test data
        report = get_report(self.ReportClass, month, year)
        # saved report snapshot
        snapshot = OpmReportSnapshot.by_month(month, year,
                                              self.ReportClass.__name__)
        errors = []
        total = len(snapshot.rows)

        name_index = snapshot.slugs.index('name')

        def stringify(row):
            string_row = []
            for element in row:
                element = element or ""
                try:
                    str(element)
                except:
                    string_row.append(element)
                else:
                    string_row.append(str(element))
            return string_row

        report_rows = sorted(report.rows, key=stringify)
        self.assertEquals(
            total, len(report_rows),
            "different number of rows for %s" % self.ReportClass.__name__)
        for i, snapshot_row in enumerate(sorted(snapshot.rows, key=stringify)):
            report_row = report_rows[i]
            for snapshot_index, slug in enumerate(snapshot.slugs):
                if slug != 'bank_branch_name':
                    report_index = report.slugs.index(slug)
                    snapshot_item = snapshot_row[snapshot_index] or ""
                    report_item = report_row[report_index] or ""
                    if not self.the_same(snapshot_item, report_item):
                        errors.append('%s \t"%s" != "%s" \t%s' %
                                      (slug, snapshot_item, report_item,
                                       report_row[name_index]))
        self.assertEquals(errors, [], "\n\n" + '\n'.join(errors))
Example #3
0
    def test_all_results(self):
        print "%s Tests:" % self.ReportClass.__name__
        month, year = test_month_year
        # report produced from test data
        report = get_report(self.ReportClass, month, year)
        # saved report snapshot
        snapshot = OpmReportSnapshot.by_month(month, year,
            self.ReportClass.__name__)
        errors = []
        total = len(snapshot.rows)

        name_index = snapshot.slugs.index('name')
        def stringify(row):
            string_row = []
            for element in row:
                element = element or ""
                try:
                    str(element)
                except:
                    string_row.append(element)
                else:
                    string_row.append(str(element))
            return string_row

        report_rows = sorted(report.rows, key=stringify)
        self.assertEquals(total, len(report_rows),
            "different number of rows for %s" % self.ReportClass.__name__)
        for i, snapshot_row in enumerate(sorted(snapshot.rows, key=stringify)):
            report_row = report_rows[i]
            for snapshot_index, slug in enumerate(snapshot.slugs):
                if slug != 'bank_branch_name':
                    report_index = report.slugs.index(slug)
                    snapshot_item = snapshot_row[snapshot_index] or ""
                    report_item = report_row[report_index] or ""
                    if not self.the_same(snapshot_item, report_item):
                        errors.append('%s \t"%s" != "%s" \t%s' %
                            (slug, snapshot_item, report_item, report_row[name_index]))
        self.assertEquals(errors, [], "\n\n" + '\n'.join(errors))