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

Add method to detect rows are written to the MockBackend #292

Merged
merged 14 commits into from
Sep 19, 2024
12 changes: 12 additions & 0 deletions src/databricks/labs/lsql/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ def rows_written_for(self, full_name: str, mode: str) -> list[DataclassInstance]
rows += stub_rows
return rows

def has_rows_written_for(self, full_name: str) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I think I came up with this name, I'm not sure if there's something better? What troubles me is that the name is misleading for when [] was written: the plain-language description of this situation is that the table has been written but doesn't contain any rows for the table, but this doesn't really match the function name.

Any ideas?

"""Check if the table has been written to.

This method allows to differentiate between "never written to the table" (returns False) and "zero rows written
to the table" (return True).
Otherwise, the check is the same as: `assert (not) mock_backend.rows_written_for(full_name, mode)`
"""
for stub_full_name, _, _ in self._save_table:
if stub_full_name == full_name:
return True
return False

@staticmethod
def rows(*column_names: str):
"""This method is used to create rows for the mock backend."""
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_backends.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a missing test for:

  • Write to a table (mode irrelevant)
  • Write to a table: mode=overwrite, rows=[]
  • Check that has-rows is true.

Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,43 @@ def test_mock_backend_overwrite():
]


@pytest.mark.parametrize("mode", ["append", "overwrite"])
def test_mock_backend_has_no_rows_written(mode) -> None:
mock_backend = MockBackend()
# There are no rows written
assert not mock_backend.has_rows_written_for("a.b.c")
# and the results contains no rows
assert not mock_backend.rows_written_for("a.b.c", mode)


@pytest.mark.parametrize("mode", ["append", "overwrite"])
def test_mock_backend_has_zero_rows_written(mode) -> None:
mock_backend = MockBackend()
mock_backend.save_table("a.b.c", [], Foo, mode)
# There are rows written
assert mock_backend.has_rows_written_for("a.b.c")
# while the results contains no rows
assert not mock_backend.rows_written_for("a.b.c", mode)


@pytest.mark.parametrize("mode", ["append", "overwrite"])
def test_mock_backend_has_rows_written_for_after_first_write(mode) -> None:
mock_backend = MockBackend()
mock_backend.save_table("a.b.c", [Foo("a1", True), Foo("c2", False)], Foo, mode)
assert mock_backend.has_rows_written_for("a.b.c")
assert not mock_backend.has_rows_written_for("a.b.d")


@pytest.mark.parametrize("first_mode", ["append", "overwrite"])
@pytest.mark.parametrize("second_mode", ["append", "overwrite"])
def test_mock_backend_has_rows_written_for_after_two_writes(first_mode, second_mode) -> None:
mock_backend = MockBackend()
mock_backend.save_table("a.b.c", [Foo("a1", True), Foo("c2", False)], Foo, first_mode)
mock_backend.save_table("a.b.c", [Foo("aaa", True), Foo("bbb", False)], Foo, second_mode)
assert mock_backend.has_rows_written_for("a.b.c")
assert not mock_backend.has_rows_written_for("a.b.d")


@dataclass
class Nested:
foo: Foo
Expand Down
Loading