with self.assertRaisesRegex( SyntaxError, r'error at line 1, position 256, ' r'unexpected `aaaaaaaaaaaaaaaaaaaa...`, ' r'expecting: ; or end_of_statement'): await client.query('.{} = 1'.format('a' * 1000)) with self.assertRaisesRegex( SyntaxError, r'error at line 1, position 256, ' r'unexpected character `a`, ' r'expecting: ; or end_of_statement'): await client.query('.{} = 1'.format('a' * 256)) await client.query('thing(.id())[prop] = 1', prop='b' * 255) with self.assertRaisesRegex( ValueError, r'properties must follow the naming rules'): await client.query('thing(.id())[prop] = 1', prop='b' * 256) await client.query('thing(.id()).set(prop, 1)', prop='c' * 255) with self.assertRaisesRegex( ValueError, r'properties must follow the naming rules'): await client.query('thing(.id())[prop] = 1', prop='c' * 256) async def test_invalid_syntax(self, client): with self.assertRaisesRegex( SyntaxError, r'error at line 1, position 17, expecting: :'): await client.query('|x| is_err(x)?x+2') if __name__ == '__main__': run_test(TestSyntax())
await client.query('.iris = iris;', iris=iris) Iris = await client.query('.iris') Cato = await client.query('.iris.friends[0];') self.assertEqual(Iris['name'], 'Iris') self.assertEqual(Cato['name'], 'Cato') self.assertIn('#', Cato) self.assertIn('#', Iris) async def test_multiple(self, client): args = { 'i': 42, 'd': 3.14, 'n': None, 't': True, 'f': False, } await client.query(r''' .i = i; .d = d; .n = n; .t = t; .f = f; ''', **args) self.assertEqual(await client.query('.filter(||true);'), args) if __name__ == '__main__': run_test(TestArguments())
new_type('Friend'); set_type('Person', { name: 'str', age: 'int', }); set_type('Friend', { person: 'Person', friend: 'Person', }); /* * Procedures */ new_procedure('multiply', |a, b| a * b); new_procedure('more', |a, b| { .answers.push(a * b); .answers[-1]; }); 'DONE'; '''.lstrip() await client.query(script) res = await client.query('export();') self.assertEqual(res, script) if __name__ == '__main__': run_test(TestAdvanced())
from thingsdb.exceptions import BadDataError from thingsdb.exceptions import LookupError from thingsdb.exceptions import OverflowError from thingsdb.exceptions import ZeroDivisionError from thingsdb.exceptions import OperationError class TestSimple(TestBase): title = 'Test simple run' @default_test_setup(num_nodes=3, seed=1, threshold_full_storage=10) async def run(self): await self.node0.init_and_run() client = await get_client(self.node0) client.set_default_scope('//stuff') await self.run_tests(client) client.close() await client.wait_closed() async def test_hello_world(self, client): await client.query('"Hello world!"') if __name__ == '__main__': run_test(TestSimple())
from __future__ import print_function import re from lib import run_test def parse_time(string): str_re = re.findall(r"Matching time: (.*)", string) return int(str_re[0]) # Damerau-Levenshtein run_test( ("taskset -c 0 python microtrim.py " + "-m leven --out-file leven_seq.trimmed.fastq --max-distance .1 " + "--match-only 18 --trim-to 23 --trim-first 0 --trim-last 0 " + "--workers 0 " + "2> /dev/null"), {}, n=3, simulate=False, time_unit="ms", time_parser=parse_time, )
self.assertTrue(await client.query(q, scope='/n')) self.assertTrue(await client.query(q, scope='/no')) self.assertTrue(await client.query(q, scope='/node')) self.assertTrue(await client.query(q, scope='/node/')) self.assertTrue(await client.query(q, scope='/n/0')) self.assertTrue(await client.query(q, scope='/node/0')) async def test_thingsdb_scope(self, client): q = 'users_info(); true;' self.assertTrue(await client.query(q, scope='@t')) self.assertTrue(await client.query(q, scope='@thing')) self.assertTrue(await client.query(q, scope='@thingsdb')) self.assertTrue(await client.query(q, scope='/t')) self.assertTrue(await client.query(q, scope='/thing')) self.assertTrue(await client.query(q, scope='/thingsdb')) async def test_collection_scope(self, client): q = '.id(); true;' self.assertTrue(await client.query(q, scope='@:stuff')) self.assertTrue(await client.query(q, scope='@c:stuff')) self.assertTrue(await client.query(q, scope='@col:stuff')) self.assertTrue(await client.query(q, scope='@collection:stuff')) self.assertTrue(await client.query(q, scope='//stuff')) self.assertTrue(await client.query(q, scope='/c/stuff')) self.assertTrue(await client.query(q, scope='/col/stuff')) self.assertTrue(await client.query(q, scope='/collection/stuff')) if __name__ == '__main__': run_test(TestScopes())
from __future__ import print_function import re from lib import run_test def parse_time(string): str_re = re.findall(r"Matching time: (.*)", string) return int(str_re[0]) # Adagen run_test( ("taskset -c 0 python microtrim.py " + "-m adagen --out-file adagen_seq.trimmed.fastq " + "--trim-to 23 --trim-first 0 --trim-last 0 " + "--workers 0 " + "2> /dev/null"), {}, n=3, simulate=False, time_unit="ms", time_parser=parse_time, )
f'{api0}//stuff', json=data, auth=('admin', 'pass'), ) self.assertEqual(x.status_code, 200) self.assertEqual(x.json(), 43) data = {'type': 'run', 'name': 'addone', 'args': {'x': 6}} x = requests.post( f'{api0}//stuff', json=data, auth=('admin', 'pass'), ) self.assertEqual(x.status_code, 200) self.assertEqual(x.json(), 7) data = {'type': 'run', 'name': 'addone', 'args': {'x': 7, 'y': 5}} x = requests.post( f'{api0}//stuff', json=data, auth=('admin', 'pass'), ) self.assertEqual(x.status_code, 200) self.assertEqual(x.json(), 8) if __name__ == '__main__': run_test(TestHTTPAPI())
id=t)) client1.close() await client1.wait_closed() await client.query(r''' .del('b'); ''') await asyncio.sleep(2) await self.node1.shutdown() await self.node1.run() await asyncio.sleep(4) await self.wait_nodes_ready() for val in range(50): await client.query(r''' .val = val; ''', val=val) await self.node1.shutdown() await self.node1.run() await asyncio.sleep(4) await self.wait_nodes_ready() if __name__ == '__main__': run_test(TestGC())
from thingsdb.exceptions import AssertionError from thingsdb.exceptions import BadDataError from thingsdb.exceptions import LookupError from thingsdb.exceptions import OverflowError class TestVariable(TestBase): title = 'Test variable' @default_test_setup(num_nodes=1, seed=1) async def run(self): await self.node0.init_and_run() client = await get_client(self.node0) client.set_default_scope('//stuff') self.assertEqual(await client.query('a=1;'), 1) self.assertEqual(await client.query('a=1; a;'), 1) self.assertEqual(await client.query('a=1; a=2; a;'), 2) self.assertEqual(await client.query('a=1; a=2; a+=a;'), 4) self.assertEqual(await client.query('a=1; a=2; a+=a; a;'), 4) client.close() await client.wait_closed() if __name__ == '__main__': run_test(TestVariable())
# the client points to the same node so we expect the correct result counter = await client.query(r'.counter;', scope=stuff) assert (counter == expected_counter) # a little sleep to make sure all nodes have time to process the events await asyncio.sleep(0.75) # kill three nodes at most, since one can be in away mode, and at lesat # one node must have `ready` status for node in (self.node0, self.node1, self.node2): # A soft kill and we do not wait for the result. The goal here is # to test if all the queries which follow are still returning as # expected. node.soft_kill() # Test a few times for a correct counter response, the client # will loose a connection and should reconnect while no # queries get lost. for _ in range(20): counter = await client.query(r'.counter;', scope=stuff) assert (counter == expected_counter) await asyncio.sleep(0.2) client.close() await client.wait_closed() if __name__ == '__main__': run_test(TestMultiNode())
self.assertEqual(await client.query('.list'), [1, 2, 3, 4, 5, 6]) await client0.query('.list[] = [1, 7, 4];') await asyncio.sleep(0.2) for client in (client0, client1, client2): self.assertEqual(await client.query('.list'), [1, 7, 4]) await client0.query('.list[1:2] = [2, 3];') await asyncio.sleep(0.2) for client in (client0, client1, client2): self.assertEqual(await client.query('.list'), [1, 2, 3, 4]) await client0.query('.list[10:] = [5, 6];') await asyncio.sleep(0.2) for client in (client0, client1, client2): self.assertEqual(await client.query('.list'), [1, 2, 3, 4, 5, 6]) await client0.query('.list[0:0] = [0];') await asyncio.sleep(0.2) for client in (client0, client1, client2): self.assertEqual(await client.query('.list'), list(range(7))) await client0.query('.list[-3:] = [];') await asyncio.sleep(0.2) for client in (client0, client1, client2): self.assertEqual(await client.query('.list'), [0, 1, 2, 3]) if __name__ == '__main__': run_test(TestIndexSlice())
counters = await client.query('counters();') self.assertGreater(counters["queries_with_error"], 0) self.assertIs(await client.query('reset_counters();'), None) counters = await client.query('counters();') self.assertEqual(counters["queries_with_error"], 0) async def test_set_log_level(self, client): with self.assertRaisesRegex( NumArgumentsError, 'function `set_log_level` takes 1 argument but 0 were given'): await client.query('set_log_level();') with self.assertRaisesRegex( TypeError, r'function `set_log_level` expects argument 1 to be of ' r'type `int` but got type `str` instead'): await client.query('set_log_level("DEBUG");') prev = (await client.node_info())['log_level'] self.assertIs(await client.query('set_log_level(ERROR);'), None) self.assertEqual((await client.node_info())['log_level'], 'ERROR') self.assertIs(await client.query('set_log_level(0);'), None) self.assertEqual((await client.node_info())['log_level'], 'DEBUG') self.assertIs(await client.query(f'set_log_level({prev});'), None) self.assertEqual((await client.node_info())['log_level'], prev) if __name__ == '__main__': run_test(TestNodeFunctions())
Iris = Thing(ev, iris['#']) await Iris.watch() await asyncio.sleep(0.5) self.assertEqual(Iris.age, 6) await ev.client.query('.iris.name = "Iris";') await asyncio.sleep(0.5) self.assertEqual(Iris.name, 'Iris') ninfo = await ev0.client.query('nodes_info();', scope='@n') for n in nodes_info: if n['status'] == 'AWAY' and n['node_id'] == node_id: need_check = False break else: make_iris = True break await asyncio.sleep(0.4) if __name__ == '__main__': run_test(TestWatch())
from test_thingsdb_functions import TestThingsDBFunctions from test_type import TestType from test_types import TestTypes from test_user_access import TestUserAccess from test_variable import TestVariable from test_watch import TestWatch from test_wrap import TestWrap if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--skip-doc-test', action='store_true', help='skip documentation testing') args = parser.parse_args() run_test(TestAdvanced()) run_test(TestArguments()) run_test(TestBackup()) run_test(TestCollectionFunctions()) if args.skip_doc_test is False: run_test(TestDocUrl()) run_test(TestEnum()) run_test(TestEvents()) run_test(TestGC()) run_test(TestHTTPAPI()) run_test(TestIndexSlice()) run_test(TestMultiNode()) run_test(TestNested()) run_test(TestNodeFunctions()) run_test(TestNodes()) run_test(TestOperators())
self.assertEqual(await client.query('.arr;'), check_arr) checked = True await asyncio.sleep(0.5) # expected no garbage collection for client in (client1, client2, client3): await self.close_client(client) async def close_client(self, client): counters = await client.query('counters();', scope='@node') self.assertEqual(counters['garbage_collected'], 0) self.assertEqual(counters['events_failed'], 0) client.close() await client.wait_closed() async def mquery(self, query, *clients): tasks = (client.query(query.format(client.id), timeout=10) for client in clients) await asyncio.gather(*tasks) async def loop_add(self, x, q, *clients): for _ in range(x): await self.mquery(q, *clients) await asyncio.sleep(0.1) if __name__ == '__main__': run_test(TestEvents())
await client.query(r''' run('test'); ''') self.assertEqual(await client.query('run("test", 6);'), 60) self.assertEqual(await client.query('wse(run("test_wse", 42));'), 42) async def test_thing_argument(self, client): await client.query(r''' new_procedure('test_save_thing', |t| .t = t); new_procedure('test_another_thing', |t| .other = t); new_procedure('as_named_args', |options| .name = options.name); ''') await client.run('test_save_thing', {'name': 'Iris', 'age': 6}) await client.run('as_named_args', {'name': 'Cato'}) iris = await client.query('.t') self.assertEqual(iris['name'], 'Iris') self.assertEqual(iris['age'], 6) self.assertEqual(await client.query('.name'), 'Cato') await client.run('test_another_thing', iris) self.assertTrue(await client.query('(.other == .t)')) if __name__ == '__main__': run_test(TestProcedures())
self.assertFalse(await client.query(r'''has_backup(1234);''')) async def test_restore(self, client): await client.query(r''' .foo = 'bar'; ''', scope='//stuff') backup_id = await client.query(r'''new_backup('/tmp/test.tar.gz');''') # in 50 seconds both nodes should have been in `away` mode await asyncio.sleep(50) await client.query(r'''del_collection('stuff');''', scope='@t') # in 50 seconds both nodes should have been in `away` mode await asyncio.sleep(50) await client.query(r'''restore('/tmp/test.tar.gz');''', scope='@t') client.close() await client.wait_closed() # in 30 seconds synchronization should have been finished await asyncio.sleep(30) client = await get_client(self.node0) bar = await client.query('.foo;', scope='//stuff') self.assertEqual(bar, 'bar') if __name__ == '__main__': run_test(TestBackup())
grant('@node', "test1", READ); ''') await testcl1.query(r'''nodes_info();''', scope='@node') with self.assertRaisesRegex(ForbiddenError, error_msg): await testcl1.query(r'''reset_counters();''', scope='@node') await client.query(r'''grant('@n', "test1", MODIFY);''') await testcl1.query(r'''reset_counters();''', scope='@node') await client.query(r'''del_user('test1');''') # queries should no longer work with self.assertRaisesRegex(ForbiddenError, error_msg): await testcl1.query(r'''.map(||nil);''', scope='@:junk') # should not be possible to create a new client with self.assertRaisesRegex(AuthError, 'invalid username or password'): await get_client(self.node0, auth=['test1', 'test']) testcl1.close() client.close() await testcl1.wait_closed() await client.wait_closed() if __name__ == '__main__': run_test(TestUserAccess())
.a = {}; .b = .a; ( set(.a, .a) == set(.b,) ) ''')) self.assertTrue(await client.query(r''' .a = {}; ( set([.a]) != set([]) ) ''')) self.assertTrue(await client.query(r''' .a = {}; .b = {}; ( set([.a]) != set([.b]) ) ''')) await client.query(r''' anna = {}; cato = {}; iris = {}; a = set(cato, iris); b = set(cato, anna); assert (a | b == set(anna, cato, iris)); // Union assert (a & b == set(cato)); // Intersection assert (a - b == set(iris)); // Difference assert (a ^ b == set(anna, iris)); // Symmetric difference ''') if __name__ == '__main__': run_test(TestTypes())
# Python. We require both the test packages themselves (which are # are stored outside of the repository), as well as the "lib" # module residing within the repository in the scripts directory. base_path = os.path.dirname(os.path.realpath(sys.argv[0])) if os.environ.get("TEST_IN_PLACE") is None: # Use an absolute path for the installed test packages sys.path.insert(0, "/var/local/hss/autograde_tests") else: run_dir = os.path.join(base_path, "..") sys.path.insert(0, run_dir) subprocess.call(["rsync"] + glob.glob(os.path.join(base_path, "..", "..", "..", "grading", "*")) + [os.path.join(run_dir, "src"), "-r", "--delete"]) # The directory containing lib.py should be the same as the one # that contains this file. sys.path.insert(0, base_path) # Load all test packages, which will populate the dictionary in # the "lib" module. import tests # If we were given an argument, run only the test with that name. # Otherwise, run every test. if len(sys.argv) == 1: lib.run_all() else: lib.run_test(sys.argv[1])
NumArgumentsError, 'function `rename_collection` takes 2 arguments ' 'but 0 were given'): await client.query('rename_collection();') with self.assertRaisesRegex( TypeError, 'expecting type `str` or `int` as collection ' 'but got type `float` instead'): await client.query('rename_collection(1.0, "bla");') with self.assertRaisesRegex( ValueError, 'collection name must follow the naming rules'): await client.query('rename_collection("stuff", "4bla");') with self.assertRaisesRegex(LookupError, 'collection `A` not found'): await client.query('rename_collection("A", "B");') with self.assertRaisesRegex(LookupError, '`collection:1234` not found'): await client.query('rename_collection(1234, "B");') test = await client.query('new_collection("test1");') self.assertIs( await client.query('rename_collection("test1", "test2");'), None) self.assertIs(await client.query('del_collection("test2");'), None) if __name__ == '__main__': run_test(TestThingsDBFunctions())
.a = set(x, y); .b = set(x, z); .a ^= .b; ''') await asyncio.sleep(1.0) for client in (client0, client1, client2): res = await client.query(r'.a.map(|x| x.n);') self.assertEqual(set(res), {1, 2}) async def test_ids(self, client0, client1, client2): nones, ids = await client0.query(r''' a = {b: {c: {}}}; a.b.c.d = {}; .x = [a.id(), a.b.id(), a.b.c.id(), a.b.c.d.id()]; .a = a; .y = [a.id(), a.b.id(), a.b.c.id(), a.b.c.d.id()]; [.x, .y]; ''') self.assertEqual(nones, [None, None, None, None]) self.assertGreater(ids[0], 1) self.assertGreater(ids[1], ids[0]) self.assertGreater(ids[2], ids[1]) self.assertGreater(ids[3], ids[2]) if __name__ == '__main__': run_test(TestNested())
title: 'looking for the answer', subject: 'life the universe and everything', author: .iris, body: '42.' }; .notes = set(.note); return(.notesSummary = .wrap('_NotesSummary'), 3); ''') ids = await client.query('[.id(), .note.id(), .iris.id()];') self.assertEqual( res, { '#': ids[0], 'notes': [{ '#': ids[1], 'title': 'looking for the answer', 'author': { '#': ids[2], 'name': 'Iris', }, }] }) if __name__ == '__main__': run_test(TestWrap())
@default_test_setup(num_nodes=3, seed=1, threshold_full_storage=10) async def run(self): await self.node0.init_and_run() client = await get_client(self.node0) query = client.query for fn in os.listdir(BACKUP_PATH): if not fn.endswith('.tar.gz'): continue fn = os.path.join(BACKUP_PATH, fn) collections = await query('collections_info();') for collection in collections: await query('del_collection(c);', c=collection['name']) await query('restore(fn, true);', fn=fn) await asyncio.sleep(10) await client.authenticate('admin', 'pass') client.close() await client.wait_closed() if __name__ == '__main__': run_test(TestMigrate())
self.assertEqual((await cl4.query('.hello;', scope=stuff)), 'world') cl3.close() cl4.close() client.close() await cl3.wait_closed() await cl4.wait_closed() await client.wait_closed() await self.node0.shutdown() await self.node3.shutdown() await self.node4.shutdown() await self.node0.run() await self.node3.run() await self.node4.run() await asyncio.sleep(50) # 50 seconds should be enough to sync client = await get_client(self.node0) nodes = await client.query(r'nodes_info();', scope='@node') for node in nodes: self.assertEqual(node['committed_event_id'], 9) client.close() await client.wait_closed() if __name__ == '__main__': run_test(TestNodes())
-!!-! 42; '''), 0) self.assertEqual( await client.query(r''' --3.14; '''), 3.14) self.assertEqual( await client.query(r''' -true; '''), -1) self.assertEqual( await client.query(r''' ++true; '''), 1) self.assertEqual( await client.query(r''' +false; '''), 0) self.assertEqual( await client.query(r''' (|x| - ! x).def(); '''), "|x| -!x") self.assertEqual( await client.query(r''' (|| - !! +5).def(); '''), "|| -!!+5") if __name__ == '__main__': run_test(TestOperators())
''') with self.assertRaisesRegex(LookupError, r'enum `unknown` not found'): await client.query(r''' rename_enum('unknown', 'Colors'); ''') with self.assertRaisesRegex( NumArgumentsError, r'function `rename_enum` takes 2 arguments ' r'but 1 was given'): await client.query(r'''rename_enum('Color'); ''') with self.assertRaisesRegex( TypeError, r'function `rename_enum` expects argument 2 to be of ' r'type `str` but got type `int` instead;'): await client.query(r'''rename_enum('Color', 123); ''') await client.query(r''' rename_enum('Color', 'Colors'); ''') with self.assertRaisesRegex(LookupError, r'enum `Color` is undefined'): self.assertEqual(await client.query('.get_color(0);'), '#FF0000') self.assertEqual(await client.query('Colors()'), '#FF0000') if __name__ == '__main__': run_test(TestEnum())
import re from lib import run_test def parse_time(string): str_re = re.findall(r"Matching time: (.*)", string) return int(str_re[0]) MAX_CORE = 48 WORKERS = [1] + list(range(0, MAX_CORE + 1, 4))[1:] CORES = map(lambda x: x - 1, WORKERS) # Adagen Fast run_test( ("taskset -c 0-{0[worker][1]} python microtrim.py " + "-m adagen-fast --out-file adagen_fast.trimmed.fastq " + "--trim-to 23 --trim-first 0 --trim-last 0 " + "--workers {0[worker][0]} --chunk {0[chunk]} " + "2> /dev/null"), { "worker": list(zip(WORKERS, CORES)), "chunk": [1000] }, n=3, simulate=False, time_unit="ms", time_parser=parse_time, )
set_type('Brick2', { part_nr: 'int', color: 'thing', }); set_type('_Name', { name: 'str' }); set_type('_ColorName', { color: '_Name' }); ''') # self.assertEqual(len(brick_color_names), 1) # for brick in brick_color_names: # self.assertIn('#', brick) # self.assertIn('color', brick) # self.assertEqual(len(brick), 2) # color = brick['color'] # self.assertIn('#', color) # self.assertIn('name', color) # self.assertEqual(len(color), 2) if __name__ == '__main__': run_test(TestTmp())