Skip to content

Commit

Permalink
YAML round-trip (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
kokorin authored Jul 7, 2024
1 parent d44c45b commit 5694472
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Inspired by [dbt-osmosis](https://z3z1ma.github.io/dbt-osmosis/)

### Botstrap DBT Resources

`dbt-osmosis` allows to create DBT YAML schema files for Seeds, Models and Snapshots. For that one has to add
`dbt-pumpkin` allows to create DBT YAML schema files for Seeds, Models and Snapshots. For that one has to add
[dbt-pumpking-path](#dbt-pumpkin-path) configuration property for a Resource. Then `dbt-pumpkin` will be able to
understand where YAML files should be located.

Expand Down Expand Up @@ -39,7 +39,7 @@ Options:

### Relocate DBT Resources

`dbt-osmosis` also allows to move DBT YAML schema files for Sources, Seeds, Models and Snapshots. As
`dbt-pumpkin` also allows to move DBT YAML schema files for Sources, Seeds, Models and Snapshots. As
with [bootstrap](#botstrap-dbt-resources) command [dbt-pumpking-path](#dbt-pumpkin-path) configuration property is
required for a Resource. Then `dbt-pumpkin` will be able to understand where YAML files should be located.

Expand Down
2 changes: 1 addition & 1 deletion dbt_pumpkin/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DiskStorage(Storage):
def __init__(self, root_dir: Path, *, read_only: bool):
self._root_dir = root_dir
self._read_only = read_only
self._yaml = YAML(typ="safe")
self._yaml = YAML(typ="rt")

def load_yaml(self, files: set[Path]) -> dict[Path, Any]:
result: dict[Path, Any] = {}
Expand Down
50 changes: 47 additions & 3 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import platform
import textwrap
from pathlib import Path

Expand All @@ -6,7 +7,7 @@
from dbt_pumpkin.storage import DiskStorage


def test_load_yaml(tmp_path):
def test_load_yaml(tmp_path: Path):
(tmp_path / "schema.yml").write_text(
textwrap.dedent("""\
version: 2
Expand All @@ -19,7 +20,7 @@ def test_load_yaml(tmp_path):
assert files == {Path("schema.yml"): {"version": 2, "models": [{"name": "my_model"}]}}


def test_save_yaml(tmp_path):
def test_save_yaml(tmp_path: Path):
DiskStorage(tmp_path, read_only=False).save_yaml(
{Path("schema.yml"): {"version": 2, "models": [{"name": "my_other_model"}]}}
)
Expand All @@ -28,9 +29,52 @@ def test_save_yaml(tmp_path):
assert actual == {"version": 2, "models": [{"name": "my_other_model"}]}


def test_save_yaml_read_only(tmp_path):
def test_save_yaml_read_only(tmp_path: Path):
DiskStorage(tmp_path, read_only=True).save_yaml(
{Path("schema.yml"): {"version": 2, "models": [{"name": "my_other_model"}]}}
)

assert not (tmp_path / "schema.yml").exists()


def test_roundtrip(tmp_path: Path):
content = textwrap.dedent("""\
version: 2
models:
# TODO rename it!
- name: my_model
description: my very first model
columns:
- name: id
data_type: short # or is it int actually?
- name: name
""")

(tmp_path / "my_model.yml").write_text(content)

storage = DiskStorage(tmp_path, read_only=False)
files = storage.load_yaml({Path("my_model.yml")})
storage.save_yaml(files)

actual = (tmp_path / "my_model.yml").read_text()

# Ruamel adds newlines after comments on Windows, but not on Ubuntu
# It should be fine as we don't re-write a file if there is nothing to change in it
if platform.system().lower() == "windows":
expected = textwrap.dedent("""\
version: 2
models:
# TODO rename it!
- name: my_model
description: my very first model
columns:
- name: id
data_type: short # or is it int actually?
- name: name
""")
else:
expected = content

assert expected == actual

0 comments on commit 5694472

Please sign in to comment.