Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: refactor test cases to drop index/table after the case is tested. #970

Merged
merged 11 commits into from
Apr 10, 2024
7 changes: 3 additions & 4 deletions python/test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ def test_create_db_with_invalid_name(self):
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
assert infinity_obj

try:
db_name = ""
with pytest.raises(Exception,
match=f"DB name '{db_name}' is not valid. It should start with a letter and can contain only letters, numbers and underscores"):
db = infinity_obj.create_database("")
except Exception as e:
print(e)

assert infinity_obj.disconnect()

@pytest.mark.parametrize("check_data", [{"file_name": "embedding_int_dim3.csv",
Expand Down
36 changes: 22 additions & 14 deletions python/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestConvert:
def test_to_pl(self):
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_to_pl", True)
db_obj.drop_table("test_to_pl", ConflictType.Ignore)
db_obj.create_table("test_to_pl", {
"c1": "int", "c2": "float"}, ConflictType.Error)

Expand All @@ -23,11 +23,12 @@ def test_to_pl(self):
print(res)
res = table_obj.output(["c1", "c2", "c1"]).to_pl()
print(res)
db_obj.drop_table("test_to_pl", ConflictType.Error)

def test_to_pa(self):
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_to_pa", True)
db_obj.drop_table("test_to_pa", ConflictType.Ignore)
db_obj.create_table("test_to_pa", {
"c1": "int", "c2": "float"}, ConflictType.Error)

Expand All @@ -40,15 +41,16 @@ def test_to_pa(self):
print(res)
res = table_obj.output(["c1", "c2", "c1"]).to_arrow()
print(res)
db_obj.drop_table("test_to_pa", ConflictType.Error)

def test_to_df(self):
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_to_pa", True)
db_obj.create_table("test_to_pa", {
db_obj.drop_table("test_to_df", ConflictType.Ignore)
db_obj.create_table("test_to_df", {
"c1": "int", "c2": "float"}, ConflictType.Error)

table_obj = db_obj.get_table("test_to_pa")
table_obj = db_obj.get_table("test_to_df")
table_obj.insert([{"c1": 1, "c2": 2.0}])
print()
res = table_obj.output(["c1", "c2"]).to_df()
Expand All @@ -57,12 +59,13 @@ def test_to_df(self):
print(res)
res = table_obj.output(["c1", "c2", "c1"]).to_df()
print(res)
db_obj.drop_table("test_to_df", ConflictType.Error)

def test_without_output_select_list(self):
# connect
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_without_output_select_list", True)
db_obj.drop_table("test_without_output_select_list", ConflictType.Ignore)
table_obj = db_obj.create_table("test_without_output_select_list", {
"c1": "int", "c2": "float"}, ConflictType.Error)

Expand All @@ -73,6 +76,7 @@ def test_without_output_select_list(self):
insert_res_pl = table_obj.output([]).to_pl()
print(insert_res_df, insert_res_arrow, insert_res_pl)

db_obj.drop_table("test_without_output_select_list", ConflictType.Error)
# disconnect
res = infinity_obj.disconnect()
assert res.error_code == ErrorCode.OK
Expand All @@ -88,8 +92,8 @@ def test_with_valid_select_list_output(self, condition_list):
# connect
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_without_output_select_list", True)
table_obj = db_obj.create_table("test_without_output_select_list", {
db_obj.drop_table("test_with_valid_select_list_output", ConflictType.Ignore)
table_obj = db_obj.create_table("test_with_valid_select_list_output", {
"c1": "int", "c2": "float"}, ConflictType.Error)
table_obj.insert([{"c1": 1, "c2": 2.0},
{"c1": 10, "c2": 2.0},
Expand All @@ -100,6 +104,7 @@ def test_with_valid_select_list_output(self, condition_list):
insert_res_df = table_obj.output(["c1", condition_list]).to_pl()
print(insert_res_df)

db_obj.drop_table("test_with_valid_select_list_output", ConflictType.Error)
# disconnect
res = infinity_obj.disconnect()
assert res.error_code == ErrorCode.OK
Expand All @@ -112,8 +117,8 @@ def test_with_invalid_select_list_output(self, condition_list):
# connect
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_without_output_select_list", True)
table_obj = db_obj.create_table("test_without_output_select_list", {
db_obj.drop_table("test_with_invalid_select_list_output", ConflictType.Ignore)
table_obj = db_obj.create_table("test_with_invalid_select_list_output", {
"c1": "int", "c2": "float"}, ConflictType.Error)
table_obj.insert([{"c1": 1, "c2": 2.0},
{"c1": 10, "c2": 2.0},
Expand All @@ -125,6 +130,7 @@ def test_with_invalid_select_list_output(self, condition_list):
insert_res_df = table_obj.output(["c1", condition_list]).to_pl()
print(insert_res_df)

db_obj.drop_table("test_with_invalid_select_list_output", ConflictType.Error)
# disconnect
res = infinity_obj.disconnect()
assert res.error_code == ErrorCode.OK
Expand All @@ -142,8 +148,8 @@ def test_output_with_valid_filter_function(self, filter_list):
# connect
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_output_filter_function", True)
table_obj = db_obj.create_table("test_output_filter_function", {
db_obj.drop_table("test_output_with_valid_filter_function", ConflictType.Ignore)
table_obj = db_obj.create_table("test_output_with_valid_filter_function", {
"c1": "int", "c2": "float"}, ConflictType.Error)
table_obj.insert([{"c1": 1, "c2": 2.0},
{"c1": 10, "c2": 2.0},
Expand All @@ -154,6 +160,7 @@ def test_output_with_valid_filter_function(self, filter_list):
insert_res_df = InfinityThriftQueryBuilder(table_obj).output(["*"]).filter(filter_list).to_pl()
print(str(insert_res_df))

db_obj.drop_table("test_output_with_valid_filter_function", ConflictType.Error)
# disconnect
res = infinity_obj.disconnect()
assert res.error_code == ErrorCode.OK
Expand All @@ -172,8 +179,8 @@ def test_output_with_invalid_filter_function(self, filter_list):
# connect
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
db_obj = infinity_obj.get_database("default")
db_obj.drop_table("test_output_filter_function", True)
table_obj = db_obj.create_table("test_output_filter_function", {
db_obj.drop_table("test_output_with_invalid_filter_function", ConflictType.Ignore)
table_obj = db_obj.create_table("test_output_with_invalid_filter_function", {
"c1": "int", "c2": "float"}, ConflictType.Error)
table_obj.insert([{"c1": 1, "c2": 2.0},
{"c1": 10, "c2": 2.0},
Expand All @@ -185,6 +192,7 @@ def test_output_with_invalid_filter_function(self, filter_list):
insert_res_df = InfinityThriftQueryBuilder(table_obj).output(["*"]).filter(filter_list).to_pl()
print(str(insert_res_df))

db_obj.drop_table("test_output_with_invalid_filter_function", ConflictType.Error)
# disconnect
res = infinity_obj.disconnect()
assert res.error_code == ErrorCode.OK
74 changes: 33 additions & 41 deletions python/test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,23 @@ def test_database(self):
"""
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)

infinity_obj.drop_database("my_database", ConflictType.Ignore)

# infinity
db = infinity_obj.create_database("my_database")
assert db

try:
db = infinity_obj.create_database("my_database!@#")
except Exception as e:
print(e)
with pytest.raises(Exception):
infinity_obj.create_database("my_database!@#")

try:
db = infinity_obj.create_database("my-database-dash")
except Exception as e:
print(e)
with pytest.raises(Exception):
infinity_obj.create_database("my-database-dash")

try:
db = infinity_obj.create_database("123_database")
except Exception as e:
print(e)
with pytest.raises(Exception):
infinity_obj.create_database("123_database")

try:
db = infinity_obj.create_database("")
except Exception as e:
print(e)
with pytest.raises(Exception):
infinity_obj.create_database("")

res = infinity_obj.list_databases()
assert res is not None
Expand Down Expand Up @@ -112,12 +106,8 @@ def test_create_database_invalid_name(self):

# 2. create db with invalid name
for db_name in common_values.invalid_name_array:
try:
# print('db name: ', db_name)
db = infinity_obj.create_database(db_name)
assert False
except Exception as e:
print(e)
with pytest.raises(Exception):
infinity_obj.create_database(db_name)

# 3. disconnect
res = infinity_obj.disconnect()
Expand Down Expand Up @@ -273,10 +263,8 @@ def test_drop_database_with_invalid_name(self):

# 2. drop db with invalid name
for db_name in common_values.invalid_name_array:
try:
db = infinity_obj.drop_database(db_name)
except Exception as e:
print(e)
with pytest.raises(Exception):
infinity_obj.drop_database(db_name)

# 3. disconnect
res = infinity_obj.disconnect()
Expand Down Expand Up @@ -305,7 +293,7 @@ def test_get_db(self):
print(e)

# 1. create db
db = infinity_obj.create_database("my_database")
db = infinity_obj.create_database("my_database", ConflictType.Error)

# 2. get db(using db)
db = infinity_obj.get_database("my_database")
Expand All @@ -316,16 +304,13 @@ def test_get_db(self):
print(db._db_name)

# 4.
res = infinity_obj.drop_database("my_database")
res = infinity_obj.drop_database("my_database", ConflictType.Error)
assert res.error_code == ErrorCode.OK

# 2. drop db with invalid name
for db_name in common_values.invalid_name_array:
try:
db = infinity_obj.drop_database(db_name)
raise Exception(f"Can drop db: {db_name}")
except Exception as e:
print(e)
with pytest.raises(Exception):
infinity_obj.drop_database(db_name)

# disconnect
res = infinity_obj.disconnect()
Expand All @@ -351,14 +336,14 @@ def test_db_ops_after_disconnection(self):

# 2. create db
try:
db = infinity_obj.create_database("my_database")
db = infinity_obj.create_database("my_database", ConflictType.Error)
assert False
except Exception as e:
print(f'Caught exception: {e}')

# 3. drop db
try:
db = infinity_obj.drop_database("my_database")
db = infinity_obj.drop_database("my_database", ConflictType.Error)
except Exception as e:
print(f'Caught exception: {e}')

Expand Down Expand Up @@ -456,12 +441,12 @@ def test_show_database(self):
# create db
infinity_obj = infinity.connect(common_values.TEST_REMOTE_HOST)
infinity_obj.drop_database("test_show_database", ConflictType.Ignore)
infinity_obj.create_database("test_show_database")
infinity_obj.create_database("test_show_database", ConflictType.Error)

res = infinity_obj.show_database("test_show_database")
assert res.database_name == "test_show_database"

infinity_obj.drop_database("test_show_database", ConflictType.Ignore)
infinity_obj.drop_database("test_show_database", ConflictType.Error)
# disconnect
res = infinity_obj.disconnect()
assert res.error_code == ErrorCode.OK
Expand Down Expand Up @@ -550,9 +535,10 @@ def test_drop_option(self, conflict_type):
def test_show_valid_table(self, get_infinity_db, table_name):
db_obj = get_infinity_db
db_obj.drop_table("test_show_table", ConflictType.Ignore)
db_obj.create_table("test_show_table", {"c1": "int", "c2": "vector,3,int"})
db_obj.create_table("test_show_table", {"c1": "int", "c2": "vector,3,int"}, ConflictType.Error)

res = db_obj.show_table(table_name)
db_obj.drop_table("test_show_table", ConflictType.Error)
print(res)

@pytest.mark.parametrize("table_name", [pytest.param("Invalid name"),
Expand All @@ -566,28 +552,33 @@ def test_show_valid_table(self, get_infinity_db, table_name):
def test_show_invalid_table(self, get_infinity_db, table_name):
db_obj = get_infinity_db
db_obj.drop_table("test_show_table", ConflictType.Ignore)
db_obj.create_table("test_show_table", {"c1": "int", "c2": "vector,3,int"})
db_obj.create_table("test_show_table", {"c1": "int", "c2": "vector,3,int"}, ConflictType.Error)

with pytest.raises(Exception):
db_obj.show_table(table_name)

db_obj.drop_table("test_show_table", ConflictType.Error)

@pytest.mark.parametrize("table_name", [pytest.param("not_exist_name")])
def test_show_not_exist_table(self, get_infinity_db, table_name):
db_obj = get_infinity_db
db_obj.drop_table("test_show_table", ConflictType.Ignore)
db_obj.create_table("test_show_table", {"c1": "int", "c2": "vector,3,int"})
db_obj.create_table("test_show_table", {"c1": "int", "c2": "vector,3,int"}, ConflictType.Error)

with pytest.raises(Exception, match=f"ERROR:3022, Table {table_name} doesn't exist"):
db_obj.show_table(table_name)

db_obj.drop_table("test_show_table", ConflictType.Error)

@pytest.mark.parametrize("column_name", ["test_show_table_columns"])
def test_show_table_columns_with_valid_name(self, get_infinity_db, column_name):
db_obj = get_infinity_db
db_obj.drop_table("test_show_table_columns", ConflictType.Ignore)

db_obj.create_table("test_show_table_columns", {"c1": "int", "c2": "vector,3,int"})
db_obj.create_table("test_show_table_columns", {"c1": "int", "c2": "vector,3,int"}, ConflictType.Error)

res = db_obj.show_columns(column_name)
db_obj.drop_table("test_show_table_columns", ConflictType.Error)
print(res)

@pytest.mark.parametrize("column_name", [pytest.param("Invalid name"),
Expand All @@ -607,4 +598,5 @@ def test_show_table_columns_with_invalid_name(self, get_infinity_db, column_name

with pytest.raises(Exception):
db_obj.show_columns(column_name)
db_obj.drop_table("test_show_table_columns", ConflictType.Error)

Loading
Loading