0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 import unittest
0018
0019 from py4j.protocol import Py4JJavaError
0020
0021 from pyspark import keyword_only
0022 from pyspark.testing.utils import PySparkTestCase
0023
0024
0025 class KeywordOnlyTests(unittest.TestCase):
0026 class Wrapped(object):
0027 @keyword_only
0028 def set(self, x=None, y=None):
0029 if "x" in self._input_kwargs:
0030 self._x = self._input_kwargs["x"]
0031 if "y" in self._input_kwargs:
0032 self._y = self._input_kwargs["y"]
0033 return x, y
0034
0035 def test_keywords(self):
0036 w = self.Wrapped()
0037 x, y = w.set(y=1)
0038 self.assertEqual(y, 1)
0039 self.assertEqual(y, w._y)
0040 self.assertIsNone(x)
0041 self.assertFalse(hasattr(w, "_x"))
0042
0043 def test_non_keywords(self):
0044 w = self.Wrapped()
0045 self.assertRaises(TypeError, lambda: w.set(0, y=1))
0046
0047 def test_kwarg_ownership(self):
0048
0049 class Setter(object):
0050 @keyword_only
0051 def set(self, x=None, other=None, other_x=None):
0052 if "other" in self._input_kwargs:
0053 self._input_kwargs["other"].set(x=self._input_kwargs["other_x"])
0054 self._x = self._input_kwargs["x"]
0055
0056 a = Setter()
0057 b = Setter()
0058 a.set(x=1, other=b, other_x=2)
0059 self.assertEqual(a._x, 1)
0060 self.assertEqual(b._x, 2)
0061
0062
0063 class UtilTests(PySparkTestCase):
0064 def test_py4j_exception_message(self):
0065 from pyspark.util import _exception_message
0066
0067 with self.assertRaises(Py4JJavaError) as context:
0068
0069 self.sc._jvm.java.lang.String(None)
0070
0071 self.assertTrue('NullPointerException' in _exception_message(context.exception))
0072
0073 def test_parsing_version_string(self):
0074 from pyspark.util import VersionUtils
0075 self.assertRaises(ValueError, lambda: VersionUtils.majorMinorVersion("abced"))
0076
0077
0078 if __name__ == "__main__":
0079 from pyspark.tests.test_util import *
0080
0081 try:
0082 import xmlrunner
0083 testRunner = xmlrunner.XMLTestRunner(output='target/test-reports', verbosity=2)
0084 except ImportError:
0085 testRunner = None
0086 unittest.main(testRunner=testRunner, verbosity=2)