Skip to content

Commit

Permalink
tests: Create fixture for common scafolding code
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasyager committed Oct 5, 2024
1 parent 8e44c03 commit b8316f6
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions tests/test_manifest_loaders.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -55,24 +56,20 @@ 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"])

output = ManifestLoader.load_from_http(file_config)

process.terminate()
path.unlink()

assert output == example_content

0 comments on commit b8316f6

Please sign in to comment.