def test_query_builder_chained_scalar_filter(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.key == 'foo').filter(Sensor.key == 'bar') self.assertTrue(isinstance(qb.selection['devices'].selection, ScalarSelector)) self.assertTrue(isinstance(qb.selection['sensors'].selection, ScalarSelector))
def test_query_builder_invalid_with_attr_selection(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.attributes['foo'] == 'bar') with self.assertRaises(ValueError) as e: qb.delete(start='then', end='now') self.assertEquals(e.exception.args[0], DELETEKEYMSG)
def test_query_builder_sensor_only_search(self): qb = QueryBuilder(self.client, Device) qb.filter(Sensor.key == 'foo') qb.read() j = json.dumps(qb, default=self.read_encoder.default) expected = '{"search": {"filters": {"sensors": {"key": "foo"}, "devices": "all"}, "select": "devices"}, "find": {"quantifier": "all"}}' self.assertEquals(j, expected)
def test_query_builder_chained_scalar_filter(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.key == 'foo').filter(Sensor.key == 'bar') self.assertTrue( isinstance(qb.selection['devices'].selection, ScalarSelector)) self.assertTrue( isinstance(qb.selection['sensors'].selection, ScalarSelector))
def test_single_value_with_pipeline(self): qb = QueryBuilder(None, Sensor) qb.convert_timezone('America/Chicago') try: qb.single(function='exact', timestamp=None) except: pass self.assertEquals(qb.pipeline[0].name, 'convert_tz') self.assertEquals(qb.pipeline[0].args[0], 'America/Chicago')
def test_single_value_with_include_selection(self): qb = QueryBuilder(None, Sensor) #this will raise an error which is fine, just want to check a side #effect try: qb.single('latest', include_selection=True) except: pass self.assertEquals(qb.operation.args, {'include_selection': True, 'function': 'latest'})
def test_latest_value(self): qb = QueryBuilder(None, Sensor) #this will raise an error which is fine, just want to check a side #effect try: qb.latest() except: pass self.assertEquals(qb.operation.name, 'single') self.assertEquals(qb.operation.args, {'include_selection': False, 'function': 'latest'})
def test_query_builder_with_valid_delete_datapoints(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.key == 'bar') qb.filter(Sensor.key == 'foo') try: qb.delete(start='then', end='now') except: pass self.assertEquals(qb.operation.args, {'start': 'then', 'stop': 'now', 'device_key': 'bar', 'sensor_key': 'foo'})
def test_single_value_with_timestamp(self): qb = QueryBuilder(None, Sensor) #this will raise an error which is fine, just want to check a side #effect try: qb.single('before', timestamp='2014-09-15T00:00:01Z') except: pass self.assertEquals(qb.operation.name, 'single') self.assertEquals(qb.operation.args, {'include_selection': False, 'function': 'before', 'timestamp': '2014-09-15T00:00:01Z'})
def test_single_value_with_include_selection(self): qb = QueryBuilder(None, Sensor) #this will raise an error which is fine, just want to check a side #effect try: qb.single('latest', include_selection=True) except: pass self.assertEquals(qb.operation.args, { 'include_selection': True, 'function': 'latest' })
def test_latest_value(self): qb = QueryBuilder(None, Sensor) #this will raise an error which is fine, just want to check a side #effect try: qb.latest() except: pass self.assertEquals(qb.operation.name, 'single') self.assertEquals(qb.operation.args, { 'include_selection': False, 'function': 'latest' })
def test_query_builder_with_optional_pipeline_args(self): qb = QueryBuilder(self.client, Sensor) start = datetime.datetime(2014, 1, 1, 12, 0) end = datetime.datetime(2014, 1, 3, 12, 0) rollup_start = datetime.datetime(2014, 1, 1, 0, 0) qb.filter(Device.key == 'foo').filter(Sensor.key == 'bar') qb.rollup('min', '1min', rollup_start) qb.read(start=start, end=end) j = json.dumps(qb, default=self.read_encoder.default) expected = { 'search': { 'select': 'sensors', 'filters': { 'devices': {'key': 'foo'}, 'sensors': {'key': 'bar'} } }, 'fold': { 'functions': [ { 'name': 'rollup', 'arguments': [ 'min', '1min', '2014-01-01T00:00:00' ] } ] }, 'read': { 'start': '2014-01-01T12:00:00', 'stop': '2014-01-03T12:00:00' } } self.assertEquals(json.loads(j), expected)
def test_single_value_with_timestamp(self): qb = QueryBuilder(None, Sensor) #this will raise an error which is fine, just want to check a side #effect try: qb.single('before', timestamp='2014-09-15T00:00:01Z') except: pass self.assertEquals(qb.operation.name, 'single') self.assertEquals( qb.operation.args, { 'include_selection': False, 'function': 'before', 'timestamp': '2014-09-15T00:00:01Z' })
def test_query_builder_invalid_with_compound_selection(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.key == 'bar') qb.filter(Sensor.key == 'foo') qb.filter(Sensor.key == 'baz') with self.assertRaises(ValueError) as e: qb.delete(start='then', end='now') self.assertEquals(e.exception.args[0], DELETEKEYMSG)
def test_query_builder_with_ordering(self): qb = QueryBuilder(self.client, Sensor) start = datetime.datetime(2014, 1, 1, 12, 0) end = datetime.datetime(2014, 1, 3, 12, 0) rollup_start = datetime.datetime(2014, 1, 1, 0, 0) qb.filter(Device.key == 'foo') qb.order_by('date_created', 'asc') qb.read(start=start, end=end) j = json.dumps(qb, default=self.read_encoder.default) expected = { 'search': { 'select': 'sensors', 'filters': { 'devices': {'key': 'foo'}, 'sensors': 'all' }, 'ordering': { 'attribute': 'date_created', 'direction': 'asc' } }, 'read': { 'start': '2014-01-01T12:00:00', 'stop': '2014-01-03T12:00:00' } } self.assertEquals(json.loads(j), expected)
def test_encode_query_builder(self): qb = QueryBuilder(self.client, Sensor) start = datetime.datetime(2014, 1, 1) end = datetime.datetime(2014, 1, 2) qb.filter(Device.key == 'foo').filter(Sensor.key == 'bar') qb.read(start=start, end=end) j = json.dumps(qb, default=self.read_encoder.default) expected = { 'search': { 'select': 'sensors', 'filters': { 'devices': {'key': 'foo'}, 'sensors': {'key': 'bar'} } }, 'read': { 'start': '2014-01-01T00:00:00', 'stop': '2014-01-02T00:00:00' } } self.assertEquals(json.loads(j), expected)
def test_query_builder_with_valid_delete_datapoints(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.key == 'bar') qb.filter(Sensor.key == 'foo') try: qb.delete(start='then', end='now') except: pass self.assertEquals( qb.operation.args, { 'start': 'then', 'stop': 'now', 'device_key': 'bar', 'sensor_key': 'foo' })
def test_query_builder_to_device_search(self): qb = QueryBuilder(self.client, Device) qb.filter(Device.key == 'foo').read() expected = '{"search": {"filters": {"sensors": {}, "devices": {"key": "foo"}}, "select": "devices"}, "find": {"quantifier": "all"}}'
def test_query_builder_chained_dict_filter(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.attributes['foo'] == 'bar').filter( Device.attributes['baz'] == 'boz') self.assertTrue(isinstance(qb.selection['devices'].selection, AndClause))
def test_query_builder_or_clause(self): qb = QueryBuilder(None, Sensor) selector = or_([Device.key == 'foo', Device.key == 'bar']) qb.filter(selector) self.assertTrue(isinstance(qb.selection['devices'].selection, OrClause))
def test_encode_query_builder_with_pipeline(self): qb = QueryBuilder(self.client, Sensor) start = datetime.datetime(2014, 1, 1) end = datetime.datetime(2014, 1, 2) qb.filter(Device.key == 'foo').filter(Sensor.key == 'bar') qb.aggregate('max').convert_timezone('CDT') qb.rollup('min', '1min').multi_rollup(['max', 'min'], '1min') qb.interpolate('linear', '1min').find('max', '1min') qb.read(start=start, end=end) j = json.dumps(qb, default=self.read_encoder.default) expected = { 'search': { 'select': 'sensors', 'filters': { 'devices': {'key': 'foo'}, 'sensors': {'key': 'bar'} } }, 'fold': { 'functions': [ { 'name': 'aggregation', 'arguments': ['max'] }, { 'name': 'convert_tz', 'arguments': ['CDT'] }, { 'name': 'rollup', 'arguments': [ 'min', '1min', '2014-01-01T00:00:00' ] }, { 'name': 'multi_rollup', 'arguments': [ ['max', 'min'], '1min', '2014-01-01T00:00:00' ] }, { 'name': 'interpolate', 'arguments': [ 'linear', '1min', '2014-01-01T00:00:00', '2014-01-02T00:00:00' ] }, { 'name': 'find', 'arguments': [ 'max', '1min', '2014-01-01T00:00:00' ] }, ] }, 'read': { 'start': '2014-01-01T00:00:00', 'stop': '2014-01-02T00:00:00' } } self.assertEquals(json.loads(j), expected)
def test_query_builder_extract_monitoring_key(self): qb = QueryBuilder(None, Rule) qb.filter(Rule.key == 'foo') key = extract_key_for_monitoring(qb.selection['rules']) self.assertEquals(key, 'foo')
def test_single_value_with_invalid_selection(self): qb = QueryBuilder(None, Rule) with self.assertRaises(TypeError): qb.single('latest')
def test_query_builder_to_monitoring_read(self): qb = QueryBuilder(self.client, Rule) qb.filter(Rule.key == 'foo').read()
def test_query_builder_chained_dict_filter(self): qb = QueryBuilder(None, Sensor) qb.filter(Device.attributes['foo'] == 'bar').filter( Device.attributes['baz'] == 'boz') self.assertTrue( isinstance(qb.selection['devices'].selection, AndClause))
def test_query_builder_with_invalid_with_no_selection(self): qb = QueryBuilder(None, Sensor) with self.assertRaises(ValueError) as e: qb.delete(start='then', end='now') self.assertEquals(e.exception.args[0], DELETEKEYMSG)