|
10 | 10 | from sqlalchemy import MetaData
|
11 | 11 | from sqlalchemy import String
|
12 | 12 | from sqlalchemy import Table
|
| 13 | +from sqlalchemy import text |
13 | 14 |
|
14 | 15 | from sqlalchemy_singlestoredb import ColumnGroup
|
15 | 16 | from sqlalchemy_singlestoredb.ddlelement import compile_column_group
|
@@ -231,5 +232,151 @@ def test_table_column_group_with_other_ddl_elements(self) -> None:
|
231 | 232 | assert 'COLUMN GROUP cg_complex (*)' in self.compiled_ddl
|
232 | 233 |
|
233 | 234 |
|
| 235 | +class TestColumnGroupReflection: |
| 236 | + """Test ColumnGroup reflection from actual database tables.""" |
| 237 | + |
| 238 | + def test_reflect_basic_column_group( |
| 239 | + self, test_engine: Any, clean_tables: Any, |
| 240 | + ) -> None: |
| 241 | + """Test reflection of basic ColumnGroup.""" |
| 242 | + table_name = 'test_column_group_reflection' |
| 243 | + |
| 244 | + with test_engine.connect() as conn: |
| 245 | + try: |
| 246 | + with conn.begin(): |
| 247 | + # Create table with ColumnGroup (try different syntax) |
| 248 | + conn.execute( |
| 249 | + text(f""" |
| 250 | + CREATE TABLE {table_name} ( |
| 251 | + id INT PRIMARY KEY, |
| 252 | + data1 VARCHAR(100), |
| 253 | + data2 VARCHAR(100), |
| 254 | + data3 INT, |
| 255 | + KEY cg_all_data ALL COLUMNS |
| 256 | + ) |
| 257 | + """), |
| 258 | + ) |
| 259 | + except Exception as e: |
| 260 | + if 'syntax' in str(e).lower() or 'not supported' in str(e).lower(): |
| 261 | + pytest.skip(f'ColumnGroup syntax not supported: {e}') |
| 262 | + else: |
| 263 | + raise |
| 264 | + |
| 265 | + # Show the generated CREATE TABLE |
| 266 | + result = conn.execute(text(f'SHOW CREATE TABLE {table_name}')) |
| 267 | + create_sql = result.fetchone()[1] |
| 268 | + print(f'\nGenerated CREATE TABLE for {table_name}:') |
| 269 | + print(create_sql) |
| 270 | + |
| 271 | + # Verify reflection works |
| 272 | + metadata = MetaData() |
| 273 | + reflected_table = Table(table_name, metadata, autoload_with=test_engine) |
| 274 | + |
| 275 | + # Should have expected columns |
| 276 | + assert len(reflected_table.columns) == 4 |
| 277 | + assert 'id' in reflected_table.columns |
| 278 | + assert 'data1' in reflected_table.columns |
| 279 | + assert 'data2' in reflected_table.columns |
| 280 | + assert 'data3' in reflected_table.columns |
| 281 | + |
| 282 | + def test_reflect_named_column_group( |
| 283 | + self, test_engine: Any, clean_tables: Any, |
| 284 | + ) -> None: |
| 285 | + """Test reflection of named ColumnGroup.""" |
| 286 | + table_name = 'test_named_column_group_reflection' |
| 287 | + |
| 288 | + with test_engine.connect() as conn: |
| 289 | + try: |
| 290 | + with conn.begin(): |
| 291 | + # Create table with named ColumnGroup |
| 292 | + conn.execute( |
| 293 | + text(f""" |
| 294 | + CREATE TABLE {table_name} ( |
| 295 | + user_id INT, |
| 296 | + name VARCHAR(100), |
| 297 | + email VARCHAR(200), |
| 298 | + created_at TIMESTAMP, |
| 299 | + PRIMARY KEY (user_id), |
| 300 | + KEY cg_user_info ALL COLUMNS |
| 301 | + ) |
| 302 | + """), |
| 303 | + ) |
| 304 | + except Exception as e: |
| 305 | + if 'syntax' in str(e).lower() or 'not supported' in str(e).lower(): |
| 306 | + pytest.skip(f'ColumnGroup syntax not supported: {e}') |
| 307 | + else: |
| 308 | + raise |
| 309 | + |
| 310 | + # Show the generated CREATE TABLE |
| 311 | + result = conn.execute(text(f'SHOW CREATE TABLE {table_name}')) |
| 312 | + create_sql = result.fetchone()[1] |
| 313 | + print(f'\nGenerated CREATE TABLE for {table_name}:') |
| 314 | + print(create_sql) |
| 315 | + |
| 316 | + # Verify reflection works |
| 317 | + metadata = MetaData() |
| 318 | + reflected_table = Table(table_name, metadata, autoload_with=test_engine) |
| 319 | + |
| 320 | + # Should have expected columns |
| 321 | + assert len(reflected_table.columns) == 4 |
| 322 | + assert set(col.name for col in reflected_table.columns) == { |
| 323 | + 'user_id', 'name', 'email', 'created_at', |
| 324 | + } |
| 325 | + |
| 326 | + def test_reflect_column_group_with_other_keys( |
| 327 | + self, test_engine: Any, clean_tables: Any, |
| 328 | + ) -> None: |
| 329 | + """Test reflection of ColumnGroup combined with other keys.""" |
| 330 | + table_name = 'test_column_group_complex_reflection' |
| 331 | + |
| 332 | + with test_engine.connect() as conn: |
| 333 | + try: |
| 334 | + with conn.begin(): |
| 335 | + # Create complex table with ColumnGroup and other keys |
| 336 | + conn.execute( |
| 337 | + text(f""" |
| 338 | + CREATE TABLE {table_name} ( |
| 339 | + user_id INT, |
| 340 | + doc_id INT, |
| 341 | + title VARCHAR(200), |
| 342 | + content TEXT, |
| 343 | + created_at TIMESTAMP, |
| 344 | + PRIMARY KEY (user_id, doc_id), |
| 345 | + SHARD KEY (user_id), |
| 346 | + SORT KEY (created_at), |
| 347 | + KEY idx_title (title), |
| 348 | + KEY cg_all_data ALL COLUMNS |
| 349 | + ) |
| 350 | + """), |
| 351 | + ) |
| 352 | + except Exception as e: |
| 353 | + if 'syntax' in str(e).lower() or 'not supported' in str(e).lower(): |
| 354 | + pytest.skip(f'ColumnGroup syntax not supported: {e}') |
| 355 | + else: |
| 356 | + raise |
| 357 | + |
| 358 | + # Show the generated CREATE TABLE |
| 359 | + result = conn.execute(text(f'SHOW CREATE TABLE {table_name}')) |
| 360 | + create_sql = result.fetchone()[1] |
| 361 | + print(f'\nGenerated CREATE TABLE for {table_name}:') |
| 362 | + print(create_sql) |
| 363 | + |
| 364 | + # Verify reflection works |
| 365 | + metadata = MetaData() |
| 366 | + reflected_table = Table(table_name, metadata, autoload_with=test_engine) |
| 367 | + |
| 368 | + # Should have expected columns |
| 369 | + assert len(reflected_table.columns) == 5 |
| 370 | + assert set(col.name for col in reflected_table.primary_key.columns) == { |
| 371 | + 'user_id', 'doc_id', |
| 372 | + } |
| 373 | + |
| 374 | + # Should have regular indexes |
| 375 | + indexes = reflected_table.indexes |
| 376 | + index_names = {idx.name for idx in indexes} |
| 377 | + print(f'\nReflected indexes: {index_names}') |
| 378 | + assert 'idx_title' in index_names |
| 379 | + |
| 380 | + |
234 | 381 | if __name__ == '__main__':
|
235 | 382 | pytest.main([__file__])
|
0 commit comments