def test_insert(setup): # This test looks at some of the core server functions # Normally, this would be split over multiple tests, but the server threading makes it # more complex to stop the server nicely, resulting in the port not being freed before the next test while not setup.running: time.sleep(0.01) client = TSDBClient(TEST_PORT) print('Populating DB...') client.populate_db() print('Looking for similarity...') _, q = client._tsmaker(0.5, 0.2, 0.1) res = client.find_similar(q, 5) print ('Client return vals:', res) # assert 0
def test_insert(setup): # This test looks at some of the core server functions # Normally, this would be split over multiple tests, but the server threading makes it # more complex to stop the server nicely, resulting in the port not being freed before the next test while not setup.running: time.sleep(0.01) client = TSDBClient(TEST_PORT) print('Populating DB...') client.populate_db() print('Looking for similarity...') _, q = client._tsmaker(0.5, 0.2, 0.1) res = client.find_similar(q, 5) print('Client return vals:', res)
res = self.client.select( metadata_dict={'pk': pk}, fields=['ts']) self._print_select_result(res) def _print_select_result(self, result): status, payload = result if status is not TSDBStatus.OK: self.print('Error! %r' % payload) return for key, values in payload.items(): self.print(key) for vkey in sorted(values.keys()): vvalues = values[vkey] if isinstance(vvalues, OrderedDict): self.print(' ', vkey) for hkey in sorted(vvalues.keys()): self.print(' ', hkey, ': ', vvalues[hkey]) else: self.print(' ', vkey, ': ', vvalues) if __name__ == '__main__': client = TSDBClient(port=30000) r = REPL(client) # r.onecmd("insert [1,2,3] @ [4, 5, 6] into tabby") # r.onecmd("insert [8,9,10] @ [11, 12, 13] into ginger") r.cmdloop()
def test_repl(): t = setup() t.daemon = True t.start() while not t.running: time.sleep(0.01) asyncio.set_event_loop(asyncio.new_event_loop()) client = TSDBClient() r = repl.REPL(client) output = [] def print_to_output(*args): output.append(' '.join(str(a) for a in args)) r.print = print_to_output def reset(): del output[:] # test do_hello r.onecmd('hello') assert output == ['Hello!'] reset() # test do_insert with syntax error r.onecmd('insert abc @ [4,5,6] into pke') r.onecmd('dump') assert output == ['Error!'] reset() # test do_insert r.onecmd('insert [1,2,3] @ [4, 5, 6] into tabby') r.onecmd('dump') assert [line.strip() for line in output] == \ ['OK!', 'tabby', 'ts', 'times : [4.0, 5.0, 6.0]', 'values : [1.0, 2.0, 3.0]'] reset() # test do_select r.onecmd('select ts') assert [line.strip() for line in output] == \ ['tabby', 'ts', 'times : [4.0, 5.0, 6.0]', 'values : [1.0, 2.0, 3.0]'] reset() r.onecmd('select from tabby') print([line.strip() for line in output]) assert [line.strip() for line in output] == \ ['tabby', 'label : null', 'order : -1', 'pk : tabby'] reset() r.onecmd('select ts, pk') assert [line.strip() for line in output] == \ ['tabby', 'pk : tabby', 'ts', 'times : [4.0, 5.0, 6.0]', 'values : [1.0, 2.0, 3.0]'] reset() r.onecmd('select limit 1') assert output == ['Error!'] reset() r.onecmd('select ts limit 1') assert [line.strip() for line in output] == \ ['tabby', 'ts', 'times : [4.0, 5.0, 6.0]', 'values : [1.0, 2.0, 3.0]'] reset() r.onecmd('select stats() as mean, std') assert [line.strip() for line in output] == \ ['tabby', 'mean : 2.0', 'std : 0.816496580927726'] reset() r.onecmd('insert [8, 9, 10] @ [11.0, 12.0, 13.0] into calico') r.onecmd('select ts order by pk') assert [line.strip() for line in output] == \ ['OK!', 'calico', 'ts', 'times : [11.0, 12.0, 13.0]', 'values : [8.0, 9.0, 10.0]', 'tabby', 'ts', 'times : [4.0, 5.0, 6.0]', 'values : [1.0, 2.0, 3.0]'] reset() r.onecmd('select ts order by pk desc') assert [line.strip() for line in output] == \ ['tabby', 'ts', 'times : [4.0, 5.0, 6.0]', 'values : [1.0, 2.0, 3.0]', 'calico', 'ts', 'times : [11.0, 12.0, 13.0]', 'values : [8.0, 9.0, 10.0]'] reset() r.onecmd('upsert calico {"label": "cute"}') r.onecmd('select label from calico') assert [line.strip() for line in output] == ['OK!', 'calico', 'label : cute'] reset()
def test_insert(setup): # This test looks at some of the core server functions # Normally, this would be split over multiple tests, but the server threading makes it # more complex to stop the server nicely, resulting in the port not being freed before the next test while not setup.running: time.sleep(0.01) print('Running...') info = { 'pk': 'ts-1', 'ts': ts.TimeSeries([1, 2, 3, 4, 5], [10, 15, 20, 15, 20]) } info2 = { 'pk': 'ts-2', 'ts': ts.TimeSeries([1, 2, 3, 4, 5], [10, 15, 30, 15, 20]) } client = TSDBClient(TEST_PORT) # Test select _, res = client.select() assert not res client.insert_ts(info['pk'], info['ts']) client.insert_ts(info2['pk'], info2['ts']) _, res = client.select(fields=['ts']) # Check primary keys and times/values for the time series assert 'ts-1' in res and 'ts-2' in res assert _list_equal(res['ts-1']['ts']['values'], info['ts'].values) and _list_equal( res['ts-1']['ts']['times'], info['ts'].times) assert _list_equal(res['ts-2']['ts']['values'], info2['ts'].values) and _list_equal( res['ts-2']['ts']['times'], info2['ts'].times) # Augumented select _, res = client.augmented_select('corr', ['dist'], info['ts']) # client.augmented_select('corr', ['dist'], query, {'vp': True}) print(res) assert 'ts-1' in res and 'ts-2' in res assert (res['ts-2']['dist'] - 0.9601936788280847) < EPS and res['ts-1']['dist'] < EPS # Upsert client.upsert_meta('ts-1', {'order': 10}) _, res = client.select(fields=['order']) assert (res['ts-1']['order'] == 10) and (res['ts-2']['order'] == -1) print(res) # Check delete works client.delete('ts-1') _, res = client.select(fields=['ts']) assert 'ts-1' not in res and 'ts-2' in res assert _list_equal(res['ts-2']['ts']['values'], info2['ts'].values) and _list_equal( res['ts-2']['ts']['times'], info2['ts'].times) # Delete the last element - DB should be empty client.delete('ts-2') _, res = client.select() assert not res
def test_insert(setup): # This test looks at some of the core server functions # Normally, this would be split over multiple tests, but the server threading makes it # more complex to stop the server nicely, resulting in the port not being freed before the next test while not setup.running: time.sleep(0.01) print ('Running...') info = {'pk':'ts-1', 'ts':ts.TimeSeries([1, 2, 3, 4, 5], [10, 15, 20, 15, 20])} info2 = {'pk':'ts-2', 'ts':ts.TimeSeries([1, 2, 3, 4, 5], [10, 15, 30, 15, 20])} client = TSDBClient(TEST_PORT) # Test select _, res = client.select() assert not res client.insert_ts( info['pk'], info['ts']) client.insert_ts( info2['pk'], info2['ts']) _, res = client.select(fields=['ts']) # Check primary keys and times/values for the time series assert 'ts-1' in res and 'ts-2' in res assert _list_equal(res['ts-1']['ts']['values'], info['ts'].values) and _list_equal(res['ts-1']['ts']['times'], info['ts'].times) assert _list_equal(res['ts-2']['ts']['values'], info2['ts'].values) and _list_equal(res['ts-2']['ts']['times'], info2['ts'].times) # Augumented select _, res = client.augmented_select('corr', ['dist'], info['ts']) # client.augmented_select('corr', ['dist'], query, {'vp': True}) print (res) assert 'ts-1' in res and 'ts-2' in res assert (res['ts-2']['dist'] - 0.9601936788280847) < EPS and res['ts-1']['dist'] < EPS # Upsert client.upsert_meta('ts-1', {'order':10}) _, res = client.select(fields=['order']) assert (res['ts-1']['order'] == 10) and (res['ts-2']['order'] == -1) print (res) # Check delete works client.delete('ts-1') _, res = client.select(fields=['ts']) assert 'ts-1' not in res and 'ts-2' in res assert _list_equal(res['ts-2']['ts']['values'], info2['ts'].values) and _list_equal(res['ts-2']['ts']['times'], info2['ts'].times) # Delete the last element - DB should be empty client.delete('ts-2') _, res = client.select() assert not res # assert 0