示例#1
0
def test_pickling():
    @parameterize
    def check(node_class, attribute, value, expected):
        """{0.__name__}.{1} pickling works as expected"""
        node = node_class()
        setattr(node, attribute, value)

        unpickled_node = _pickle_unpickle(node)

        if isinstance(expected, type) and issubclass(expected, Exception):
            assert_raises(expected, getattr, unpickled_node, attribute)
        else:
            value = getattr(unpickled_node, attribute)
            if callable(expected):
                assert expected(value)
            elif isinstance(expected, np.ndarray):
                assert vector.vector_eq(expected, value)
            else:
                assert expected == value

    # arguments: (node class, attribute, value to set,
    #             expected value/exception/test)
    yield check(vector.Input, 'name', 'constant',
                'constant')
    yield check(vector.Input, '_value', 42.0,
                42.0)
    yield check(vector.Input, '_value', DIRTY,
                DIRTY)
    yield check(vector.Input, '_value', np.array([42.0]),
                np.array([42.0]))
    yield check(vector.Input, 'last_timestamp', 1234,
                1234)
    yield check(vector.Input, 'extra_attribute', 42.0,
                AttributeError)

    yield check(vector.Node, 'name', 'constant',
                'constant')
    yield check(vector.Node, '_value', 42.0,
                42.0)
    yield check(vector.Node, '_value', np.array([42.0]),
                np.array([42.0]))
    yield check(vector.Node, '_action', sum,
                lambda _action: _action == sum)
    yield check(vector.Node, 'triggered', True,
                True)
    yield check(vector.Node, '_positional_inputs',
                (vector.Input(name='foo'),),
                (lambda _positional_inputs:
                 [n.name for n in _positional_inputs] == ['foo']))
    yield check(vector.Node, '_keyword_inputs',
                {'foo': vector.Input(name='bar')},
                (lambda _keyword_inputs:
                 {k: v.name for k, v in _keyword_inputs.items()}
                 == {'foo': 'bar'}))
    yield check(vector.Node, 'extra_attribute', 42.0,
                AttributeError)
示例#2
0
    def test_array_value(self):
        inp = vector.Input(value=np.array([1, 2]))

        eq_(None, inp.last_timestamp)
示例#3
0
    def test_scalar_value(self):
        inp = vector.Input(value=100000.0)

        eq_(None, inp.last_timestamp)
示例#4
0
    def test_set_value(self):
        inp = vector.Input()
        inp.value = pd.Series([1, 2], index=[1001, 1002])

        eq_(1002, inp.last_timestamp)
示例#5
0
    def test_initial_value(self):
        inp = vector.Input(value=pd.Series([1, 2], [1001, 1002]))

        eq_(1002, inp.last_timestamp)
示例#6
0
    def test_no_value(self):
        inp = vector.Input()

        eq_(None, inp.last_timestamp)
示例#7
0
    def test_dirty_value(self):
        inp = vector.Input(value=DIRTY)

        eq_(None, inp.last_timestamp)