|
23 | 23 | SQLITE_URL = "sqlite+aiosqlite:///test.db" |
24 | 24 | DB_URL_ENV_KEY = f"{PACKAGE_NAME.upper()}_DB_URL" |
25 | 25 | DB_POOL_SIZE_ENV_KEY = f"{PACKAGE_NAME.upper()}_DB_POOL_SIZE" |
| 26 | +ENV_FILE_ENV_KEY = f"{PACKAGE_NAME.upper()}_ENV_FILE" |
26 | 27 | ERROR_MSG_FRAGMENT = "set_context" |
27 | 28 | VCS_REF_VALUE = "abc123" |
28 | 29 | VCS_REF_OVERRIDE = "ci-override-ref" |
@@ -675,6 +676,78 @@ def test_from_package_called_twice_is_safe() -> None: |
675 | 676 | assert result.returncode == 0, result.stderr |
676 | 677 |
|
677 | 678 |
|
| 679 | +@pytest.mark.integration |
| 680 | +def test_from_package_sets_database_when_db_url_only_in_env_file(tmp_path: Path) -> None: |
| 681 | + """from_package() populates database when DB_URL is only in a .env file, not in OS env.""" |
| 682 | + env_file = tmp_path / ".env" |
| 683 | + env_file.write_text(f"{DB_URL_ENV_KEY}={SQLITE_URL}\n") |
| 684 | + |
| 685 | + script = textwrap.dedent(f""" |
| 686 | + from aignostics_foundry_core.foundry import FoundryContext |
| 687 | + ctx = FoundryContext.from_package("{PACKAGE_NAME}") |
| 688 | + assert ctx.database is not None, "database should not be None" |
| 689 | + assert ctx.database.get_url() == "{SQLITE_URL}", f"expected {SQLITE_URL!r}, got {{ctx.database.get_url()!r}}" |
| 690 | + """) |
| 691 | + env = os.environ.copy() |
| 692 | + env.pop(DB_URL_ENV_KEY, None) |
| 693 | + env[ENV_FILE_ENV_KEY] = str(env_file) |
| 694 | + result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, env=env, check=False) |
| 695 | + assert result.returncode == 0, result.stderr |
| 696 | + |
| 697 | + |
| 698 | +@pytest.mark.integration |
| 699 | +def test_from_package_database_is_none_when_db_url_absent_from_env_file(tmp_path: Path) -> None: |
| 700 | + """from_package() sets database=None when DB_URL is absent from both OS env and .env file.""" |
| 701 | + env_file = tmp_path / ".env" |
| 702 | + env_file.write_text("SOME_OTHER_KEY=value\n") |
| 703 | + |
| 704 | + script = textwrap.dedent(f""" |
| 705 | + from aignostics_foundry_core.foundry import FoundryContext |
| 706 | + ctx = FoundryContext.from_package("{PACKAGE_NAME}") |
| 707 | + assert ctx.database is None, "database should be None when DB_URL is absent" |
| 708 | + """) |
| 709 | + env = os.environ.copy() |
| 710 | + env.pop(DB_URL_ENV_KEY, None) |
| 711 | + env[ENV_FILE_ENV_KEY] = str(env_file) |
| 712 | + result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, env=env, check=False) |
| 713 | + assert result.returncode == 0, result.stderr |
| 714 | + |
| 715 | + |
| 716 | +@pytest.mark.integration |
| 717 | +def test_from_package_reads_pool_settings_from_env_file(tmp_path: Path) -> None: |
| 718 | + """from_package() reads pool_size from .env file when DB_URL is also in the .env file.""" |
| 719 | + env_file = tmp_path / ".env" |
| 720 | + env_file.write_text(f"{DB_URL_ENV_KEY}={SQLITE_URL}\n{DB_POOL_SIZE_ENV_KEY}=3\n") |
| 721 | + |
| 722 | + script = textwrap.dedent(f""" |
| 723 | + from aignostics_foundry_core.foundry import FoundryContext |
| 724 | + ctx = FoundryContext.from_package("{PACKAGE_NAME}") |
| 725 | + assert ctx.database is not None, "database should not be None" |
| 726 | + assert ctx.database.pool_size == 3, f"expected pool_size=3, got {{ctx.database.pool_size}}" |
| 727 | + """) |
| 728 | + env = os.environ.copy() |
| 729 | + env.pop(DB_URL_ENV_KEY, None) |
| 730 | + env.pop(DB_POOL_SIZE_ENV_KEY, None) |
| 731 | + env[ENV_FILE_ENV_KEY] = str(env_file) |
| 732 | + result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, env=env, check=False) |
| 733 | + assert result.returncode == 0, result.stderr |
| 734 | + |
| 735 | + |
| 736 | +@pytest.mark.integration |
| 737 | +def test_from_package_sets_database_when_db_url_in_os_env_and_env_file_absent(tmp_path: Path) -> None: |
| 738 | + """from_package() populates database when DB_URL is in OS env and no .env file is set — regression.""" |
| 739 | + script = textwrap.dedent(f""" |
| 740 | + from aignostics_foundry_core.foundry import FoundryContext |
| 741 | + ctx = FoundryContext.from_package("{PACKAGE_NAME}") |
| 742 | + assert ctx.database is not None, "database should not be None when DB_URL is in OS env" |
| 743 | + """) |
| 744 | + env = os.environ.copy() |
| 745 | + env[DB_URL_ENV_KEY] = SQLITE_URL |
| 746 | + env.pop(ENV_FILE_ENV_KEY, None) |
| 747 | + result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True, env=env, check=False) |
| 748 | + assert result.returncode == 0, result.stderr |
| 749 | + |
| 750 | + |
678 | 751 | @pytest.mark.integration |
679 | 752 | def test_make_context_without_prior_from_package() -> None: |
680 | 753 | """Constructing FoundryContext directly (no from_package()) has database=None.""" |
|
0 commit comments