|
1 | 1 | """ |
2 | 2 | Integration tests for basic Cassandra operations. |
3 | 3 |
|
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. |
6 | 7 | """ |
7 | 8 |
|
8 | 9 | import asyncio |
9 | 10 | import uuid |
10 | 11 |
|
11 | 12 | import pytest |
12 | 13 | from cassandra import InvalidRequest |
13 | | -from cassandra.query import BatchStatement, BatchType |
14 | 14 | from test_utils import generate_unique_table |
15 | 15 |
|
16 | 16 |
|
17 | 17 | @pytest.mark.asyncio |
18 | 18 | @pytest.mark.integration |
19 | 19 | class TestBasicOperations: |
20 | | - """Test basic CRUD operations with real Cassandra.""" |
| 20 | + """Test connection, error handling, and async patterns with real Cassandra.""" |
21 | 21 |
|
22 | 22 | async def test_connection_and_keyspace( |
23 | 23 | self, cassandra_cluster, shared_keyspace_setup, pytestconfig |
@@ -54,141 +54,6 @@ async def test_connection_and_keyspace( |
54 | 54 | finally: |
55 | 55 | await session.close() |
56 | 56 |
|
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 | | - |
192 | 57 | async def test_async_iteration(self, cassandra_session): |
193 | 58 | """Test async iteration over results with proper patterns.""" |
194 | 59 | # Use the unique users table created for this test |
@@ -300,50 +165,3 @@ async def insert_user(i: int): |
300 | 165 |
|
301 | 166 | except Exception as e: |
302 | 167 | 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