Skip to content

Commit

Permalink
Merge pull request FRosner#131 from Gerrrr/issue/111
Browse files Browse the repository at this point in the history
Check if the ddq jar is available in pyddq
  • Loading branch information
FRosner authored Feb 6, 2017
2 parents edb03ee + 33e130f commit 7f07d21
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 26 deletions.
10 changes: 7 additions & 3 deletions python/pyddq/core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from reporters import ConsoleReporter
from streams import ByteArrayOutputStream
import jvm_conversions as jc
from pyddq.reporters import ConsoleReporter
from pyddq.streams import ByteArrayOutputStream
from pyddq.exceptions import JavaClassNotFoundException
import pyddq.jvm_conversions as jc

from pyspark import sql
import py4j.java_gateway as jg


class Check(object):
Expand Down Expand Up @@ -31,6 +33,8 @@ def __init__(self, dataFrame, displayName=None, cacheMethod=None,
self.jvmCheck = jvmCheck
else:
ddq_check = self._jvm.de.frosner.ddq.core.Check
if not isinstance(ddq_check, jg.JavaClass):
raise JavaClassNotFoundException("de.frosner.ddq.core.Check")
self.jvmCheck = ddq_check(
self._dataFrame._jdf,
self._jvm_display_name,
Expand Down
16 changes: 16 additions & 0 deletions python/pyddq/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class JavaClassNotFoundException(Exception):
"""
Raise if required Java class is not found by py4j
"""

def __init__(self, java_class):
Exception.__init__(self)
self.java_class = java_class

def __str__(self):
return "%s. Did you forget to add the jar to the class path?" % (
self.java_class
)

def __repr__(self):
return "%s: %s" % (self.__class__.__name__, self.java_class)
56 changes: 34 additions & 22 deletions python/tests/unit/test_check.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
import unittest
from mock import Mock
from mock import Mock, call
from pyddq.core import Check

from pyddq.exceptions import JavaClassNotFoundException
from utils import get_df

class ConstructorTest(unittest.TestCase):
def setUp(self):
self.df = get_df()

def test_default_args(self):
df = Mock()
check = Check(df)
ddq_check = check._jvm.de.frosner.ddq.core.Check
ddq_check.assert_called_with(
df._jdf,
getattr(ddq_check, "apply$default$2")(),
getattr(ddq_check, "apply$default$3")(),
getattr(ddq_check, "apply$default$4")(),
getattr(ddq_check, "apply$default$5")(),
)
check = Check(self.df)
ddq_core = check._jvm.de.frosner.ddq.core
ddq_check = ddq_core.Check

ddq_core.assert_has_calls([
call.Check(
self.df._jdf,
getattr(ddq_check, "apply$default$2")(),
getattr(ddq_check, "apply$default$3")(),
getattr(ddq_check, "apply$default$4")(),
getattr(ddq_check, "apply$default$5")()
)
])

def test_passed_args(self):
df = Mock()
display_name = Mock()
cache_method = Mock()
id = Mock()

df._sc._jvm.scala.Some.apply = Mock(
self.df._sc._jvm.scala.Some.apply = Mock(
side_effect=["Some(displayName)", "Some(cacheMethod)"]
)
check = Check(df, display_name, cache_method, id)
check = Check(self.df, display_name, cache_method, id)
ddq_core = check._jvm.de.frosner.ddq.core
ddq_check = check._jvm.de.frosner.ddq.core.Check

ddq_check.assert_called_with(
df._jdf,
"Some(displayName)",
"Some(cacheMethod)",
getattr(ddq_check, "apply$default$4")(),
id
)
ddq_core.assert_has_calls([
call.Check(
self.df._jdf,
"Some(displayName)",
"Some(cacheMethod)",
getattr(ddq_check, "apply$default$4")(),
id
)
])

def test_ddq_jar(self):
self.assertRaises(JavaClassNotFoundException, Check, Mock())

if __name__ == '__main__':
unittest.main()
5 changes: 4 additions & 1 deletion python/tests/unit/test_constraints.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import unittest
from mock import Mock
from py4j.java_gateway import JavaClass
from pyddq.core import Check
from utils import get_df


class ConstraintTest(unittest.TestCase):
COLUMN_NAME = "column name"

def setUp(self):
self.check = Check(Mock())
df = get_df()
self.check = Check(df)
self.jvmCheck = self.check.jvmCheck

def test_hasUniqueKey(self):
Expand Down
11 changes: 11 additions & 0 deletions python/tests/unit/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from mock import Mock
from py4j.java_gateway import JavaClass

def get_df():
df = Mock()
df._sc._jvm.de.frosner.ddq.core.Check = Mock(spec=JavaClass)
setattr(df._sc._jvm.de.frosner.ddq.core.Check, "apply$default$2", Mock())
setattr(df._sc._jvm.de.frosner.ddq.core.Check, "apply$default$3", Mock())
setattr(df._sc._jvm.de.frosner.ddq.core.Check, "apply$default$4", Mock())
setattr(df._sc._jvm.de.frosner.ddq.core.Check, "apply$default$5", Mock())
return df

0 comments on commit 7f07d21

Please sign in to comment.