Back to home page

OSCL-LXR

 
 

    


0001 #
0002 # Licensed to the Apache Software Foundation (ASF) under one or more
0003 # contributor license agreements.  See the NOTICE file distributed with
0004 # this work for additional information regarding copyright ownership.
0005 # The ASF licenses this file to You under the Apache License, Version 2.0
0006 # (the "License"); you may not use this file except in compliance with
0007 # the License.  You may obtain a copy of the License at
0008 #
0009 #    http://www.apache.org/licenses/LICENSE-2.0
0010 #
0011 # Unless required by applicable law or agreed to in writing, software
0012 # distributed under the License is distributed on an "AS IS" BASIS,
0013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 # See the License for the specific language governing permissions and
0015 # limitations under the License.
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         # test _input_kwargs is owned by each class instance and not a shared static variable
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             # This attempts java.lang.String(null) which throws an NPE.
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)