-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_bulk_insert_optimization.py
More file actions
136 lines (102 loc) · 4.46 KB
/
Copy pathtest_bulk_insert_optimization.py
File metadata and controls
136 lines (102 loc) · 4.46 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
#!/usr/bin/env python3
"""
Test script to verify bulk insert optimizations.
This tests:
1. Batched insert functionality
2. Foreign key deferral
3. Generator handling for strain annotated variants
"""
import os
import sys
# Set up environment for testing
os.environ["MODULE_DB_OPERATIONS_CONNECTION_TYPE"] = "memory"
# Add the package to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src/pkg'))
from caendr.services.logger import logger
from caendr.services.cloud.postgresql import db
from caendr.models.sql import Strain, WormbaseGeneSummary, WormbaseGene, Homolog, StrainAnnotatedVariant
from caendr.services.sql.db import bulk_insert_with_batching
def test_bulk_insert_basic():
"""Test basic bulk insert functionality."""
logger.info("Testing basic bulk insert...")
# Create test data generator
def test_data_gen():
for i in range(150): # Test batching with batch_size=100
yield {
'name': f'strain_{i}',
'isotype': f'iso_{i}',
'sequenced': i % 2 == 0,
}
total = bulk_insert_with_batching(db, Strain, test_data_gen(), batch_size=100, defer_fks=False)
assert total == 150, f"Expected 150 insertions, got {total}"
count = db.session.query(Strain).count()
assert count == 150, f"Expected 150 records in DB, got {count}"
logger.info(f"✓ Basic bulk insert test passed: {total} records inserted and verified")
def test_bulk_insert_with_single_batch():
"""Test bulk insert with data smaller than batch size."""
logger.info("Testing bulk insert with single batch...")
def test_data_gen():
for i in range(50):
yield {
'name': f'strain_small_{i}',
'isotype': f'iso_small_{i}',
'sequenced': False,
}
total = bulk_insert_with_batching(db, Strain, test_data_gen(), batch_size=100, defer_fks=False)
assert total == 50, f"Expected 50 insertions, got {total}"
logger.info(f"✓ Single batch test passed: {total} records inserted")
def test_bulk_insert_large_batch():
"""Test bulk insert with large dataset."""
logger.info("Testing bulk insert with large dataset...")
def test_data_gen():
for i in range(25000): # Larger dataset
yield {
'name': f'strain_large_{i}',
'isotype': f'iso_large_{i}',
'sequenced': i % 3 == 0,
}
total = bulk_insert_with_batching(db, Strain, test_data_gen(), batch_size=10000, defer_fks=False)
assert total == 25000, f"Expected 25000 insertions, got {total}"
count = db.session.query(Strain).count()
# Count includes previous tests
assert count >= 25000 + 150 + 50, f"Expected at least 25200 records in DB"
logger.info(f"✓ Large batch test passed: {total} records inserted")
def test_generator_handling():
"""Test that generators are properly consumed."""
logger.info("Testing generator handling...")
data_yielded = [0] # Track how many items were yielded
def counting_gen():
for i in range(500):
data_yielded[0] += 1
yield {
'name': f'strain_gen_{i}',
'isotype': f'iso_gen_{i}',
'sequenced': False,
}
total = bulk_insert_with_batching(db, Strain, counting_gen(), batch_size=100, defer_fks=False)
assert total == data_yielded[0], f"Mismatch: inserted {total}, but {data_yielded[0]} items were yielded"
assert total == 500, f"Expected 500 insertions, got {total}"
logger.info(f"✓ Generator handling test passed: {total} records inserted")
def run_all_tests():
"""Run all tests."""
logger.info("Starting bulk insert optimization tests...")
logger.info("=" * 60)
try:
# Initialize database
from caendr.models.sql import create_app
app = create_app()
with app.app_context():
db.create_all()
test_bulk_insert_basic()
test_bulk_insert_with_single_batch()
test_bulk_insert_large_batch()
test_generator_handling()
logger.info("=" * 60)
logger.info("✓ All tests passed successfully!")
return True
except Exception as e:
logger.error(f"✗ Test failed with error: {e}", exc_info=True)
return False
if __name__ == '__main__':
success = run_all_tests()
sys.exit(0 if success else 1)