def test_context_defaults(self): gauged = Gauged(self.driver, resolution=1000, block_size=10000) with gauged.writer as writer: writer.add('bar', 123, timestamp=10000) writer.add('foo', 456, timestamp=20000) self.assertEqual(gauged.keys(), ['bar', 'foo']) gauged = Gauged(self.driver, resolution=1000, block_size=10000, defaults={'limit': 1}) self.assertEqual(gauged.keys(), ['bar'])
def test_context_defaults(self): gauged = Gauged(self.driver, resolution=1000, block_size=10000) with gauged.writer as writer: writer.add('bar', 123, timestamp=10000) writer.add('foo', 456, timestamp=20000) self.assertListEqual(gauged.keys(), [ 'bar', 'foo' ]) gauged = Gauged(self.driver, resolution=1000, block_size=10000, defaults={ 'limit': 1 }) self.assertListEqual(gauged.keys(), [ 'bar' ])
def test_gauge(self): gauged = Gauged(self.driver, block_size=50000, gauge_nan=Gauged.IGNORE) self.assertEqual(gauged.value('foobar'), None) with gauged.writer as writer: writer.add('foobar', 200, timestamp=23000) self.assertEqual(gauged.value('foobar'), 200) self.assertEqual(gauged.value('foobar', timestamp=22000), None) with gauged.writer as writer: writer.add({'foobar': 300, 'invalid': 'nan'}, timestamp=50000) self.assertEqual(gauged.value('foobar'), 300) self.assertEqual(gauged.value('foobar', 30000), 200) timestamp = datetime.datetime( 1970, 1, 1) + datetime.timedelta(seconds=60) self.assertEqual(gauged.value('foobar', timestamp), 300) with gauged.writer as writer: writer.add({'foobar': 350}, timestamp=90000) writer.add('foobar', 100, timestamp=120000) writer.add('bar', 150, timestamp=130000) self.assertItemsEqual(gauged.keys(), ['foobar', 'bar']) self.assertEqual(gauged.value('foobar'), 100) with gauged.writer as writer: writer.add('foobar', 500, timestamp=150000) self.assertEqual(gauged.value('foobar'), 500) with gauged.writer as writer: writer.add('foobar', 1500, timestamp=10000, namespace=1) self.assertEqual(gauged.value('foobar', namespace=1), 1500) self.assertEqual(gauged.value('foobar'), 500) with gauged.writer as writer: writer.clear_from(100000) self.assertEqual(gauged.value('foobar'), 350) with self.assertRaises(GaugedDateRangeError): self.assertEqual( gauged.value('foobar', timestamp=-10000000000000), None)
def test_gauge(self): gauged = Gauged(self.driver, block_size=50000, gauge_nan=Gauged.IGNORE) self.assertEqual(gauged.value('foobar'), None) with gauged.writer as writer: writer.add('foobar', 200, timestamp=23000) self.assertEqual(gauged.value('foobar'), 200) self.assertEqual(gauged.value('foobar', timestamp=22000), None) with gauged.writer as writer: writer.add({ 'foobar': 300, 'invalid': 'nan' }, timestamp=50000) self.assertEqual(gauged.value('foobar'), 300) self.assertEqual(gauged.value('foobar', 30000), 200) timestamp = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=60) self.assertEqual(gauged.value('foobar', timestamp), 300) with gauged.writer as writer: writer.add({ 'foobar': 350 }, timestamp=90000) writer.add('foobar', 100, timestamp=120000) writer.add('bar', 150, timestamp=130000) self.assertItemsEqual(gauged.keys(), [ 'foobar', 'bar' ]) self.assertEqual(gauged.value('foobar'), 100) with gauged.writer as writer: writer.add('foobar', 500, timestamp=150000) self.assertEqual(gauged.value('foobar'), 500) with gauged.writer as writer: writer.add('foobar', 1500, timestamp=10000, namespace=1) self.assertEqual(gauged.value('foobar', namespace=1), 1500) self.assertEqual(gauged.value('foobar'), 500) with gauged.writer as writer: writer.clear_from(100000) self.assertEqual(gauged.value('foobar'), 350) with self.assertRaises(GaugedDateRangeError): self.assertEqual(gauged.value('foobar', timestamp=-10000000000000), None)
def test_no_data(self): gauged = Gauged(self.driver) self.assertEqual(len(gauged.namespaces()), 0) self.assertEqual(len(gauged.value_series('foo')), 0) self.assertEqual(len(gauged.aggregate_series('foo', Gauged.SUM)), 0) self.assertEqual(gauged.value('foo'), None) self.assertEqual(gauged.aggregate('foo', Gauged.SUM), None) self.assertEqual(len(gauged.keys()), 0) stats = gauged.statistics() for attr in ['data_points', 'byte_count']: self.assertEqual(getattr(stats, attr), 0)
def test_keys(self): gauged = Gauged(self.driver) with gauged.writer as writer: writer.add('foobar', 1, timestamp=1000) writer.add('foobaz', 1, timestamp=1000) writer.add('bar', 1, timestamp=1000, namespace=1) self.assertListEqual(gauged.keys(), ['foobar', 'foobaz']) self.assertListEqual(gauged.keys(prefix='bar'), []) self.assertListEqual(gauged.keys(namespace=1), ['bar']) self.assertListEqual(gauged.keys(namespace=1, prefix='bar'), ['bar']) self.assertListEqual(gauged.keys(limit=1), ['foobar']) self.assertListEqual(gauged.keys(limit=1, offset=1), ['foobaz'])
def test_gauged_init_store(self): gauged = Gauged('sqlite+foo://') self.assertEqual(len(gauged.metadata()), 0) with self.assertRaises(GaugedSchemaError): gauged.keys()