apexbase 1.25.0

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

This module tests:
- Column addition with various data types
- Column deletion operations
- Column renaming operations
- Column data type retrieval
- Edge cases and error handling
- Column operations with existing data
- Performance considerations
"""

import pytest
import tempfile
import shutil
from pathlib import Path
import sys
import os

# 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
except ImportError as e:
    pytest.skip(f"ApexBase not available: {e}", allow_module_level=True)


class TestColumnAddition:
    """Test column addition operations"""
    
    def test_add_column_basic(self):
        """Test basic column addition"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Add new column - note: column management behavior may vary
            try:
                client.add_column("city", "string")
            except Exception as e:
                print(f"add_column: {e}")
            
            # Store data with new column
            client.store({"name": "Bob", "age": 30, "city": "NYC"})
            
            # Verify new data includes new column
            result = client.retrieve(2)
            assert result is not None
            # The city field should be stored
            if "city" in result:
                assert result["city"] == "NYC"
            
            client.close()
    
    def test_add_column_different_types(self):
        """Test adding columns with different data types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice"})
            
            # Add columns with different types - note: column management may vary
            try:
                client.add_column("age", "integer")
                client.add_column("salary", "float")
                client.add_column("active", "boolean")
                client.add_column("notes", "string")
            except Exception as e:
                print(f"add_column types: {e}")
            
            # Store data with all fields
            client.store({
                "name": "Bob",
                "age": 30,
                "salary": 50000.50,
                "active": True,
                "notes": "Test employee"
            })
            
            # Verify data types are preserved in stored data
            result = client.retrieve(2)
            assert result is not None
            assert result["age"] == 30
            assert result["salary"] == 50000.50
            assert result["active"] is True
            assert result["notes"] == "Test employee"
            
            client.close()
    
    def test_add_column_with_existing_data(self):
        """Test adding column to table with existing data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store multiple records
            test_data = [
                {"name": "Alice", "age": 25},
                {"name": "Bob", "age": 30},
                {"name": "Charlie", "age": 35},
            ]
            client.store(test_data)
            
            # Add new column - behavior may vary
            try:
                client.add_column("city", "string")
            except Exception as e:
                print(f"add_column with data: {e}")
            
            # Store new record with the new column
            client.store({"name": "Diana", "age": 28, "city": "Boston"})
            
            # Verify the new record has the city
            diana = client.retrieve(4)
            assert diana is not None
            assert diana["name"] == "Diana"
            if "city" in diana:
                assert diana["city"] == "Boston"
            
            client.close()
    
    def test_add_column_duplicate_name(self):
        """Test adding column with duplicate name"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Try to add existing column - may raise error or handle gracefully
            try:
                client.add_column("name", "string")
            except Exception as e:
                # Expected to raise an error for duplicate
                print(f"Duplicate column handled: {e}")
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            assert result["name"] == "Alice"
            
            client.close()
    
    def test_add_column_invalid_type(self):
        """Test adding column with invalid data type"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice"})
            
            # Try to add column with invalid type - may raise error or handle gracefully
            try:
                client.add_column("invalid_col", "invalid_type")
            except Exception as e:
                # Expected to raise an error
                print(f"Invalid type handled: {e}")
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            assert result["name"] == "Alice"
            
            client.close()
    
    def test_add_column_special_characters(self):
        """Test adding column with special characters in name"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice"})
            
            # Try various special character column names
            special_names = [
                "column_with_underscores",
                "column-with-dashes",
                "column.with.dots",
                "columnWithCamelCase",
                "column_with_numbers123",
            ]
            
            for col_name in special_names:
                try:
                    client.add_column(col_name, "string")
                    fields = client.list_fields()
                    assert col_name in fields
                except Exception as e:
                    print(f"Column name '{col_name}' not supported: {e}")
            
            client.close()
    
    def test_add_column_unicode_name(self):
        """Test adding column with unicode characters in name"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice"})
            
            # Try unicode column names
            unicode_names = [
                "列名",  # Chinese
                "колонка",  # Russian
                "colonne",  # French with accent
            ]
            
            for col_name in unicode_names:
                try:
                    client.add_column(col_name, "string")
                    fields = client.list_fields()
                    assert col_name in fields
                except Exception as e:
                    print(f"Unicode column name '{col_name}' not supported: {e}")
            
            client.close()


class TestColumnDeletion:
    """Test column deletion operations"""
    
    def test_drop_column_basic(self):
        """Test basic column deletion"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with multiple columns
            client.store({"name": "Alice", "age": 25, "city": "NYC", "salary": 50000})
            
            # Drop column - behavior may vary
            try:
                client.drop_column("city")
                
                # Verify existing records may no longer have the column
                result = client.retrieve(1)
                if result is not None:
                    # City should be dropped
                    assert "name" in result
                    assert "age" in result
            except Exception as e:
                print(f"drop_column: {e}")
            
            client.close()
    
    def test_drop_nonexistent_column(self):
        """Test dropping nonexistent column"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Try to drop nonexistent column - may raise error or handle gracefully
            try:
                client.drop_column("nonexistent_column")
            except Exception as e:
                # Expected to raise an error
                print(f"Drop nonexistent: {e}")
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            assert result["name"] == "Alice"
            
            client.close()
    
    def test_drop_id_column(self):
        """Test dropping _id column (should be prevented)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Try to drop _id column
            with pytest.raises(ValueError, match="Cannot drop _id column"):
                client.drop_column("_id")
            
            # Verify _id column still exists (internally)
            result = client.retrieve(1)
            # _id should still be managed internally
            
            client.close()
    
    def test_drop_multiple_columns(self):
        """Test dropping multiple columns sequentially"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with many columns
            client.store({
                "name": "Alice",
                "age": 25,
                "city": "NYC",
                "salary": 50000,
                "department": "Engineering",
                "active": True
            })
            
            # Drop columns one by one - behavior may vary
            try:
                client.drop_column("city")
                client.drop_column("salary")
                client.drop_column("department")
            except Exception as e:
                print(f"Drop multiple: {e}")
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            assert result["name"] == "Alice"
            
            client.close()
    
    def test_drop_column_with_data(self):
        """Test dropping column that contains data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store multiple records with data in the column to be dropped
            test_data = [
                {"name": "Alice", "age": 25, "city": "NYC"},
                {"name": "Bob", "age": 30, "city": "LA"},
                {"name": "Charlie", "age": 35, "city": "Chicago"},
            ]
            client.store(test_data)
            
            # Drop column with data - behavior may vary
            try:
                client.drop_column("city")
            except Exception as e:
                print(f"Drop with data: {e}")
            
            # Verify records are still accessible
            for i in range(3):
                result = client.retrieve(i + 1)
                assert result is not None
                assert "name" in result
                assert "age" in result
            
            client.close()


class TestColumnRenaming:
    """Test column renaming operations"""
    
    def test_rename_column_basic(self):
        """Test basic column renaming"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with original column name
            client.store({"name": "Alice", "age": 25, "city": "NYC"})
            
            # Rename column - behavior may vary
            try:
                client.rename_column("city", "location")
                
                # Verify data is accessible
                result = client.retrieve(1)
                assert result is not None
            except Exception as e:
                print(f"rename_column: {e}")
            
            client.close()
    
    def test_rename_nonexistent_column(self):
        """Test renaming nonexistent column"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Try to rename nonexistent column - may raise error or handle gracefully
            try:
                client.rename_column("nonexistent_column", "new_name")
            except Exception as e:
                print(f"Rename nonexistent: {e}")
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            assert result["name"] == "Alice"
            
            client.close()
    
    def test_rename_to_existing_column(self):
        """Test renaming column to name that already exists"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with multiple columns
            client.store({"name": "Alice", "age": 25, "city": "NYC"})
            
            # Try to rename to existing column name - may raise error
            try:
                client.rename_column("age", "name")
            except Exception as e:
                print(f"Rename to existing: {e}")
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            
            client.close()
    
    def test_rename_id_column(self):
        """Test renaming _id column (should be prevented)"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Try to rename _id column
            with pytest.raises(ValueError, match="Cannot rename _id column"):
                client.rename_column("_id", "new_id")
            
            # Verify _id column is still managed internally
            result = client.retrieve(1)
            
            client.close()
    
    def test_rename_column_with_data(self):
        """Test renaming column that contains data"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store multiple records with data in the column to be renamed
            test_data = [
                {"name": "Alice", "age": 25, "city": "NYC"},
                {"name": "Bob", "age": 30, "city": "LA"},
                {"name": "Charlie", "age": 35, "city": "Chicago"},
            ]
            client.store(test_data)
            
            # Rename column - behavior may vary
            try:
                client.rename_column("city", "location")
            except Exception as e:
                print(f"Rename with data: {e}")
            
            # Verify all records are still accessible
            for i in range(3):
                result = client.retrieve(i + 1)
                assert result is not None
                assert "name" in result
            
            client.close()
    
    def test_rename_column_special_characters(self):
        """Test renaming column with special characters"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Rename to column with special characters
            try:
                client.rename_column("age", "new_age_with_underscores")
                fields = client.list_fields()
                assert "new_age_with_underscores" in fields
                assert "age" not in fields
                
                # Verify data is accessible
                result = client.retrieve(1)
                assert result["new_age_with_underscores"] == 25
            except Exception as e:
                print(f"Special character rename not supported: {e}")
            
            client.close()


class TestColumnDataTypeRetrieval:
    """Test column data type retrieval operations"""
    
    def test_get_column_dtype_basic(self):
        """Test getting basic column data types"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with various types
            client.store({
                "name": "Alice",
                "age": 25,
                "salary": 50000.50,
                "active": True,
                "notes": "Test notes"
            })
            
            # Get column types - behavior may vary
            try:
                name_type = client.get_column_dtype("name")
                assert name_type is not None
            except Exception as e:
                print(f"get_column_dtype: {e}")
            
            client.close()
    
    def test_get_column_dtype_added_columns(self):
        """Test getting types of explicitly added columns"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice"})
            
            # Add columns with specific types - behavior may vary
            try:
                client.add_column("age", "integer")
                age_type = client.get_column_dtype("age")
                assert age_type is not None
            except Exception as e:
                print(f"get_column_dtype added: {e}")
            
            client.close()
    
    def test_get_column_dtype_nonexistent(self):
        """Test getting type of nonexistent column"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Try to get type of nonexistent column - may raise error
            try:
                dtype = client.get_column_dtype("nonexistent_column")
            except Exception as e:
                # Expected to raise an error
                print(f"Get dtype nonexistent: {e}")
            
            client.close()
    
    def test_get_column_dtype_after_rename(self):
        """Test getting column type after renaming"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store initial data
            client.store({"name": "Alice", "age": 25})
            
            # Rename column and get type - behavior may vary
            try:
                client.rename_column("age", "user_age")
                user_age_type = client.get_column_dtype("user_age")
                assert user_age_type is not None
            except Exception as e:
                print(f"Dtype after rename: {e}")
            
            client.close()


class TestColumnOperationsEdgeCases:
    """Test edge cases and error handling for column operations"""
    
    def test_column_operations_on_closed_client(self):
        """Test column operations 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.add_column("new_col", "string")
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.drop_column("name")
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.rename_column("name", "new_name")
            
            with pytest.raises(RuntimeError, match="connection has been closed"):
                client.get_column_dtype("name")
    
    def test_column_operations_empty_table(self):
        """Test column operations on empty table"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Add column to empty table - behavior may vary
            try:
                client.add_column("new_column", "string")
            except Exception as e:
                print(f"add_column empty: {e}")
            
            # Store some data to verify
            client.store({"test": "data"})
            result = client.retrieve(1)
            assert result is not None
            
            client.close()
    
    def test_column_operations_with_fts(self):
        """Test column 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 data with FTS content
            client.store({"content": "Searchable text", "metadata": "not indexed"})
            
            # Add new column
            client.add_column("category", "string")
            
            # Verify FTS still works
            results = client.search_text("searchable")
            assert len(results) > 0
            
            # Drop non-indexed column
            client.drop_column("metadata")
            
            # Verify FTS still works
            results = client.search_text("searchable")
            assert len(results) > 0
            
            client.close()
    
    def test_column_operations_large_dataset(self):
        """Test column operations with large dataset"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store large dataset
            large_data = [{"id": i, "value": f"item_{i}"} for i in range(1000)]
            client.store(large_data)
            
            import time
            
            # Add column to large dataset - behavior may vary
            start_time = time.time()
            try:
                client.add_column("category", "string")
            except Exception as e:
                print(f"add_column large: {e}")
            add_time = time.time() - start_time
            
            # Should be reasonably fast
            assert add_time < 10.0
            
            # Verify data is still accessible
            result = client.retrieve(1)
            assert result is not None
            assert result["id"] == 0
            
            client.close()
    
    def test_column_operations_with_various_data(self):
        """Test column operations with various data types and values"""
        with tempfile.TemporaryDirectory() as temp_dir:
            client = ApexClient(dirpath=temp_dir)
            client.create_table("default")
            
            # Store data with various types
            test_data = {
                "string_field": "test_string",
                "int_field": 42,
                "float_field": 3.14159,
                "bool_field": True,
            }
            client.store(test_data)
            
            # Add new column - behavior may vary
            try:
                client.add_column("new_field", "string")
            except Exception as e:
                print(f"add_column various: {e}")
            
            # Verify existing data is preserved
            result = client.retrieve(1)
            assert result is not None
            assert result["string_field"] == "test_string"
            assert result["int_field"] == 42
            
            client.close()
    
    def test_column_operations_across_tables(self):
        """Test column operations 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", "age": 25})
            
            # Create and configure another table
            client.create_table("users")
            client.store({"username": "bob123", "email": "bob@example.com"})
            
            # Add column to default table - behavior may vary
            client.use_table("default")
            try:
                client.add_column("city", "string")
            except Exception as e:
                print(f"add_column across: {e}")
            
            # Verify data is accessible from both tables
            client.use_table("default")
            result = client.retrieve(1)
            assert result is not None
            assert result["name"] == "Alice"
            
            client.use_table("users")
            result = client.retrieve(1)
            assert result is not None
            assert result["username"] == "bob123"
            
            client.close()


if __name__ == "__main__":
    pytest.main([__file__, "-v"])