def gen_full_db_op_set(self, test_label): n_0, n_0_id = generate_random_node_dict(test_label) n_1, n_1_id = generate_random_node_dict(test_label) l_0, l_0_id = generate_random_link_dict(test_label, n_0_id, n_1_id) n_set = [n_0, n_1] l_set = [l_0] topo_diff = Topo_Diff(node_set_add=n_set, link_set_add=l_set) attr_diff = Attr_Diff() attr_diff.add_node_attr_write(n_0_id, 'attr_0', 0) test_rzdoc = generate_random_RZDoc(test_label) op_set = [ DBO_rzdoc__clone(), DBO_add_node_set(meta_attr_list_to_meta_attr_map(n_set)), DBO_add_link_set( meta_attr_list_to_meta_attr_map(l_set, meta_attr='__type')), DBO_diff_commit__attr(attr_diff), DBO_diff_commit__topo(topo_diff), DBO_rm_node_set(id_set=[n_0_id]), # block chain DBO_block_chain__init(test_rzdoc), DBO_block_chain__commit(commit_obj=topo_diff.to_json_dict()), # rzdoc DBO_rzdoc__create(test_rzdoc), DBO_rzdoc__delete(test_rzdoc), DBO_rzdoc__list(), DBO_rzdoc__lookup_by_name(test_rzdoc.name), ] return op_set
def gen_full_db_op_set(self, test_label): n_0, n_0_id = generate_random_node_dict(test_label) n_1, n_1_id = generate_random_node_dict(test_label) l_0, l_0_id = generate_random_link_dict(test_label, n_0_id, n_1_id) n_set = [n_0, n_1] l_set = [l_0] topo_diff = Topo_Diff(node_set_add=n_set, link_set_add=l_set) attr_diff = Attr_Diff() attr_diff.add_node_attr_write(n_0_id, 'attr_0', 0) test_rzdoc = generate_random_RZDoc(test_label) op_set = [ DBO_rzdoc__clone(), DBO_add_node_set(meta_attr_list_to_meta_attr_map(n_set)), DBO_add_link_set(meta_attr_list_to_meta_attr_map(l_set, meta_attr='__type')), DBO_diff_commit__attr(attr_diff), DBO_diff_commit__topo(topo_diff), DBO_rm_node_set(id_set=[n_0_id]), # block chain DBO_block_chain__init(test_rzdoc), DBO_block_chain__commit(commit_obj=topo_diff.to_json_dict()), # rzdoc DBO_rzdoc__create(test_rzdoc), DBO_rzdoc__delete(test_rzdoc), DBO_rzdoc__list(), DBO_rzdoc__lookup_by_name(test_rzdoc.name), ] return op_set
def test_add_link_set(self): test_label = neo4j_test_util.rand_label() n_0, n_0_id = generate_random_node_dict(test_label) n_1, n_1_id = generate_random_node_dict(test_label) n_2, n_2_id = generate_random_node_dict(test_label) l_0, l_0_id = generate_random_link_dict(test_label, n_0_id, n_1_id) l_1, l_1_id = generate_random_link_dict(test_label, n_0_id, n_2_id) n_map = meta_attr_list_to_meta_attr_map([n_0, n_1, n_2]) op = DBO_add_node_set(n_map) self.db_ctl.exec_op(op) l_map = { test_label : [l_0, l_1]} op = DBO_add_link_set(l_map) self.assertEqual(len(op.statement_set), 2) # no support yet for parameterized statements for link creation ret_id_set = self.db_ctl.exec_op(op) self.assertEqual(len(ret_id_set), 2) self.assertTrue(l_0_id in ret_id_set) self.assertTrue(l_1_id in ret_id_set)
def test_add_link_set(self): test_label = neo4j_test_util.rand_label() n_0, n_0_id = generate_random_node_dict(test_label) n_1, n_1_id = generate_random_node_dict(test_label) n_2, n_2_id = generate_random_node_dict(test_label) l_0, l_0_id = generate_random_link_dict(test_label, n_0_id, n_1_id) l_1, l_1_id = generate_random_link_dict(test_label, n_0_id, n_2_id) n_map = meta_attr_list_to_meta_attr_map([n_0, n_1, n_2]) op = DBO_add_node_set(n_map) self.db_ctl.exec_op(op) l_map = {test_label: [l_0, l_1]} op = DBO_add_link_set(l_map) self.assertEqual( len(op.statement_set), 2) # no support yet for parameterized statements for link creation ret_id_set = self.db_ctl.exec_op(op) self.assertEqual(len(ret_id_set), 2) self.assertTrue(l_0_id in ret_id_set) self.assertTrue(l_1_id in ret_id_set)
def test_ws_event__topo_diff(self): class NS_test(BaseNamespace): def on_diff_commit__topo(self, *data): greenlet.getcurrent().data = data raise KeyboardInterrupt( ) # TODO: cleanup: properly close socket test_label = neo4j_test_util.rand_label() n_0, n_0_id = test_util.generate_random_node_dict(test_label) n_1, n_1_id = test_util.generate_random_node_dict(test_label) l, l_id = test_util.generate_random_link_dict(test_label, n_0_id, n_1_id) topo_diff = Topo_Diff(node_set_add=[n_0, n_1], link_set_add=[l]) def c_0(): with RZ_websocket(namespace=NS_test) as (_, ns_sock): c1_t.switch() # allow peer to connect data = json.dumps(topo_diff, cls=Topo_Diff.JSON_Encoder) ns_sock.emit('diff_commit__topo', data) c1_t.switch() def c_1(): with RZ_websocket(namespace=NS_test) as (sock, _): c0_t.switch() # allow peer to emit sock.wait(8) # allow self to receive c0_t = greenlet(c_0) c1_t = greenlet(c_1) c1_t.data = None c0_t.switch() self.assertTrue(None != c1_t.data) self.assertEqual(2, len(c1_t.data)) diff_in = Topo_Diff.from_json_dict(c1_t.data[0]) commit_ret = Topo_Diff.Commit_Result_Type.from_json_dict(c1_t.data[1]) self.assertEqual(Topo_Diff, type(diff_in)) self.assertEqual(Topo_Diff.Commit_Result_Type, type(commit_ret))
def test_ws_event__topo_diff(self): class NS_test(BaseNamespace): def on_diff_commit__topo(self, *data): greenlet.getcurrent().data = data raise KeyboardInterrupt() # TODO: cleanup: properly close socket test_label = neo4j_test_util.rand_label() n_0, n_0_id = test_util.generate_random_node_dict(test_label) n_1, n_1_id = test_util.generate_random_node_dict(test_label) l, l_id = test_util.generate_random_link_dict(test_label, n_0_id, n_1_id) topo_diff = Topo_Diff(node_set_add=[n_0, n_1], link_set_add=[l]) def c_0(): with RZ_websocket(namespace=NS_test) as (_, ns_sock): c1_t.switch() # allow peer to connect data = json.dumps(topo_diff, cls=Topo_Diff.JSON_Encoder) ns_sock.emit('diff_commit__topo', data) c1_t.switch() def c_1(): with RZ_websocket(namespace=NS_test) as (sock, _): c0_t.switch() # allow peer to emit sock.wait(8) # allow self to receive c0_t = greenlet(c_0) c1_t = greenlet(c_1) c1_t.data = None c0_t.switch() self.assertTrue(None != c1_t.data) self.assertEqual(2, len(c1_t.data)) diff_in = Topo_Diff.from_json_dict(c1_t.data[0]) commit_ret = Topo_Diff.Commit_Result_Type.from_json_dict(c1_t.data[1]) self.assertEqual(Topo_Diff, type(diff_in)) self.assertEqual(Topo_Diff.Commit_Result_Type, type(commit_ret))
def test_diff_commit__topo(self): test_label = neo4j_test_util.rand_label() n_0, n_0_id = generate_random_node_dict(test_label) n_1, n_1_id = generate_random_node_dict(test_label) n_2, n_2_id = generate_random_node_dict(test_label) l_0, l_0_id = generate_random_link_dict(test_label, n_0_id, n_1_id) l_1, l_1_id = generate_random_link_dict(test_label, n_0_id, n_2_id) n_set = [n_0, n_1, n_2] l_set = [l_0, l_1] topo_diff = Topo_Diff(node_set_add=n_set, link_set_add=l_set) # commit diff op = DBO_diff_commit__topo(topo_diff) ret_topo_diff = self.db_ctl.exec_op(op) # test return type self.assertTrue(hasattr(ret_topo_diff, 'node_id_set_add')) self.assertTrue(hasattr(ret_topo_diff, 'link_id_set_add')) self.assertTrue(hasattr(ret_topo_diff, 'node_id_set_rm')) self.assertTrue(hasattr(ret_topo_diff, 'link_id_set_rm')) # test return set lengths self.assertEqual(len(ret_topo_diff.node_id_set_add), len(n_set)) self.assertEqual(len(ret_topo_diff.link_id_set_add), len(l_set)) self.assertEqual(len(ret_topo_diff.node_id_set_rm), 0) self.assertEqual(len(ret_topo_diff.link_id_set_rm), 0) # assert nodes persisted id_set = self.db_ctl.exec_op(DBO_match_node_set_by_id_attribute([n_0_id, n_1_id])) self.assertEqual(len(id_set), 2) # assert links persisted l_ptr_0 = Link.link_ptr(src_id=n_0_id, dst_id=n_1_id) l_ptr_1 = Link.link_ptr(src_id=n_0_id, dst_id=n_2_id) op = DBO_load_link_set.init_from_link_ptr_set([l_ptr_0, l_ptr_1]) id_set = self.db_ctl.exec_op(op) self.assertEqual(len(id_set), 2) # remova links topo_diff = Topo_Diff(link_id_set_rm=[l_0_id, l_1_id]) op = DBO_diff_commit__topo(topo_diff) ret_topo_diff = self.db_ctl.exec_op(op) self.assertEqual(len(ret_topo_diff.link_id_set_rm), 2) # assert links removed op = DBO_load_link_set.init_from_link_ptr_set([l_ptr_0, l_ptr_1]) id_set = self.db_ctl.exec_op(op) self.assertEqual(len(id_set), 0) # removal nodes topo_diff = Topo_Diff(node_id_set_rm=[n_2_id]) op = DBO_diff_commit__topo(topo_diff) ret_topo_diff = self.db_ctl.exec_op(op) self.assertEqual(len(ret_topo_diff.node_id_set_rm), 1) # assert nodes removed op = DBO_match_node_set_by_id_attribute([n_2_id]) id_set = self.db_ctl.exec_op(op) self.assertEqual(len(id_set), 0)
def test_diff_commit__topo(self): test_label = neo4j_test_util.rand_label() n_0, n_0_id = generate_random_node_dict(test_label) n_1, n_1_id = generate_random_node_dict(test_label) n_2, n_2_id = generate_random_node_dict(test_label) l_0, l_0_id = generate_random_link_dict(test_label, n_0_id, n_1_id) l_1, l_1_id = generate_random_link_dict(test_label, n_0_id, n_2_id) n_set = [n_0, n_1, n_2] l_set = [l_0, l_1] topo_diff = Topo_Diff(node_set_add=n_set, link_set_add=l_set) # commit diff op = DBO_diff_commit__topo(topo_diff) ret_topo_diff = self.db_ctl.exec_op(op) # test return type self.assertTrue(hasattr(ret_topo_diff, 'node_id_set_add')) self.assertTrue(hasattr(ret_topo_diff, 'link_id_set_add')) self.assertTrue(hasattr(ret_topo_diff, 'node_id_set_rm')) self.assertTrue(hasattr(ret_topo_diff, 'link_id_set_rm')) # test return set lengths self.assertEqual(len(ret_topo_diff.node_id_set_add), len(n_set)) self.assertEqual(len(ret_topo_diff.link_id_set_add), len(l_set)) self.assertEqual(len(ret_topo_diff.node_id_set_rm), 0) self.assertEqual(len(ret_topo_diff.link_id_set_rm), 0) # assert nodes persisted id_set = self.db_ctl.exec_op( DBO_match_node_set_by_id_attribute([n_0_id, n_1_id])) self.assertEqual(len(id_set), 2) # assert links persisted l_ptr_0 = Link.link_ptr(src_id=n_0_id, dst_id=n_1_id) l_ptr_1 = Link.link_ptr(src_id=n_0_id, dst_id=n_2_id) op = DBO_load_link_set.init_from_link_ptr_set([l_ptr_0, l_ptr_1]) id_set = self.db_ctl.exec_op(op) self.assertEqual(len(id_set), 2) # remova links topo_diff = Topo_Diff(link_id_set_rm=[l_0_id, l_1_id]) op = DBO_diff_commit__topo(topo_diff) ret_topo_diff = self.db_ctl.exec_op(op) self.assertEqual(len(ret_topo_diff.link_id_set_rm), 2) # assert links removed op = DBO_load_link_set.init_from_link_ptr_set([l_ptr_0, l_ptr_1]) id_set = self.db_ctl.exec_op(op) self.assertEqual(len(id_set), 0) # removal nodes topo_diff = Topo_Diff(node_id_set_rm=[n_2_id]) op = DBO_diff_commit__topo(topo_diff) ret_topo_diff = self.db_ctl.exec_op(op) self.assertEqual(len(ret_topo_diff.node_id_set_rm), 1) # assert nodes removed op = DBO_match_node_set_by_id_attribute([n_2_id]) id_set = self.db_ctl.exec_op(op) self.assertEqual(len(id_set), 0)