|
6 | 6 |
|
7 | 7 | import pytest |
8 | 8 |
|
9 | | -try: |
10 | | - from snowflake.connector._utils import _CoreLoader, _TrackedQueryCancellationTimer |
11 | | -except ImportError: |
12 | | - pass |
| 9 | +from snowflake.connector._utils import ( |
| 10 | + _CoreLoader, |
| 11 | + _TrackedQueryCancellationTimer, |
| 12 | + build_minicore_usage_for_session, |
| 13 | + build_minicore_usage_for_telemetry, |
| 14 | +) |
13 | 15 |
|
14 | 16 | pytestmark = pytest.mark.skipolddriver |
15 | 17 |
|
@@ -476,3 +478,169 @@ def test_core_loader_fails_gracefully_on_incompatible_library(): |
476 | 478 | assert loader._error is not None |
477 | 479 | assert loader._version is None |
478 | 480 | assert "incompatible library version" in loader.get_load_error() |
| 481 | + |
| 482 | + |
| 483 | +class TestBuildMinicoreUsage: |
| 484 | + """Tests for build_minicore_usage_for_session and build_minicore_usage_for_telemetry functions.""" |
| 485 | + |
| 486 | + def test_build_minicore_usage_for_session_returns_expected_keys(self): |
| 487 | + """Test that build_minicore_usage_for_session returns dict with expected keys.""" |
| 488 | + result = build_minicore_usage_for_session() |
| 489 | + |
| 490 | + assert isinstance(result, dict) |
| 491 | + assert "ISA" in result |
| 492 | + assert "CORE_VERSION" in result |
| 493 | + assert "CORE_FILE_NAME" in result |
| 494 | + |
| 495 | + def test_build_minicore_usage_for_session_isa_matches_platform(self): |
| 496 | + """Test that ISA value matches platform.machine().""" |
| 497 | + import platform |
| 498 | + |
| 499 | + result = build_minicore_usage_for_session() |
| 500 | + |
| 501 | + assert result["ISA"] == platform.machine() |
| 502 | + |
| 503 | + def test_build_minicore_usage_for_session_with_mocked_core_loader(self): |
| 504 | + """Test build_minicore_usage_for_session with mocked core loader values.""" |
| 505 | + with mock.patch( |
| 506 | + "snowflake.connector._utils._core_loader.get_core_version", |
| 507 | + return_value="1.2.3", |
| 508 | + ): |
| 509 | + with mock.patch( |
| 510 | + "snowflake.connector._utils._core_loader.get_file_name", |
| 511 | + return_value="/path/to/lib.so", |
| 512 | + ): |
| 513 | + result = build_minicore_usage_for_session() |
| 514 | + |
| 515 | + assert result["CORE_VERSION"] == "1.2.3" |
| 516 | + assert result["CORE_FILE_NAME"] == "/path/to/lib.so" |
| 517 | + |
| 518 | + def test_build_minicore_usage_for_session_with_failed_load(self): |
| 519 | + """Test build_minicore_usage_for_session when core loading has failed.""" |
| 520 | + with mock.patch( |
| 521 | + "snowflake.connector._utils._core_loader.get_core_version", |
| 522 | + return_value=None, |
| 523 | + ): |
| 524 | + with mock.patch( |
| 525 | + "snowflake.connector._utils._core_loader.get_file_name", |
| 526 | + return_value=None, |
| 527 | + ): |
| 528 | + result = build_minicore_usage_for_session() |
| 529 | + |
| 530 | + assert result["CORE_VERSION"] is None |
| 531 | + assert result["CORE_FILE_NAME"] is None |
| 532 | + |
| 533 | + def test_build_minicore_usage_for_telemetry_returns_expected_keys(self): |
| 534 | + """Test that build_minicore_usage_for_telemetry returns dict with expected keys.""" |
| 535 | + result = build_minicore_usage_for_telemetry() |
| 536 | + |
| 537 | + assert isinstance(result, dict) |
| 538 | + # Telemetry-specific keys |
| 539 | + assert "OS" in result |
| 540 | + assert "OS_VERSION" in result |
| 541 | + assert "CORE_LOAD_ERROR" in result |
| 542 | + # Session keys (inherited) |
| 543 | + assert "ISA" in result |
| 544 | + assert "CORE_VERSION" in result |
| 545 | + assert "CORE_FILE_NAME" in result |
| 546 | + |
| 547 | + def test_build_minicore_usage_for_telemetry_os_matches_platform(self): |
| 548 | + """Test that OS value matches platform.system().""" |
| 549 | + import platform |
| 550 | + |
| 551 | + result = build_minicore_usage_for_telemetry() |
| 552 | + |
| 553 | + assert result["OS"] == platform.system() |
| 554 | + |
| 555 | + def test_build_minicore_usage_for_telemetry_os_version_matches_platform(self): |
| 556 | + """Test that OS_VERSION value matches platform.version().""" |
| 557 | + import platform |
| 558 | + |
| 559 | + result = build_minicore_usage_for_telemetry() |
| 560 | + |
| 561 | + assert result["OS_VERSION"] == platform.version() |
| 562 | + |
| 563 | + def test_build_minicore_usage_for_telemetry_includes_session_data(self): |
| 564 | + """Test that build_minicore_usage_for_telemetry includes all session data.""" |
| 565 | + with mock.patch( |
| 566 | + "snowflake.connector._utils._core_loader.get_core_version", |
| 567 | + return_value="2.0.0", |
| 568 | + ): |
| 569 | + with mock.patch( |
| 570 | + "snowflake.connector._utils._core_loader.get_file_name", |
| 571 | + return_value="/custom/path/lib.dylib", |
| 572 | + ): |
| 573 | + with mock.patch( |
| 574 | + "snowflake.connector._utils._core_loader.get_load_error", |
| 575 | + return_value="None", |
| 576 | + ): |
| 577 | + session_result = build_minicore_usage_for_session() |
| 578 | + telemetry_result = build_minicore_usage_for_telemetry() |
| 579 | + |
| 580 | + # All session keys should be present in telemetry result |
| 581 | + for key in session_result: |
| 582 | + assert key in telemetry_result |
| 583 | + assert telemetry_result[key] == session_result[key] |
| 584 | + |
| 585 | + def test_build_minicore_usage_for_telemetry_with_mocked_values(self): |
| 586 | + """Test build_minicore_usage_for_telemetry with mocked core loader values.""" |
| 587 | + with mock.patch( |
| 588 | + "snowflake.connector._utils._core_loader.get_core_version", |
| 589 | + return_value="3.0.0", |
| 590 | + ): |
| 591 | + with mock.patch( |
| 592 | + "snowflake.connector._utils._core_loader.get_file_name", |
| 593 | + return_value="/path/to/lib.dylib", |
| 594 | + ): |
| 595 | + with mock.patch( |
| 596 | + "snowflake.connector._utils._core_loader.get_load_error", |
| 597 | + return_value="None", |
| 598 | + ): |
| 599 | + result = build_minicore_usage_for_telemetry() |
| 600 | + |
| 601 | + assert result["CORE_VERSION"] == "3.0.0" |
| 602 | + assert result["CORE_FILE_NAME"] == "/path/to/lib.dylib" |
| 603 | + assert result["CORE_LOAD_ERROR"] == "None" |
| 604 | + |
| 605 | + def test_build_minicore_usage_for_telemetry_with_disabled_core(self): |
| 606 | + """Test build_minicore_usage_for_telemetry when core is disabled.""" |
| 607 | + with mock.patch( |
| 608 | + "snowflake.connector._utils._core_loader.get_core_version", |
| 609 | + return_value=None, |
| 610 | + ): |
| 611 | + with mock.patch( |
| 612 | + "snowflake.connector._utils._core_loader.get_file_name", |
| 613 | + return_value=None, |
| 614 | + ): |
| 615 | + with mock.patch( |
| 616 | + "snowflake.connector._utils._core_loader.get_load_error", |
| 617 | + return_value="mini-core-disabled", |
| 618 | + ): |
| 619 | + result = build_minicore_usage_for_telemetry() |
| 620 | + |
| 621 | + assert result["CORE_VERSION"] is None |
| 622 | + assert result["CORE_FILE_NAME"] is None |
| 623 | + assert result["CORE_LOAD_ERROR"] == "mini-core-disabled" |
| 624 | + # OS info should still be present |
| 625 | + assert result["OS"] is not None |
| 626 | + assert result["OS_VERSION"] is not None |
| 627 | + |
| 628 | + def test_build_minicore_usage_for_telemetry_with_load_error(self): |
| 629 | + """Test build_minicore_usage_for_telemetry when core loading has failed.""" |
| 630 | + with mock.patch( |
| 631 | + "snowflake.connector._utils._core_loader.get_core_version", |
| 632 | + return_value=None, |
| 633 | + ): |
| 634 | + with mock.patch( |
| 635 | + "snowflake.connector._utils._core_loader.get_file_name", |
| 636 | + return_value=None, |
| 637 | + ): |
| 638 | + with mock.patch( |
| 639 | + "snowflake.connector._utils._core_loader.get_load_error", |
| 640 | + return_value="Library not found", |
| 641 | + ): |
| 642 | + result = build_minicore_usage_for_telemetry() |
| 643 | + |
| 644 | + assert result["CORE_VERSION"] is None |
| 645 | + assert result["CORE_FILE_NAME"] is None |
| 646 | + assert result["CORE_LOAD_ERROR"] == "Library not found" |
0 commit comments