From 2b3a23e90f1aa71d8e69460ebfe0e8e76ba39486 Mon Sep 17 00:00:00 2001 From: Benjamin Winger Date: Thu, 16 Nov 2023 16:40:04 -0500 Subject: [PATCH] Remove test_main and allow pytest to be run from any directory --- Makefile | 3 +- tools/python_api/test/conftest.py | 31 +++++++++---------- tools/python_api/test/test_arrow.py | 2 -- tools/python_api/test/test_df.py | 1 - tools/python_api/test/test_exception.py | 1 - tools/python_api/test/test_helper.py | 8 +++++ tools/python_api/test/test_main.py | 20 ------------ .../test/test_query_result_close.py | 5 ++- tools/python_api/test/test_scan_pandas.py | 2 -- .../test_torch_geometric_remote_backend.py | 4 +-- 10 files changed, 28 insertions(+), 49 deletions(-) create mode 100644 tools/python_api/test/test_helper.py delete mode 100644 tools/python_api/test/test_main.py diff --git a/Makefile b/Makefile index 4d191db9415..df6c7e408b6 100644 --- a/Makefile +++ b/Makefile @@ -129,8 +129,7 @@ nodejstest: nodejs npm test pytest: python - cd tools/python_api/test - python3 -m pytest -v test_main.py + cmake -E PYTHONPATH=tools/python_api/build python3 -m pytest -v tools/python_api/test rusttest: rust cd tools/rust_api diff --git a/tools/python_api/test/conftest.py b/tools/python_api/test/conftest.py index 827d47299a3..d6016114709 100644 --- a/tools/python_api/test/conftest.py +++ b/tools/python_api/test/conftest.py @@ -1,9 +1,8 @@ import os -import sys import pytest import shutil +from test_helper import KUZU_ROOT -sys.path.append('../build/') import kuzu @@ -12,18 +11,18 @@ def init_npy(conn): 'create node table npyoned (i64 INT64,i32 INT32,i16 INT16,f64 DOUBLE,f32 FLOAT, PRIMARY KEY(i64));' ) conn.execute( - 'copy npyoned from ("../../../dataset/npy-1d/one_dim_int64.npy", "../../../dataset/npy-1d/one_dim_int32.npy", ' - ' "../../../dataset/npy-1d/one_dim_int16.npy", "../../../dataset/npy-1d/one_dim_double.npy", ' - '"../../../dataset/npy-1d/one_dim_float.npy") by column;' + f'copy npyoned from ("{KUZU_ROOT}/dataset/npy-1d/one_dim_int64.npy", "{KUZU_ROOT}/dataset/npy-1d/one_dim_int32.npy", ' + f' "{KUZU_ROOT}/dataset/npy-1d/one_dim_int16.npy", "{KUZU_ROOT}/dataset/npy-1d/one_dim_double.npy", ' + f'"{KUZU_ROOT}/dataset/npy-1d/one_dim_float.npy") by column;' ) conn.execute( 'create node table npytwod (id INT64, i64 INT64[3], i32 INT32[3], i16 INT16[3], f64 DOUBLE[3], f32 FLOAT[3],' 'PRIMARY KEY(id));' ) conn.execute( - 'copy npytwod from ("../../../dataset/npy-2d/id_int64.npy", "../../../dataset/npy-2d/two_dim_int64.npy", ' - '"../../../dataset/npy-2d/two_dim_int32.npy", "../../../dataset/npy-2d/two_dim_int16.npy", ' - ' "../../../dataset/npy-2d/two_dim_double.npy", "../../../dataset/npy-2d/two_dim_float.npy") by column;' + f'copy npytwod from ("{KUZU_ROOT}/dataset/npy-2d/id_int64.npy", "{KUZU_ROOT}/dataset/npy-2d/two_dim_int64.npy", ' + f'"{KUZU_ROOT}/dataset/npy-2d/two_dim_int32.npy", "{KUZU_ROOT}/dataset/npy-2d/two_dim_int16.npy", ' + f' "{KUZU_ROOT}/dataset/npy-2d/two_dim_double.npy", "{KUZU_ROOT}/dataset/npy-2d/two_dim_float.npy") by column;' ) @@ -31,25 +30,25 @@ def init_tensor(conn): conn.execute('create node table tensor (ID INT64, boolTensor BOOLEAN[], doubleTensor DOUBLE[][], ' 'intTensor INT64[][][], oneDimInt INT64, PRIMARY KEY (ID));') conn.execute( - 'COPY tensor FROM "../../../dataset/tensor-list/vTensor.csv" (HEADER=true)') + f'COPY tensor FROM "{KUZU_ROOT}/dataset/tensor-list/vTensor.csv" (HEADER=true)') def init_long_str(conn): conn.execute( - "CREATE NODE TABLE personLongString (name STRING, spouse STRING, PRIMARY KEY(name))") + f"CREATE NODE TABLE personLongString (name STRING, spouse STRING, PRIMARY KEY(name))") conn.execute( - 'COPY personLongString FROM "../../../dataset/long-string-pk-tests/vPerson.csv"') + f'COPY personLongString FROM "{KUZU_ROOT}/dataset/long-string-pk-tests/vPerson.csv"') conn.execute( - "CREATE REL TABLE knowsLongString (FROM personLongString TO personLongString, MANY_MANY)") + f"CREATE REL TABLE knowsLongString (FROM personLongString TO personLongString, MANY_MANY)") conn.execute( - 'COPY knowsLongString FROM "../../../dataset/long-string-pk-tests/eKnows.csv"') + f'COPY knowsLongString FROM "{KUZU_ROOT}/dataset/long-string-pk-tests/eKnows.csv"') def init_tinysnb(conn): tiny_snb_path = os.path.abspath( os.path.join( os.path.dirname(os.path.abspath(__file__)), - "../../../dataset/tinysnb") + f"{KUZU_ROOT}/dataset/tinysnb") ) schema_path = os.path.join(tiny_snb_path, "schema.cypher") with open(schema_path, "r") as f: @@ -61,7 +60,7 @@ def init_tinysnb(conn): with open(copy_path, "r") as f: for line in f.readlines(): line = line.strip() - line = line.replace("dataset/tinysnb", "../../../dataset/tinysnb") + line = line.replace("dataset/tinysnb", f"{KUZU_ROOT}/dataset/tinysnb") if line: conn.execute(line) @@ -71,7 +70,7 @@ def init_movie_serial(conn): "create node table moviesSerial (ID SERIAL, name STRING, length INT32, note STRING, PRIMARY KEY (ID));" ) conn.execute( - 'copy moviesSerial from "../../../dataset/tinysnb-serial/vMovies.csv"' + f'copy moviesSerial from "{KUZU_ROOT}/dataset/tinysnb-serial/vMovies.csv"' ) diff --git a/tools/python_api/test/test_arrow.py b/tools/python_api/test/test_arrow.py index 6b7adf948d3..276f8ddafc4 100644 --- a/tools/python_api/test/test_arrow.py +++ b/tools/python_api/test/test_arrow.py @@ -1,7 +1,5 @@ -import sys import time -sys.path.append('../build/') import kuzu import pyarrow as pa import datetime diff --git a/tools/python_api/test/test_df.py b/tools/python_api/test/test_df.py index 9a64c3b8c4d..a7b0bad1e2e 100644 --- a/tools/python_api/test/test_df.py +++ b/tools/python_api/test/test_df.py @@ -3,7 +3,6 @@ import sys from decimal import Decimal -sys.path.append('../build/') import kuzu from pandas import Timestamp, Timedelta, isna diff --git a/tools/python_api/test/test_exception.py b/tools/python_api/test/test_exception.py index 508cd73b6e0..328d5f20701 100644 --- a/tools/python_api/test/test_exception.py +++ b/tools/python_api/test/test_exception.py @@ -1,6 +1,5 @@ import pytest import sys -sys.path.append('../build/') import kuzu diff --git a/tools/python_api/test/test_helper.py b/tools/python_api/test/test_helper.py new file mode 100644 index 00000000000..57a0696b12e --- /dev/null +++ b/tools/python_api/test/test_helper.py @@ -0,0 +1,8 @@ +from pathlib import Path +import sys + +KUZU_ROOT = Path(__file__).parent.parent.parent.parent + +if sys.platform == "win32": + # \ in paths is not supported by kuzu's parser + KUZU_ROOT=str(KUZU_ROOT).replace("\\", "/") diff --git a/tools/python_api/test/test_main.py b/tools/python_api/test/test_main.py deleted file mode 100644 index 3315178b156..00000000000 --- a/tools/python_api/test/test_main.py +++ /dev/null @@ -1,20 +0,0 @@ -import pytest - -from test_arrow import * -from test_datatype import * -from test_df import * -from test_exception import * -from test_get_header import * -from test_networkx import * -from test_parameter import * -from test_prepared_statement import * -from test_query_result import * -from test_query_result_close import * -from test_timeout import * -from test_torch_geometric import * -from test_torch_geometric_remote_backend import * -from test_write_to_csv import * -from test_scan_pandas import * - -if __name__ == "__main__": - raise SystemExit(pytest.main([__file__])) diff --git a/tools/python_api/test/test_query_result_close.py b/tools/python_api/test/test_query_result_close.py index 8abd8a16e9a..82abe3af101 100644 --- a/tools/python_api/test/test_query_result_close.py +++ b/tools/python_api/test/test_query_result_close.py @@ -1,11 +1,10 @@ import subprocess import sys +from test_helper import KUZU_ROOT def test_query_result_close(get_tmp_path): code = [ - 'import sys', - 'sys.path.append("../build/")', 'import kuzu', # Note: Windows paths include backslashes, which need to be raw strings or escaped. 'db = kuzu.Database(r"' + get_tmp_path + '")', @@ -15,7 +14,7 @@ def test_query_result_close(get_tmp_path): birthdate DATE, registerTime TIMESTAMP, lastJobDuration INTERVAL,\ workedHours INT64[], usedNames STRING[], courseScoresPerTerm INT64[][], grades INT64[4], height float, \ PRIMARY KEY (ID))\')', - 'conn.execute(\'COPY person FROM \"../../../dataset/tinysnb/vPerson.csv\" (HEADER=true)\')', + f'conn.execute(\'COPY person FROM \"{KUZU_ROOT}/dataset/tinysnb/vPerson.csv\" (HEADER=true)\')', 'result = conn.execute("MATCH (a:person) WHERE a.ID = 0 RETURN a.isStudent;")', # 'result.close()', ] diff --git a/tools/python_api/test/test_scan_pandas.py b/tools/python_api/test/test_scan_pandas.py index 84aaef8ff7a..8aa9cce1c12 100644 --- a/tools/python_api/test/test_scan_pandas.py +++ b/tools/python_api/test/test_scan_pandas.py @@ -1,11 +1,9 @@ -import sys import numpy as np import pandas as pd import datetime import pytest import re -sys.path.append('../build/') import kuzu diff --git a/tools/python_api/test/test_torch_geometric_remote_backend.py b/tools/python_api/test/test_torch_geometric_remote_backend.py index c9d26b18e87..e44b0ff959b 100644 --- a/tools/python_api/test/test_torch_geometric_remote_backend.py +++ b/tools/python_api/test/test_torch_geometric_remote_backend.py @@ -2,7 +2,7 @@ import random import sys -sys.path.append('../build/') +from test_helper import KUZU_ROOT import kuzu TINY_SNB_KNOWS_GROUND_TRUTH = { @@ -103,7 +103,7 @@ def test_remote_backend_20k(establish_connection): _, db = establish_connection conn = kuzu.Connection(db, num_threads=1) conn.execute('create node table npy20k (id INT64,f32 FLOAT[10],PRIMARY KEY(id));') - conn.execute('copy npy20k from ("../../../dataset/npy-20k/id_int64.npy", "../../../dataset/npy-20k/two_dim_float.npy") by column;') + conn.execute(f'copy npy20k from ("{KUZU_ROOT}/dataset/npy-20k/id_int64.npy", "{KUZU_ROOT}/dataset/npy-20k/two_dim_float.npy") by column;') del conn fs, _ = db.get_torch_geometric_remote_backend(8) for i in range(20000):