Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support csv file fixtures for unit tests #9014

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20231106-194752.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add support of csv file fixtures to unit testing
time: 2023-11-06T19:47:52.501495-06:00
custom:
Author: emmyoop
Issue: "8290"
36 changes: 28 additions & 8 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,22 +787,40 @@
def rows(self) -> Union[str, List[Dict[str, Any]]]:
return []

@property
def fixture(self) -> Optional[str]: # TODO: typing
return None

Check warning on line 792 in core/dbt/contracts/graph/unparsed.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/unparsed.py#L792

Added line #L792 was not covered by tests

def get_rows(self) -> List[Dict[str, Any]]:
if self.format == UnitTestFormat.Dict:
assert isinstance(self.rows, List)
return self.rows
elif self.format == UnitTestFormat.CSV:
assert isinstance(self.rows, str)
dummy_file = StringIO(self.rows)
reader = csv.DictReader(dummy_file)
rows = []
for row in reader:
rows.append(row)
if self.fixture:
# TODO: need to add logic to parse csv file into rows list. this is the exact logis as inline csv for now?
assert isinstance(self.fixture, str)
dummy_file = StringIO(self.fixture)
reader = csv.DictReader(dummy_file)
rows = []
for row in reader:
rows.append(row)

Check warning on line 806 in core/dbt/contracts/graph/unparsed.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/unparsed.py#L806

Added line #L806 was not covered by tests
else: # using inline csv
assert isinstance(self.rows, str)
dummy_file = StringIO(self.rows)
reader = csv.DictReader(dummy_file)
rows = []
for row in reader:
rows.append(row)
return rows

def validate_fixture(self, fixture_type, test_name) -> None:
if (self.format == UnitTestFormat.Dict and not isinstance(self.rows, list)) or (
self.format == UnitTestFormat.CSV and not isinstance(self.rows, str)
if self.format == UnitTestFormat.Dict and not isinstance(self.rows, list):
raise ParsingError(

Check warning on line 818 in core/dbt/contracts/graph/unparsed.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/unparsed.py#L818

Added line #L818 was not covered by tests
f"Unit test {test_name} has {fixture_type} rows which do not match format {self.format}"
)

if self.format == UnitTestFormat.CSV and not (
isinstance(self.rows, str) or (self.fixture is not None)
):
raise ParsingError(
f"Unit test {test_name} has {fixture_type} rows which do not match format {self.format}"
Expand All @@ -814,12 +832,14 @@
input: str
rows: Union[str, List[Dict[str, Any]]] = ""
format: UnitTestFormat = UnitTestFormat.Dict
fixture: Optional[str] = None


@dataclass
class UnitTestOutputFixture(dbtClassMixin, UnitTestFixture):
rows: Union[str, List[Dict[str, Any]]] = ""
format: UnitTestFormat = UnitTestFormat.Dict
fixture: Optional[str] = None


@dataclass
Expand Down
Loading