|
27 | 27 | import firebird.driver as driver
|
28 | 28 | from firebird.driver.types import ImpData, ImpDataOld
|
29 | 29 | from firebird.driver import (NetProtocol, connect, Isolation, tpb, DefaultAction,
|
30 |
| - DbInfoCode, DbWriteMode, DbAccessMode, DbSpaceReservation) |
| 30 | + DbInfoCode, DbWriteMode, DbAccessMode, DbSpaceReservation, |
| 31 | + driver_config) |
31 | 32 |
|
32 | 33 | def test_connect_helper():
|
33 | 34 | DB_LINUX_PATH = '/path/to/db/employee.fdb'
|
@@ -376,3 +377,177 @@ def test_db_info(db_connection, fb_vars, db_file):
|
376 | 377 | guid = con.info.get_info(DbInfoCode.DB_GUID)
|
377 | 378 | assert isinstance(guid, str)
|
378 | 379 | assert len(guid) == 38 # Example check for {GUID} format
|
| 380 | + |
| 381 | +def test_connect_with_driver_config_server_defaults_local(driver_cfg, db_file, fb_vars): |
| 382 | + """ |
| 383 | + Tests connect() using driver_config.server_defaults for a local connection. |
| 384 | + The database alias registered for this test will have its 'server' attribute |
| 385 | + set to None, which means it should pick up settings from server_defaults. |
| 386 | + """ |
| 387 | + db_alias = "pytest_cfg_local_db" |
| 388 | + db_path_str = str(db_file) |
| 389 | + |
| 390 | + # Save original server_defaults to restore them, though driver_cfg fixture handles full reset |
| 391 | + original_s_host = driver_config.server_defaults.host.value |
| 392 | + original_s_port = driver_config.server_defaults.port.value |
| 393 | + original_s_user = driver_config.server_defaults.user.value |
| 394 | + original_s_password = driver_config.server_defaults.password.value |
| 395 | + |
| 396 | + # Configure server_defaults for a local connection |
| 397 | + driver_config.server_defaults.host.value = None |
| 398 | + driver_config.server_defaults.port.value = None # Explicitly None for local |
| 399 | + driver_config.server_defaults.user.value = fb_vars['user'] |
| 400 | + driver_config.server_defaults.password.value = fb_vars['password'] |
| 401 | + |
| 402 | + # Ensure the test-specific DB alias is clean if it exists from a prior failed run |
| 403 | + if driver_config.get_database(db_alias): |
| 404 | + driver_config.databases.value = [db_cfg for db_cfg in driver_config.databases.value if db_cfg.name != db_alias] |
| 405 | + |
| 406 | + # Register a database alias that will use these server_defaults |
| 407 | + test_db_config_entry = driver_config.register_database(db_alias) |
| 408 | + test_db_config_entry.database.value = db_path_str |
| 409 | + test_db_config_entry.server.value = None # Key: This tells driver to use server_defaults |
| 410 | + |
| 411 | + # For a local connection (host=None, port=None), DSN is just the database path |
| 412 | + expected_dsn = db_path_str |
| 413 | + |
| 414 | + conn = None |
| 415 | + try: |
| 416 | + conn = driver.connect(db_alias, charset='UTF8') |
| 417 | + assert conn._att is not None, "Connection attachment failed" |
| 418 | + assert conn.dsn == expected_dsn, f"Expected DSN '{expected_dsn}', got '{conn.dsn}'" |
| 419 | + |
| 420 | + # Verify connection is usable with a simple query |
| 421 | + with conn.cursor() as cur: |
| 422 | + cur.execute("SELECT 1 FROM RDB$DATABASE") |
| 423 | + assert cur.fetchone()[0] == 1, "Query failed on the connection" |
| 424 | + finally: |
| 425 | + if conn and not conn.is_closed(): |
| 426 | + conn.close() |
| 427 | + # Restore original server_defaults values (driver_cfg also handles full reset) |
| 428 | + driver_config.server_defaults.host.value = original_s_host |
| 429 | + driver_config.server_defaults.port.value = original_s_port |
| 430 | + driver_config.server_defaults.user.value = original_s_user |
| 431 | + driver_config.server_defaults.password.value = original_s_password |
| 432 | + |
| 433 | + |
| 434 | +def test_connect_with_driver_config_server_defaults_remote(driver_cfg, db_file, fb_vars): |
| 435 | + """ |
| 436 | + Tests connect() using driver_config.server_defaults for a remote-like connection. |
| 437 | + This test relies on fb_vars providing a host (and optionally port) from conftest.py. |
| 438 | + If no host is configured in fb_vars, this test variant is skipped. |
| 439 | + """ |
| 440 | + db_alias = "pytest_cfg_remote_db" |
| 441 | + db_path_str = str(db_file) |
| 442 | + |
| 443 | + test_host = fb_vars.get('host') |
| 444 | + test_port = fb_vars.get('port') # Can be None or empty string |
| 445 | + |
| 446 | + if not test_host: |
| 447 | + pytest.skip("Skipping remote server_defaults test as no host is configured in fb_vars. " |
| 448 | + "This test requires a configured host (and optionally port) for execution.") |
| 449 | + return |
| 450 | + |
| 451 | + # Save original server_defaults |
| 452 | + original_s_host = driver_config.server_defaults.host.value |
| 453 | + original_s_port = driver_config.server_defaults.port.value |
| 454 | + original_s_user = driver_config.server_defaults.user.value |
| 455 | + original_s_password = driver_config.server_defaults.password.value |
| 456 | + |
| 457 | + # Configure server_defaults for a "remote" connection |
| 458 | + driver_config.server_defaults.host.value = test_host |
| 459 | + driver_config.server_defaults.port.value = str(test_port) if test_port else None |
| 460 | + driver_config.server_defaults.user.value = fb_vars['user'] |
| 461 | + driver_config.server_defaults.password.value = fb_vars['password'] |
| 462 | + |
| 463 | + # Ensure the test-specific DB alias is clean |
| 464 | + if driver_config.get_database(db_alias): |
| 465 | + driver_config.databases.value = [db_cfg for db_cfg in driver_config.databases.value if db_cfg.name != db_alias] |
| 466 | + |
| 467 | + test_db_config_entry = driver_config.register_database(db_alias) |
| 468 | + test_db_config_entry.database.value = db_path_str |
| 469 | + test_db_config_entry.server.value = None # Use server_defaults |
| 470 | + |
| 471 | + # Determine expected DSN based on _connect_helper logic for non-protocol DSNs |
| 472 | + if test_host.startswith("\\\\"): # Windows Named Pipes |
| 473 | + if test_port: |
| 474 | + expected_dsn = f"{test_host}@{test_port}\\{db_path_str}" |
| 475 | + else: |
| 476 | + expected_dsn = f"{test_host}\\{db_path_str}" |
| 477 | + elif test_port: # TCP/IP with port |
| 478 | + expected_dsn = f"{test_host}/{test_port}:{db_path_str}" |
| 479 | + else: # TCP/IP without port (or other local-like with host) |
| 480 | + expected_dsn = f"{test_host}:{db_path_str}" |
| 481 | + |
| 482 | + conn = None |
| 483 | + try: |
| 484 | + conn = driver.connect(db_alias, charset='UTF8') |
| 485 | + assert conn._att is not None, "Connection attachment failed" |
| 486 | + assert conn.dsn == expected_dsn, f"Expected DSN '{expected_dsn}', got '{conn.dsn}'" |
| 487 | + |
| 488 | + with conn.cursor() as cur: |
| 489 | + cur.execute("SELECT 1 FROM RDB$DATABASE") |
| 490 | + assert cur.fetchone()[0] == 1, "Query failed on the connection" |
| 491 | + finally: |
| 492 | + if conn and not conn.is_closed(): |
| 493 | + conn.close() |
| 494 | + # Restore original server_defaults |
| 495 | + driver_config.server_defaults.host.value = original_s_host |
| 496 | + driver_config.server_defaults.port.value = original_s_port |
| 497 | + driver_config.server_defaults.user.value = original_s_user |
| 498 | + driver_config.server_defaults.password.value = original_s_password |
| 499 | + |
| 500 | +def test_connect_with_driver_config_db_defaults_local(driver_cfg, db_file, fb_vars): |
| 501 | + """ |
| 502 | + Tests connect() when db_defaults provides the database path, and |
| 503 | + server_defaults provides local connection info (host=None, port=None). |
| 504 | + Here, connect() is called with a DSN-like string that is *not* a registered alias. |
| 505 | + """ |
| 506 | + db_path_str = str(db_file) # This will be our "DSN" to connect to |
| 507 | + |
| 508 | + # Save original defaults |
| 509 | + original_s_host = driver_config.server_defaults.host.value |
| 510 | + original_s_port = driver_config.server_defaults.port.value |
| 511 | + original_s_user = driver_config.server_defaults.user.value |
| 512 | + original_s_password = driver_config.server_defaults.password.value |
| 513 | + original_db_database = driver_config.db_defaults.database.value |
| 514 | + original_db_server = driver_config.db_defaults.server.value |
| 515 | + |
| 516 | + |
| 517 | + # Configure server_defaults for local connection |
| 518 | + driver_config.server_defaults.host.value = None |
| 519 | + driver_config.server_defaults.port.value = None |
| 520 | + driver_config.server_defaults.user.value = fb_vars['user'] |
| 521 | + driver_config.server_defaults.password.value = fb_vars['password'] |
| 522 | + |
| 523 | + # Configure db_defaults (it won't be used for database path if DSN is absolute path) |
| 524 | + # but it's good to ensure it's set to something known for the test. |
| 525 | + # The key here is that if connect(db_path_str) is called and db_path_str is |
| 526 | + # an absolute path, it's treated as the DSN. Server info then comes from |
| 527 | + # server_defaults IF db_path_str is NOT a full DSN with host/port. |
| 528 | + # If db_path_str is an absolute path, it's treated as the direct database target. |
| 529 | + driver_config.db_defaults.database.value = "some_default_db_ignore" # Should not be used if DSN is absolute |
| 530 | + driver_config.db_defaults.server.value = None # Use server_defaults |
| 531 | + |
| 532 | + expected_dsn = db_path_str # For local connection with absolute path, DSN is the path |
| 533 | + |
| 534 | + conn = None |
| 535 | + try: |
| 536 | + # Connect using the absolute path as the DSN |
| 537 | + conn = driver.connect(db_path_str, charset='UTF8') |
| 538 | + assert conn._att is not None, "Connection attachment failed" |
| 539 | + assert conn.dsn == expected_dsn, f"Expected DSN '{expected_dsn}', got '{conn.dsn}'" |
| 540 | + |
| 541 | + with conn.cursor() as cur: |
| 542 | + cur.execute("SELECT 1 FROM RDB$DATABASE") |
| 543 | + assert cur.fetchone()[0] == 1, "Query failed on the connection" |
| 544 | + finally: |
| 545 | + if conn and not conn.is_closed(): |
| 546 | + conn.close() |
| 547 | + # Restore originals |
| 548 | + driver_config.server_defaults.host.value = original_s_host |
| 549 | + driver_config.server_defaults.port.value = original_s_port |
| 550 | + driver_config.server_defaults.user.value = original_s_user |
| 551 | + driver_config.server_defaults.password.value = original_s_password |
| 552 | + driver_config.db_defaults.database.value = original_db_database |
| 553 | + driver_config.db_defaults.server.value = original_db_server |
0 commit comments