|
29 | 29 | MF6_REPO = TEST_DFN_REPO.split("/")[1] |
30 | 30 | MF6_REF = TEST_DFN_REF |
31 | 31 |
|
| 32 | +# Path to cloned MF6 repository for autodiscovery (set by CI or local testing) |
| 33 | +# If set, use this instead of fetching individual DFN files |
| 34 | +TEST_DFN_PATH = os.getenv("TEST_DFN_PATH") |
| 35 | + |
32 | 36 |
|
33 | 37 | @pytest.fixture(scope="module") |
34 | 38 | def dfn_dir(): |
35 | | - """Ensure DFN files are downloaded for testing.""" |
| 39 | + """ |
| 40 | + Provide path to DFN files for testing. |
| 41 | +
|
| 42 | + Priority: |
| 43 | + 1. If TEST_DFN_PATH is set, use the DFN directory from a cloned MF6 repo (autodiscovery) |
| 44 | + 2. Otherwise, fetch individual DFN files to temp directory (legacy behavior) |
| 45 | +
|
| 46 | + The autodiscovery approach is preferred in CI to avoid needing registry files. |
| 47 | + """ |
| 48 | + # If TEST_DFN_PATH is set, use it (points to cloned MF6 DFN directory) |
| 49 | + if TEST_DFN_PATH: |
| 50 | + dfn_path = Path(TEST_DFN_PATH).expanduser().resolve() |
| 51 | + if not dfn_path.exists(): |
| 52 | + raise ValueError(f"TEST_DFN_PATH={TEST_DFN_PATH} does not exist") |
| 53 | + if not any(dfn_path.glob("*.dfn")): |
| 54 | + raise ValueError(f"No DFN files found in TEST_DFN_PATH={TEST_DFN_PATH}") |
| 55 | + return dfn_path |
| 56 | + |
| 57 | + # Fall back to fetching individual DFN files (legacy behavior for local development) |
36 | 58 | if not any(DFN_DIR.glob("*.dfn")): |
37 | 59 | fetch_dfns(MF6_OWNER, MF6_REPO, MF6_REF, DFN_DIR, verbose=True) |
38 | 60 | return DFN_DIR |
@@ -731,3 +753,95 @@ def test_get_sync_status(self): |
731 | 753 | assert isinstance(status, dict) |
732 | 754 | # All refs should be either True or False |
733 | 755 | assert all(isinstance(v, bool) for v in status.values()) |
| 756 | + |
| 757 | + |
| 758 | +@requires_pkg("boltons", "pydantic") |
| 759 | +class TestGetRegistryWithPath: |
| 760 | + """Tests for get_registry() with path parameter.""" |
| 761 | + |
| 762 | + def test_get_registry_with_path_returns_local_registry(self, dfn_dir): |
| 763 | + """Test that get_registry with path returns LocalDfnRegistry.""" |
| 764 | + from modflow_devtools.dfns.registry import LocalDfnRegistry, get_registry |
| 765 | + |
| 766 | + registry = get_registry(path=dfn_dir) |
| 767 | + |
| 768 | + assert isinstance(registry, LocalDfnRegistry) |
| 769 | + assert registry.path == dfn_dir.resolve() |
| 770 | + |
| 771 | + def test_get_registry_with_path_and_metadata(self, dfn_dir): |
| 772 | + """Test that source/ref metadata is preserved with path.""" |
| 773 | + from modflow_devtools.dfns.registry import get_registry |
| 774 | + |
| 775 | + registry = get_registry(path=dfn_dir, source="test", ref="local") |
| 776 | + |
| 777 | + assert registry.source == "test" |
| 778 | + assert registry.ref == "local" |
| 779 | + |
| 780 | + def test_get_registry_without_path_returns_remote_registry(self): |
| 781 | + """Test that get_registry without path still returns RemoteDfnRegistry.""" |
| 782 | + from modflow_devtools.dfns.registry import RemoteDfnRegistry, get_registry |
| 783 | + |
| 784 | + registry = get_registry(source="modflow6", ref="develop", auto_sync=False) |
| 785 | + |
| 786 | + assert isinstance(registry, RemoteDfnRegistry) |
| 787 | + |
| 788 | + |
| 789 | +@requires_pkg("boltons", "pydantic") |
| 790 | +class TestConvenienceFunctionsWithPath: |
| 791 | + """Tests for convenience functions with path parameter.""" |
| 792 | + |
| 793 | + def test_get_dfn_with_path(self, dfn_dir): |
| 794 | + """Test get_dfn() with path parameter.""" |
| 795 | + from modflow_devtools.dfns import get_dfn |
| 796 | + |
| 797 | + dfn = get_dfn("gwf-chd", path=dfn_dir) |
| 798 | + |
| 799 | + assert dfn.name == "gwf-chd" |
| 800 | + assert dfn.parent == "gwf-nam" |
| 801 | + |
| 802 | + def test_get_dfn_path_with_path(self, dfn_dir): |
| 803 | + """Test get_dfn_path() with path parameter.""" |
| 804 | + from modflow_devtools.dfns import get_dfn_path |
| 805 | + |
| 806 | + file_path = get_dfn_path("gwf-chd", path=dfn_dir) |
| 807 | + |
| 808 | + assert file_path.exists() |
| 809 | + assert file_path.name == "gwf-chd.dfn" |
| 810 | + |
| 811 | + def test_list_components_with_path(self, dfn_dir): |
| 812 | + """Test list_components() with path parameter.""" |
| 813 | + from modflow_devtools.dfns import list_components |
| 814 | + |
| 815 | + components = list_components(path=dfn_dir) |
| 816 | + |
| 817 | + assert len(components) > 100 |
| 818 | + assert "gwf-chd" in components |
| 819 | + |
| 820 | + |
| 821 | +@requires_pkg("boltons", "pydantic") |
| 822 | +def test_autodiscovery_workflow(dfn_dir): |
| 823 | + """Test complete autodiscovery workflow.""" |
| 824 | + from modflow_devtools.dfns import get_dfn, get_registry, list_components |
| 825 | + |
| 826 | + # Get registry pointing at local directory |
| 827 | + registry = get_registry(path=dfn_dir, ref="local") |
| 828 | + |
| 829 | + # List components |
| 830 | + components = list(registry.spec.keys()) |
| 831 | + assert len(components) > 100 |
| 832 | + |
| 833 | + # Get specific DFN |
| 834 | + gwf_chd = registry.get_dfn("gwf-chd") |
| 835 | + assert gwf_chd.name == "gwf-chd" |
| 836 | + assert gwf_chd.blocks is not None |
| 837 | + |
| 838 | + # Get file path |
| 839 | + chd_path = registry.get_dfn_path("gwf-chd") |
| 840 | + assert chd_path.exists() |
| 841 | + |
| 842 | + # Use convenience functions |
| 843 | + components_list = list_components(path=dfn_dir) |
| 844 | + assert "gwf-chd" in components_list |
| 845 | + |
| 846 | + dfn = get_dfn("gwf-wel", path=dfn_dir) |
| 847 | + assert dfn.name == "gwf-wel" |
0 commit comments