-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from woonstadrotterdam/feature/referentiedata-test
ReferentiedataTest
- Loading branch information
Showing
8 changed files
with
2,486 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,3 +164,5 @@ cython_debug/ | |
.python-version | ||
lcov.info | ||
_version.py | ||
.DS_store | ||
tutorial.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .referentiedata import ReferentiedataTest | ||
|
||
__all__ = ["ReferentiedataTest"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import csv | ||
from typing import Literal, Optional | ||
|
||
from testframework.dataquality.tests import ValidCategory | ||
|
||
|
||
class ReferentiedataTest(ValidCategory): # type: ignore | ||
""" | ||
Initialize a ReferentiedataTest instance. | ||
Args: | ||
name (Optional[str]): The name of the test. If not provided, defaults to "VERAStandaard". | ||
soort (str): The type/category of the data, which will be converted to uppercase. | ||
attribuut (Literal["Code", "Naam"]): The attribute to use, either "Code" or "Naam". It will be capitalized. | ||
Raises: | ||
TypeError: If soort is not a string. | ||
ValueError: If attribuut is not "Code" or "Naam". | ||
""" | ||
|
||
with open( | ||
"src/vera_testframework/data/Referentiedata.csv", newline="", encoding="utf-8" | ||
) as csvfile: | ||
referentiedata = [row for row in csv.DictReader(csvfile, delimiter=";")] | ||
|
||
def __init__( | ||
self, | ||
*, | ||
name: Optional[str] = None, | ||
soort: str, | ||
attribuut: Literal["Code", "Naam"], | ||
): | ||
if not isinstance(soort, str): | ||
raise TypeError("soort must be a string") | ||
if attribuut not in ["Code", "Naam"]: | ||
raise ValueError("attribuut must be either 'Code' or 'Naam'") | ||
|
||
self.soort = soort.upper() | ||
self.attribuut = attribuut.capitalize() | ||
|
||
name = name if name else "VERAStandaard" | ||
super().__init__(name=name, categories=self._categorieen()) | ||
|
||
def _categorieen(self) -> set[str]: | ||
categorieen_rows = [ | ||
row for row in self.referentiedata if row["Soort"] == self.soort | ||
] | ||
if not categorieen_rows: | ||
mogelijke_soorten = {row["Soort"] for row in self.referentiedata} | ||
raise ValueError( | ||
f"Geen soorten gevonden voor soort '{self.soort}'. Opties zijn: {', '.join(sorted(mogelijke_soorten))}" | ||
) | ||
|
||
return {row[self.attribuut] for row in categorieen_rows} | ||
|
||
def __str__(self) -> str: | ||
return f"ReferentiedataTest({self.soort}, {self.attribuut})" | ||
|
||
def __repr__(self) -> str: | ||
return self.__str__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +0,0 @@ | ||
import pytest | ||
|
||
|
||
def pytest_collection_modifyitems(config, items): | ||
if not items: | ||
pytest.exit("No tests found.", returncode=0) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import pytest | ||
from pyspark.sql import SparkSession | ||
|
||
# Adjust the import according to your module structure | ||
from vera_testframework.pyspark import ReferentiedataTest | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def spark(): | ||
return SparkSession.builder.master("local[2]").appName("pytest").getOrCreate() | ||
|
||
|
||
@pytest.fixture | ||
def ruimten_df(spark): | ||
ruimten = [ | ||
(1, "LOG", "Loggia"), | ||
(2, "WOO", "Woonkamer"), | ||
(3, "BAD", "Badruimte"), | ||
(4, "BAD", "Badkamer"), | ||
(5, None, "Kelder"), | ||
(6, "SLA", None), | ||
] | ||
return spark.createDataFrame(ruimten, ["id", "code", "naam"]) | ||
|
||
|
||
def test_referentiedata_valid_code(ruimten_df): | ||
test = ReferentiedataTest(soort="RUIMTEDETAILSOORT", attribuut="Code") | ||
|
||
# Get valid codes from referentiedata in the test object | ||
valid_codes = set( | ||
row["Code"] | ||
for row in test.referentiedata | ||
if row["Soort"] == "RUIMTEDETAILSOORT" | ||
) | ||
|
||
# Apply the test | ||
result_df = test.test(ruimten_df, "code", "id", False) | ||
|
||
# Collect the results | ||
results = result_df.select("code", "code__VERAStandaard").collect() | ||
for row in results: | ||
if row["code"] is not None: | ||
assert (row["code"] in valid_codes) == row["code__VERAStandaard"] | ||
else: | ||
assert row["code__VERAStandaard"] is False | ||
|
||
|
||
def test_referentiedata_valid_naam(ruimten_df): | ||
test = ReferentiedataTest(soort="RUIMTEDETAILSOORT", attribuut="Naam") | ||
|
||
# Get valid names from referentiedata in the test object | ||
valid_namen = set( | ||
row["Naam"] | ||
for row in test.referentiedata | ||
if row["Soort"] == "RUIMTEDETAILSOORT" | ||
) | ||
|
||
# Apply the test | ||
result_df = test.test(ruimten_df, "naam", "id", False) | ||
|
||
# Collect the results | ||
results = result_df.select("naam", "naam__VERAStandaard").collect() | ||
for row in results: | ||
if row["naam"] is not None: | ||
assert (row["naam"] in valid_namen) == row["naam__VERAStandaard"] | ||
else: | ||
assert row["naam__VERAStandaard"] is False | ||
|
||
|
||
def test_referentiedata_invalid_soort(): | ||
with pytest.raises(ValueError): | ||
ReferentiedataTest(soort="INVALID", attribuut="Code") | ||
|
||
|
||
def test_referentiedata_invalid_attribuut(): | ||
with pytest.raises(ValueError): | ||
ReferentiedataTest(soort="RUIMTEDETAILSOORT", attribuut="InvalidAttribuut") | ||
|
||
|
||
def test_wrong_type_soort(): | ||
with pytest.raises(TypeError): | ||
ReferentiedataTest(soort=123, attribuut="Code") | ||
|
||
|
||
def test_str_and_repr(): | ||
assert ( | ||
str(ReferentiedataTest(soort="RUIMTEDETAILSOORT", attribuut="Code")) | ||
== "ReferentiedataTest(RUIMTEDETAILSOORT, Code)" | ||
) | ||
assert ( | ||
repr(ReferentiedataTest(soort="RUIMTEDETAILSOORT", attribuut="Code")) | ||
== "ReferentiedataTest(RUIMTEDETAILSOORT, Code)" | ||
) |
Oops, something went wrong.