def test_bootstrap_sets_time(self): before_now = datetime.utcnow() - timedelta(microseconds=1) pillow = ConfigurableIndicatorPillow() pillow.bootstrap([]) after_now = datetime.utcnow() + timedelta(microseconds=1) self.assertTrue(pillow.bootstrapped) self.assertTrue(before_now < pillow.last_bootstrapped) self.assertTrue(after_now > pillow.last_bootstrapped) self.assertFalse(pillow.needs_bootstrap())
def test_bootstrap_sets_time(self): before_now = datetime.utcnow() - timedelta(microseconds=1) pillow = ConfigurableIndicatorPillow() pillow.bootstrap([]) after_now = datetime.utcnow() + timedelta(microseconds=1) self.assertTrue(pillow.bootstrapped) self.assertTrue(before_now < pillow.last_bootstrapped) self.assertTrue(after_now > pillow.last_bootstrapped) self.assertFalse(pillow.needs_bootstrap())
def test_pillow_save_to_multiple_databases(self): self.assertNotEqual(self.ds1_adapter.engine.url, self.ds2_adapter.engine.url) pillow = ConfigurableIndicatorPillow() pillow.bootstrap(configs=[self.ds_1, self.ds_2]) self.assertNotEqual(self.ds1_adapter.engine.url, self.ds2_adapter.engine.url) sample_doc, _ = get_sample_doc_and_indicators() pillow.change_transport(sample_doc) self.assertNotEqual(self.ds1_adapter.engine.url, self.ds2_adapter.engine.url) self.assertEqual(1, self.ds1_adapter.get_query_object().count()) self.assertEqual(1, self.ds2_adapter.get_query_object().count())
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 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 setUp(self): self.config = get_sample_data_source() self.pillow = ConfigurableIndicatorPillow() self.engine = connection_manager.get_engine(DEFAULT_ENGINE_ID) self.pillow.bootstrap(configs=[self.config]) self.adapter = IndicatorSqlAdapter(self.config) self.adapter.rebuild_table() self.fake_time_now = datetime(2015, 4, 24, 12, 30, 8, 24886)
def test_needs_bootstrap_window(self): before_now = datetime.utcnow() - timedelta(microseconds=1) pillow = ConfigurableIndicatorPillow() pillow.bootstrap([]) pillow.last_bootstrapped = before_now - timedelta( seconds=REBUILD_CHECK_INTERVAL - 5) self.assertFalse(pillow.needs_bootstrap()) pillow.last_bootstrapped = before_now - timedelta( seconds=REBUILD_CHECK_INTERVAL) self.assertTrue(pillow.needs_bootstrap())
def test_needs_bootstrap_window(self): before_now = datetime.utcnow() - timedelta(microseconds=1) pillow = ConfigurableIndicatorPillow() pillow.bootstrap([]) pillow.last_bootstrapped = before_now - timedelta(seconds=REBUILD_CHECK_INTERVAL - 5) self.assertFalse(pillow.needs_bootstrap()) pillow.last_bootstrapped = before_now - timedelta(seconds=REBUILD_CHECK_INTERVAL) self.assertTrue(pillow.needs_bootstrap())
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 test_pillow_save_to_one_database_at_a_time(self): pillow = ConfigurableIndicatorPillow() pillow.bootstrap(configs=[self.ds_1]) sample_doc, _ = get_sample_doc_and_indicators() pillow.change_transport(sample_doc) self.assertEqual(1, self.ds1_adapter.get_query_object().count()) self.assertEqual(0, self.ds2_adapter.get_query_object().count()) # save to the other pillow.bootstrap(configs=[self.ds_2]) sample_doc['_id'] = uuid.uuid4().hex pillow.change_transport(sample_doc) self.assertEqual(1, self.ds1_adapter.get_query_object().count()) self.assertEqual(1, self.ds2_adapter.get_query_object().count()) self.assertEqual(1, self.ds1_adapter.get_query_object().filter_by(doc_id='some-doc-id').count()) self.assertEqual(1, self.ds2_adapter.get_query_object().filter_by(doc_id=sample_doc['_id']).count())
def test_pillow_save_to_one_database_at_a_time(self): pillow = ConfigurableIndicatorPillow() pillow.bootstrap(configs=[self.ds_1]) sample_doc, _ = get_sample_doc_and_indicators() pillow.change_transport(sample_doc) self.assertEqual(1, self.ds1_adapter.get_query_object().count()) self.assertEqual(0, self.ds2_adapter.get_query_object().count()) # save to the other pillow.bootstrap(configs=[self.ds_2]) sample_doc['_id'] = uuid.uuid4().hex pillow.change_transport(sample_doc) self.assertEqual(1, self.ds1_adapter.get_query_object().count()) self.assertEqual(1, self.ds2_adapter.get_query_object().count()) self.assertEqual( 1, self.ds1_adapter.get_query_object().filter_by( doc_id='some-doc-id').count()) self.assertEqual( 1, self.ds2_adapter.get_query_object().filter_by( doc_id=sample_doc['_id']).count())
def test_pillow_save_to_multiple_databases(self): self.assertNotEqual(self.ds1_adapter.engine.url, self.ds2_adapter.engine.url) pillow = ConfigurableIndicatorPillow() pillow.bootstrap(configs=[self.ds_1, self.ds_2]) self.assertNotEqual(self.ds1_adapter.engine.url, self.ds2_adapter.engine.url) sample_doc, _ = get_sample_doc_and_indicators() pillow.change_transport(sample_doc) self.assertNotEqual(self.ds1_adapter.engine.url, self.ds2_adapter.engine.url) self.assertEqual(1, self.ds1_adapter.get_query_object().count()) self.assertEqual(1, self.ds2_adapter.get_query_object().count())
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)
def test_needs_bootstrap_on_initialization(self): pillow = ConfigurableIndicatorPillow() self.assertTrue(pillow.needs_bootstrap())
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)
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)
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)
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.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)
def test_needs_bootstrap_on_initialization(self): pillow = ConfigurableIndicatorPillow() self.assertTrue(pillow.needs_bootstrap())