diff --git a/Orange/widgets/data/tests/test_owfeatureconstructor.py b/Orange/widgets/data/tests/test_owfeatureconstructor.py index e1adb72dbb4..a2929d830a9 100644 --- a/Orange/widgets/data/tests/test_owfeatureconstructor.py +++ b/Orange/widgets/data/tests/test_owfeatureconstructor.py @@ -2,6 +2,8 @@ import ast import sys import math +import pickle +import copy import numpy as np @@ -14,9 +16,11 @@ construct_variables, OWFeatureConstructor, FeatureEditor, DiscreteFeatureEditor) -from Orange.widgets.data.owfeatureconstructor import freevars, validate_exp +from Orange.widgets.data.owfeatureconstructor import ( + freevars, validate_exp, FeatureFunc +) -import dill as pickle # Import dill after Orange because patched +import dill # Import dill after Orange because patched class FeatureConstructorTest(unittest.TestCase): @@ -105,9 +109,9 @@ def nested_func(x, local_const=7): return x * local_const * NONLOCAL_CONST * self.CLASS_CONST * GLOBAL_CONST self.assertEqual(lambda_func(11), - pickle.loads(pickle.dumps(lambda_func))(11)) + dill.loads(dill.dumps(lambda_func))(11)) self.assertEqual(nested_func(11), - pickle.loads(pickle.dumps(nested_func))(11)) + dill.loads(dill.dumps(nested_func))(11)) class TestTools(unittest.TestCase): @@ -218,6 +222,30 @@ def validate_(source): validate_("{a:1 for a in s}") +class FeatureFuncTest(unittest.TestCase): + def test_reconstruct(self): + f = FeatureFunc("a * x + c", [("x", "x")], {"a": 2, "c": 10}) + self.assertEqual(f({"x": 2}), 14) + f1 = pickle.loads(pickle.dumps(f)) + self.assertEqual(f1({"x": 2}), 14) + fc = copy.copy(f) + self.assertEqual(fc({"x": 3}), 16) + + def test_repr(self): + self.assertEqual(repr(FeatureFunc("a + 1", [("a", 2)])), + "FeatureFunc('a + 1', [('a', 2)], {})") + + def test_call(self): + f = FeatureFunc("a + 1", [("a", "a")]) + self.assertEqual(f({"a": 2}), 3) + + iris = Table("iris") + f = FeatureFunc("sepal_width + 10", + [("sepal_width", iris.domain["sepal width"])]) + r = f(iris) + np.testing.assert_array_equal(r, iris.X[:, 1] + 10) + + class OWFeatureConstructorTests(WidgetTest): def setUp(self): self.widget = OWFeatureConstructor()