def test_sharing(self): """ saving, loading and sharing semantics """ def _menu(cred, private=[], unit=[], public=[], quick=[]): opts = reports.ReportMenu(cred, 2, 'line').by_sharing self.assertEqual([i.label for i in opts['private']], private) self.assertEqual([i.label for i in opts['unit']], unit) self.assertEqual([i.label for i in opts['public']], public) self.assertEqual([i.label for i in opts['quick']], quick) cred_user = testcommon.DummyCredentials() cred_unit = testcommon.DummyCredentials(user_id=2) cred_other = testcommon.DummyCredentials(user_id=2,unit_id=2) params = reports.new_report(2, 'line') params.label = 'Test Save' # Only visible to us params.sharing = 'private' params.save(cred_user) _menu(cred_user, private=['Test Save']) _menu(cred_unit) _menu(cred_other) # Visible to our unit params.sharing = 'unit' params.save(cred_user) _menu(cred_user, unit=['Test Save']) _menu(cred_unit, unit=['Test Save']) _menu(cred_other) # Visible to all params.sharing = 'public' params.save(cred_user) _menu(cred_user, public=['Test Save']) _menu(cred_unit, public=['Test Save']) _menu(cred_other, public=['Test Save']) # Visible to all & on home page (quick reports) reports.reports_cache.load() qr = reports.reports_cache.get_synd_unit(2, 1) self.assertEqual([r.label for r in qr], []) params.sharing = 'quick' params.save(cred_user) _menu(cred_user, quick=['Test Save']) _menu(cred_unit, quick=['Test Save']) _menu(cred_other, quick=['Test Save']) reports.reports_cache.load() qr = reports.reports_cache.get_synd_unit(2, 1) self.assertEqual([r.label for r in qr], ['Test Save']) # Loading loaded_params = reports.load(params.loaded_from_id, cred_user) self.failIf(loaded_params.check().have_errors()) reports.delete(loaded_params.loaded_from_id) _menu(cred_user) _menu(cred_unit) _menu(cred_other) # "last" autosave params.autosave(cred_user) _menu(cred_user, private=['Most recent: Test Save']) _menu(cred_unit) _menu(cred_other)
def test_import_named_update(self): cred = testcommon.DummyCredentials() rules = xmlload(StringIO(import_named_xml)) rules.add(ImportSource('local_case_id', 'Id')) src = datasrc.DataImpSrc('foo', StringIO(data)) imp = dataimp.DataImp(cred, 1, src, rules) self.failIf(imp.errors, imp.errors) self.assertEqual(imp.errors.count(), 0) self.assertEqual(imp.new_cnt, 3) self.assertEqual(imp.update_cnt, 0) self.assertListEq(self._fetch_rows(), [ ('BLOGS', dt('2001-11-24'), 0, 'confirmed', 'NSW', None, '100', 1.0, 'Unknown', None, True, False, False), ('SMITH', age3y, 366, 'confirmed', 'NSW', None, '101', 2.0, 'Unknown', None, False, True, True), ('JONES', age4m, 31, 'preliminary', 'NSW', None, '102', None, 'Unknown', None, False, False, False), ]) src = datasrc.DataImpSrc('foo', StringIO(data + data_update)) imp = dataimp.DataImp(cred, 1, src, rules) self.assertEqual(imp.new_cnt, 1) self.assertEqual(imp.update_cnt, 4) self.failIf(imp.errors, imp.errors) self.assertEqual(imp.errors.count(), 0) self.assertListEq(self._fetch_rows(), [ ('BLOGS', dt('2001-11-24'), 0, 'confirmed', 'NSW', None, '100', 1.0, 'Unknown', None, True, False, False), ('SMITH', dt('2000-01-20'), 0, 'confirmed', 'NSW', None, '101', 2.0, 'Unknown', None, False, False, False), ('JONES', age4m, 31, 'preliminary', 'NSW', None, '102', None, 'Unknown', None, False, False, False), ('WILLIAMS', dt('1940-12-02'), 0, 'excluded', 'NSW', None, '104', 3.0, 'Unknown', None, False, True, False), ])
def test_export_forms(self): """ CSV report export (by form) """ credentials = testcommon.DummyCredentials() params = self.make_report() params.export_row_type = 'forms' params.export_column_labels = 'fields' rows = list(params.export_rows(credentials)) self.assertEqual(rows, self.export_forms_label_expect) params.export_column_labels = 'dbcols' rows = list(params.export_rows(credentials)) self.assertEqual(rows, self.export_forms_dbcols_expect)
def test_import_named(self): cred = testcommon.DummyCredentials() rules = xmlload(StringIO(import_named_xml)) src = datasrc.DataImpSrc('foo', StringIO(data)) imp = dataimp.DataImp(cred, 1, src, rules) self.failIf(imp.errors, imp.errors) self.assertEqual(imp.errors.count(), 0) self.assertEqual(imp.new_cnt, 3) self.assertEqual(imp.update_cnt, 0) self.assertListEq(self._fetch_rows(), [ ('BLOGS', dt('2001-11-24'), 0, 'confirmed', 'NSW', None, None, 1.0, 'Unknown', None, True, False, False), ('SMITH', age3y, 366, 'confirmed', 'NSW', None, None, 2.0, 'Unknown', None, False, True, True), ('JONES', age4m, 31, 'preliminary', 'NSW', None, None, None, 'Unknown', None, False, False, False), ])
def test_report(self): """ report generation """ credentials = testcommon.DummyCredentials() params = self.make_report() self.assertEqual(params.get_case_ids(credentials), [1, 2, 5]) self.assertEqual(params.count(credentials), 3) msgs = messages.Messages() chunker = params.report(credentials, msgs) report_headings = list(common.expect_headings) report_headings.remove('Date of birth/Age') self.assertEqual(chunker.headings(), report_headings) result = list(chunker) self.assertEqual(len(result), len(expectrows)) for got, (col_want, text_want) in izip(result, expectrows): self.assertEqual(got.columns, col_want) self.assertEqual(got.freetext, text_want)
def test_preview_errors(self): cred = testcommon.DummyCredentials() rules = xmlload(StringIO(error_named_xml)) data = '''\ Surname,Status,DoB,Age,Duration,Contact,Date,Id,Likes blogs,XXX,24/11/2001,,,False,,100, smith,confirmed,,10000,2,True,,101,Apples jones,suspected,2008-1-30,,,Unknown,,102,Tomato ,,,,,,,, williams,,,,a,XX,XX,103, ,,,,,, ''' src = datasrc.DataImpSrc('foo', StringIO(data)) imp = dataimp.PreviewImport(cred, 1, src, rules) self.assertListEq(list(imp.errors), [ 'foo: record 5 (line 7): Column count is not constant: has 7 columns, expected 9', "record 1 (line 2): Status: 'XXX' not a valid choice", "record 2 (line 3): Date of birth/Age: date/time '10000' does not match format 'DD/MM/YYYY'", "record 3 (line 4): Date of birth/Age: date/time '2008-1-30' does not match format 'DD/MM/YYYY'", 'record 3 (line 4): Favourite foods: tomato not valid choice(s)', 'record 4 (line 5): Either Surname or Local ID must be specified', 'record 4 (line 5): Exposure History (SARS): Contact with case: this field must be answered', "record 5 (line 6): Contact with case: 'XX' not a valid choice", 'record 5 (line 6): Exposure History (SARS): Contact with case: this field must be answered', 'record 5 (line 6): Exposure History (SARS): Contact duration (hours): value must be a number', 'record 5 (line 6): Exposure History (SARS): Date of first contact: could not parse date "XX"', ]) self.assertEqual(imp.errors.count(), 11) self.failUnless(0 not in imp.errors) self.failUnless(1 in imp.errors) self.failUnless(5 in imp.errors) self.failUnless(6 not in imp.errors) self.assertEqual(imp.errors.get(4), [ 'record 4 (line 5): Either Surname or Local ID must be specified', 'record 4 (line 5): Exposure History (SARS): Contact with case: this field must be answered', ]) # Check "too many errors" handling lines = data.splitlines() data = '\n'.join([lines[0]] + [lines[1]] * 101) src = datasrc.DataImpSrc('foo', StringIO(data)) imp = dataimp.PreviewImport(cred, 1, src, rules) self.assertEqual(list(imp.errors)[0], 'More than %s errors, giving up' % imp.errors.MAX_ERRORS) self.assertEqual(imp.errors.count(), 100)
def _test_preview(self, rules_xml, data): cred = testcommon.DummyCredentials() rules = xmlload(StringIO(rules_xml)) src = datasrc.DataImpSrc('foo', StringIO(data)) now = DateTime.DateTime(2010,7,20,17,23,1) imp = testcommon.freeze_time(now, dataimp.PreviewImport, cred, 1, src, rules) self.failIf(imp.errors, imp.errors) self.assertEqual(imp.group_header, [('Demographics', 4), ('Exposure History (SARS)', 3)]) self.assertEqual(imp.header, [ 'Status', 'Surname', 'Date of birth/Age', 'Locality/Suburb', 'Contact with case', 'Contact duration (hours)', 'Favourite foods', ]) self.assertEqual(imp.rows, [ ['Confirmed', 'BLOGS', '24/11/2001 (8y)', 'NSW', 'Unknown', '1', 'Icecream'], ['Confirmed', 'SMITH', '3 years', 'NSW', 'Unknown', '2', 'Lamb Chops/Apples'], ['Preliminary', 'JONES', '4 months', 'NSW', 'Unknown', None, None] ])
def test_crosstab(self): credentials = testcommon.DummyCredentials() params = self.make_report(crosstab) self.assertEqual(params.row.form_options(), [ ('none:', 'None'), ('demog:', 'Demographic fields'), ('form:sars_exposure', 'Exposure History (SARS)'), ('form:hospital_admit', 'Hospital admission'), ]) self.assertEqual(params.row.show_fields(), True) self.assertEqual(params.col.show_fields(), True) self.assertEqual(params.page.show_fields(), True) self.assertListEq(params.row.col_options(), demog_col_options) self.assertListEq(params.col.col_options(), demog_col_options) self.assertListEq(params.page.col_options(), [('close_contact', 'Contact with case')]) msgs = messages.Messages() report = params.report(credentials, msgs) self.assertEqual(report.row.options, row_options) self.assertEqual(report.col.options, col_options) self.assertEqual(report.page.options, page_options) # Check counts pages = [] for page_val, page_lab in report.page.options: page = [] for row_val, row_label in report.row.options: row = [] for col_val, col_label in report.col.options: key = row_val, col_val, page_val row.append(report.tally[key]) page.append(row) pages.append(page) self.assertEqual(pages, cells_expect) # Check styling pages = [] for page_val, page_lab in report.page.options: page = [] for row_val, row_label in report.row.options: row = [] for col_val, col_label in report.col.options: row.append(report.style(row_val, col_val)) page.append(row) pages.append(page) self.assertEqual(pages, style_expect) # Case ID Lookup pages = [] for page_idx in range(len(report.page.options)): page = [] for row_idx in range(len(report.row.options)): row = [] for col_idx in range(len(report.col.options)): coords = row_idx, col_idx, page_idx row.append(report.get_key_case_ids(*coords)) page.append(row) pages.append(page) self.assertEqual(pages, id_expect) # Cell desc Lookup pages = [] for page_idx in range(len(report.page.options)): page = [] for row_idx in range(len(report.row.options)): row = [] for col_idx in range(len(report.col.options)): coords = row_idx, col_idx, page_idx row.append(report.desc_key(*coords)) page.append(row) pages.append(page) self.assertEqual(pages, desc_expect)
def test_errors(self): rules = xmlload(StringIO(import_named_xml)) cred = testcommon.DummyCredentials() imp = dataimp.PreviewImport(cred, 1, datasrc.NullDataImpSrc, rules) self.assertEqual(list(imp.errors), ['No data source selected'])