Esempio n. 1
0
def test_fieldalias():
    f = ar.Field(ar.IntValueType())
    f.name = "f"
    fa = ar.FieldAlias(f)

    for attr in [
            "name", "dbname", "indexed", "required", "default", "validate"
    ]:
        assert getattr(fa, attr) == getattr(f, attr)

    assert "fld_name=f" in repr(fa)
def test_namedtuple():
    import collections

    TT = collections.namedtuple("TT", ["a", "b"])
    vt = ar.NamedTupleValueType(TT, [ar.IntValueType(), ar.StringValueType()])
    vt2 = ar.NamedTupleValueType(TT)
    with pytest.raises(ar.IllegalSpecError):
        vt3 = ar.NamedTupleValueType(TT, [ar.IntValueType()])

    v = TT(1, "Hello")
    for _vt in [vt, vt2]:
        assert _vt.validate(v) == _vt
        assert _vt.pyval_to_dbval(v) == v._asdict()
        assert _vt.pyval_to_dbval(None) == None
        assert _vt.dbval_to_pyval(v._asdict()) == v
        assert _vt.dbval_to_pyval(None) == None

    v = TT("Hello", 1)
    assert vt2.validate(v) == vt2
    with pytest.raises(ar.ValidationError):
        vt.validate(v)
def test_dict():
    vt = ar.DictValueType(ar.IntValueType(), ar.StringValueType())

    v = {1: "Hello", 2: "World"}
    assert vt.validate(v) == vt
    assert vt.pyval_to_dbval(v) == v
    assert vt.pyval_to_dbval(None) == None
    assert vt.dbval_to_pyval(v) == v
    assert vt.dbval_to_pyval(None) == None

    v = {1: "hello", "world": 2}
    with pytest.raises(ar.ValidationError):
        vt.validate(v)
def test_set():
    vt = ar.SetValueType(ar.IntValueType())

    v = {1, 2, 3}
    assert vt.validate(v) == vt
    assert vt.pyval_to_dbval(v) == [1, 2, 3]
    assert vt.pyval_to_dbval(None) == None
    assert vt.dbval_to_pyval([1, 2, 3]) == {1, 2, 3}
    assert vt.dbval_to_pyval(None) == None

    v = {"a", "b", "c"}
    with pytest.raises(ar.ValidationError):
        vt.validate(v)
def test_list():
    vt = ar.ListValueType(ar.IntValueType())

    v = [1, 2, 3]
    assert vt.validate(v) == vt
    assert vt.pyval_to_dbval(v) == [1, 2, 3]
    assert vt.pyval_to_dbval(None) == None
    assert vt.dbval_to_pyval([1, 2, 3]) == [1, 2, 3]
    assert vt.dbval_to_pyval(None) == None

    v = ["a", "b", "c"]
    with pytest.raises(ar.ValidationError):
        vt.validate(v)
def test_tuple():
    vt = ar.TupleValueType(ar.IntValueType())

    t = (1, 2, 3)
    assert vt.validate(t) == vt
    assert vt.pyval_to_dbval(t) == [1, 2, 3]
    assert vt.pyval_to_dbval(None) == None
    assert vt.dbval_to_pyval([1, 2, 3]) == (1, 2, 3)
    assert vt.dbval_to_pyval(None) == None

    t = ["a", "b", "c"]
    with pytest.raises(ar.ValidationError):
        vt.validate(t)

    t = (1, 2, "a")
    with pytest.raises(ar.ValidationError):
        vt.validate(t)

    t = ("a", "b", "c")
    with pytest.raises(ar.ValidationError):
        vt.validate(t)
Esempio n. 7
0
async def test_aiter_changes(event_loop, db_conn, aiorethink_db_session):
    cn = await db_conn

    await r.table_create("test").run(cn)

    async def track_table_changes(num_changes, vt):
        i = 0
        q = r.table("test").pluck("v").changes(include_states=True,
                                               include_initial=True)
        values = []
        async for obj, msg in await ar.aiter_changes(q, vt):
            i += 1
            assert obj == None or isinstance(list(obj.values())[0], set)
            if obj != None:
                values.append(list(obj.values())[0])
            assert "new_val" in msg
            if i >= num_changes:
                break
        return values

    table_tracker = event_loop.create_task(
        track_table_changes(
            3,
            ar.DictValueType(ar.StringValueType(),
                             ar.SetValueType(ar.IntValueType()))))

    await asyncio.sleep(
        0.5)  # make reasonably sure that table_tracker is listening
    await r.table("test").insert({"id": 1, "v": [1, 2, 3]}).run(cn)
    await r.table("test").insert({"v": [4, 5]}).run(cn)
    await r.table("test").get(1).delete().run(cn)
    await r.table("test").insert({"v": [6]}).run(cn)
    await r.table("test").insert({"v": [7]}).run(cn)

    done, pending = await asyncio.wait([table_tracker], timeout=5.0)
    assert table_tracker in done
    if table_tracker.exception() != None:
        table_tracker.print_stack()
    assert table_tracker.exception() == None
    assert table_tracker.result() == [{1, 2, 3}, {4, 5}]
Esempio n. 8
0
def test_field_with_type_and_default():
    with pytest.raises(ar.IllegalSpecError):
        f = ar.Field(ar.IntValueType(), default="hello")

    f = ar.Field(ar.StringValueType(), default="hello")
Esempio n. 9
0
def test_field_with_type():
    f = ar.Field(ar.IntValueType())

    assert f.validate(1) == f
    with pytest.raises(ar.ValidationError):
        f.validate("hello")
Esempio n. 10
0
 class Movie(ar.Document):
     name = ar.Field(ar.StringValueType(), indexed=True)
     year = ar.Field(ar.IntValueType(), indexed=True)
     studio = ar.Field(ar.LazyDocRefValueType(Studio))
     heroes = ar.Field(ar.ListValueType(HeroRef))
def test_int():
    vt = ar.IntValueType()
    assert vt.validate(0) == vt
    assert vt.validate(None) == vt
    with pytest.raises(ar.ValidationError):
        vt.validate("abc")