Ejemplo n.º 1
0
 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))
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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))
Ejemplo n.º 5
0
 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')
Ejemplo n.º 6
0
 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'})
Ejemplo n.º 7
0
 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'})
Ejemplo n.º 8
0
 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'})
Ejemplo n.º 9
0
 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'})
Ejemplo n.º 10
0
 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'
     })
Ejemplo n.º 11
0
 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'
     })
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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'
         })
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
 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)
Ejemplo n.º 16
0
 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)
Ejemplo n.º 17
0
 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)
Ejemplo n.º 18
0
 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)
Ejemplo n.º 19
0
 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')
Ejemplo n.º 20
0
 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'
         })
Ejemplo n.º 21
0
 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)
Ejemplo n.º 22
0
 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"}}'
Ejemplo n.º 23
0
 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))
Ejemplo n.º 24
0
 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))
Ejemplo n.º 25
0
 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)
Ejemplo n.º 26
0
 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')
Ejemplo n.º 27
0
 def test_single_value_with_invalid_selection(self):
     qb = QueryBuilder(None, Rule)
     with self.assertRaises(TypeError):
         qb.single('latest')
Ejemplo n.º 28
0
 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)
Ejemplo n.º 29
0
 def test_single_value_with_invalid_selection(self):
     qb = QueryBuilder(None, Rule)
     with self.assertRaises(TypeError):
         qb.single('latest')
Ejemplo n.º 30
0
 def test_query_builder_to_monitoring_read(self):
     qb = QueryBuilder(self.client, Rule)
     qb.filter(Rule.key == 'foo').read()
Ejemplo n.º 31
0
 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')
Ejemplo n.º 32
0
 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))
Ejemplo n.º 33
0
 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))
Ejemplo n.º 34
0
 def test_query_builder_to_monitoring_read(self):
     qb = QueryBuilder(self.client, Rule)
     qb.filter(Rule.key == 'foo').read()
Ejemplo n.º 35
0
 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)
Ejemplo n.º 36
0
 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"}}'
Ejemplo n.º 37
0
 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)