def test_string_re():
    vt = ar.StringValueType(regex="^[a-z]+$")
    assert vt.validate("abcde")
    with pytest.raises(ar.ValidationError):
        vt.validate("ABCDE")
    with pytest.raises(ar.ValidationError):
        vt.validate(None)
    class SimpleLazyValue(ar.LazyValue):
        _val_type = ar.StringValueType(regex="^[a-z]*$")

        async def _load(self, dbval, conn=None):
            return dbval.lower()

        def _convert_to_db(self, pyval):
            return pyval.upper()
def test_string_re_and_maxlen():
    vt = ar.StringValueType(max_length=3, regex="^[a-z]+$")
    assert vt.validate("abc")
    with pytest.raises(ar.ValidationError):
        vt.validate("abcde")
    with pytest.raises(ar.ValidationError):
        vt.validate("ABC")
    with pytest.raises(ar.ValidationError):
        vt.validate(None)
예제 #4
0
    class Hero(ar.Document):
        name = ar.Field(ar.StringValueType(), indexed=True)
        publisher = ar.Field(ar.LazyDocRefValueType(Publisher))

        async def get_movies(self):
            return await Movie.from_query(Movie.cq().filter(
                r.row["heroes"].contains(self.pkey)))

        async def get_movies_as_list(self):
            iterator = await self.get_movies()
            return await iterator.as_list()
def test_complex_dict():
    vt = ar.DictValueType(ar.StringValueType(regex="^[a-z]+$"),
                          ar.SetValueType(ar.StringValueType(max_length=3)))

    v = {"a": {"abc"}, "abcd": {"xyz"}}
    assert vt.validate(v) == vt
    assert vt.pyval_to_dbval(v) == {"a": ["abc"], "abcd": ["xyz"]}
    assert vt.pyval_to_dbval(None) == None
    assert vt.dbval_to_pyval({"a": ["abc"], "abcd": ["xyz"]}) == v

    v = {"A": {"abc"}}
    with pytest.raises(ar.ValidationError):
        vt.validate(v)

    v = {"a": {"abcd"}}
    with pytest.raises(ar.ValidationError):
        vt.validate(v)

    v = {"a": ["abcd"]}
    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_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)
예제 #8
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}]
예제 #9
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")
예제 #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))
예제 #11
0
 class Studio(ar.Document):
     name = ar.Field(ar.StringValueType(), indexed=True)
예제 #12
0
 class Publisher(ar.Document):
     name = ar.Field(ar.StringValueType(), indexed=True)
def test_string_maxlen():
    vt = ar.StringValueType(max_length=3)
    assert vt.validate("Hel") == vt
    assert vt.validate(None) == vt
    with pytest.raises(ar.ValidationError):
        vt.validate("Hello")
def test_string_simple():
    vt = ar.StringValueType()
    assert vt.validate("Hello") == vt
    assert vt.validate(None) == vt
    with pytest.raises(ar.ValidationError):
        vt.validate(0)