def setUpClass(cls): """ Make sure we have some sort of schema and data in DB, only done once """ super(test_client_base, cls).setUpClass() global client, session client = GraknClient("localhost:48555") keyspace = "test_" + str(uuid.uuid4()).replace("-", "_")[:8] session = client.session(keyspace) # temp tx to set up DB, don't save it with session.transaction().write() as tx: try: # define parentship roles to test agains tx.query( "define " "parent sub role; " "child sub role; " "mother sub role; " "son sub role; " "person sub entity, has age, has gender, plays parent, plays child, plays mother, plays son; " "age sub attribute, value long; " "gender sub attribute, value string; " "parentship sub relation, relates parent, relates child, relates mother, relates son;" ) except GraknError as ce: print(ce) answers = list(tx.query("match $x isa person, has age 20; get;")) if len(answers) == 0: tx.query("insert $x isa person, has age 20;") tx.commit()
def test_client_session_close(self): client = GraknClient('localhost:48555') a_session = client.session('test') a_session.close() with self.assertRaises(GraknError): a_session.transaction().read() client.close()
def test_client_session_invalid_keyspace(self): client = GraknClient('localhost:48555') with self.assertRaises(TypeError): a_session = client.session(123) tx = a_session.transaction().read( ) # won't fail until opening a transaction inst2 = GraknClient('localhost:48555') with self.assertRaises(GraknError): a_session = inst2.session('') tx = a_session.transaction().read( ) # won't fail until opening a transaction client.close()
def test_client_init_invalid_uri(self): """ Test invalid URI """ with self.assertRaises(GraknError): a_inst = GraknClient('localhost:1000') a_session = a_inst.session('testkeyspace') a_session.transaction().read() with self.assertRaises(GraknError): a_inst = GraknClient('localhost:1000') with a_inst.session("test") as s: with s.transaction().read() as tx: pass a_inst.close()
def test_client_session_valid_keyspace(self): """ Test OK uri and keyspace """ a_inst = GraknClient('localhost:48555') a_session = a_inst.session('test') self.assertIsInstance(a_session, grakn.rpc.Session) tx = a_session.transaction().read() tx.close() a_session.close() # test the `with` statement with a_inst.session('test') as session: self.assertIsInstance(session, grakn.rpc.Session) tx = session.transaction().read() tx.close() a_inst.close()
def test_client_with_statement(self): """ Test that client is compatible with using `with` """ with GraknClient("localhost:48555") as client: with client.session("testing") as session: with session.transaction().read() as tx: tx.query("match $x sub thing; get;")
def test_client_init_valid(self): """ Test valid URI """ a_inst = GraknClient('localhost:48555') self.assertIsInstance(a_inst, GraknClient) a_inst.close()
def test_client_tx_invalid_enum(self): client = GraknClient('localhost:48555') a_session = client.session('test') with self.assertRaises(Exception): a_session.transaction('foo') client.close()
def test_client_tx_valid_enum(self): client = GraknClient('localhost:48555') a_session = client.session('test') tx = a_session.transaction().read() self.assertIsInstance(tx, grakn.rpc.Transaction) client.close()