def test_connection_secure_fail(memgraph_server): # server doesn't use SSL host, port, sslmode, _ = memgraph_server with pytest.raises(mgclient.OperationalError): mgclient.connect(host=host, port=port, sslmode=mgclient.MG_SSLMODE_REQUIRE)
def test_connection_secure_success(secure_memgraph_server): host, port, is_long_running = secure_memgraph_server with pytest.raises(mgclient.OperationalError): conn = mgclient.connect(host=host, port=port) def good_trust_callback(hostname, ip_address, key_type, fingerprint): if not is_long_running: assert hostname == "localhost" assert ip_address == "127.0.0.1" return True def bad_trust_callback(hostname, ip_address, key_type, fingerprint): if not is_long_running: assert hostname == "localhost" assert ip_address == "127.0.0.1" return False with pytest.raises(mgclient.OperationalError): conn = mgclient.connect( host=host, port=port, sslmode=mgclient.MG_SSLMODE_REQUIRE, trust_callback=bad_trust_callback, ) conn = mgclient.connect( host=host, port=port, sslmode=mgclient.MG_SSLMODE_REQUIRE, trust_callback=good_trust_callback, ) assert conn.status == mgclient.CONN_STATUS_READY
def test_cursor_runtime_error(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) cursor = conn.cursor() cursor.execute("RETURN 100") assert cursor.fetchall() == [(100,)] cursor.execute("UNWIND [true, true, false] AS p RETURN assert(p)") with pytest.raises(mgclient.DatabaseError): assert cursor.fetchone() == (True,) # NOTE: The exception is going to happen here which is unexpected. # The reason for that is because server pulls one more result ahead # of time to know are there more results. assert cursor.fetchone() == (True,) # <- HERE cursor.fetchone() cursor.execute("UNWIND [true, true, false] AS p RETURN assert(p)") with pytest.raises(mgclient.DatabaseError): cursor.fetchmany(5) cursor.execute("UNWIND [true, true, false] AS p RETURN assert(p)") with pytest.raises(mgclient.DatabaseError): cursor.fetchall()
def test_cursor_close(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("UNWIND range(1, 10) AS n RETURN n") cursor.close() # closing again does nothing cursor.close() with pytest.raises(mgclient.InterfaceError): cursor.fetchone() with pytest.raises(mgclient.InterfaceError): cursor.execute("RETURN 100") with pytest.raises(mgclient.InterfaceError): cursor.fetchmany() with pytest.raises(mgclient.InterfaceError): cursor.fetchall() with pytest.raises(mgclient.InterfaceError): cursor.setinputsizes([]) with pytest.raises(mgclient.InterfaceError): cursor.setoutputsizes(100)
def main(): conn = mgclient.connect(host="127.0.0.1", port=7687) cursor = conn.cursor() for _ in range(1, 10000): cursor.execute( """ CREATE (n:Person {name: 'John'})-[e:KNOWS]-> (m:Person {name: 'Steve'}); """ ) while True: tracemalloc.start() cursor.execute( """ MATCH (n:Person {name: 'John'})-[e:KNOWS]-> (m:Person {name: 'Steve'}) RETURN n, m, e """ ) cursor.fetchone() snapshot = tracemalloc.take_snapshot() display_top(snapshot)
def test_autocommit_regular(memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) # autocommit should be turned off by default assert not conn.autocommit cursor = conn.cursor() cursor.execute("RETURN 5") assert conn.status == mgclient.CONN_STATUS_IN_TRANSACTION # can't update autocommit while in transaction with pytest.raises(mgclient.InterfaceError): conn.autocommit = True conn.rollback() conn.autocommit = True assert conn.autocommit assert conn.status == mgclient.CONN_STATUS_READY cursor.execute("RETURN 5") assert conn.status == mgclient.CONN_STATUS_READY with pytest.raises(mgclient.InterfaceError): del conn.autocommit
def test_cursor_fetchmany(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) cursor = conn.cursor() with pytest.raises(mgclient.InterfaceError): cursor.fetchmany() cursor.execute("UNWIND range(1, 10) AS n RETURN n") with pytest.raises(OverflowError): cursor.fetchmany(10 ** 100) assert cursor.fetchmany() == [(1,)] cursor.arraysize = 4 assert cursor.fetchmany() == [(2,), (3,), (4,), (5,)] assert cursor.fetchmany() == [(6,), (7,), (8,), (9,)] assert cursor.fetchmany() == [(10,)] assert cursor.fetchmany() == [] assert cursor.fetchone() is None cursor.execute("RETURN 100") assert cursor.fetchmany() == [(100,)] assert cursor.fetchmany() == [] assert cursor.fetchone() is None
def test_cursor_fetchmany_without_result(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) cursor = conn.cursor() cursor.execute("MATCH (n:NonExistingLabel) RETURN n") assert cursor.fetchmany() == []
def test_cursor_fetchone_without_result(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("MATCH (n:NonExistingLabel) RETURN n") result = cursor.fetchone() assert result is None
def _create_connection(self): sslmode = mgclient.MG_SSLMODE_REQUIRE if self.encrypted else mgclient.MG_SSLMODE_DISABLE return mgclient.connect(host=self.host, port=self.port, username=self.username, password=self.password, sslmode=sslmode, lazy=self.lazy)
def memgraph_connection(): memgraph = start_memgraph() conn = mgclient.connect(host=memgraph.host, port=memgraph.port, sslmode=memgraph.sslmode()) conn.autocommit = True yield conn memgraph.kill() conn.close()
def test_execute_closed_connection(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() conn.close() with pytest.raises(mgclient.InterfaceError): cursor.execute("RETURN 100")
def test_close_doesnt_commit(memgraph_server): host, port, sslmode, is_long_running = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("MATCH (n) RETURN count(n)") original_count = cursor.fetchall()[0][0] assert is_long_running or original_count == 0 cursor.execute("CREATE (:Node)") conn.close() conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("MATCH (n) RETURN count(n)") assert cursor.fetchall() == [(original_count, )]
def test_autocommit_lazy(memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) # autocommit is always true for lazy connections assert conn.autocommit with pytest.raises(mgclient.InterfaceError): conn.autocommit = False
def _create_connection(self) -> Connection: """Creates and returns a connection with Memgraph.""" sslmode = mgclient.MG_SSLMODE_REQUIRE if self.encrypted else mgclient.MG_SSLMODE_DISABLE return mgclient.connect( host=self.host, port=self.port, username=self.username, password=self.password, sslmode=sslmode, lazy=self.lazy, client_name=self.client_name, )
def test_cursor_syntax_error(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("RETURN 100") with pytest.raises(mgclient.DatabaseError): cursor.execute("fjdkalfjdsalfaj") with pytest.raises(mgclient.InterfaceError): cursor.fetchall()
def test_commit(memgraph_server): host, port, sslmode, is_long_running = memgraph_server conn1 = mgclient.connect(host=host, port=port, sslmode=sslmode) conn2 = mgclient.connect(host=host, port=port, sslmode=sslmode) conn2.autocommit = True cursor1 = conn1.cursor() cursor1.execute("MATCH (n) RETURN count(n)") original_count = cursor1.fetchall()[0][0] assert is_long_running or original_count == 0 cursor1.execute("CREATE (:Node)") cursor2 = conn2.cursor() cursor2.execute("MATCH (n) RETURN count(n)") assert cursor2.fetchall() == [(original_count, )] conn1.commit() cursor2.execute("MATCH (n) RETURN count(n)") assert cursor2.fetchall() == [(original_count + 1, )]
def test_cursor_multiple_queries(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor1 = conn.cursor() cursor2 = conn.cursor() cursor1.execute("UNWIND range(1, 10) AS n RETURN n") cursor2.execute("UNWIND range(1, 10) AS n RETURN n") for n in range(1, 11): assert cursor1.fetchone() == (n,) assert cursor2.fetchone() == (n,)
def test_commit_rollback_lazy(memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) cursor = conn.cursor() cursor.execute("CREATE (:Node) RETURN 1") conn.rollback() assert conn.status == mgclient.CONN_STATUS_EXECUTING conn.commit() assert conn.status == mgclient.CONN_STATUS_EXECUTING assert cursor.fetchall() == [(1, )] assert conn.status == mgclient.CONN_STATUS_READY
def test_cursor_runtime_error(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("RETURN 100") with pytest.raises(mgclient.DatabaseError): cursor.execute("UNWIND [true, true, false] AS p RETURN assert(p)") cursor.fetchall() cursor.execute("RETURN 200") assert cursor.fetchall() == [(200,)]
def test_cursor_description(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("RETURN 5 AS x, 6 AS y") assert len(cursor.description) == 2 assert cursor.description[0].name == "x" assert cursor.description[1].name == "y" with pytest.raises(mgclient.DatabaseError): cursor.execute("jdfklfjkdalfja") assert cursor.description is None
def test_autocommit_failure(memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) conn.autocommit = False assert conn.status == mgclient.CONN_STATUS_READY cursor = conn.cursor() cursor.execute("RETURN 5") assert conn.status == mgclient.CONN_STATUS_IN_TRANSACTION with pytest.raises(mgclient.DatabaseError): cursor.execute("SHOW INDEX INFO") assert conn.status == mgclient.CONN_STATUS_READY cursor.execute("RETURN 5") assert conn.status == mgclient.CONN_STATUS_IN_TRANSACTION
def test_cursor_multiple_queries(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) cursor1 = conn.cursor() cursor2 = conn.cursor() cursor1.execute("UNWIND range(1, 10) AS n RETURN n") with pytest.raises(mgclient.InterfaceError): cursor2.execute("UNWIND range(1, 10) AS n RETURN n") assert cursor1.fetchall() == [(n,) for n in range(1, 11)] with pytest.raises(mgclient.InterfaceError): cursor2.fetchall()
def test_connection_close_lazy(memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) cursor = conn.cursor() assert conn.status == mgclient.CONN_STATUS_READY cursor.execute("RETURN 100") assert conn.status == mgclient.CONN_STATUS_EXECUTING with pytest.raises(mgclient.InterfaceError): conn.close() cursor.fetchall() conn.close() assert conn.status == mgclient.CONN_STATUS_CLOSED
def test_cursor_close(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, lazy=True, sslmode=sslmode) cursor = conn.cursor() cursor.execute("UNWIND range(1, 10) AS n RETURN n") cursor2 = conn.cursor() with pytest.raises(mgclient.InterfaceError): cursor.close() cursor2.close() # NOTE: This here is a bit strange again because of double fetch / # server ahead of time pull because of the need for has_more info. As # soon as the last record is returned, the cursor will become # closeable. assert cursor.fetchmany(9) == [(n,) for n in range(1, 10)] with pytest.raises(mgclient.InterfaceError): cursor.close() assert cursor.fetchone() == (10,) assert cursor.fetchone() is None cursor.close() # closing again does nothing cursor.close() with pytest.raises(mgclient.InterfaceError): cursor.fetchone() with pytest.raises(mgclient.InterfaceError): cursor.execute("RETURN 100") with pytest.raises(mgclient.InterfaceError): cursor.fetchmany() with pytest.raises(mgclient.InterfaceError): cursor.fetchall() with pytest.raises(mgclient.InterfaceError): cursor.setinputsizes([]) with pytest.raises(mgclient.InterfaceError): cursor.setoutputsizes(100)
def test_cursor_result_ref_counts(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() cursor.execute("UNWIND [1, 2, 3, 4, 5] AS n RETURN n") fetchone_result = cursor.fetchone() # Refs are the following: # 1. fetchone_result # 2. temp reference in sys.getrefcount # 3. cursor->rows assert sys.getrefcount(fetchone_result) == 3 fetchmany_result = cursor.fetchmany(2) # Refs are the following: # 1. fetchmany_result # 2. temp reference in sys.getrefcount assert sys.getrefcount(fetchmany_result) == 2 row1 = fetchmany_result[0] row2 = fetchmany_result[1] del fetchmany_result # Refs are the following: # 1. row{1,2} # 2. temp reference in sys.getrefcount # 3. cursor->rows assert sys.getrefcount(row1) == 3 assert sys.getrefcount(row2) == 3 fetchall_result = cursor.fetchall() # Refs are the following: # 1. fetchall_result # 2. temp reference in sys.getrefcount assert sys.getrefcount(fetchall_result) == 2 row1 = fetchall_result[0] row2 = fetchall_result[1] del fetchall_result # Refs are the following: # 1. row{1,2} # 2. temp reference in sys.getrefcount # 3. cursor->rows assert sys.getrefcount(row1) == 3 assert sys.getrefcount(row2) == 3
def test_cursor_fetchone(self, memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) cursor = conn.cursor() with pytest.raises(mgclient.InterfaceError): cursor.fetchone() cursor.execute("UNWIND range(1, 10) AS n RETURN n") for n in range(1, 11): assert cursor.fetchone() == (n,) assert cursor.fetchone() is None assert cursor.fetchone() is None cursor.execute("RETURN 100") assert cursor.fetchone() == (100,) assert cursor.fetchone() is None
def test_connection_close(memgraph_server): host, port, sslmode, _ = memgraph_server conn = mgclient.connect(host=host, port=port, sslmode=sslmode) assert conn.status == mgclient.CONN_STATUS_READY conn.close() assert conn.status == mgclient.CONN_STATUS_CLOSED # closing twice doesn't do anything conn.close() assert conn.status == mgclient.CONN_STATUS_CLOSED with pytest.raises(mgclient.InterfaceError): conn.commit() with pytest.raises(mgclient.InterfaceError): conn.rollback() with pytest.raises(mgclient.InterfaceError): conn.cursor()
def test_connect_args_validation(): # bad port with pytest.raises(ValueError): mgclient.connect(host="127.0.0.1", port=12344567) # bad SSL mode with pytest.raises(ValueError): mgclient.connect(host="127.0.0.1", port=7687, sslmode=55) # trust_callback not callable with pytest.raises(TypeError): mgclient.connect( host="127.0.0.1", port=7687, sslmode=mgclient.MG_SSLMODE_REQUIRE, trust_callback="not callable", )
import mgclient from traceback import print_exc con = mgclient.connect(host='127.0.0.1', port=27687) con.autocommit = True cur = con.cursor() try: cur.execute("CREATE INDEX ON :Message") cur.execute("CREATE INDEX ON :Comment") cur.execute("CREATE INDEX ON :Post") cur.execute("CREATE INDEX ON :Person") cur.execute("CREATE INDEX ON :Forum") cur.execute("CREATE INDEX ON :City") cur.execute("CREATE INDEX ON :Country") cur.execute("CREATE INDEX ON :Tag") cur.execute("CREATE INDEX ON :TagClass") except mgclient.DatabaseError: print_exc()