import pytest
import tempfile
import shutil
from pathlib import Path
import sys
import os
import numpy as np
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'apexbase', 'python'))
try:
from apexbase import ApexClient, FTS_AVAILABLE
except ImportError as e:
pytest.skip(f"ApexBase not available: {e}", allow_module_level=True)
class TestDeleteOperations:
def test_delete_single_record(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
test_data = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 35},
]
client.store(test_data)
assert client.count_rows() == 3
result = client.delete(2)
assert result is True
assert client.count_rows() == 2
alice = client.retrieve(1)
assert alice["name"] == "Alice"
bob = client.retrieve(2)
assert bob is None
charlie = client.retrieve(3)
assert charlie["name"] == "Charlie"
client.close()
def test_delete_nonexistent_record(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
result = client.delete(999)
assert result is False
assert client.count_rows() == 1
client.close()
def test_delete_batch_records(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
test_data = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 35},
{"name": "Diana", "age": 28},
{"name": "Eve", "age": 32},
]
client.store(test_data)
assert client.count_rows() == 5
result = client.delete([2, 4])
assert result is True
assert client.count_rows() == 3
alice = client.retrieve(1)
assert alice["name"] == "Alice"
bob = client.retrieve(2)
assert bob is None
charlie = client.retrieve(3)
assert charlie["name"] == "Charlie"
diana = client.retrieve(4)
assert diana is None
eve = client.retrieve(5)
assert eve["name"] == "Eve"
client.close()
def test_delete_batch_with_nonexistent_ids(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
test_data = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 35},
]
client.store(test_data)
try:
result = client.delete([1, 999, 3, 888])
bob = client.retrieve(2)
assert bob is not None
assert bob["name"] == "Bob"
except Exception as e:
print(f"Delete batch mixed: {e}")
client.close()
def test_delete_all_nonexistent_ids(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
result = client.delete([999, 888, 777])
assert result is False assert client.count_rows() == 1
client.close()
def test_delete_empty_list(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
result = client.delete([])
assert result is False or result is True assert client.count_rows() == 1
client.close()
def test_delete_from_empty_database(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
result = client.delete(1)
assert result is False
result = client.delete([1, 2, 3])
assert result is False
client.close()
def test_delete_with_various_data_types(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
test_data = [
{
"string_field": "test_string",
"int_field": 42,
"float_field": 3.14159,
"bool_field": True,
},
{
"string_field": "another_string",
"int_field": -100,
"float_field": 0.0,
"bool_field": False,
},
]
client.store(test_data)
result = client.delete(1)
remaining = client.retrieve(2)
assert remaining is not None
assert remaining["string_field"] == "another_string"
assert remaining["int_field"] == -100
client.close()
class TestReplaceOperations:
def test_replace_single_record(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
test_data = [
{"name": "Alice", "age": 25, "city": "NYC"},
{"name": "Bob", "age": 30, "city": "LA"},
]
client.store(test_data)
new_data = {"name": "Alice Updated", "age": 26, "city": "Boston", "status": "active"}
result = client.replace(1, new_data)
assert result is True
alice = client.retrieve(1)
assert alice["name"] == "Alice Updated"
assert alice["age"] == 26
assert alice["city"] == "Boston"
assert alice["status"] == "active"
bob = client.retrieve(2)
assert bob["name"] == "Bob"
assert bob["age"] == 30
assert bob["city"] == "LA"
client.close()
def test_replace_nonexistent_record(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
new_data = {"name": "New Record", "age": 30}
result = client.replace(999, new_data)
assert result is False
assert client.count_rows() == 1
alice = client.retrieve(1)
assert alice["name"] == "Alice"
assert alice["age"] == 25
client.close()
def test_replace_with_different_schema(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25, "city": "NYC"})
new_data = {
"title": "Ms",
"first_name": "Alice",
"department": "Engineering",
}
try:
result = client.replace(1, new_data)
updated = client.retrieve(1)
assert updated is not None
except Exception as e:
print(f"Replace different schema: {e}")
client.close()
def test_replace_with_partial_data(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({
"name": "Alice",
"age": 25,
"city": "NYC",
})
new_data = {"name": "Alice Updated", "age": 26}
try:
result = client.replace(1, new_data)
except Exception as e:
print(f"Replace partial: {e}")
updated = client.retrieve(1)
assert updated is not None
client.close()
def test_replace_with_empty_data(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
try:
result = client.replace(1, {})
updated = client.retrieve(1)
except Exception as e:
print(f"Replace empty: {e}")
client.close()
def test_replace_with_various_data_types(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
new_data = {
"string_field": "test_string",
"int_field": 42,
"float_field": 3.14159,
"bool_field": True,
}
try:
result = client.replace(1, new_data)
updated = client.retrieve(1)
assert updated is not None
assert updated["string_field"] == "test_string"
assert updated["int_field"] == 42
except Exception as e:
print(f"Replace various types: {e}")
client.close()
class TestBatchReplaceOperations:
def test_batch_replace_basic(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
test_data = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 35},
]
client.store(test_data)
replace_data = {
1: {"name": "Alice Updated", "age": 26},
3: {"name": "Charlie Updated", "age": 36},
}
success_ids = client.batch_replace(replace_data)
assert len(success_ids) == 2
assert 1 in success_ids
assert 3 in success_ids
alice = client.retrieve(1)
assert alice["name"] == "Alice Updated"
assert alice["age"] == 26
bob = client.retrieve(2)
assert bob["name"] == "Bob" assert bob["age"] == 30
charlie = client.retrieve(3)
assert charlie["name"] == "Charlie Updated"
assert charlie["age"] == 36
client.close()
def test_batch_replace_with_nonexistent_ids(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
test_data = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
]
client.store(test_data)
replace_data = {
1: {"name": "Alice Updated", "age": 26},
999: {"name": "Nonexistent", "age": 99},
2: {"name": "Bob Updated", "age": 31},
}
success_ids = client.batch_replace(replace_data)
assert 1 in success_ids
assert 2 in success_ids
assert 999 not in success_ids
alice = client.retrieve(1)
assert alice["name"] == "Alice Updated"
bob = client.retrieve(2)
assert bob["name"] == "Bob Updated"
client.close()
def test_batch_replace_empty_dict(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
success_ids = client.batch_replace({})
assert len(success_ids) == 0
assert client.count_rows() == 1
client.close()
def test_batch_replace_all_nonexistent(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
replace_data = {
999: {"name": "Nonexistent 1", "age": 99},
888: {"name": "Nonexistent 2", "age": 88},
}
success_ids = client.batch_replace(replace_data)
assert len(success_ids) == 0
assert client.count_rows() == 1
client.close()
class TestModificationWithFTS:
def test_delete_with_fts_enabled(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.init_fts(index_fields=['content'])
documents = [
{"content": "Python programming tutorial"},
{"content": "JavaScript development guide"},
{"content": "Database management system"},
]
client.store(documents)
results = client.search_text("python")
assert len(results) > 0
result = client.delete(1) assert result is True
results = client.search_text("python")
assert len(results) == 0
results = client.search_text("javascript")
assert len(results) > 0
client.close()
def test_replace_with_fts_enabled(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.init_fts(index_fields=['content'])
client.store({"content": "Python programming tutorial"})
results = client.search_text("python")
initial_found = len(results) > 0
new_data = {"content": "JavaScript development guide"}
try:
result = client.replace(1, new_data)
updated = client.retrieve(1)
assert updated is not None
except Exception as e:
print(f"Replace with FTS: {e}")
client.close()
def test_batch_operations_with_fts(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.init_fts(index_fields=['content'])
documents = [
{"content": "Python programming"},
{"content": "JavaScript development"},
{"content": "Database management"},
]
client.store(documents)
result = client.delete([1, 3]) assert result is True
results = client.search_text("python")
assert len(results) == 0
results = client.search_text("database")
assert len(results) == 0
results = client.search_text("javascript")
assert len(results) > 0
client.close()
class TestModificationEdgeCases:
def test_modifications_on_closed_client(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.close()
with pytest.raises(RuntimeError, match="connection has been closed"):
client.delete(1)
with pytest.raises(RuntimeError, match="connection has been closed"):
client.delete([1, 2])
with pytest.raises(RuntimeError, match="connection has been closed"):
client.replace(1, {"test": "data"})
with pytest.raises(RuntimeError, match="connection has been closed"):
client.batch_replace({1: {"test": "data"}})
def test_delete_invalid_id_types(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
try:
client.delete(-1)
except (TypeError, ValueError, OverflowError):
pass
result = client.retrieve(1)
assert result is not None
client.close()
def test_replace_invalid_id_types(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "age": 25})
try:
client.replace(-1, {"test": "data"})
except (TypeError, ValueError, OverflowError):
pass
result = client.retrieve(1)
assert result is not None
assert result["name"] == "Alice"
client.close()
def test_modifications_with_unicode_data(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
unicode_data = {
"chinese": "你好世界",
"emoji": "🌍🚀",
}
client.store(unicode_data)
new_unicode_data = {
"russian": "Привет мир",
"french": "Bonjour le monde",
}
try:
result = client.replace(1, new_unicode_data)
updated = client.retrieve(1)
assert updated is not None
except Exception as e:
print(f"Unicode replace: {e}")
client.close()
def test_modifications_with_large_data(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
large_string = "x" * 100000 large_data = {
"large_text": large_string,
"normal_field": "test",
}
client.store(large_data)
new_large_string = "y" * 100000
new_large_data = {
"large_text": new_large_string,
"another_field": "updated",
}
try:
result = client.replace(1, new_large_data)
updated = client.retrieve(1)
assert updated is not None
except Exception as e:
print(f"Large data replace: {e}")
client.close()
def test_modifications_consistency(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
initial_data = [
{"id": 1, "name": "Alice", "age": 25},
{"id": 2, "name": "Bob", "age": 30},
{"id": 3, "name": "Charlie", "age": 35},
]
client.store(initial_data)
client.delete(2) client.replace(1, {"id": 1, "name": "Alice Updated", "age": 26})
client.store({"id": 4, "name": "Diana", "age": 28})
all_records = client.retrieve_all()
assert len(all_records) == 3
alice = client.retrieve(1)
assert alice["name"] == "Alice Updated"
assert alice["age"] == 26
bob = client.retrieve(2)
assert bob is None
charlie = client.retrieve(3)
assert charlie["name"] == "Charlie"
diana = client.retrieve(4)
assert diana["name"] == "Diana"
client.close()
def test_modifications_performance(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
data = [{"id": i, "value": f"item_{i}"} for i in range(100)]
client.store(data)
import time
start_time = time.time()
try:
result = client.delete([1, 10, 20])
except Exception as e:
print(f"Delete perf: {e}")
delete_time = time.time() - start_time
assert delete_time < 5.0
client.close()
class TestModificationWithDifferentTables:
def test_modifications_table_isolation(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.store({"name": "Alice", "table": "default"})
client.create_table("users")
client.store({"name": "Bob", "table": "users"})
client.use_table("default")
try:
client.replace(1, {"name": "Alice Updated", "table": "default"})
except Exception as e:
print(f"Replace isolation: {e}")
client.use_table("default")
alice = client.retrieve(1)
assert alice is not None
client.use_table("users")
bob = client.retrieve(1)
assert bob is not None
assert bob["name"] == "Bob"
client.close()
def test_fts_modifications_table_specific(self):
with tempfile.TemporaryDirectory() as temp_dir:
client = ApexClient(dirpath=temp_dir)
client.create_table("default")
client.create_table("articles")
client.use_table("articles")
client.init_fts(index_fields=['content'])
client.store({"content": "Python programming article"})
client.create_table("comments")
client.use_table("comments")
client.init_fts(index_fields=['text'])
client.store({"text": "Python is great comment"})
client.use_table("articles")
article = client.retrieve(1)
assert article is not None
client.use_table("comments")
comment = client.retrieve(1)
assert comment is not None
client.close()
if __name__ == "__main__":
pytest.main([__file__, "-v"])