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

added support to where method and tests #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions pydeequ/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def addConstraint(self, constraint):
self.constraints.append(constraint)
self._Check = constraint._Check

def where(self, filter: str):
test = self._Check.getClass()
if self._Check.getClass().toString().endswith("CheckWithLastConstraintFilterable"):
choyrim marked this conversation as resolved.
Show resolved Hide resolved
self._Check = self._Check.where(filter)
else:
raise TypeError(f"Expected CheckWithLastConstraintFilterable class, not {self._Check.getClass()}")
return self

def addFilterableContstraint(self, creationFunc):
"""Adds a constraint that can subsequently be replaced with a filtered version
:param creationFunc:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ def hasNumberOfDistinctValues(self, column, assertion, binningUdf, maxBins, hint
df = VerificationResult.checkResultsAsDataFrame(self.spark, result)
return df.select("constraint_status").collect()

def where(self, assertion, filter, hint=None):
check = Check(self.spark, CheckLevel.Warning, "test where")
result = VerificationSuite(self.spark).onData(self.df).addCheck(check.hasSize(assertion, hint).where(filter)).run()
df = VerificationResult.checkResultsAsDataFrame(self.spark, result)
return df.select("constraint_status").collect()

def test_hasSize(self):
self.assertEqual(self.hasSize(lambda x: x == 3.0), [Row(constraint_status="Success")])
self.assertEqual(
Expand Down Expand Up @@ -1245,6 +1251,30 @@ def test_fail_isGreaterThanOrEqualTo(self):
)
self.assertEqual(self.isGreaterThanOrEqualTo("h", "f", lambda x: x == 1), [Row(constraint_status="Success")])

def test_where(self):
self.assertEqual(self.where(lambda x: x == 2.0, "boolean='true'", "column 'boolean' has two values true"),
[Row(constraint_status="Success")])
self.assertEqual(
self.where(lambda x: x == 3.0, "d=5", "column 'd' has three values 3"),
[Row(constraint_status="Success")],
)
self.assertEqual(
self.where(lambda x: x == 2.0, "ssn='000-00-0000'", "column 'ssn' has one value 000-00-0000"),
[Row(constraint_status="Failure")],
)

@pytest.mark.xfail(reason="@unittest.expectedFailure")
def test_fail_where(self):
self.assertEqual(self.where(lambda x: x == 2.0, "boolean='false'", "column 'boolean' has one value false"),
[Row(constraint_status="Success")])
self.assertEqual(
self.where(lambda x: x == 3.0, "a='bar'", "column 'a' has one value 'bar'"),
[Row(constraint_status="Success")],
)
self.assertEqual(
self.where(lambda x: x == 1.0, "f=1", "column 'f' has one value 1"),
[Row(constraint_status="Failure")],
)
# def test_hasNumberOfDistinctValues(self):
# #Todo: test binningUDf
# self.assertEqual(self.hasNumberOfDistinctValues('b', lambda x: x == 3, None, 3, "Column B has 3 distinct values"),
Expand Down