apexbase 1.23.0

High-performance HTAP embedded database with Rust core
Documentation
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
"""
Comprehensive test suite for ApexBase Data Modification Operations

This module tests:
- Delete operations (single and batch)
- Replace operations (single and batch)
- Data consistency after modifications
- FTS index updates after modifications
- Edge cases and error handling
- Performance considerations
- Transaction-like behavior
"""

import pytest
import tempfile
import shutil
from pathlib import Path
import sys
import os
import numpy as np

# Add the apexbase python module to path
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:
    """Test delete operations"""
    
    def test_delete_single_record(self):
        """Test deleting a single record"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Verify initial state
            assert client.count_rows() == 3
            
            # Delete single record
            result = client.delete(2)  # Delete Bob
            
            assert result is True
            assert client.count_rows() == 2
            
            # Verify Bob is deleted
            alice = client.retrieve(1)
            assert alice["name"] == "Alice"
            
            bob = client.retrieve(2)
            assert bob is None  # Should be deleted
            
            charlie = client.retrieve(3)
            assert charlie["name"] == "Charlie"
            
            client.close()
    
    def test_delete_nonexistent_record(self):
        """Test deleting a nonexistent record"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            client.store({"name": "Alice", "age": 25})
            
            # Try to delete nonexistent record
            result = client.delete(999)
            
            # Should return False (not found)
            assert result is False
            assert client.count_rows() == 1
            
            client.close()
    
    def test_delete_batch_records(self):
        """Test deleting multiple records"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            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)
            
            # Verify initial state
            assert client.count_rows() == 5
            
            # Delete multiple records
            result = client.delete([2, 4])  # Delete Bob and Diana
            
            assert result is True
            assert client.count_rows() == 3
            
            # Verify specific records are deleted
            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):
        """Test deleting batch with some nonexistent IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Delete mix of existing and nonexistent IDs - behavior may vary
            try:
                result = client.delete([1, 999, 3, 888])
                # After deletion, Bob should remain
                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):
        """Test deleting batch with all nonexistent IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            client.store({"name": "Alice", "age": 25})
            
            # Try to delete all nonexistent IDs
            result = client.delete([999, 888, 777])
            
            assert result is False  # No records deleted
            assert client.count_rows() == 1  # Original record still exists
            
            client.close()
    
    def test_delete_empty_list(self):
        """Test deleting with empty list"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            client.store({"name": "Alice", "age": 25})
            
            # Delete with empty list
            result = client.delete([])
            
            # Should not delete anything
            assert result is False or result is True  # Implementation may vary
            assert client.count_rows() == 1
            
            client.close()
    
    def test_delete_from_empty_database(self):
        """Test deleting from empty database"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Try to delete from empty database
            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):
        """Test deleting records with various data types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with various types (excluding bytes which may have issues)
            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)
            
            # Delete first record
            result = client.delete(1)
            
            # Verify remaining record is accessible
            remaining = client.retrieve(2)
            assert remaining is not None
            assert remaining["string_field"] == "another_string"
            assert remaining["int_field"] == -100
            
            client.close()


class TestReplaceOperations:
    """Test replace operations"""
    
    def test_replace_single_record(self):
        """Test replacing a single record"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25, "city": "NYC"},
                {"name": "Bob", "age": 30, "city": "LA"},
            ]
            client.store(test_data)
            
            # Replace Alice's record
            new_data = {"name": "Alice Updated", "age": 26, "city": "Boston", "status": "active"}
            result = client.replace(1, new_data)
            
            assert result is True
            
            # Verify the replacement
            alice = client.retrieve(1)
            assert alice["name"] == "Alice Updated"
            assert alice["age"] == 26
            assert alice["city"] == "Boston"
            assert alice["status"] == "active"
            
            # Verify Bob is unchanged
            bob = client.retrieve(2)
            assert bob["name"] == "Bob"
            assert bob["age"] == 30
            assert bob["city"] == "LA"
            
            client.close()
    
    def test_replace_nonexistent_record(self):
        """Test replacing a nonexistent record"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            client.store({"name": "Alice", "age": 25})
            
            # Try to replace nonexistent record
            new_data = {"name": "New Record", "age": 30}
            result = client.replace(999, new_data)
            
            assert result is False
            assert client.count_rows() == 1  # Should not create new record
            
            # Verify original record is unchanged
            alice = client.retrieve(1)
            assert alice["name"] == "Alice"
            assert alice["age"] == 25
            
            client.close()
    
    def test_replace_with_different_schema(self):
        """Test replacing record with different field structure"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial record
            client.store({"name": "Alice", "age": 25, "city": "NYC"})
            
            # Replace with completely different fields - behavior may vary
            new_data = {
                "title": "Ms",
                "first_name": "Alice",
                "department": "Engineering",
            }
            try:
                result = client.replace(1, new_data)
                
                # Verify the replacement
                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):
        """Test replacing record with fewer fields"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial record with many fields
            client.store({
                "name": "Alice",
                "age": 25,
                "city": "NYC",
            })
            
            # Replace with fewer fields - behavior may vary
            new_data = {"name": "Alice Updated", "age": 26}
            try:
                result = client.replace(1, new_data)
                # Result may be True or False depending on implementation
            except Exception as e:
                print(f"Replace partial: {e}")
            
            # Verify data is still accessible
            updated = client.retrieve(1)
            assert updated is not None
            
            client.close()
    
    def test_replace_with_empty_data(self):
        """Test replacing record with empty data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial record
            client.store({"name": "Alice", "age": 25})
            
            # Replace with empty data - behavior may vary
            try:
                result = client.replace(1, {})
                # Verify result is accessible
                updated = client.retrieve(1)
                # May be empty or have _id only
            except Exception as e:
                print(f"Replace empty: {e}")
            
            client.close()
    
    def test_replace_with_various_data_types(self):
        """Test replacing record with various data types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial record
            client.store({"name": "Alice", "age": 25})
            
            # Replace with various data types (excluding bytes which may have issues)
            new_data = {
                "string_field": "test_string",
                "int_field": 42,
                "float_field": 3.14159,
                "bool_field": True,
            }
            try:
                result = client.replace(1, new_data)
                
                # Verify types are preserved
                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:
    """Test batch replace operations"""
    
    def test_batch_replace_basic(self):
        """Test basic batch replace operation"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Batch replace
            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
            
            # Verify replacements
            alice = client.retrieve(1)
            assert alice["name"] == "Alice Updated"
            assert alice["age"] == 26
            
            bob = client.retrieve(2)
            assert bob["name"] == "Bob"  # Unchanged
            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):
        """Test batch replace with some nonexistent IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
            ]
            client.store(test_data)
            
            # Batch replace with mix of existing and nonexistent IDs
            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)
            
            # Should only succeed for existing IDs
            assert 1 in success_ids
            assert 2 in success_ids
            assert 999 not in success_ids
            
            # Verify successful replacements
            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):
        """Test batch replace with empty dictionary"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            client.store({"name": "Alice", "age": 25})
            
            # Batch replace with empty dict
            success_ids = client.batch_replace({})
            
            assert len(success_ids) == 0
            assert client.count_rows() == 1  # Unchanged
            
            client.close()
    
    def test_batch_replace_all_nonexistent(self):
        """Test batch replace with all nonexistent IDs"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store test data
            client.store({"name": "Alice", "age": 25})
            
            # Batch replace with all nonexistent IDs
            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  # Original unchanged
            
            client.close()


class TestModificationWithFTS:
    """Test data modifications with FTS enabled"""
    
    def test_delete_with_fts_enabled(self):
        """Test delete operations with FTS enabled"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            client.init_fts(index_fields=['content'])
            
            # Store searchable documents
            documents = [
                {"content": "Python programming tutorial"},
                {"content": "JavaScript development guide"},
                {"content": "Database management system"},
            ]
            client.store(documents)
            
            # Verify search works initially
            results = client.search_text("python")
            assert len(results) > 0
            
            # Delete a document
            result = client.delete(1)  # Delete Python document
            assert result is True
            
            # Verify search reflects the deletion
            results = client.search_text("python")
            assert len(results) == 0  # Python document should be gone
            
            # Verify other documents are still searchable
            results = client.search_text("javascript")
            assert len(results) > 0
            
            client.close()
    
    def test_replace_with_fts_enabled(self):
        """Test replace operations with FTS enabled"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            client.init_fts(index_fields=['content'])
            
            # Store searchable document
            client.store({"content": "Python programming tutorial"})
            
            # Verify search works initially
            results = client.search_text("python")
            initial_found = len(results) > 0
            
            # Replace the document - FTS update behavior may vary
            new_data = {"content": "JavaScript development guide"}
            try:
                result = client.replace(1, new_data)
                # Verify document was replaced
                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):
        """Test batch operations with FTS enabled"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            client.init_fts(index_fields=['content'])
            
            # Store multiple documents
            documents = [
                {"content": "Python programming"},
                {"content": "JavaScript development"},
                {"content": "Database management"},
            ]
            client.store(documents)
            
            # Batch delete
            result = client.delete([1, 3])  # Delete Python and Database
            assert result is True
            
            # Verify search reflects deletions
            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  # JavaScript should remain
            
            client.close()


class TestModificationEdgeCases:
    """Test edge cases and error handling for modifications"""
    
    def test_modifications_on_closed_client(self):
        """Test modifications on closed client"""
        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):
        """Test delete with invalid ID types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store some data
            client.store({"name": "Alice", "age": 25})
            
            # Test invalid ID types - may raise exception or handle gracefully
            try:
                client.delete(-1)
            except (TypeError, ValueError, OverflowError):
                pass  # Expected
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            
            client.close()
    
    def test_replace_invalid_id_types(self):
        """Test replace with invalid ID types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store some data
            client.store({"name": "Alice", "age": 25})
            
            # Test invalid ID types - may raise exception or handle gracefully
            try:
                client.replace(-1, {"test": "data"})
            except (TypeError, ValueError, OverflowError):
                pass  # Expected
            
            # Verify original data is still accessible
            result = client.retrieve(1)
            assert result is not None
            assert result["name"] == "Alice"
            
            client.close()
    
    def test_modifications_with_unicode_data(self):
        """Test modifications with unicode data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store unicode data
            unicode_data = {
                "chinese": "你好世界",
                "emoji": "🌍🚀",
            }
            client.store(unicode_data)
            
            # Replace with unicode data
            new_unicode_data = {
                "russian": "Привет мир",
                "french": "Bonjour le monde",
            }
            try:
                result = client.replace(1, new_unicode_data)
                # Verify unicode data is accessible
                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):
        """Test modifications with large data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store large data
            large_string = "x" * 100000  # 100KB string
            large_data = {
                "large_text": large_string,
                "normal_field": "test",
            }
            client.store(large_data)
            
            # Replace with different 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)
                # Verify large data replacement
                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):
        """Test data consistency after modifications"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            initial_data = [
                {"id": 1, "name": "Alice", "age": 25},
                {"id": 2, "name": "Bob", "age": 30},
                {"id": 3, "name": "Charlie", "age": 35},
            ]
            client.store(initial_data)
            
            # Perform various modifications
            client.delete(2)  # Delete Bob
            client.replace(1, {"id": 1, "name": "Alice Updated", "age": 26})
            
            # Add new data
            client.store({"id": 4, "name": "Diana", "age": 28})
            
            # Verify consistency
            all_records = client.retrieve_all()
            assert len(all_records) == 3
            
            # Check specific records
            alice = client.retrieve(1)
            assert alice["name"] == "Alice Updated"
            assert alice["age"] == 26
            
            bob = client.retrieve(2)
            assert bob is None  # Should be deleted
            
            charlie = client.retrieve(3)
            assert charlie["name"] == "Charlie"
            
            diana = client.retrieve(4)
            assert diana["name"] == "Diana"
            
            client.close()
    
    def test_modifications_performance(self):
        """Test performance of modification operations"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store dataset
            data = [{"id": i, "value": f"item_{i}"} for i in range(100)]
            client.store(data)
            
            import time
            
            # Test delete performance - behavior may vary
            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  # Should be reasonably fast
            
            client.close()


class TestModificationWithDifferentTables:
    """Test modifications across different tables"""
    
    def test_modifications_table_isolation(self):
        """Test that modifications are isolated to specific tables"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data in default table
            client.store({"name": "Alice", "table": "default"})
            
            # Create and store data in another table
            client.create_table("users")
            client.store({"name": "Bob", "table": "users"})
            
            # Modify in default table
            client.use_table("default")
            try:
                client.replace(1, {"name": "Alice Updated", "table": "default"})
            except Exception as e:
                print(f"Replace isolation: {e}")
            
            # Verify data is accessible in both tables
            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):
        """Test FTS modifications are table-specific"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Create tables with FTS
            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"})
            
            # Verify data in both tables
            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"])