Exemple #1
0
def test_syn_types_functionality():
    c1 = STT1(a=3)
    n = SynTypesTest(c1, a=1, b=2.3)
    n.validate()

    assert not is_hashable(n)
    assert is_hashable(hashable(n))
Exemple #2
0
def test_type():
    t = Type(1)
    assert t.obj == 1
    assert t.rstr() == '1'
    assert t.hashable() is t.obj

    class Foo(object):
        __hash__ = None

    f = Foo()
    f.a = 1

    g = Foo()
    g.a = 2

    assert not is_hashable(f)
    assert is_hashable(hashable(f))
    assert_inequivalent(hashable(f), hashable(g))

    dct = serialize(f)
    assert dct[SER_KEYS.attrs]['a'] == 1
    
    assert t.find_ne(0) == NotEqual(1, 0)
    assert list(t.visit(0)) == [1]
    assert t.visit_len() == 1

    t = Type.type_dispatch(Foo)
    assert_raises(NotImplementedError, t._enumeration_value, 1)
    assert_raises(NotImplementedError, t._generate)

    assert TYPE_REGISTRY[object] is Type

    assert isinstance(find_ne(1, 1.2), DifferentTypes)
Exemple #3
0
def test_none():
    n = None
    t = Type.dispatch(n)
    assert isinstance(t, NONE)
    assert type(t) is NONE

    assert t._find_ne(None, None) is None
    assert t._find_ne(1, None) is None

    assert hashable(n) is t.hashable() is n
    assert is_hashable(n)
    assert is_hashable(hashable(n))
    assert deserialize(serialize(n)) is n
    assert deserialize(serialize(type(None))) is type(None)
    assert primitive_form(None) is None

    val = NONE.generate()
    assert val is None
    assert rstr(n) == 'None'
    assert eval(estr(n)) is n

    for item in enumerate_(type(None), max_enum=10):
        assert item is None

    assert list(visit(None)) == [None]
    assert find_ne(None, None) is None
    assert isinstance(find_ne(None, 1), DifferentTypes)
Exemple #4
0
def test_none():
    n = None
    t = Type.dispatch(n)
    assert isinstance(t, NONE)
    assert type(t) is NONE

    assert t._find_ne(None, None) is None
    assert t._find_ne(1, None) is None

    assert hashable(n) is t.hashable() is n
    assert is_hashable(n)
    assert is_hashable(hashable(n))
    assert deserialize(serialize(n)) is n
    assert deserialize(serialize(type(None))) is type(None)
    assert primitive_form(None) is None

    val = NONE.generate()
    assert val is None
    assert rstr(n) == 'None'
    assert eval(estr(n)) is n

    for item in enumerate_(type(None), max_enum=10):
        assert item is None

    assert list(visit(None)) == [None]
    assert find_ne(None, None) is None
    assert isinstance(find_ne(None, 1), DifferentTypes)
Exemple #5
0
def test_syn_types_functionality():
    c1 = STT1(a=3)
    n = SynTypesTest(c1, a=1, b=2.3)
    n.validate()

    assert not is_hashable(n)
    assert is_hashable(hashable(n))
Exemple #6
0
def test_type():
    t = Type(1)
    assert t.obj == 1
    assert t.rstr() == '1'
    assert t.hashable() is t.obj

    class Foo(object):
        __hash__ = None

    f = Foo()
    f.a = 1

    g = Foo()
    g.a = 2

    assert not is_hashable(f)
    assert is_hashable(hashable(f))
    assert_inequivalent(hashable(f), hashable(g))

    dct = serialize(f)
    assert dct[SER_KEYS.attrs]['a'] == 1

    assert t.find_ne(0) == NotEqual(1, 0)
    assert list(t.visit(0)) == [1]
    assert t.visit_len() == 1

    t = Type.type_dispatch(Foo)
    assert_raises(NotImplementedError, t._enumeration_value, 1)
    assert_raises(NotImplementedError, t._generate)

    assert TYPE_REGISTRY[object] is Type

    assert isinstance(find_ne(1, 1.2), DifferentTypes)
Exemple #7
0
def test_sequence():
    l = [1, 2.3, 'abc']
    t = Type.dispatch(l)
    assert isinstance(t, Sequence)
    assert type(t) is List
    if PY2:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'__builtin__.list', 1, 2.3, 'abc'}
    else:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'builtins.list', 1, 2.3, 'abc'}

    assert not is_hashable(l)
    assert is_hashable(hashable(l))

    l1 = [1, 2, 3]
    l2 = [1, 2, 3, 4]
    l3 = [1, 2, 4]
    assert find_ne(l1, l2) == DifferentLength(l1, l2)
    assert find_ne(l2, l1) == DifferentLength(l2, l1)
    assert find_ne(l1, l3) == DiffersAtIndex(l1, l3, 2)

    e1 = eval(estr(l1))
    assert_equivalent(e1, l1)

    tup = tuple(l)
    examine_sequence(List, l)
    examine_sequence(Tuple, tup)

    examine_sequence(Tuple, ([
        -1839677305294322342, b'', b'\x05l\xbf', b'0\xcfXp\xaa',
        -8468204163727415930
    ], True))

    for cls in subclasses(Sequence):
        for k in xrange(SAMPLES):
            val = cls.generate()
            with on_error(elog, examine_sequence, (cls, val)):
                hangwatch(1, examine_sequence, cls, val)

        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item

        assert is_unique(buf)

    assert list(visit(l, enumerate=True)) == [(0, 1), (1, 2.3), (2, 'abc')]
    assert list(visit([])) == []

    l = [1, 2, (3, 4)]
    assert primitive_form(l) == [1, 2, [3, 4]]
    assert collect(l) == primitive_form(l)
Exemple #8
0
def test_sequence():
    l = [1, 2.3, 'abc']
    t = Type.dispatch(l)
    assert isinstance(t, Sequence)
    assert type(t) is List
    if PY2:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'__builtin__.list', 1, 2.3, 'abc'}
    else:
        assert set(hashable(l)) == set(t.hashable()) == \
            {'builtins.list', 1, 2.3, 'abc'}

    assert not is_hashable(l)
    assert is_hashable(hashable(l))

    l1 = [1, 2, 3]
    l2 = [1, 2, 3, 4]
    l3 = [1, 2, 4]
    assert find_ne(l1, l2) == DifferentLength(l1, l2)
    assert find_ne(l2, l1) == DifferentLength(l2, l1)
    assert find_ne(l1, l3) == DiffersAtIndex(l1, l3, 2)

    e1 = eval(estr(l1))
    assert_equivalent(e1, l1)

    tup = tuple(l)
    examine_sequence(List, l)
    examine_sequence(Tuple, tup)

    examine_sequence(Tuple, ([-1839677305294322342, b'', b'\x05l\xbf', 
                              b'0\xcfXp\xaa', -8468204163727415930], True))

    for cls in subclasses(Sequence):
        for k in xrange(SAMPLES):
            val = cls.generate()
            with on_error(elog, examine_sequence, (cls, val)):
                hangwatch(1, examine_sequence, cls, val)

        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item

        assert is_unique(buf)

    assert list(visit(l, enumerate=True)) == [(0, 1), (1, 2.3), (2, 'abc')]
    assert list(visit([])) == []

    l = [1, 2, (3, 4)]
    assert primitive_form(l) == [1, 2, [3, 4]]
    assert collect(l) == primitive_form(l)
Exemple #9
0
def test_normal_type():
    b = Bar(1, 2.3)
    b2 = Bar(1, 2.4)
    b3 = Bar(2, 2.3)

    assert b != b2
    assert_equivalent(Bar(1, 2.3), Bar(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    # Is evaluable, but not correct, because we haven't given the
    # types system the proper information for this class
    e1 = eval(estr(b))
    assert b != e1

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert attrs(b) == ['a', 'b']
    assert rstr(b).startswith('<{} object at '.format(get_fullname(Bar)))

    sval =  deserialize(serialize(b))
    assert_equivalent(b, sval)
    assert deep_feq(b, sval)

    assert primitive_form(Bar) is Bar
    assert primitive_form(b) == dict(a=1, b=2.3)

    b4 = Bar(2, b2)
    assert primitive_form(b4) == dict(a=2, b=dict(a=1, b=2.4))

    assert collect(b4) == primitive_form(b4)
    
    def dothing(obj):
        if isinstance(obj, collections.Mapping):
            return safe_sorted(obj.values())
        return safe_sorted(obj)
    assert collect(b4, dothing) in [[2, [1, 2.4]],
                                    [2, [2.4, 1]],
                                    [[1, 2.4], 2],
                                    [[2.4, 1], 2]]

    # Because the types system knows nothing of the Bar class
    assert_raises(NotImplementedError, generate, Bar)
    assert_raises(NotImplementedError, list, enum(Bar, max_enum=50))
Exemple #10
0
def test_normal_type():
    b = Bar(1, 2.3)
    b2 = Bar(1, 2.4)
    b3 = Bar(2, 2.3)

    assert b != b2
    assert_equivalent(Bar(1, 2.3), Bar(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    # Is evaluable, but not correct, because we haven't given the
    # types system the proper information for this class
    e1 = eval(estr(b))
    assert b != e1

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert attrs(b) == ['a', 'b']
    assert rstr(b).startswith('<{} object at '.format(get_fullname(Bar)))

    sval = deserialize(serialize(b))
    assert_equivalent(b, sval)
    assert deep_feq(b, sval)

    assert primitive_form(Bar) is Bar
    assert primitive_form(b) == dict(a=1, b=2.3)

    b4 = Bar(2, b2)
    assert primitive_form(b4) == dict(a=2, b=dict(a=1, b=2.4))

    assert collect(b4) == primitive_form(b4)

    def dothing(obj):
        if isinstance(obj, collections.Mapping):
            return safe_sorted(obj.values())
        return safe_sorted(obj)

    assert collect(b4, dothing) in [[2, [1, 2.4]], [2, [2.4, 1]], [[1, 2.4],
                                                                   2],
                                    [[2.4, 1], 2]]

    # Because the types system knows nothing of the Bar class
    assert_raises(NotImplementedError, generate, Bar)
    assert_raises(NotImplementedError, list, enum(Bar, max_enum=50))
Exemple #11
0
def test_syn_types_functionality():
    s = SynTypesTest(a=1, b=1.2, c='abc')
    s2 = SynTypesTest(a=1, b=1.2, c='abcd')

    assert not is_hashable(s)
    assert is_hashable(hashable(s))

    assert find_ne(s, s) is None
    assert find_ne(s, s2) == DiffersAtAttribute(s, s2, 'c')

    e1 = eval(estr(s))
    assert_equivalent(e1, s)

    assert list(visit(s)) == [('a', 1), ('b', 1.2), ('c', 'abc')]
    assert rstr(s) == "SynTypesTest(a = 1, b = 1.2, c = 'abc')"

    assert attrs(s) == ['a', 'b', 'c']
    assert pairs(s) == [('a', 1), ('b', 1.2), ('c', 'abc')]

    sval = deserialize(serialize(s))
    assert_equivalent(sval, s)
    assert deep_feq(sval, s)

    assert primitive_form(s) == dict(a=1, b=1.2, c='abc')
    assert primitive_form(SynTypesTest) is SynTypesTest

    assert SynTypesTest is deserialize(serialize(SynTypesTest))

    val = generate(SynTypesTest)
    assert type(val) is SynTypesTest

    buf = []
    last = None
    for item in enum(SynTypesTest, max_enum=SAMPLES * 10, step=100):
        assert type(item) is SynTypesTest
        assert item != last
        buf.append(item)
        last = item

    assert enumeration_value(SynTypesTest, 0) == \
        first(enum(SynTypesTest, max_enum=1))
    assert is_unique(buf)

    val = generate(SynTypesTest2)
    assert type(val) is not SynTypesTest2
    assert type(val) is SynTypesTest

    item = first(enum(SynTypesTest3, max_enum=1))
    assert type(item) is SynTypesTest3
    assert not hasattr(item, 'd')
Exemple #12
0
def test_string():
    s = u'abc'

    t = Type.dispatch(s)
    assert isinstance(t, String)

    if PY2:
        assert type(t) is Unicode
        examine_string(Unicode, s)
    else:
        assert type(t) is String
        examine_string(String, s)

    assert hashable(s) == t.hashable() == s
    assert is_hashable(s)
    assert is_hashable(hashable(s))

    assert find_ne('abc', 'abcd') == DifferentLength('abc', 'abcd')
    assert find_ne('abcd', 'abc') == DifferentLength('abcd', 'abc')
    assert find_ne('abc', 'abd') == DiffersAtIndex('abc', 'abd', 2)

    for cls in subclasses(String, [String]):
        if cls.type is None or cls is Basestring:
            continue

        for k in xrange(SAMPLES):
            val = generate(cls.type)
            with on_error(elog, examine_string, (cls, val)):
                examine_string(cls, val)

        x = 0
        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item
            x += 100

        assert is_unique(buf)

    # estr potential edge cases

    cases = [
        "abc'de\r7fghi", "\x00", "\\", "\'", '\"', '\a', '\b', '\t', '\v',
        u'\u2013', '\\u2'
    ]
    for case in cases:
        assert eval(estr(case)) == case
Exemple #13
0
def test_syn_types_functionality():
    s = SynTypesTest(a=1, b=1.2, c='abc')
    s2 = SynTypesTest(a=1, b=1.2, c='abcd')

    assert not is_hashable(s)
    assert is_hashable(hashable(s))

    assert find_ne(s, s) is None
    assert find_ne(s, s2) == DiffersAtAttribute(s, s2, 'c')

    e1 = eval(estr(s))
    assert_equivalent(e1, s)

    assert list(visit(s)) == [('a', 1), ('b', 1.2), ('c', 'abc')]
    assert rstr(s) == "SynTypesTest(a = 1, b = 1.2, c = 'abc')"

    assert attrs(s) == ['a', 'b', 'c']
    assert pairs(s) == [('a', 1), ('b', 1.2), ('c', 'abc')]

    sval = deserialize(serialize(s))
    assert_equivalent(sval, s)
    assert deep_feq(sval, s)

    assert primitive_form(s) == dict(a=1, b=1.2, c='abc')
    assert primitive_form(SynTypesTest) is SynTypesTest

    assert SynTypesTest is deserialize(serialize(SynTypesTest))

    val = generate(SynTypesTest)
    assert type(val) is SynTypesTest

    buf = []
    last = None
    for item in enum(SynTypesTest,  max_enum=SAMPLES * 10, step=100):
        assert type(item) is SynTypesTest
        assert item != last
        buf.append(item)
        last = item

    assert enumeration_value(SynTypesTest, 0) == \
        first(enum(SynTypesTest, max_enum=1))
    assert is_unique(buf)

    val = generate(SynTypesTest2)
    assert type(val) is not SynTypesTest2
    assert type(val) is SynTypesTest

    item = first(enum(SynTypesTest3, max_enum=1))
    assert type(item) is SynTypesTest3
    assert not hasattr(item, 'd')
Exemple #14
0
def test_custom_type():
    b = Baz(1, 2.3)
    b2 = Baz(1, 2.4)
    b3 = Baz(2, 2.3)

    assert b != b2
    assert_equivalent(Baz(1, 2.3), Baz(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    e1 = eval(estr(b))
    assert_equivalent(e1, b)

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert rstr(b) == 'Baz(1,2.3)'

    assert attrs(b) == ['a', 'b']

    sval = deserialize(serialize(b))
    assert_equivalent(sval, b)
    assert deep_feq(sval, b)

    assert Baz is deserialize(serialize(Baz))
    assert primitive_form(Baz) is Baz
    assert primitive_form(b) == dict(a=1, b=2.3)

    val = generate(Baz)
    assert type(val) is Baz
    assert isinstance(val.a, int)
    assert isinstance(val.b, float)

    buf = []
    last = None
    for item in enum(Baz, max_enum=SAMPLES * 10, step=100):
        assert type(item) is Baz
        assert item != last
        buf.append(item)
        last = item

    assert is_unique(buf)
Exemple #15
0
def test_string():
    s = u'abc'

    t = Type.dispatch(s)
    assert isinstance(t, String)

    if PY2:
        assert type(t) is Unicode
        examine_string(Unicode, s)
    else:
        assert type(t) is String
        examine_string(String, s)

    assert hashable(s) == t.hashable() == s
    assert is_hashable(s)
    assert is_hashable(hashable(s))

    assert find_ne('abc', 'abcd') == DifferentLength('abc', 'abcd')
    assert find_ne('abcd', 'abc') == DifferentLength('abcd', 'abc')
    assert find_ne('abc', 'abd') == DiffersAtIndex('abc', 'abd', 2)

    for cls in subclasses(String, [String]):
        if cls.type is None or cls is Basestring:
            continue

        for k in xrange(SAMPLES):
            val = generate(cls.type)
            with on_error(elog, examine_string, (cls, val)):
                examine_string(cls, val)

        x = 0
        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item
            x += 100

        assert is_unique(buf)

    # estr potential edge cases

    cases = ["abc'de\r7fghi", "\x00", "\\", "\'", '\"', '\a', '\b', '\t', '\v',
             u'\u2013', '\\u2']
    for case in cases:
        assert eval(estr(case)) == case
Exemple #16
0
def test_custom_type():
    b = Baz(1, 2.3)
    b2 = Baz(1, 2.4)
    b3 = Baz(2, 2.3)

    assert b != b2
    assert_equivalent(Baz(1, 2.3), Baz(1, 2.3))

    if PY3:
        assert not is_hashable(b)
    else:
        assert is_hashable(b)
    assert is_hashable(hashable(b))

    assert find_ne(b, b) is None
    assert find_ne(b, b2) == DiffersAtAttribute(b, b2, 'b')
    assert find_ne(b, b3) == DiffersAtAttribute(b, b3, 'a')

    e1 = eval(estr(b))
    assert_equivalent(e1, b)

    assert list(visit(b)) == [('a', 1), ('b', 2.3)]
    assert rstr(b) == 'Baz(1,2.3)'

    assert attrs(b) == ['a', 'b']

    sval = deserialize(serialize(b))
    assert_equivalent(sval, b)
    assert deep_feq(sval, b)

    assert Baz is deserialize(serialize(Baz))
    assert primitive_form(Baz) is Baz
    assert primitive_form(b) == dict(a=1, b=2.3)

    val = generate(Baz)
    assert type(val) is Baz
    assert isinstance(val.a, int)
    assert isinstance(val.b, float)

    buf = []
    last = None
    for item in enum(Baz,  max_enum=SAMPLES * 10, step=100):
        assert type(item) is Baz
        assert item != last
        buf.append(item)
        last = item
        
    assert is_unique(buf)
Exemple #17
0
def test_schema_attrs():
    obj = SchemaTest([1, 2.3], [1, [2.3]], 5)
    obj2 = SchemaTest([1, 2.3], [1, [2.4]], 5)

    assert is_hashable(hashable(obj))

    assert find_ne(obj, obj) is None
    assert find_ne(obj, obj2) == DiffersAtAttribute(obj, obj2, 'b')

    e1 = eval(estr(obj))
    assert_equivalent(e1, obj)

    assert list(visit(obj)) == [('a', [1, 2.3]), ('b', [1, [2.3]]), ('c', 5)]
    assert rstr(obj) == "SchemaTest(a = [1, 2.3], b = [1, [2.3]], c = 5)"

    sval = deserialize(serialize(obj))
    assert_equivalent(sval, obj)
    assert deep_feq(sval, obj)

    val = generate(SchemaTest)
    assert type(val) is SchemaTest

    assert_raises(TypeError, SchemaTest, [1], [2.3], 5)
    assert_raises(TypeError, SchemaTest, [11, 2.3], [1, [2.3]], 5)
    assert_raises(TypeError, SchemaTest, [1, 2.3], [1, [2.3]], 9)
Exemple #18
0
def test_yatr():
    yatr = Yatr('foo bar')
    assert yatr.name == 'foo bar'
    assert yatr.order == Yatr.order
    assert is_hashable(yatr)

    assert not yatr.check()
    assert not yatr.installed()

    with assign(depd, 'command', MagicMock()):
        ops = yatr.satisfy()
        assert len(ops) == 1

        Operation.optimize(ops)
        for op in ops:
            op.execute()
        assert depd.command.call_count == 1
        depd.command.assert_called_with('yatr foo bar')


    yatr2 = Yatr.from_conf({'bar baz':
                            {'order': -30,
                             'yatrfile': '/foo/bar.yml'}})
    assert yatr2.order == -30

    with assign(depd, 'command', MagicMock()):
        ops = yatr2.satisfy()
        assert len(ops) == 1

        Operation.optimize(ops)
        for op in ops:
            op.execute()
        assert depd.command.call_count == 1
        depd.command.assert_called_with('yatr -f /foo/bar.yml bar baz')
Exemple #19
0
def test_dependency():
    assert depd.DEPENDENCY_KEYS == dict(apt = Apt,
                                        pip = Pip,
                                        yatr = Yatr)
    assert depd.DEPENDENCY_ORDERS == dict(apt = Apt.order,
                                          pip = Pip.order,
                                          yatr = Yatr.order)
    

    dep = Dependency('foo')
    assert dep.name == 'foo'
    assert not dep.installed()
    assert not dep.check()
    assert_raises(NotImplementedError, dep.satisfy)

    assert_equivalent(Dependency.from_conf('foo'), dep)
    assert_raises(TypeError, Dependency.from_conf, [])
    assert_raises(TypeError, Dependency.from_conf, dict(a = 1, b = 2))

    class Foo(Dependency):
        _attrs = dict(a = Attr(int),
                      b = Attr(int))
        
    assert_equivalent(Foo.from_conf(dict(foo = dict(a = 1, b = 2))),
                      Foo('foo', a = 1, b = 2))

    f = Foo('bar', a = 1, b = 2, order=5)
    assert is_hashable(f)
Exemple #20
0
def test_schema_attrs():
    obj = SchemaTest([1, 2.3], [1, [2.3]], 5)
    obj2 = SchemaTest([1, 2.3], [1, [2.4]], 5)

    assert is_hashable(hashable(obj))
    
    assert find_ne(obj, obj) is None
    assert find_ne(obj, obj2) == DiffersAtAttribute(obj, obj2, 'b')

    e1 = eval(estr(obj))
    assert_equivalent(e1, obj)

    assert list(visit(obj)) == [('a', [1, 2.3]), ('b', [1, [2.3]]), ('c', 5)]
    assert rstr(obj) == "SchemaTest(a = [1, 2.3], b = [1, [2.3]], c = 5)"

    sval = deserialize(serialize(obj))
    assert_equivalent(sval, obj)
    assert deep_feq(sval, obj)

    val = generate(SchemaTest)
    assert type(val) is SchemaTest

    assert_raises(TypeError, SchemaTest, [1], [2.3], 5)
    assert_raises(TypeError, SchemaTest, [11, 2.3], [1, [2.3]], 5)
    assert_raises(TypeError, SchemaTest, [1, 2.3], [1, [2.3]], 9)
Exemple #21
0
def test_rand_hashable():
    from syn.base_utils import rand_hashable

    for k in xrange(SAMPLES):
        x = rand_hashable()

        with on_error(elog, is_hashable, (x,)):
            assert is_hashable(x)
Exemple #22
0
def test_custom_object():
    f = Foo(1, 1.2)
    f2 = Foo(1, 1.3)
    f3 = Foo(2, 1.2)

    assert f != f2
    assert_equivalent(Foo(1, 2.3), Foo(1, 2.3))

    assert not is_hashable(f)
    assert is_hashable(hashable(f))

    assert find_ne(f, f) is None
    assert find_ne(f, f2) == DiffersAtAttribute(f, f2, 'b')
    assert find_ne(f, f3) == DiffersAtAttribute(f, f3, 'a')

    e1 = eval(estr(f))
    assert_equivalent(e1, f)

    assert list(visit(f)) == [('a', 1), ('b', 1.2)]
    assert rstr(f) == 'Foo(1,1.2)'

    assert attrs(f) == ['a', 'b']
    assert pairs(f) == [('a', 1), ('b', 1.2)]

    sval = deserialize(serialize(f))
    assert_equivalent(sval, f)
    assert deep_feq(sval, f)

    assert Foo is deserialize(serialize(Foo))
    assert primitive_form(Foo) is Foo
    assert primitive_form(f) == dict(a=1, b=1.2)
    assert collect(f) == primitive_form(f)

    val = generate(Foo)
    assert type(val) is Foo

    buf = []
    last = None
    for item in enum(Foo,  max_enum=SAMPLES * 10, step=100):
        assert type(item) is Foo
        assert item != last
        buf.append(item)
        last = item

    assert enumeration_value(Foo, 0) == first(enum(Foo, max_enum=1))
    assert is_unique(buf)
Exemple #23
0
def test_custom_object():
    f = Foo(1, 1.2)
    f2 = Foo(1, 1.3)
    f3 = Foo(2, 1.2)

    assert f != f2
    assert_equivalent(Foo(1, 2.3), Foo(1, 2.3))

    assert not is_hashable(f)
    assert is_hashable(hashable(f))

    assert find_ne(f, f) is None
    assert find_ne(f, f2) == DiffersAtAttribute(f, f2, 'b')
    assert find_ne(f, f3) == DiffersAtAttribute(f, f3, 'a')

    e1 = eval(estr(f))
    assert_equivalent(e1, f)

    assert list(visit(f)) == [('a', 1), ('b', 1.2)]
    assert rstr(f) == 'Foo(1,1.2)'

    assert attrs(f) == ['a', 'b']
    assert pairs(f) == [('a', 1), ('b', 1.2)]

    sval = deserialize(serialize(f))
    assert_equivalent(sval, f)
    assert deep_feq(sval, f)

    assert Foo is deserialize(serialize(Foo))
    assert primitive_form(Foo) is Foo
    assert primitive_form(f) == dict(a=1, b=1.2)
    assert collect(f) == primitive_form(f)

    val = generate(Foo)
    assert type(val) is Foo

    buf = []
    last = None
    for item in enum(Foo, max_enum=SAMPLES * 10, step=100):
        assert type(item) is Foo
        assert item != last
        buf.append(item)
        last = item

    assert enumeration_value(Foo, 0) == first(enum(Foo, max_enum=1))
    assert is_unique(buf)
Exemple #24
0
def test_mapping():
    d = dict(a = 1, b = 2.3)
    t = Type.dispatch(d)
    assert isinstance(t, Mapping)
    assert type(t) is Dict
    if PY2:
        assert set(hashable(d)) == set(t.hashable()) == \
            {'__builtin__.dict', ('a', 1), ('b', 2.3)}
    else:
        assert set(hashable(d)) == set(t.hashable()) == \
            {'builtins.dict', ('a', 1), ('b', 2.3)}

    d1 = dict(a=1, b=2)
    d2 = dict(a=1, b=2, c=3)
    d3 = dict(a=1, b=3)
    assert find_ne(d1, d2) == KeyDifferences(d1, d2)
    assert find_ne(d2, d1) == KeyDifferences(d2, d1)
    assert find_ne(d1, d3) == DiffersAtKey(d1, d3, 'b')

    e1 = eval(estr(d1))
    assert_equivalent(e1, d1)

    assert not is_hashable(d)
    assert is_hashable(hashable(d))
    examine_mapping(Dict, d)

    for cls in subclasses(Mapping):
        for k in xrange(SAMPLES):
            val = cls.generate()
            with on_error(elog, examine_mapping, (cls, val)):
                hangwatch(1, examine_mapping, cls, val)

        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item

        assert is_unique(buf)

    d = dict(a=1, b=[1, 2, (3, 4)])
    assert primitive_form(d) == dict(a=1, b=[1, 2, [3, 4]])
    assert collect(d) == primitive_form(d)
Exemple #25
0
def examine_set(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val)
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == safe_sorted(list(val))
    assert find_ne(val, val) is None
Exemple #26
0
def examine_sequence(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val) or deep_feq(collect(sval, ss), collect(val, ss))
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None
Exemple #27
0
def examine_mapping(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val) or deep_feq(collect(sval, ss), collect(val, ss))
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == safe_sorted(list(val.items()))
    assert find_ne(val, val) is None
Exemple #28
0
def examine_sequence(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val) or deep_feq(collect(sval, ss), collect(val, ss))
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None
Exemple #29
0
def examine_set(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    sval = deserialize(serialize(val))
    assert deep_feq(sval, val)
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)

    assert list(visit(val)) == safe_sorted(list(val))
    assert find_ne(val, val) is None
Exemple #30
0
def test_type():
    t = Type()

    assert t == Type()
    assert t != 1
    assert is_hashable(t)
    assert_raises(NotImplementedError, t.check, 1)
    assert_raises(NotImplementedError, t.coerce, 1)
    assert_raises(NotImplementedError, t.display)
    assert_raises(NotImplementedError, t.enumeration_value, 1)
    assert_raises(NotImplementedError, t.generate)
    assert_raises(NotImplementedError, t.rst)
    assert_raises(NotImplementedError, t.validate, 1)
Exemple #31
0
def test_set():
    s = frozenset([1, 2.3, 'abc'])
    t = Type.dispatch(s)
    assert isinstance(t, Set)
    assert type(t) is FrozenSet

    assert hashable(s) == t.hashable() == s
    assert is_hashable(s)
    assert is_hashable(hashable(s))

    s1 = {1, 2, 3}
    s2 = {2, 3, 4}
    assert find_ne(s1, s2) == SetDifferences(s1, s2)

    e1 = eval(estr(s1))
    assert_equivalent(e1, s1)

    examine_set(Set, set(s))
    examine_set(FrozenSet, s)

    for cls in subclasses(Set, [Set]):
        for k in xrange(SAMPLES):
            val = cls.generate()
            with on_error(elog, examine_set, (cls, val)):
                hangwatch(1, examine_set, cls, val)

        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item

        assert is_unique(buf)

    s = {1, 2, (3, 4)}
    assert primitive_form(s) == [1, 2, [3, 4]]
    assert collect(s) == primitive_form(s)
Exemple #32
0
def test_type():
    t = Type()

    assert t == Type()
    assert t != 1
    assert is_hashable(t)
    assert_raises(NotImplementedError, t.check, 1)
    assert_raises(NotImplementedError, t.coerce, 1)
    assert_raises(NotImplementedError, t.display)
    assert_raises(NotImplementedError, t.enumeration_value, 1)
    assert_raises(NotImplementedError, t.generate)
    assert_raises(NotImplementedError, t.rst)
    assert_raises(NotImplementedError, t.validate, 1)
Exemple #33
0
def test_set():
    s = frozenset([1, 2.3, 'abc'])
    t = Type.dispatch(s)
    assert isinstance(t, Set)
    assert type(t) is FrozenSet

    assert hashable(s) == t.hashable() == s
    assert is_hashable(s)
    assert is_hashable(hashable(s))

    s1 = {1, 2, 3}
    s2 = {2, 3, 4}
    assert find_ne(s1, s2) == SetDifferences(s1, s2)

    e1 = eval(estr(s1))
    assert_equivalent(e1, s1)

    examine_set(Set, set(s))
    examine_set(FrozenSet, s)

    for cls in subclasses(Set, [Set]):
        for k in xrange(SAMPLES):
            val = cls.generate()
            with on_error(elog, examine_set, (cls, val)):
                hangwatch(1, examine_set, cls, val)

        buf = []
        last = None
        for item in enumerate_(cls.type, max_enum=SAMPLES * 10, step=100):
            assert type(item) is cls.type
            assert item != last
            buf.append(item)
            last = item

        assert is_unique(buf)

    s = {1, 2, (3, 4)}
    assert primitive_form(s) == [1, 2, [3, 4]]
    assert collect(s) == primitive_form(s)
Exemple #34
0
def examine_numeric(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == [val]
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert cfeq(eitem, val, relative=True)
    assert type(eitem) is cls.type
Exemple #35
0
def examine_string(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert eitem == val
    assert type(eitem) is cls.type
Exemple #36
0
def examine_numeric(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == [val]
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert cfeq(eitem, val, relative=True)
    assert type(eitem) is cls.type
Exemple #37
0
def examine_string(cls, val):
    assert type(val) is cls.type
    assert is_hashable(hashable(val))
    assert deserialize(serialize(val)) == val
    assert deserialize(serialize(cls.type)) is cls.type
    assert isinstance(rstr(val), str)
    assert primitive_form(val) == val

    assert list(visit(val)) == list(val)
    assert find_ne(val, val) is None

    eitem = eval(estr(val))
    assert eitem == val
    assert type(eitem) is cls.type
Exemple #38
0
def test_base():
    kwargs = dict(a=5, b=3.4, c=u'abc')
    obj = A(**kwargs)

    assert obj.a == 5
    assert obj.b == 3.4
    assert obj.c == u'abc'

    assert obj.to_dict() == kwargs
    assert obj.to_tuple() == (5, 3.4, u'abc')
    assert not is_hashable(obj)

    assert obj != 5
    assert_equivalent(obj, A(**kwargs))
    assert_inequivalent(obj, A(a=6, b=3.4, c=u'abc'))

    assert A(a=5, b=3.4).to_dict() == dict(a=5, b=3.4)

    check_idempotence(obj)

    assert_raises(TypeError, A, a=5.1, b=3.4)
    assert_raises(AttributeError, A, a=5)

    assert_equivalent(A(**kwargs), A(**kwargs))
    assert_inequivalent(A2(**kwargs), A2(**kwargs))
    assert_raises(AssertionError, assert_pickle_idempotent, A2(**kwargs))

    obj2 = A2(**kwargs)
    assert_type_equivalent(obj2.to_dict(), dict(a=5, b=3.4, c=u'abc'))
    assert obj2.to_dict(include=['getstate_exclude']) == dict(b=3.4)

    obj3 = A3(**kwargs)
    assert obj3.to_tuple() == (5, 3.4, u'abc')
    assert is_hashable(obj3)

    obj4 = A4(a=[1, 2, 3], b=3.4, c='abc')
    assert is_hashable(obj4)
Exemple #39
0
def test_base():
    kwargs = dict(a=5, b=3.4, c=u'abc')
    obj = A(**kwargs)

    assert obj.a == 5
    assert obj.b == 3.4
    assert obj.c == u'abc'
    
    assert obj.to_dict() == kwargs
    assert obj.to_tuple() == (5, 3.4, u'abc')
    assert not is_hashable(obj)

    assert obj != 5
    assert_equivalent(obj, A(**kwargs))
    assert_inequivalent(obj, A(a=6, b=3.4, c=u'abc'))

    assert A(a=5, b=3.4).to_dict() == dict(a=5, b=3.4)
    
    check_idempotence(obj)

    assert_raises(TypeError, A, a=5.1, b=3.4)
    assert_raises(AttributeError, A, a=5)

    assert_equivalent(A(**kwargs), A(**kwargs))
    assert_inequivalent(A2(**kwargs), A2(**kwargs))
    assert_raises(AssertionError, assert_pickle_idempotent, A2(**kwargs))

    obj2 = A2(**kwargs)
    assert_type_equivalent(obj2.to_dict(), dict(a=5, b=3.4, c=u'abc'))
    assert obj2.to_dict(include=['getstate_exclude']) == dict(b=3.4)

    obj3 = A3(**kwargs)
    assert obj3.to_tuple() == (5, 3.4, u'abc')
    assert is_hashable(obj3)

    obj4 = A4(a=[1, 2, 3], b=3.4, c='abc')
    assert is_hashable(obj4)
Exemple #40
0
def test_pip():
    pip = Pip('six')
    assert pip.order == Pip.order
    assert not pip.always_upgrade
    assert is_hashable(pip)

    assert 'syn' in pip._pkgs
    assert 'six' in pip._pkgs

    assert pip.check()

    with assign(pip, 'version', Ge('0')):
        assert pip.check()

    with assign(pip, 'version', Ge('100000000000')):
        assert not pip.check()

    with assign(pip, 'name', 'foobarbaz123789'):
        assert not pip.check()

    with assign(depd, 'command', MagicMock()):
        pip.satisfy()
        assert depd.command.call_count == 0

        with assign(pip, 'name', 'foobarbaz123789'):
            ops = pip.satisfy()
            assert ops == [Install('foobarbaz123789', order=Pip.order)]
            Operation.optimize(ops)
            for op in ops:
                op.execute()
            depd.command.assert_called_with(
                'pip install --upgrade foobarbaz123789')

        with assign(pip, 'always_upgrade', True):
            ops = pip.satisfy()
            assert ops == [Install('six', order=Pip.order)]
            Operation.optimize(ops)
            for op in ops:
                op.execute()
            depd.command.assert_called_with('pip install --upgrade six')

    def bad_output(*args, **kwargs):
        raise OSError()

    with assign(depd, 'output', bad_output):
        with assign(Pip, '_pkgs', dict()):
            Pip._populate_pkgs()
            assert Pip._pkgs == {}
Exemple #41
0
def test_pip():
    pip = Pip('six')
    assert pip.order == Pip.order
    assert not pip.always_upgrade
    assert is_hashable(pip)

    assert 'syn' in pip._pkgs
    assert 'six' in pip._pkgs

    assert pip.check()

    with assign(pip, 'version', Ge('0')):
        assert pip.check()

    with assign(pip, 'version', Ge('100000000000')):
        assert not pip.check()
        
    with assign(pip, 'name', 'foobarbaz123789'):
        assert not pip.check()

    with assign(depd, 'command', MagicMock()):
        pip.satisfy()
        assert depd.command.call_count == 0

        with assign(pip, 'name', 'foobarbaz123789'):
            ops = pip.satisfy()
            assert ops == [Install('foobarbaz123789', order=Pip.order)]
            Operation.optimize(ops)
            for op in ops:
                op.execute()
            depd.command.assert_called_with('pip install --upgrade foobarbaz123789')

        with assign(pip, 'always_upgrade', True):
            ops = pip.satisfy()
            assert ops == [Install('six', order=Pip.order)]
            Operation.optimize(ops)
            for op in ops:
                op.execute()
            depd.command.assert_called_with('pip install --upgrade six')

    def bad_output(*args, **kwargs):
        raise OSError()

    with assign(depd, 'output', bad_output):
        with assign(Pip, '_pkgs', dict()):
            Pip._populate_pkgs()
            assert Pip._pkgs == {}
Exemple #42
0
def test_apt():
    apt = Apt('make')
    assert apt.name == 'make'
    assert apt.order == Apt.order
    assert is_hashable(apt)

    if depd.output('which apt-get'):
        if depd.output('which make'):
            assert 'make' in Apt._pkgs

    with assign(depd, 'command', MagicMock()):
        with assign(Apt, '_pkgs', dict(make='1.0')):
            assert apt.check()
            assert apt.satisfy() == []
            assert depd.command.call_count == 0

        with assign(Apt, '_pkgs', dict()):
            assert not apt.check()

            ops = apt.satisfy()
            assert ops == [Update(order=Apt.order),
                           Install('make', order=Apt.order)]

            # Make sure update precedes install
            assert ops[0].order == Apt.order - 1
            assert ops[1].order == Apt.order

            Operation.optimize(ops)
            for op in ops:
                op.execute()
            assert depd.command.call_count == 2
            depd.command.assert_any_call('apt-get update')
            depd.command.assert_called_with('apt-get install -y make')

    def bad_output(*args, **kwargs):
        raise OSError()

    with assign(depd, 'output', bad_output):
        with assign(Apt, '_pkgs', dict()):
            Apt._populate_pkgs()
            assert Apt._pkgs == {}

    if depd.output('which apt-get'):
        if depd.output('which make'):
            assert 'make' in Apt._pkgs
Exemple #43
0
 def check(self, value):
     if not is_hashable(value):
         raise TypeError('Value is not hashable: {}'.format(value))
Exemple #44
0
def test_is_hashable():
    assert is_hashable(3)
    assert not is_hashable(dict(a = 3))
Exemple #45
0
def test_is_hashable():
    assert is_hashable(3)
    assert not is_hashable(dict(a=3))