Skip to content

Commit

Permalink
owfeatureconstructor: Add tests for FeatureFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-erjavec committed Jan 16, 2018
1 parent 2c3fd58 commit 794f5fa
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions Orange/widgets/data/tests/test_owfeatureconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import ast
import sys
import math
import pickle
import copy

import numpy as np

Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 794f5fa

Please sign in to comment.