예제 #1
0
def test_comparison_builder_types(field, rhs1, rhs2, supports_none, op,
                                  symbol):
    cls = getattr(kv, field)
    rec = cls('foo')

    x1 = op(rec, rhs1)
    assert kv.is_condition(x1)
    assert isinstance(x1, kv.comparison)
    assert repr(x1) == "%s('foo') %s %r" % (field, symbol, rhs1)

    x2 = op(rec, rhs2)
    assert kv.is_condition(x2)
    assert isinstance(x2, kv.comparison)
    assert repr(x2) == "%s('foo') %s %r" % (field, symbol, rhs2)

    assert x1 == copy.deepcopy(x1)
    assert x1 != x2

    if supports_none and symbol in ('==', '!='):
        op(rec, None)
    else:
        with pytest.raises(TypeError):
            op(rec, None)

    with pytest.raises(TypeError):
        op(rec, 123)

    for attr, val in [('key', 'foo'), ('field', field), ('operator', symbol),
                      ('rhs', rhs1)]:
        assert getattr(x1, attr) == val

        with pytest.raises(AttributeError):
            setattr(x1, attr, val)  # immutable
예제 #2
0
def test_field_accessor_types(field):
    cls = getattr(kv, field)
    rec = cls('foo')

    assert repr(rec) == "%s('foo')" % field

    assert not kv.is_condition(rec)

    rec2 = copy.deepcopy(rec)
    assert rec2.key == 'foo'
    assert isinstance(rec2, cls)

    with pytest.raises(TypeError):
        cls(1)
예제 #3
0
def test_exists_and_missing_types(cls):
    x = cls('foo')
    assert repr(x) == ("%s('foo')" % cls.__name__)
    assert x == copy.deepcopy(x)
    assert x != cls('bar')

    assert kv.is_condition(x)
    assert kv.is_operation(x)

    with pytest.raises(AttributeError):
        x.foobar = 1

    with pytest.raises(TypeError):
        cls(1)