-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tidesdb.py
More file actions
971 lines (748 loc) · 31.1 KB
/
test_tidesdb.py
File metadata and controls
971 lines (748 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
"""
Tests for TidesDB Python bindings.
These tests require the TidesDB shared library to be installed.
"""
import os
import shutil
import tempfile
import time
import pytest
import tidesdb
@pytest.fixture
def temp_db_path():
"""Create a temporary directory for test database."""
path = tempfile.mkdtemp(prefix="tidesdb_test_")
yield path
shutil.rmtree(path, ignore_errors=True)
@pytest.fixture
def db(temp_db_path):
"""Create a test database."""
database = tidesdb.TidesDB.open(temp_db_path)
yield database
database.close()
@pytest.fixture
def cf(db):
"""Create a test column family."""
db.create_column_family("test_cf")
cf = db.get_column_family("test_cf")
yield cf
try:
db.drop_column_family("test_cf")
except tidesdb.TidesDBError:
pass
class TestOpenClose:
"""Tests for database open/close operations."""
def test_open_close(self, temp_db_path):
"""Test basic open and close."""
db = tidesdb.TidesDB.open(temp_db_path)
assert db is not None
db.close()
def test_open_with_config(self, temp_db_path):
"""Test open with custom configuration."""
config = tidesdb.Config(
db_path=temp_db_path,
num_flush_threads=4,
num_compaction_threads=4,
log_level=tidesdb.LogLevel.LOG_WARN,
block_cache_size=32 * 1024 * 1024,
max_open_sstables=128,
)
db = tidesdb.TidesDB(config)
assert db is not None
db.close()
def test_context_manager(self, temp_db_path):
"""Test database as context manager."""
with tidesdb.TidesDB.open(temp_db_path) as db:
assert db is not None
class TestColumnFamilies:
"""Tests for column family operations."""
def test_create_drop_column_family(self, db):
"""Test creating and dropping a column family."""
db.create_column_family("test_cf")
cf = db.get_column_family("test_cf")
assert cf is not None
assert cf.name == "test_cf"
db.drop_column_family("test_cf")
def test_create_with_config(self, db):
"""Test creating column family with custom config."""
config = tidesdb.default_column_family_config()
config.write_buffer_size = 32 * 1024 * 1024
config.compression_algorithm = tidesdb.CompressionAlgorithm.LZ4_COMPRESSION
config.enable_bloom_filter = True
config.bloom_fpr = 0.01
db.create_column_family("custom_cf", config)
cf = db.get_column_family("custom_cf")
assert cf is not None
stats = cf.get_stats()
assert stats.config is not None
assert stats.config.enable_bloom_filter is True
db.drop_column_family("custom_cf")
def test_create_with_btree_config(self, db):
"""Test creating column family with B+tree format enabled."""
config = tidesdb.default_column_family_config()
config.use_btree = True
db.create_column_family("btree_cf", config)
cf = db.get_column_family("btree_cf")
assert cf is not None
stats = cf.get_stats()
assert stats.config is not None
assert stats.config.use_btree is True
assert stats.use_btree is True
db.drop_column_family("btree_cf")
def test_default_config_use_btree(self, db):
"""Test that default config has use_btree=False."""
config = tidesdb.default_column_family_config()
assert config.use_btree is False
def test_list_column_families(self, db):
"""Test listing column families."""
db.create_column_family("cf1")
db.create_column_family("cf2")
names = db.list_column_families()
assert "cf1" in names
assert "cf2" in names
db.drop_column_family("cf1")
db.drop_column_family("cf2")
def test_get_nonexistent_column_family(self, db):
"""Test getting a non-existent column family."""
with pytest.raises(tidesdb.TidesDBError):
db.get_column_family("nonexistent")
class TestTransactions:
"""Tests for transaction operations."""
def test_put_get(self, db, cf):
"""Test basic put and get."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
with db.begin_txn() as txn:
value = txn.get(cf, b"key1")
assert value == b"value1"
def test_delete(self, db, cf):
"""Test delete operation."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
with db.begin_txn() as txn:
txn.delete(cf, b"key1")
txn.commit()
with db.begin_txn() as txn:
with pytest.raises(tidesdb.TidesDBError):
txn.get(cf, b"key1")
def test_rollback(self, db, cf):
"""Test transaction rollback."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.rollback()
with db.begin_txn() as txn:
with pytest.raises(tidesdb.TidesDBError):
txn.get(cf, b"key1")
def test_multiple_operations(self, db, cf):
"""Test multiple operations in one transaction."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.put(cf, b"key2", b"value2")
txn.put(cf, b"key3", b"value3")
txn.delete(cf, b"key2")
txn.commit()
with db.begin_txn() as txn:
assert txn.get(cf, b"key1") == b"value1"
assert txn.get(cf, b"key3") == b"value3"
with pytest.raises(tidesdb.TidesDBError):
txn.get(cf, b"key2")
def test_isolation_level(self, db, cf):
"""Test transaction with specific isolation level."""
txn = db.begin_txn_with_isolation(tidesdb.IsolationLevel.SERIALIZABLE)
txn.put(cf, b"key1", b"value1")
txn.commit()
txn.close()
class TestSavepoints:
"""Tests for savepoint operations."""
def test_savepoint_rollback(self, db, cf):
"""Test savepoint and rollback to savepoint."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.savepoint("sp1")
txn.put(cf, b"key2", b"value2")
txn.rollback_to_savepoint("sp1")
txn.commit()
with db.begin_txn() as txn:
assert txn.get(cf, b"key1") == b"value1"
with pytest.raises(tidesdb.TidesDBError):
txn.get(cf, b"key2")
def test_release_savepoint(self, db, cf):
"""Test releasing a savepoint."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.savepoint("sp1")
txn.put(cf, b"key2", b"value2")
txn.release_savepoint("sp1")
txn.commit()
with db.begin_txn() as txn:
assert txn.get(cf, b"key1") == b"value1"
assert txn.get(cf, b"key2") == b"value2"
class TestIterators:
"""Tests for iterator operations."""
def test_forward_iteration(self, db, cf):
"""Test forward iteration."""
with db.begin_txn() as txn:
txn.put(cf, b"a", b"1")
txn.put(cf, b"b", b"2")
txn.put(cf, b"c", b"3")
txn.commit()
with db.begin_txn() as txn:
with txn.new_iterator(cf) as it:
it.seek_to_first()
items = list(it)
assert len(items) == 3
assert items[0] == (b"a", b"1")
assert items[1] == (b"b", b"2")
assert items[2] == (b"c", b"3")
def test_backward_iteration(self, db, cf):
"""Test backward iteration."""
with db.begin_txn() as txn:
txn.put(cf, b"a", b"1")
txn.put(cf, b"b", b"2")
txn.put(cf, b"c", b"3")
txn.commit()
with db.begin_txn() as txn:
with txn.new_iterator(cf) as it:
it.seek_to_last()
items = []
while it.valid():
items.append((it.key(), it.value()))
it.prev()
assert len(items) == 3
assert items[0] == (b"c", b"3")
assert items[1] == (b"b", b"2")
assert items[2] == (b"a", b"1")
def test_seek(self, db, cf):
"""Test seek operations."""
with db.begin_txn() as txn:
txn.put(cf, b"a", b"1")
txn.put(cf, b"c", b"3")
txn.put(cf, b"e", b"5")
txn.commit()
with db.begin_txn() as txn:
with txn.new_iterator(cf) as it:
it.seek(b"b")
assert it.valid()
assert it.key() == b"c"
it.seek_for_prev(b"d")
assert it.valid()
assert it.key() == b"c"
class TestTTL:
"""Tests for TTL functionality."""
def test_ttl_expiration(self, db, cf):
"""Test that keys with expired TTL are eventually not returned."""
expired_ttl = int(time.time()) - 1
with db.begin_txn() as txn:
txn.put(cf, b"expired_key", b"value", ttl=expired_ttl)
txn.commit()
cf.flush_memtable()
time.sleep(2)
with db.begin_txn() as txn:
try:
txn.get(cf, b"expired_key")
except tidesdb.TidesDBError:
pass
def test_no_ttl(self, db, cf):
"""Test that keys without TTL persist."""
with db.begin_txn() as txn:
txn.put(cf, b"permanent_key", b"value", ttl=-1)
txn.commit()
with db.begin_txn() as txn:
value = txn.get(cf, b"permanent_key")
assert value == b"value"
class TestCloneColumnFamily:
"""Tests for column family clone operations."""
def test_clone_column_family(self, db, cf):
"""Test cloning a column family with data."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.put(cf, b"key2", b"value2")
txn.commit()
db.clone_column_family("test_cf", "cloned_cf")
cloned = db.get_column_family("cloned_cf")
assert cloned is not None
assert cloned.name == "cloned_cf"
with db.begin_txn() as txn:
assert txn.get(cloned, b"key1") == b"value1"
assert txn.get(cloned, b"key2") == b"value2"
db.drop_column_family("cloned_cf")
def test_clone_independence(self, db, cf):
"""Test that clone is independent from source."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"original")
txn.commit()
db.clone_column_family("test_cf", "cloned_cf")
cloned = db.get_column_family("cloned_cf")
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"modified")
txn.commit()
with db.begin_txn() as txn:
assert txn.get(cloned, b"key1") == b"original"
assert txn.get(cf, b"key1") == b"modified"
db.drop_column_family("cloned_cf")
def test_clone_nonexistent_source(self, db):
"""Test cloning a non-existent column family raises error."""
with pytest.raises(tidesdb.TidesDBError):
db.clone_column_family("nonexistent", "cloned_cf")
def test_clone_to_existing_name(self, db, cf):
"""Test cloning to an already existing name raises error."""
db.create_column_family("existing_cf")
with pytest.raises(tidesdb.TidesDBError):
db.clone_column_family("test_cf", "existing_cf")
db.drop_column_family("existing_cf")
def test_clone_listed(self, db, cf):
"""Test that cloned column family appears in list."""
db.clone_column_family("test_cf", "cloned_cf")
names = db.list_column_families()
assert "test_cf" in names
assert "cloned_cf" in names
db.drop_column_family("cloned_cf")
class TestTransactionReset:
"""Tests for transaction reset operations."""
def test_reset_after_commit(self, db, cf):
"""Test resetting a transaction after commit."""
txn = db.begin_txn()
txn.put(cf, b"key1", b"value1")
txn.commit()
txn.reset(tidesdb.IsolationLevel.READ_COMMITTED)
txn.put(cf, b"key2", b"value2")
txn.commit()
txn.close()
with db.begin_txn() as txn:
assert txn.get(cf, b"key1") == b"value1"
assert txn.get(cf, b"key2") == b"value2"
def test_reset_after_rollback(self, db, cf):
"""Test resetting a transaction after rollback."""
txn = db.begin_txn()
txn.put(cf, b"key1", b"value1")
txn.rollback()
txn.reset(tidesdb.IsolationLevel.READ_COMMITTED)
txn.put(cf, b"key2", b"value2")
txn.commit()
txn.close()
with db.begin_txn() as txn:
with pytest.raises(tidesdb.TidesDBError):
txn.get(cf, b"key1")
assert txn.get(cf, b"key2") == b"value2"
def test_reset_with_different_isolation(self, db, cf):
"""Test resetting with a different isolation level."""
txn = db.begin_txn()
txn.put(cf, b"key1", b"value1")
txn.commit()
txn.reset(tidesdb.IsolationLevel.SERIALIZABLE)
txn.put(cf, b"key2", b"value2")
txn.commit()
txn.close()
with db.begin_txn() as txn:
assert txn.get(cf, b"key1") == b"value1"
assert txn.get(cf, b"key2") == b"value2"
def test_reset_reuse_loop(self, db, cf):
"""Test resetting in a loop for batch processing."""
txn = db.begin_txn()
for i in range(5):
txn.put(cf, f"batch_key_{i}".encode(), f"batch_value_{i}".encode())
txn.commit()
if i < 4:
txn.reset(tidesdb.IsolationLevel.READ_COMMITTED)
txn.close()
with db.begin_txn() as txn:
for i in range(5):
value = txn.get(cf, f"batch_key_{i}".encode())
assert value == f"batch_value_{i}".encode()
def test_reset_closed_transaction_raises(self, db, cf):
"""Test that resetting a closed transaction raises error."""
txn = db.begin_txn()
txn.put(cf, b"key1", b"value1")
txn.commit()
txn.close()
with pytest.raises(tidesdb.TidesDBError):
txn.reset(tidesdb.IsolationLevel.READ_COMMITTED)
class TestStats:
"""Tests for statistics operations."""
def test_column_family_stats(self, db, cf):
"""Test getting column family statistics."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
stats = cf.get_stats()
assert stats.num_levels >= 0
assert stats.memtable_size >= 0
def test_column_family_stats_btree_fields(self, db, cf):
"""Test that B+tree stats fields are present."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
stats = cf.get_stats()
# B+tree stats should be present (even if 0 for non-btree CF)
assert isinstance(stats.use_btree, bool)
assert isinstance(stats.btree_total_nodes, int)
assert isinstance(stats.btree_max_height, int)
assert isinstance(stats.btree_avg_height, float)
assert stats.btree_total_nodes >= 0
assert stats.btree_max_height >= 0
assert stats.btree_avg_height >= 0.0
def test_cache_stats(self, db):
"""Test getting cache statistics."""
stats = db.get_cache_stats()
assert isinstance(stats.enabled, bool)
assert stats.hits >= 0
assert stats.misses >= 0
class TestMaintenance:
"""Tests for maintenance operations."""
def test_flush_memtable(self, db, cf):
"""Test manual memtable flush."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
cf.flush_memtable()
time.sleep(0.5)
def test_compact(self, db, cf):
"""Test manual compaction."""
with db.begin_txn() as txn:
for i in range(100):
txn.put(cf, f"key{i}".encode(), f"value{i}".encode())
txn.commit()
cf.flush_memtable()
time.sleep(0.5)
try:
cf.compact()
except tidesdb.TidesDBError:
pass
time.sleep(0.5)
class TestCheckpoint:
"""Tests for checkpoint operations."""
def test_checkpoint_creates_snapshot(self, db, cf, temp_db_path):
"""Test that checkpoint creates a usable snapshot."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.put(cf, b"key2", b"value2")
txn.commit()
checkpoint_dir = temp_db_path + "_checkpoint"
try:
db.checkpoint(checkpoint_dir)
assert os.path.isdir(checkpoint_dir)
with tidesdb.TidesDB.open(checkpoint_dir) as checkpoint_db:
cp_cf = checkpoint_db.get_column_family("test_cf")
with checkpoint_db.begin_txn() as txn:
assert txn.get(cp_cf, b"key1") == b"value1"
assert txn.get(cp_cf, b"key2") == b"value2"
finally:
shutil.rmtree(checkpoint_dir, ignore_errors=True)
def test_checkpoint_existing_dir_raises(self, db, cf, temp_db_path):
"""Test that checkpoint to a non-empty directory raises error."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
checkpoint_dir = temp_db_path + "_checkpoint"
try:
db.checkpoint(checkpoint_dir)
with pytest.raises(tidesdb.TidesDBError):
db.checkpoint(checkpoint_dir)
finally:
shutil.rmtree(checkpoint_dir, ignore_errors=True)
def test_checkpoint_independence(self, db, cf, temp_db_path):
"""Test that checkpoint is independent from the live database."""
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"original")
txn.commit()
checkpoint_dir = temp_db_path + "_checkpoint"
try:
db.checkpoint(checkpoint_dir)
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"modified")
txn.commit()
with tidesdb.TidesDB.open(checkpoint_dir) as checkpoint_db:
cp_cf = checkpoint_db.get_column_family("test_cf")
with checkpoint_db.begin_txn() as txn:
assert txn.get(cp_cf, b"key1") == b"original"
finally:
shutil.rmtree(checkpoint_dir, ignore_errors=True)
class TestRangeCost:
"""Tests for range cost estimation."""
def test_range_cost_returns_float(self, db, cf):
"""Test that range_cost returns a float value."""
with db.begin_txn() as txn:
txn.put(cf, b"key_a", b"value_a")
txn.put(cf, b"key_z", b"value_z")
txn.commit()
cost = cf.range_cost(b"key_a", b"key_z")
assert isinstance(cost, float)
assert cost >= 0.0
def test_range_cost_empty_cf(self, db, cf):
"""Test range_cost on an empty column family."""
cost = cf.range_cost(b"a", b"z")
assert isinstance(cost, float)
assert cost >= 0.0
def test_range_cost_key_order_irrelevant(self, db, cf):
"""Test that key order does not matter."""
with db.begin_txn() as txn:
txn.put(cf, b"aaa", b"1")
txn.put(cf, b"zzz", b"2")
txn.commit()
cost_ab = cf.range_cost(b"aaa", b"zzz")
cost_ba = cf.range_cost(b"zzz", b"aaa")
assert cost_ab == cost_ba
def test_range_cost_narrow_vs_wide(self, db, cf):
"""Test that a wider range costs at least as much as a narrow one."""
with db.begin_txn() as txn:
for i in range(50):
txn.put(cf, f"key:{i:04d}".encode(), f"val:{i}".encode())
txn.commit()
narrow = cf.range_cost(b"key:0010", b"key:0015")
wide = cf.range_cost(b"key:0000", b"key:0049")
# Wide range should generally cost >= narrow range
assert wide >= narrow
def test_range_cost_comparison(self, db, cf):
"""Test comparing costs of different ranges."""
with db.begin_txn() as txn:
for i in range(100):
txn.put(cf, f"user:{i:04d}".encode(), f"data:{i}".encode())
txn.commit()
cost_a = cf.range_cost(b"user:0000", b"user:0009")
cost_b = cf.range_cost(b"user:0000", b"user:0099")
# Both should be valid floats
assert isinstance(cost_a, float)
assert isinstance(cost_b, float)
class TestLoadConfigFromIni:
"""Tests for loading column family config from INI files."""
def test_save_and_load_roundtrip(self, temp_db_path):
"""Test that saving and loading config produces equivalent results."""
original = tidesdb.default_column_family_config()
original.write_buffer_size = 32 * 1024 * 1024
original.compression_algorithm = tidesdb.CompressionAlgorithm.ZSTD_COMPRESSION
original.enable_bloom_filter = True
original.bloom_fpr = 0.001
original.sync_mode = tidesdb.SyncMode.SYNC_FULL
original.min_levels = 7
original.use_btree = True
ini_path = os.path.join(temp_db_path, "test_config.ini")
tidesdb.save_config_to_ini(ini_path, "my_cf", original)
loaded = tidesdb.load_config_from_ini(ini_path, "my_cf")
assert loaded.write_buffer_size == original.write_buffer_size
assert loaded.compression_algorithm == original.compression_algorithm
assert loaded.enable_bloom_filter == original.enable_bloom_filter
assert abs(loaded.bloom_fpr - original.bloom_fpr) < 1e-9
assert loaded.sync_mode == original.sync_mode
assert loaded.min_levels == original.min_levels
assert loaded.use_btree == original.use_btree
def test_load_nonexistent_file_raises(self, temp_db_path):
"""Test that loading from a non-existent file raises error."""
ini_path = os.path.join(temp_db_path, "nonexistent.ini")
with pytest.raises(tidesdb.TidesDBError):
tidesdb.load_config_from_ini(ini_path, "my_cf")
def test_load_preserves_all_fields(self, temp_db_path):
"""Test that all configuration fields survive a save/load roundtrip."""
original = tidesdb.default_column_family_config()
original.level_size_ratio = 8
original.dividing_level_offset = 3
original.klog_value_threshold = 1024
original.index_sample_ratio = 2
original.block_index_prefix_len = 32
original.sync_interval_us = 500000
original.skip_list_max_level = 16
original.skip_list_probability = 0.5
original.min_disk_space = 200 * 1024 * 1024
original.l1_file_count_trigger = 8
original.l0_queue_stall_threshold = 30
ini_path = os.path.join(temp_db_path, "full_config.ini")
tidesdb.save_config_to_ini(ini_path, "full_cf", original)
loaded = tidesdb.load_config_from_ini(ini_path, "full_cf")
assert loaded.level_size_ratio == original.level_size_ratio
assert loaded.dividing_level_offset == original.dividing_level_offset
assert loaded.klog_value_threshold == original.klog_value_threshold
assert loaded.index_sample_ratio == original.index_sample_ratio
assert loaded.block_index_prefix_len == original.block_index_prefix_len
assert loaded.sync_interval_us == original.sync_interval_us
assert loaded.skip_list_max_level == original.skip_list_max_level
assert loaded.min_disk_space == original.min_disk_space
assert loaded.l1_file_count_trigger == original.l1_file_count_trigger
assert loaded.l0_queue_stall_threshold == original.l0_queue_stall_threshold
class TestCommitHook:
"""Tests for commit hook (change data capture) API."""
def test_hook_fires_on_commit(self, db, cf):
"""Test that the commit hook fires when a transaction commits."""
captured = []
def hook(ops, commit_seq):
captured.append({"ops": ops, "seq": commit_seq})
return 0
cf.set_commit_hook(hook)
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
assert len(captured) == 1
assert len(captured[0]["ops"]) == 1
assert captured[0]["ops"][0].key == b"key1"
assert captured[0]["ops"][0].value == b"value1"
assert captured[0]["ops"][0].is_delete is False
assert captured[0]["seq"] > 0
cf.clear_commit_hook()
def test_hook_captures_deletes(self, db, cf):
"""Test that the hook correctly reports delete operations."""
captured = []
with db.begin_txn() as txn:
txn.put(cf, b"del_key", b"del_val")
txn.commit()
def hook(ops, commit_seq):
captured.append(ops)
return 0
cf.set_commit_hook(hook)
with db.begin_txn() as txn:
txn.delete(cf, b"del_key")
txn.commit()
assert len(captured) == 1
delete_ops = [op for op in captured[0] if op.is_delete]
assert len(delete_ops) >= 1
assert delete_ops[0].key == b"del_key"
assert delete_ops[0].value is None
cf.clear_commit_hook()
def test_hook_multi_op_batch(self, db, cf):
"""Test that the hook receives all operations in a batch."""
captured = []
def hook(ops, commit_seq):
captured.append(ops)
return 0
cf.set_commit_hook(hook)
with db.begin_txn() as txn:
txn.put(cf, b"batch1", b"v1")
txn.put(cf, b"batch2", b"v2")
txn.put(cf, b"batch3", b"v3")
txn.commit()
assert len(captured) == 1
assert len(captured[0]) == 3
keys = {op.key for op in captured[0]}
assert keys == {b"batch1", b"batch2", b"batch3"}
cf.clear_commit_hook()
def test_hook_commit_seq_increases(self, db, cf):
"""Test that commit_seq is monotonically increasing."""
seqs = []
def hook(ops, commit_seq):
seqs.append(commit_seq)
return 0
cf.set_commit_hook(hook)
for i in range(3):
with db.begin_txn() as txn:
txn.put(cf, f"seq_key_{i}".encode(), f"seq_val_{i}".encode())
txn.commit()
assert len(seqs) == 3
assert seqs[0] < seqs[1] < seqs[2]
cf.clear_commit_hook()
def test_clear_hook_stops_firing(self, db, cf):
"""Test that clearing the hook stops callbacks."""
captured = []
def hook(ops, commit_seq):
captured.append(ops)
return 0
cf.set_commit_hook(hook)
with db.begin_txn() as txn:
txn.put(cf, b"before_clear", b"v")
txn.commit()
assert len(captured) == 1
cf.clear_commit_hook()
with db.begin_txn() as txn:
txn.put(cf, b"after_clear", b"v")
txn.commit()
# No new captures after clearing
assert len(captured) == 1
def test_hook_failure_does_not_rollback(self, db, cf):
"""Test that a hook returning non-zero does not affect the commit."""
def failing_hook(ops, commit_seq):
return -1 # Simulate failure
cf.set_commit_hook(failing_hook)
with db.begin_txn() as txn:
txn.put(cf, b"survive", b"value")
txn.commit()
cf.clear_commit_hook()
# Data should still be committed despite hook failure
with db.begin_txn() as txn:
assert txn.get(cf, b"survive") == b"value"
def test_hook_with_ttl(self, db, cf):
"""Test that the hook reports TTL values."""
captured = []
def hook(ops, commit_seq):
captured.append(ops)
return 0
cf.set_commit_hook(hook)
ttl_val = int(time.time()) + 3600 # 1 hour from now
with db.begin_txn() as txn:
txn.put(cf, b"ttl_key", b"ttl_val", ttl=ttl_val)
txn.commit()
assert len(captured) == 1
assert captured[0][0].ttl == ttl_val
cf.clear_commit_hook()
def test_hook_exception_handled(self, db, cf):
"""Test that Python exceptions in the hook don't crash the process."""
def crashing_hook(ops, commit_seq):
raise RuntimeError("hook crashed!")
cf.set_commit_hook(crashing_hook)
# Should not raise - exception is caught internally
with db.begin_txn() as txn:
txn.put(cf, b"crash_key", b"crash_val")
txn.commit()
cf.clear_commit_hook()
# Data should still be committed
with db.begin_txn() as txn:
assert txn.get(cf, b"crash_key") == b"crash_val"
class TestGetComparator:
"""Tests for get_comparator operations."""
def test_builtin_comparator_exists(self, db):
"""Test that built-in comparators are registered."""
assert db.get_comparator("memcmp") is True
assert db.get_comparator("reverse") is True
assert db.get_comparator("lexicographic") is True
def test_nonexistent_comparator(self, db):
"""Test that a non-existent comparator returns False."""
assert db.get_comparator("nonexistent_comp") is False
def test_custom_comparator_registered(self, db):
"""Test that a custom registered comparator is found."""
def my_cmp(k1: bytes, k2: bytes) -> int:
if k1 < k2:
return -1
elif k1 > k2:
return 1
return 0
db.register_comparator("my_test_cmp", my_cmp)
assert db.get_comparator("my_test_cmp") is True
class TestDeleteColumnFamily:
"""Tests for delete_column_family (by pointer) operations."""
def test_delete_by_pointer(self, db):
"""Test deleting a column family by pointer."""
db.create_column_family("del_cf")
cf = db.get_column_family("del_cf")
assert cf is not None
db.delete_column_family(cf)
with pytest.raises(tidesdb.TidesDBError):
db.get_column_family("del_cf")
def test_delete_with_data(self, db):
"""Test deleting a column family that contains data."""
db.create_column_family("data_cf")
cf = db.get_column_family("data_cf")
with db.begin_txn() as txn:
txn.put(cf, b"key1", b"value1")
txn.commit()
db.delete_column_family(cf)
with pytest.raises(tidesdb.TidesDBError):
db.get_column_family("data_cf")
class TestMaxMemoryUsage:
"""Tests for max_memory_usage configuration."""
def test_config_default(self):
"""Test that default max_memory_usage is 0."""
config = tidesdb.Config(db_path="/tmp/test")
assert config.max_memory_usage == 0
def test_open_with_max_memory_usage(self, temp_db_path):
"""Test opening a database with max_memory_usage set."""
db = tidesdb.TidesDB.open(temp_db_path, max_memory_usage=0)
assert db is not None
db.close()
def test_config_with_max_memory_usage(self, temp_db_path):
"""Test Config with explicit max_memory_usage."""
config = tidesdb.Config(
db_path=temp_db_path,
max_memory_usage=512 * 1024 * 1024, # 512MB
)
db = tidesdb.TidesDB(config)
assert db is not None
db.close()
if __name__ == "__main__":
pytest.main([__file__, "-v"])