def _delete_data_source_shared(request, domain, config_id): config = get_document_or_404(DataSourceConfiguration, domain, config_id) adapter = IndicatorSqlAdapter(get_engine(), config) adapter.drop_table() config.delete() messages.success(request, _(u'Data source "{}" has been deleted.'.format(config.display_name)))
class IndicatorPillowTestBase(TestCase): def setUp(self): self.config = get_sample_data_source() self.config.save() self.adapter = IndicatorSqlAdapter(self.config) self.fake_time_now = datetime(2015, 4, 24, 12, 30, 8, 24886) def tearDown(self): self.config.delete() self.adapter.drop_table() @patch('corehq.apps.userreports.specs.datetime') def _check_sample_doc_state(self, expected_indicators, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now self.assertEqual(1, self.adapter.get_query_object().count()) row = self.adapter.get_query_object()[0] for k in row.keys(): v = getattr(row, k) if isinstance(expected_indicators[k], decimal.Decimal): self.assertAlmostEqual(expected_indicators[k], v) else: self.assertEqual( expected_indicators[k], v, 'mismatched property: {} (expected {}, was {})'.format( k, expected_indicators[k], v ) )
class IndicatorPillowTestBase(TestCase): def setUp(self): super(IndicatorPillowTestBase, self).setUp() self.config = get_sample_data_source() self.config.save() self.adapter = IndicatorSqlAdapter(self.config) self.fake_time_now = datetime(2015, 4, 24, 12, 30, 8, 24886) def tearDown(self): self.config.delete() self.adapter.drop_table() super(IndicatorPillowTestBase, self).tearDown() @patch('corehq.apps.userreports.specs.datetime') def _check_sample_doc_state(self, expected_indicators, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now self.assertEqual(1, self.adapter.get_query_object().count()) row = self.adapter.get_query_object()[0] for k in row.keys(): v = getattr(row, k) if isinstance(expected_indicators[k], decimal.Decimal): self.assertAlmostEqual(expected_indicators[k], v) else: self.assertEqual( expected_indicators[k], v, 'mismatched property: {} (expected {}, was {})'.format( k, expected_indicators[k], v))
def delete_data_source(request, domain, config_id): config = get_document_or_404(DataSourceConfiguration, domain, config_id) adapter = IndicatorSqlAdapter(get_engine(), config) adapter.drop_table() config.delete() messages.success(request, _(u'Data source "{}" has been deleted.'.format(config.display_name))) return HttpResponseRedirect(reverse('configurable_reports_home', args=[domain]))
def delete_data_source_shared(domain, config_id, request=None): config = get_document_or_404(DataSourceConfiguration, domain, config_id) adapter = IndicatorSqlAdapter(config) adapter.drop_table() config.delete() if request: messages.success( request, _(u'Data source "{}" has been deleted.'.format( config.display_name)))
class IndicatorPillowTest(TestCase): def setUp(self): folder = os.path.join(os.path.dirname(__file__), 'data', 'configs') sample_file = os.path.join(folder, 'sample_indicator_config.json') self.pillow = ConfigurableIndicatorPillow() self.engine = self.pillow.get_sql_engine() with open(sample_file) as f: structure = json.loads(f.read()) self.config = DataSourceConfiguration.wrap(structure) self.pillow.bootstrap(configs=[self.config]) self.adapter = IndicatorSqlAdapter(self.engine, self.config) self.adapter.rebuild_table() def tearDown(self): self.adapter.drop_table() self.engine.dispose() def testFilter(self): # note: this is a silly test now that python_filter always returns true not_matching = [ dict(doc_type="NotCommCareCase", domain='user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='not-user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='user-reports', type='not-ticket'), ] for document in not_matching: self.assertTrue(self.pillow.python_filter(document)) self.assertTrue(self.pillow.python_filter( dict(doc_type="CommCareCase", domain='user-reports', type='ticket') )) def testChangeTransport(self): # indicators sample_doc, expected_indicators = get_sample_doc_and_indicators() self.pillow.change_transport(sample_doc) with self.engine.begin() as connection: rows = connection.execute(sqlalchemy.select([self.adapter.get_table()])) self.assertEqual(1, rows.rowcount) row = rows.fetchone() for k, v in row.items(): self.assertEqual(expected_indicators[k], v)
class IndicatorPillowTest(TestCase): def setUp(self): self.config = get_sample_data_source() self.pillow = ConfigurableIndicatorPillow() self.pillow.bootstrap(configs=[self.config]) self.adapter = IndicatorSqlAdapter(self.config) self.fake_time_now = datetime(2015, 4, 24, 12, 30, 8, 24886) def tearDown(self): self.adapter.drop_table() def test_filter(self): # note: this is a silly test now that python_filter always returns true not_matching = [ dict(doc_type="NotCommCareCase", domain='user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='not-user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='user-reports', type='not-ticket'), ] for document in not_matching: self.assertTrue(self.pillow.python_filter(document)) self.assertTrue( self.pillow.python_filter( dict(doc_type="CommCareCase", domain='user-reports', type='ticket'))) @patch('corehq.apps.userreports.specs.datetime') def test_change_transport(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now sample_doc, _ = get_sample_doc_and_indicators(self.fake_time_now) self.pillow.change_transport(sample_doc) self._check_sample_doc_state() @patch('corehq.apps.userreports.specs.datetime') def test_rebuild_indicators(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now self.config.save() sample_doc, _ = get_sample_doc_and_indicators(self.fake_time_now) CommCareCase.get_db().save_doc(sample_doc) rebuild_indicators(self.config._id) self._check_sample_doc_state() def test_bad_integer_datatype(self): self.config.save() bad_ints = ['a', '', None] for bad_value in bad_ints: self.pillow.change_transport({ '_id': uuid.uuid4().hex, 'doc_type': 'CommCareCase', 'domain': 'user-reports', 'type': 'ticket', 'priority': bad_value }) # make sure we saved rows to the table for everything self.assertEqual(len(bad_ints), self.adapter.get_query_object().count()) @patch('corehq.apps.userreports.specs.datetime') def _check_sample_doc_state(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now _, expected_indicators = get_sample_doc_and_indicators( self.fake_time_now) self.assertEqual(1, self.adapter.get_query_object().count()) row = self.adapter.get_query_object()[0] for k in row.keys(): v = getattr(row, k) if isinstance(expected_indicators[k], decimal.Decimal): self.assertAlmostEqual(expected_indicators[k], v) else: self.assertEqual(expected_indicators[k], v)
def tearDownClass(cls): cls._call_center_domain_mock.stop() adapter = IndicatorSqlAdapter(cls.datasource) adapter.drop_table() super(BaseICDSDatasourceTest, cls).tearDownClass()
class IndicatorPillowTest(TestCase): def setUp(self): self.config = get_sample_data_source() self.pillow = ConfigurableIndicatorPillow() self.engine = self.pillow.get_sql_engine() self.pillow.bootstrap(configs=[self.config]) self.adapter = IndicatorSqlAdapter(self.engine, self.config) self.adapter.rebuild_table() def tearDown(self): self.adapter.drop_table() self.engine.dispose() def test_filter(self): # note: this is a silly test now that python_filter always returns true not_matching = [ dict(doc_type="NotCommCareCase", domain='user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='not-user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='user-reports', type='not-ticket'), ] for document in not_matching: self.assertTrue(self.pillow.python_filter(document)) self.assertTrue(self.pillow.python_filter( dict(doc_type="CommCareCase", domain='user-reports', type='ticket') )) def test_change_transport(self): sample_doc, _ = get_sample_doc_and_indicators() self.pillow.change_transport(sample_doc) self._check_sample_doc_state() def test_rebuild_indicators(self): self.config.save() sample_doc, _ = get_sample_doc_and_indicators() CommCareCase.get_db().save_doc(sample_doc) rebuild_indicators(self.config._id) self._check_sample_doc_state() def test_bad_integer_datatype(self): self.config.save() bad_ints = ['a', '', None] for bad_value in bad_ints: self.pillow.change_transport({ '_id': uuid.uuid4().hex, 'doc_type': 'CommCareCase', 'domain': 'user-reports', 'type': 'ticket', 'priority': bad_value }) # make sure we saved rows to the table for everything with self.engine.begin() as connection: rows = connection.execute(sqlalchemy.select([self.adapter.get_table()])) self.assertEqual(len(bad_ints), rows.rowcount) def _check_sample_doc_state(self): _, expected_indicators = get_sample_doc_and_indicators() with self.engine.begin() as connection: rows = connection.execute(sqlalchemy.select([self.adapter.get_table()])) self.assertEqual(1, rows.rowcount) row = rows.fetchone() for k, v in row.items(): if isinstance(expected_indicators[k], decimal.Decimal): self.assertAlmostEqual(expected_indicators[k], v) else: self.assertEqual(expected_indicators[k], v)
class IndicatorPillowTest(TestCase): def setUp(self): self.config = get_sample_data_source() self.config.save() self.pillow = ConfigurableIndicatorPillow() self.pillow.bootstrap(configs=[self.config]) self.adapter = IndicatorSqlAdapter(self.config) self.fake_time_now = datetime(2015, 4, 24, 12, 30, 8, 24886) def tearDown(self): self.config.delete() self.adapter.drop_table() def test_stale_rebuild(self): later_config = copy(self.config) later_config.save() self.assertNotEqual(self.config._rev, later_config._rev) with self.assertRaises(StaleRebuildError): self.pillow.rebuild_table(IndicatorSqlAdapter(self.config)) @patch("corehq.apps.userreports.specs.datetime") def test_change_transport(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now sample_doc, _ = get_sample_doc_and_indicators(self.fake_time_now) self.pillow.change_transport(sample_doc) self._check_sample_doc_state() @patch("corehq.apps.userreports.specs.datetime") def test_rebuild_indicators(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now self.config.save() sample_doc, _ = get_sample_doc_and_indicators(self.fake_time_now) CommCareCase.get_db().save_doc(sample_doc) rebuild_indicators(self.config._id) self._check_sample_doc_state() def test_bad_integer_datatype(self): self.config.save() bad_ints = ["a", "", None] for bad_value in bad_ints: self.pillow.change_transport( { "_id": uuid.uuid4().hex, "doc_type": "CommCareCase", "domain": "user-reports", "type": "ticket", "priority": bad_value, } ) # make sure we saved rows to the table for everything self.assertEqual(len(bad_ints), self.adapter.get_query_object().count()) @patch("corehq.apps.userreports.specs.datetime") def _check_sample_doc_state(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now _, expected_indicators = get_sample_doc_and_indicators(self.fake_time_now) self.assertEqual(1, self.adapter.get_query_object().count()) row = self.adapter.get_query_object()[0] for k in row.keys(): v = getattr(row, k) if isinstance(expected_indicators[k], decimal.Decimal): self.assertAlmostEqual(expected_indicators[k], v) else: self.assertEqual(expected_indicators[k], v)
class IndicatorPillowTest(TestCase): def setUp(self): self.config = get_sample_data_source() self.config.save() self.pillow = ConfigurableIndicatorPillow() self.pillow.bootstrap(configs=[self.config]) self.adapter = IndicatorSqlAdapter(self.config) self.fake_time_now = datetime(2015, 4, 24, 12, 30, 8, 24886) def tearDown(self): self.config.delete() self.adapter.drop_table() def test_filter(self): # note: this is a silly test now that python_filter always returns true not_matching = [ dict(doc_type="NotCommCareCase", domain='user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='not-user-reports', type='ticket'), dict(doc_type="CommCareCase", domain='user-reports', type='not-ticket'), ] for document in not_matching: self.assertTrue(self.pillow.python_filter(document)) self.assertTrue(self.pillow.python_filter( dict(doc_type="CommCareCase", domain='user-reports', type='ticket') )) def test_stale_rebuild(self): later_config = copy(self.config) later_config.save() self.assertNotEqual(self.config._rev, later_config._rev) with self.assertRaises(StaleRebuildError): self.pillow.rebuild_table(IndicatorSqlAdapter(self.config)) @patch('corehq.apps.userreports.specs.datetime') def test_change_transport(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now sample_doc, _ = get_sample_doc_and_indicators(self.fake_time_now) self.pillow.change_transport(sample_doc) self._check_sample_doc_state() @patch('corehq.apps.userreports.specs.datetime') def test_rebuild_indicators(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now self.config.save() sample_doc, _ = get_sample_doc_and_indicators(self.fake_time_now) CommCareCase.get_db().save_doc(sample_doc) rebuild_indicators(self.config._id) self._check_sample_doc_state() def test_bad_integer_datatype(self): self.config.save() bad_ints = ['a', '', None] for bad_value in bad_ints: self.pillow.change_transport({ '_id': uuid.uuid4().hex, 'doc_type': 'CommCareCase', 'domain': 'user-reports', 'type': 'ticket', 'priority': bad_value }) # make sure we saved rows to the table for everything self.assertEqual(len(bad_ints), self.adapter.get_query_object().count()) @patch('corehq.apps.userreports.specs.datetime') def _check_sample_doc_state(self, datetime_mock): datetime_mock.utcnow.return_value = self.fake_time_now _, expected_indicators = get_sample_doc_and_indicators(self.fake_time_now) self.assertEqual(1, self.adapter.get_query_object().count()) row = self.adapter.get_query_object()[0] for k in row.keys(): v = getattr(row, k) if isinstance(expected_indicators[k], decimal.Decimal): self.assertAlmostEqual(expected_indicators[k], v) else: self.assertEqual(expected_indicators[k], v)