Skip to content

Commit 7674aee

Browse files
committed
test consolidation
1 parent d2f7c9d commit 7674aee

File tree

4 files changed

+703
-206
lines changed

4 files changed

+703
-206
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Integration Test Consolidation Plan
2+
3+
## Current State
4+
- 19 integration test files with significant overlap
5+
- Multiple files testing the same functionality (prepared statements, consistency levels, etc.)
6+
- Duplicate test patterns across files
7+
8+
## Consolidation Strategy
9+
10+
### 1. **test_crud_operations.py** (NEW)
11+
Consolidate basic CRUD operations from:
12+
- `test_basic_operations.py` - Basic insert/select/update/delete
13+
- `test_select_operations.py` - SELECT query variations
14+
- Error handling tests
15+
- Non-existent data queries
16+
17+
**Remove from original files:**
18+
- Duplicate CRUD tests
19+
- Basic prepared statement tests (move to dedicated file)
20+
- Basic error handling
21+
22+
### 2. **test_concurrent_operations.py** (KEEP AS IS)
23+
Already well-focused on concurrency testing. Just ensure no duplication.
24+
25+
### 3. **test_batch_and_lwt_operations.py** (NEW)
26+
Combine atomic operations:
27+
- All tests from `test_batch_operations.py`
28+
- All tests from `test_lwt_operations.py`
29+
- Conditional batch operations
30+
31+
**Rationale:** Both deal with atomic operations and often used together
32+
33+
### 4. **test_data_types_and_counters.py** (NEW)
34+
Combine data type testing:
35+
- All tests from `test_cassandra_data_types.py`
36+
- All tests from `test_counters.py`
37+
- Remove counter test from data types file
38+
39+
**Rationale:** Counters are a special data type
40+
41+
### 5. **test_streaming_operations.py** (KEEP AS IS)
42+
Well-focused on streaming functionality.
43+
44+
### 6. **test_simple_statements.py** (KEEP AS IS)
45+
Specifically tests SimpleStatement usage (discouraged but needs testing).
46+
47+
### 7. **test_consistency_and_prepared_statements.py** (NEW)
48+
Focus on:
49+
- Consistency level tests from multiple files
50+
- Prepared statement lifecycle and reuse
51+
- Performance comparisons
52+
- Custom payloads
53+
54+
### 8. **Other files to keep as is:**
55+
- `test_network_failures.py` - Specific failure scenarios
56+
- `test_reconnection_behavior.py` - Connection recovery
57+
- `test_long_lived_connections.py` - Connection stability
58+
- `test_protocol_version.py` - Protocol compatibility
59+
- `test_driver_compatibility.py` - Driver API compatibility
60+
- `test_empty_resultsets.py` - Edge case testing
61+
- `test_stress.py` - Performance testing
62+
- `test_context_manager_safety_integration.py` - Resource management
63+
- `test_fastapi_reconnection_isolation.py` - FastAPI specific
64+
65+
## Expected Impact
66+
- Reduce from 19 to ~15 files
67+
- Eliminate duplicate test coverage
68+
- More logical organization
69+
- Easier to maintain and understand
70+
71+
## Implementation Order
72+
1. Create `test_crud_operations.py` consolidating basic operations
73+
2. Create `test_batch_and_lwt_operations.py` combining atomic operations
74+
3. Create `test_data_types_and_counters.py` combining type tests
75+
4. Create `test_consistency_and_prepared_statements.py` for advanced features
76+
5. Remove duplicates from original files
77+
6. Update conftest.py if needed
Lines changed: 4 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
"""
22
Integration tests for basic Cassandra operations.
33
4-
Demonstrates proper context manager usage and error handling patterns
5-
for production-ready code.
4+
This file focuses on connection management, error handling, async patterns,
5+
and concurrent operations. Basic CRUD operations have been moved to
6+
test_crud_operations.py.
67
"""
78

89
import asyncio
910
import uuid
1011

1112
import pytest
1213
from cassandra import InvalidRequest
13-
from cassandra.query import BatchStatement, BatchType
1414
from test_utils import generate_unique_table
1515

1616

1717
@pytest.mark.asyncio
1818
@pytest.mark.integration
1919
class TestBasicOperations:
20-
"""Test basic CRUD operations with real Cassandra."""
20+
"""Test connection, error handling, and async patterns with real Cassandra."""
2121

2222
async def test_connection_and_keyspace(
2323
self, cassandra_cluster, shared_keyspace_setup, pytestconfig
@@ -54,141 +54,6 @@ async def test_connection_and_keyspace(
5454
finally:
5555
await session.close()
5656

57-
async def test_insert_and_select(self, cassandra_session):
58-
"""Test inserting and selecting data with proper error handling."""
59-
# Use the unique users table created for this test
60-
users_table = cassandra_session._test_users_table
61-
62-
try:
63-
user_id = uuid.uuid4()
64-
65-
# Prepare statements with the unique table name
66-
insert_stmt = await cassandra_session.prepare(
67-
f"INSERT INTO {users_table} (id, name, email, age) VALUES (?, ?, ?, ?)"
68-
)
69-
select_stmt = await cassandra_session.prepare(
70-
f"SELECT * FROM {users_table} WHERE id = ?"
71-
)
72-
73-
# Insert data with error handling
74-
try:
75-
await cassandra_session.execute(
76-
insert_stmt, [user_id, "John Doe", "john@example.com", 30]
77-
)
78-
except Exception as e:
79-
pytest.fail(f"Failed to insert data: {e}")
80-
81-
# Select data with error handling
82-
try:
83-
result = await cassandra_session.execute(select_stmt, [user_id])
84-
85-
rows = result.all()
86-
assert len(rows) == 1
87-
88-
row = rows[0]
89-
assert row.id == user_id
90-
assert row.name == "John Doe"
91-
assert row.email == "john@example.com"
92-
assert row.age == 30
93-
except Exception as e:
94-
pytest.fail(f"Failed to select data: {e}")
95-
96-
except Exception as e:
97-
pytest.fail(f"Test setup failed: {e}")
98-
99-
async def test_prepared_statements(self, cassandra_session):
100-
"""Test using prepared statements with proper patterns."""
101-
# Use the unique users table created for this test
102-
users_table = cassandra_session._test_users_table
103-
104-
try:
105-
# Prepare insert statement
106-
insert_stmt = await cassandra_session.prepare(
107-
f"""
108-
INSERT INTO {users_table} (id, name, email, age)
109-
VALUES (?, ?, ?, ?)
110-
"""
111-
)
112-
113-
# Prepare select statement
114-
select_stmt = await cassandra_session.prepare(
115-
f"SELECT * FROM {users_table} WHERE id = ?"
116-
)
117-
118-
# Insert multiple users with error handling
119-
users = [
120-
(uuid.uuid4(), "Alice", "alice@example.com", 25),
121-
(uuid.uuid4(), "Bob", "bob@example.com", 35),
122-
(uuid.uuid4(), "Charlie", "charlie@example.com", 45),
123-
]
124-
125-
for user in users:
126-
try:
127-
await cassandra_session.execute(insert_stmt, user)
128-
except Exception as e:
129-
pytest.fail(f"Failed to insert user {user[1]}: {e}")
130-
131-
# Select each user with error handling
132-
for user_id, name, email, age in users:
133-
try:
134-
result = await cassandra_session.execute(select_stmt, [user_id])
135-
row = result.one()
136-
137-
assert row.id == user_id
138-
assert row.name == name
139-
assert row.email == email
140-
assert row.age == age
141-
except Exception as e:
142-
pytest.fail(f"Failed to verify user {name}: {e}")
143-
144-
except Exception as e:
145-
pytest.fail(f"Failed to prepare statements: {e}")
146-
147-
async def test_batch_operations(self, cassandra_session):
148-
"""Test batch insert operations with proper error handling."""
149-
# Use the unique users table created for this test
150-
users_table = cassandra_session._test_users_table
151-
152-
try:
153-
batch = BatchStatement(batch_type=BatchType.LOGGED)
154-
155-
# Prepare statement
156-
insert_stmt = await cassandra_session.prepare(
157-
f"""
158-
INSERT INTO {users_table} (id, name, email, age)
159-
VALUES (?, ?, ?, ?)
160-
"""
161-
)
162-
163-
# Add multiple statements to batch
164-
user_ids = []
165-
for i in range(5):
166-
user_id = uuid.uuid4()
167-
user_ids.append(user_id)
168-
batch.add(insert_stmt, [user_id, f"User{i}", f"user{i}@example.com", 20 + i])
169-
170-
# Execute batch with error handling
171-
try:
172-
await cassandra_session.execute_batch(batch)
173-
except Exception as e:
174-
pytest.fail(f"Failed to execute batch: {e}")
175-
176-
# Verify all users were inserted
177-
select_stmt = await cassandra_session.prepare(
178-
f"SELECT * FROM {users_table} WHERE id = ?"
179-
)
180-
181-
for i, user_id in enumerate(user_ids):
182-
try:
183-
result = await cassandra_session.execute(select_stmt, [user_id])
184-
row = result.one()
185-
assert row.name == f"User{i}"
186-
except Exception as e:
187-
pytest.fail(f"Failed to verify batch insert for User{i}: {e}")
188-
189-
except Exception as e:
190-
pytest.fail(f"Batch operation setup failed: {e}")
191-
19257
async def test_async_iteration(self, cassandra_session):
19358
"""Test async iteration over results with proper patterns."""
19459
# Use the unique users table created for this test
@@ -300,50 +165,3 @@ async def insert_user(i: int):
300165

301166
except Exception as e:
302167
pytest.fail(f"Concurrent query test setup failed: {e}")
303-
304-
async def test_update_and_delete(self, cassandra_session):
305-
"""Test UPDATE and DELETE operations with proper error handling."""
306-
users_table = cassandra_session._test_users_table
307-
308-
try:
309-
user_id = uuid.uuid4()
310-
311-
# Insert initial data
312-
insert_stmt = await cassandra_session.prepare(
313-
f"INSERT INTO {users_table} (id, name, email, age) VALUES (?, ?, ?, ?)"
314-
)
315-
await cassandra_session.execute(
316-
insert_stmt, [user_id, "Update Test", "update@example.com", 25]
317-
)
318-
319-
# Update data
320-
update_stmt = await cassandra_session.prepare(
321-
f"UPDATE {users_table} SET age = ? WHERE id = ?"
322-
)
323-
324-
try:
325-
await cassandra_session.execute(update_stmt, [30, user_id])
326-
except Exception as e:
327-
pytest.fail(f"Failed to update user: {e}")
328-
329-
# Verify update
330-
select_stmt = await cassandra_session.prepare(
331-
f"SELECT age FROM {users_table} WHERE id = ?"
332-
)
333-
result = await cassandra_session.execute(select_stmt, [user_id])
334-
assert result.one().age == 30
335-
336-
# Delete data
337-
delete_stmt = await cassandra_session.prepare(f"DELETE FROM {users_table} WHERE id = ?")
338-
339-
try:
340-
await cassandra_session.execute(delete_stmt, [user_id])
341-
except Exception as e:
342-
pytest.fail(f"Failed to delete user: {e}")
343-
344-
# Verify deletion
345-
result = await cassandra_session.execute(select_stmt, [user_id])
346-
assert result.one() is None
347-
348-
except Exception as e:
349-
pytest.fail(f"Update/delete test failed: {e}")

0 commit comments

Comments
 (0)