From b8316f6c820e8799d68c2575cacfacb9784ad9ac Mon Sep 17 00:00:00 2001 From: Nicholas Yager Date: Sat, 5 Oct 2024 15:01:33 -0400 Subject: [PATCH] tests: Create fixture for common scafolding code --- tests/test_manifest_loaders.py | 39 ++++++++++++++++------------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/tests/test_manifest_loaders.py b/tests/test_manifest_loaders.py index ef6b07e..3c7f5bb 100644 --- a/tests/test_manifest_loaders.py +++ b/tests/test_manifest_loaders.py @@ -1,49 +1,50 @@ import json from pathlib import Path import subprocess +from typing import Dict, Generator, Tuple from pydantic import AnyUrl import pytest from dbt_loom.config import FileReferenceConfig from dbt_loom.manifests import ManifestLoader, UnknownManifestPathType -def test_load_from_local_filesystem_pass(): - """Test that ManifestLoader can load a local JSON file.""" - +@pytest.fixture +def example_file() -> Generator[Tuple[Path, Dict], None, None]: example_content = {"foo": "bar"} path = Path("example.json") - with open(path, "w") as file: json.dump(example_content, file) + yield path, example_content + path.unlink() + + +def test_load_from_local_filesystem_pass(example_file): + """Test that ManifestLoader can load a local JSON file.""" + + path, example_content = example_file file_config = FileReferenceConfig( path=AnyUrl("file://" + str(Path(path).absolute())) ) output = ManifestLoader.load_from_local_filesystem(file_config) - path.unlink() assert output == example_content -def test_load_from_local_filesystem_local_path(): +def test_load_from_local_filesystem_local_path(example_file): """Test that ManifestLoader can load a local JSON file.""" - example_content = {"foo": "bar"} - path = Path("example.json") - - with open(path, "w") as file: - json.dump(example_content, file) + path, example_content = example_file file_config = FileReferenceConfig(path=str(path)) # type: ignore output = ManifestLoader.load_from_local_filesystem(file_config) - path.unlink() assert output == example_content -def test_load_from_path_fails_invalid_scheme(): +def test_load_from_path_fails_invalid_scheme(example_file): """ est that ManifestLoader will raise the appropriate exception if an invalid scheme is applied. @@ -55,17 +56,14 @@ def test_load_from_path_fails_invalid_scheme(): ManifestLoader.load_from_path(file_config) -def test_load_from_remote_pass(): +def test_load_from_remote_pass(example_file): """Test that ManifestLoader can load a remote JSON file via HTTP(S).""" - example_content = {"foo": "bar"} - path = Path("example3.json") - base_url = "http://127.0.0.1:8000" + path, example_content = example_file - with open(path, "w") as file: - json.dump(example_content, file) + base_url = "http://127.0.0.1:8000" - file_config = FileReferenceConfig(path=AnyUrl(f"{base_url}/example3.json")) + file_config = FileReferenceConfig(path=AnyUrl(f"{base_url}/{path}")) # Invoke a server for hosting the test file. process = subprocess.Popen(["python3", "-m", "http.server", "8000"]) @@ -73,6 +71,5 @@ def test_load_from_remote_pass(): output = ManifestLoader.load_from_http(file_config) process.terminate() - path.unlink() assert output == example_content