def test_report_data_source(self): # bootstrap report data sources against indicator data sources report_config_template = get_sample_report_config() report_config_1 = ReportConfiguration.wrap(report_config_template.to_json()) report_config_1.config_id = self.ds_1._id report_config_2 = ReportConfiguration.wrap(report_config_template.to_json()) report_config_2.config_id = self.ds_2._id # save a few docs to ds 1 sample_doc, _ = get_sample_doc_and_indicators() num_docs = 3 for i in range(num_docs): sample_doc['_id'] = uuid.uuid4().hex self.ds1_adapter.save(sample_doc) # ds 1 should have data, ds2 should not ds1_rows = ReportFactory.from_spec(report_config_1).get_data() self.assertEqual(1, len(ds1_rows)) self.assertEqual(num_docs, ds1_rows[0]['count']) ds2_rows = ReportFactory.from_spec(report_config_2).get_data() self.assertEqual(0, len(ds2_rows)) # save one doc to ds 2 sample_doc['_id'] = uuid.uuid4().hex self.ds2_adapter.save(sample_doc) # ds 1 should still have same data, ds2 should now have one row ds1_rows = ReportFactory.from_spec(report_config_1).get_data() self.assertEqual(1, len(ds1_rows)) self.assertEqual(num_docs, ds1_rows[0]['count']) ds2_rows = ReportFactory.from_spec(report_config_2).get_data() self.assertEqual(1, len(ds2_rows)) self.assertEqual(1, ds2_rows[0]['count'])
def get_all_report_configs(): from corehq.apps.userreports.models import ReportConfiguration ids = [res['id'] for res in ReportConfiguration.view( 'userreports/report_configs_by_domain', reduce=False, include_docs=False, )] for result in iter_docs(ReportConfiguration.get_db(), ids): yield ReportConfiguration.wrap(result)
def migrate_report(self, report_config): rc = ReportConfiguration.wrap(report_config) for column in rc.report_columns: if column.transform and column.transform['type'] == 'translation': column.transform['mobile_or_web'] = 'mobile' column.transform['translations'] = reformat_translations(column.transform['translations']) self.reports_using_transform.add(rc._id) if not self.dry_run: return DocUpdate(rc.to_json())
def clean_columns(json_spec): static_config = StaticReportConfiguration.wrap(deepcopy(json_spec)) report_config = ReportConfiguration.wrap(static_config.config) cleaned_columns = [] for col_spec, wrapped_col in zip(json_spec['config']['columns'], report_config.report_columns): print("Checking column '{}'".format(col_spec['column_id'])) new_col_spec = clean_spec(col_spec, wrapped_col) cleaned_columns.append(order_dict(new_col_spec, COLUMN_PARAMS_ORDER)) return cleaned_columns
def post(self, request, *args, **kwargs): try: json_spec = json.loads(self.spec) if '_id' in json_spec: del json_spec['_id'] json_spec['domain'] = self.domain report = ReportConfiguration.wrap(json_spec) report.validate() report.save() messages.success(request, _('Report created!')) return HttpResponseRedirect(reverse( EditConfigReportView.urlname, args=[self.domain, report._id] )) except (ValueError, BadSpecError) as e: messages.error(request, _('Bad report source: {}').format(e)) return self.get(request, *args, **kwargs)
def test_duplicate_column_ids_pct_columns(self): spec = self.config._doc spec["columns"].append( { "type": "percent", "column_id": "pct", "numerator": { "aggregation": "sum", "field": "pct_numerator", "type": "field", "column_id": "pct_numerator", }, "denominator": {"aggregation": "sum", "field": spec["columns"][-1]["field"], "type": "field"}, } ) wrapped = ReportConfiguration.wrap(spec) with self.assertRaises(BadSpecError): wrapped.validate()
def import_report(request, domain): if request.method == "POST": spec = request.POST['report_spec'] try: json_spec = json.loads(spec) if '_id' in json_spec: del json_spec['_id'] report = ReportConfiguration.wrap(json_spec) report.validate() report.save() messages.success(request, _('Report created!')) return HttpResponseRedirect(reverse('edit_configurable_report', args=[domain, report._id])) except (ValueError, BadSpecError) as e: messages.error(request, _('Bad report source: {}').format(e)) else: spec = _('paste report source here') context = _shared_context(domain) context['spec'] = spec return render(request, "userreports/import_report.html", context)
def get_sample_report_config(): folder = os.path.join(os.path.dirname(__file__), 'data', 'configs') sample_file = os.path.join(folder, 'sample_report_config.json') with open(sample_file, encoding='utf-8') as f: structure = json.loads(f.read()) return ReportConfiguration.wrap(structure)
def test_duplicate_column_ids(self): spec = self.config._doc spec['columns'].append(spec['columns'][-1]) wrapped = ReportConfiguration.wrap(spec) with self.assertRaises(BadSpecError): wrapped.validate()
def test_report_module(self): from corehq.apps.userreports.tests.utils import get_sample_report_config app = Application.new_app('domain', "Untitled Application") report_module = app.add_module(ReportModule.new_module('Reports', None)) report_module.unique_id = 'report_module' report = get_sample_report_config() report._id = 'd3ff18cd83adf4550b35db8d391f6008' report_app_config = ReportAppConfig( report_id=report._id, header={'en': 'CommBugz'}, uuid='ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i', xpath_description='"report description"', use_xpath_description=True ) report_app_config._report = report report_module.report_configs = [report_app_config] report_module._loaded = True self.assertXmlPartialEqual( self.get_xml('reports_module_menu'), app.create_suite(), "./menu", ) app.multimedia_map = { "jr://file/commcare/image/module0_en.png": HQMediaMapItem( multimedia_id='bb4472b4b3c702f81c0b208357eb22f8', media_type='CommCareImage', unique_id='fe06454697634053cdb75fd9705ac7e6', ), } report_module.media_image = { 'en': 'jr://file/commcare/image/module0_en.png', } self.assertXmlPartialEqual( self.get_xml('reports_module_menu_multimedia'), app.create_suite(), "./menu", ) self.assertXmlPartialEqual( self.get_xml('reports_module_select_detail'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.select']", ) self.assertXmlPartialEqual( self.get_xml('reports_module_summary_detail_use_xpath_description'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']", ) self.assertXmlPartialEqual( self.get_xml('reports_module_data_detail'), app.create_suite(), "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']", ) self.assertXmlPartialEqual( self.get_xml('reports_module_data_entry'), app.create_suite(), "./entry", ) self.assertIn( 'reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i=CommBugz', app.create_app_strings('default'), ) report_app_config.use_xpath_description = False self.assertXmlPartialEqual( self.get_xml('reports_module_summary_detail_use_localized_description'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']", ) # Tuple mapping translation formats to the expected output of each translation_formats = [ ({ u'एक': { 'en': 'one', 'es': 'uno', }, '2': { 'en': 'two', 'es': 'dos\'', 'hin': u'दो', }, }, 'reports_module_data_detail-translated'), ({ u'एक': 'one', '2': 'two', }, 'reports_module_data_detail-translated-simple'), ({ u'एक': { 'en': 'one', 'es': 'uno', }, '2': 'two', }, 'reports_module_data_detail-translated-mixed'), ] for translation_format, expected_output in translation_formats: report_app_config._report.columns[0]['transform'] = { 'type': 'translation', 'translations': translation_format, } report_app_config._report = ReportConfiguration.wrap(report_app_config._report._doc) self.assertXmlPartialEqual( self.get_xml(expected_output), app.create_suite(), "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']", )
def test_duplicate_filter_slugs(self): spec = self.config._doc spec["filters"].append(spec["filters"][-1]) wrapped = ReportConfiguration.wrap(spec) with self.assertRaises(BadSpecError): wrapped.validate()
def test_duplicate_column_ids(self): spec = self.config._doc spec['columns'].append(spec['columns'][-1]) wrapped = ReportConfiguration.wrap(spec) with self.assertRaises(BadSpecError): wrapped.validate()
def test_report_module(self): from corehq.apps.userreports.tests.utils import get_sample_report_config app = Application.new_app('domain', "Untitled Application") report_module = app.add_module(ReportModule.new_module('Reports', None)) report_module.unique_id = 'report_module' report = get_sample_report_config() report._id = 'd3ff18cd83adf4550b35db8d391f6008' report_app_config = ReportAppConfig( report_id=report._id, header={'en': 'CommBugz'}, uuid='ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i', xpath_description='"report description"', use_xpath_description=True, complete_graph_configs={ chart.chart_id: GraphConfiguration( graph_type="bar", series=[GraphSeries() for c in chart.y_axis_columns], ) for chart in report.charts }, ) report_app_config._report = report report_module.report_configs = [report_app_config] report_module._loaded = True self.assertXmlPartialEqual( self.get_xml('reports_module_menu'), app.create_suite(), "./menu", ) app.multimedia_map = { "jr://file/commcare/image/module0_en.png": HQMediaMapItem( multimedia_id='bb4472b4b3c702f81c0b208357eb22f8', media_type='CommCareImage', unique_id='fe06454697634053cdb75fd9705ac7e6', ), } report_module.media_image = { 'en': 'jr://file/commcare/image/module0_en.png', } self.assertXmlPartialEqual( self.get_xml('reports_module_menu_multimedia'), app.create_suite(), "./menu", ) self.assertXmlPartialEqual( self.get_xml('reports_module_select_detail'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.select']", ) self.assertXmlPartialEqual( self.get_xml('reports_module_summary_detail_use_xpath_description'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']", ) self.assertXmlPartialEqual( self.get_xml('reports_module_data_detail'), app.create_suite(), "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']", ) report_app_config.show_data_table = False self.assertXmlPartialEqual( self.get_xml('reports_module_summary_detail_hide_data_table'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']", ) report_app_config.show_data_table = True self.assertXmlPartialEqual( self.get_xml('reports_module_data_entry'), app.create_suite(), "./entry", ) self.assertIn( 'reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i=CommBugz', app.create_app_strings('default'), ) report_app_config.use_xpath_description = False self.assertXmlPartialEqual( self.get_xml('reports_module_summary_detail_use_localized_description'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']", ) # Tuple mapping translation formats to the expected output of each translation_formats = [ ({ u'एक': { 'en': 'one', 'es': 'uno', }, '2': { 'en': 'two', 'es': 'dos\'', 'hin': u'दो', }, }, 'reports_module_data_detail-translated'), ({ u'एक': 'one', '2': 'two', }, 'reports_module_data_detail-translated-simple'), ({ u'एक': { 'en': 'one', 'es': 'uno', }, '2': 'two', }, 'reports_module_data_detail-translated-mixed'), ] for translation_format, expected_output in translation_formats: report_app_config._report.columns[0]['transform'] = { 'type': 'translation', 'translations': translation_format, } report_app_config._report = ReportConfiguration.wrap(report_app_config._report._doc) self.assertXmlPartialEqual( self.get_xml(expected_output), app.create_suite(), "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']", )
def get_sample_report_config(): folder = os.path.join(os.path.dirname(__file__), 'data', 'configs') sample_file = os.path.join(folder, 'sample_report_config.json') with open(sample_file, encoding='utf-8') as f: structure = json.loads(f.read()) return ReportConfiguration.wrap(structure)
def test_report_module(self): from corehq.apps.userreports.tests.utils import get_sample_report_config app = Application.new_app('domain', "Untitled Application", application_version=APP_V2) report_module = app.add_module(ReportModule.new_module( 'Reports', None)) report_module.unique_id = 'report_module' report = get_sample_report_config() report._id = 'd3ff18cd83adf4550b35db8d391f6008' report_app_config = ReportAppConfig( report_id=report._id, header={'en': 'CommBugz'}, uuid='ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i', xpath_description='"report description"', use_xpath_description=True) report_app_config._report = report report_module.report_configs = [report_app_config] report_module._loaded = True self.assertXmlPartialEqual( self.get_xml('reports_module_menu'), app.create_suite(), "./menu", ) app.multimedia_map = { "jr://file/commcare/image/module0_en.png": HQMediaMapItem( multimedia_id='bb4472b4b3c702f81c0b208357eb22f8', media_type='CommCareImage', unique_id='fe06454697634053cdb75fd9705ac7e6', ), } report_module.media_image = { 'en': 'jr://file/commcare/image/module0_en.png', } self.assertXmlPartialEqual( self.get_xml('reports_module_menu_multimedia'), app.create_suite(), "./menu", ) self.assertXmlPartialEqual( self.get_xml('reports_module_select_detail'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.select']", ) self.assertXmlPartialEqual( self.get_xml( 'reports_module_summary_detail_use_xpath_description'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']", ) self.assertXmlPartialEqual( self.get_xml('reports_module_data_detail'), app.create_suite(), "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']", ) self.assertXmlPartialEqual( self.get_xml('reports_module_data_entry'), app.create_suite(), "./entry", ) self.assertIn( 'reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i=CommBugz', app.create_app_strings('default'), ) report_app_config.use_xpath_description = False self.assertXmlPartialEqual( self.get_xml( 'reports_module_summary_detail_use_localized_description'), app.create_suite(), "./detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.summary']", ) report_app_config._report.columns[0]['transform'] = { 'type': 'translation', 'translations': { u'एक': [ ['en', 'one'], ['es', 'uno'], ], '2': [ ['en', 'two'], ['es', 'dos\''], ['hin', u'दो'], ], } } report_app_config._report = ReportConfiguration.wrap( report_app_config._report._doc) self.assertXmlPartialEqual( self.get_xml('reports_module_data_detail-translated'), app.create_suite(), "./detail/detail[@id='reports.ip1bjs8xtaejnhfrbzj2r6v1fi6hia4i.data']", )