def test_remove_expression():
    class Page(Document):
        text = StringField()
        page_num = IntField()

    class Book(Document):
        isbn = StringField()
        pages = ListField(DocumentField(Page))

        isbn_index = Index().ascending('isbn')

    session = get_session()
    session.clear_collection(Book)

    ISBN = '3462784290890'
    book = Book(isbn=ISBN, pages=[])
    for i in range(0,10):
        book.pages.append(Page(text='p%d' % i, page_num = i))

    session.insert(book)

    query = session.query(Book).filter(Book.isbn == ISBN, Book.pages.page_num > 5)
    query.remove(Book.pages, Page.page_num == 8).execute()
    book = session.query(Book).filter(Book.isbn == ISBN).one()
    assert len(book.pages) == 9
예제 #2
0
def test_first():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3))
    s.insert(T(i=4))
    assert s.query(T).descending(T.i).first().i == 4
    assert s.query(T).ascending(T.i).first().i == 3
def test_remove():
    # setup
    s = get_session()
    s.clear_collection(T)
    for i in range(0, 15):
        s.insert(T(i=i))
    assert s.query(T).count() == 15

    def getall():
        return [t.i for t in s.query(T).ascending(T.i).all()]

    s.remove_query(T).filter(T.i > 8).execute()
    assert s.query(T).count() == 9

    # TODO: to /really/ test this I need to cause an error.
    remove_result = s.remove_query(T).filter(T.i > 7).set_safe(True).execute()
    assert remove_result['ok'] == 1
    assert s.query(T).count() == 8

    s.remove_query(T).or_(T.i == 7, T.i == 6).execute()
    remaining = [0, 1, 2, 3, 4, 5]
    assert remaining == getall(), getall()

    s.remove_query(T).in_(T.i, 0, 1).execute()
    remaining.remove(1)
    remaining.remove(0)
    assert remaining == getall(), getall()

    s.remove_query(T).nin(T.i, 2, 3, 4).execute()
    remaining.remove(5)
    assert remaining == getall(), getall()

    s.remove_query(T).filter_by(i=2).execute()
    remaining.remove(2)
    assert remaining == getall(), getall()
예제 #4
0
def test_sort():
    from pymongo import ASCENDING, DESCENDING
    s = get_session()
    sorted_query = s.query(T).ascending(T.i).descending(T.j)
    assert sorted_query.sort == [('i', ASCENDING),('j', DESCENDING)], sorted_query.sort
    for obj in sorted_query:
        pass
def test_find_and_modify():
    s = get_session()
    s.clear_collection(T, T2)
    # insert
    value = s.query(T).filter_by(i=12341).find_and_modify().set(i=12341).upsert().execute()
    assert value == {}, value
    assert s.query(T).one().i == 12341
    
    # update
    value = s.query(T).filter_by(i=12341).ascending(T.i).find_and_modify().set(i=9999).execute()
    assert value.i == 12341
    assert s.query(T).one().i == 9999

    # return new
    value = s.query(T).filter_by(i=9999).fields(T.i).find_and_modify(new=True).set(i=8888).execute()
    assert value.i == 8888, value.i
    assert s.query(T).one().i == 8888

    # remove
    value = s.query(T).filter_by(i=8888).find_and_modify(remove=True).execute()
    assert value.i == 8888, value.i
    assert s.query(T).first() is None

    # update
    value = s.query(T).filter_by(i=1000).ascending(T.i).find_and_modify(new=True).set(i=0).execute()
    assert value is None, value
예제 #6
0
def test_basic():
    class Doc(Document):
        count = IntField()
    s = get_session()
    d = Doc(count=0)
    s.insert(d)
    assert d.mongo_id
예제 #7
0
def test_hint():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3))
    s.insert(T(i=4))
    for item in s.query(T).hint_asc(T.i):
        pass
예제 #8
0
def setup():
    s = get_session()
    s.clear_collection(V)

    for n in xrange(5):
        obj = V(i=n, s=chr(65+n))
        s.insert(obj)
예제 #9
0
def test_save():
    class Doc(Document):
        a = AutoIncrementField()

    d = Doc()
    s = get_session()
    s.insert(d)
예제 #10
0
def test_all():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3))
    s.insert(T(i=4))
    for count, item in enumerate(s.query(T).all()):
        pass
    assert count == 1
예제 #11
0
def test_unwrapped_is_not_dirty():
    class D(Document):
        a = IntField()
    s = get_session()
    s.clear_collection(D)
    s.insert(D(a=1))
    d = s.query(D).one()
    assert len(d.get_dirty_ops()) == 0, len(d.get_dirty_ops())
예제 #12
0
def loading_test():
    s = get_session()
    s.clear_collection(TestDoc)
    t = TestDoc(int1=123431)
    s.insert(t)
    for td in s.query(TestDoc):
        break
    assert td.int1 == t.int1
def test_update_safe():
    s = get_session()
    s.clear_collection(TUnique)
    s.query(TUnique).filter_by(i=1).set(i=1, j=2).upsert().execute()
    # default safe=false -- ignore error
    s.query(TUnique).filter_by(i=1).set(i=1, j=2).upsert().execute()
    # explicit safe=false
    s.query(TUnique).filter_by(i=1).set(i=1, j=2).safe().safe(safe=False).upsert().execute()
예제 #14
0
def test_basic():
    class Doc(Document):
        count = IntField()

    s = get_session()
    d = Doc(count=0)
    s.insert(d)
    assert d.mongo_id
예제 #15
0
def test_field_filter_non_retrieved_field():
    s = get_session()
    s.clear_collection(T)
    obj = T(i=3, j=2)
    s.insert(obj)
    for t in s.query(T).fields(T.i):
        break
    assert t.j == 2
예제 #16
0
def test_count():
    s = get_session()
    s.clear_collection(T)
    assert s.query(T).count() == 0
    s.insert(T(i=3, j=4))
    s.insert(T(i=3, j=5))
    s.insert(T(i=3, j=6))
    assert s.query(T).count() == 3
예제 #17
0
def loading_test():
    s = get_session()
    s.clear_collection(TestDoc)
    t = TestDoc(int1=123431)
    s.insert(t)
    for td in s.query(TestDoc):
        break
    assert td.int1 == t.int1
예제 #18
0
def test_all():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3))
    s.insert(T(i=4))
    for count, item in enumerate(s.query(T).all()):
        pass
    assert count == 1
예제 #19
0
def test_field_filter_non_retrieved_field():
    s = get_session()
    s.clear_collection(T)
    obj = T(i=3, j=2)
    s.insert(obj)
    for t in s.query(T).fields(T.i):
        break
    assert t.j == 2
예제 #20
0
def test_with_db_field_any():
    class DocDBFieldAny(Document):
        a = AutoIncrementField(db_field='any')

    d = DocDBFieldAny()
    s = get_session()
    s.insert(d)
    assert d.a == 1
예제 #21
0
def test_count():
    s = get_session()
    s.clear_collection(T)
    assert s.query(T).count() == 0
    s.insert(T(i=3, j=4))
    s.insert(T(i=3, j=5))
    s.insert(T(i=3, j=6))
    assert s.query(T).count() == 3
예제 #22
0
def test_unwrapped_is_not_dirty():
    class D(Document):
        a = IntField()

    s = get_session()
    s.clear_collection(D)
    s.insert(D(a=1))
    d = s.query(D).one()
    assert len(d.get_dirty_ops()) == 0, len(d.get_dirty_ops())
예제 #23
0
def test_upsert_with_required():
    class D(Document):
        a = IntField()
        c = IntField()
        b = IntField(required=False)

    s = get_session()
    s.clear_collection(D)
    s.update(D(b=4, c=4), id_expression=D.b == 4, upsert=True)
예제 #24
0
def test_upsert_with_required():
    class D(Document):
        a = IntField()
        c = IntField()
        b = IntField(required=False)

    s = get_session()
    s.clear_collection(D)
    s.update(D(b=4, c=4), id_expression=D.b == 4, upsert=True)
예제 #25
0
def test_with_db_field_id():
    class DocDBFieldId(Document):
        a = AutoIncrementField(db_field='_id')

    d = DocDBFieldId()
    s = get_session()
    s.insert(d)
    assert d.a == 1
    assert d.mongo_id == 1
예제 #26
0
def test_clone_basic():
    s = get_session()
    query = s.query(V).fields(V.i, values_only=True)
    query = query.clone()

    i = 0
    for t in query.all():
        assert t[0] == i
        i += 1
예제 #27
0
def test_first_increment():
    class DocOne(Document):
        a = AutoIncrementField()

    d = DocOne()
    s = get_session()
    s.insert(d)

    assert d.a == 1
예제 #28
0
def test_update_safe():
    s = get_session()
    s.clear_collection(TUnique)
    s.query(TUnique).filter_by(i=1).set(i=1, j=2).upsert().execute()
    # default safe=false -- ignore error
    s.query(TUnique).filter_by(i=1).set(i=1, j=2).upsert().execute()
    # explicit safe=false
    s.query(TUnique).filter_by(i=1).set(
        i=1, j=2).safe().safe(safe=False).upsert().execute()
예제 #29
0
def test_clone_basic():
    s = get_session()
    query = s.query(V).fields(V.i, values_only=True)
    query = query.clone()

    i = 0
    for t in query.all():
        assert t[0] == i
        i += 1
예제 #30
0
def test_field_filter_non_retrieved_subdocument_field():
    s = get_session()
    s.clear_collection(T, T2)
    obj = T(i=3, j=2)
    obj2 = T2(t=obj)
    s.insert(obj2)
    for t2 in s.query(T2).fields(T2.t.i):
        break
    assert t2.t.j == 2
예제 #31
0
def test_resolve_fields():
    class Resolver(Document):
        i = IntField(db_field='j')
        k = IntField()
    s = get_session()
    s.clear_collection(Resolver)

    q = s.query(Resolver).filter(Resolver.i.in_(6))
    q = s.query(Resolver).set(Resolver.i, 6)
예제 #32
0
def test_distinct():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3, j=4))
    s.insert(T(i=3, j=5))
    s.insert(T(i=3, j=6))
    for count, item in enumerate(s.query(T).distinct(T.i)):
        pass
    assert count == 0, count
예제 #33
0
def test_distinct():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3, j=4))
    s.insert(T(i=3, j=5))
    s.insert(T(i=3, j=6))
    for count, item in enumerate(s.query(T).distinct(T.i)):
        pass
    assert count == 0, count
예제 #34
0
def test_field_filter_non_retrieved_subdocument_field():
    s = get_session()
    s.clear_collection(T, T2)
    obj = T(i=3, j=2)
    obj2 = T2(t=obj)
    s.insert(obj2)
    for t2 in s.query(T2).fields(T2.t.i):
        break
    assert t2.t.j == 2
예제 #35
0
def test_basic():
    s = get_session()
    i = 0
    for t in s.query(V).fields(V.i, values_only=True).all():
        assert t[0] == i
        i += 1
    i = 0
    for t in s.query(V).fields(V.s, values_only=True).all():
        assert t[0] == chr(65+i)
        i += 1
def test_remove_expression_error():
    class Page(Document):
        text = StringField()
        page_num = IntField()

    class Book(Document):
        isbn = StringField()
        pages = ListField(DocumentField(Page))
    session = get_session()
    session.query(Book).remove(Book.isbn, Page.page_num == 8)
예제 #37
0
def test_remove_obj():
    s = get_session()
    s.clear_collection(T)
    t = T(i=4)
    s.insert(t)
    assert s.query(T).count() == 1
    s.remove(t)
    assert s.query(T).count() == 0
    t2 = T(i=3)
    s.remove(t2)
예제 #38
0
def test_basic():
    s = get_session()
    i = 0
    for t in s.query(V).fields(V.i, values_only=True).all():
        assert t[0] == i
        i += 1
    i = 0
    for t in s.query(V).fields(V.s, values_only=True).all():
        assert t[0] == chr(65 + i)
        i += 1
예제 #39
0
def test_no_increment():
    class Doc(Document):
        a = AutoIncrementField()

    d = Doc()
    d.a = -1
    s = get_session()
    s.insert(d)

    assert d.a == -1
예제 #40
0
def test_resolve_fields():
    class Resolver(Document):
        i = IntField(db_field='j')
        k = IntField()

    s = get_session()
    s.clear_collection(Resolver)

    q = s.query(Resolver).filter(Resolver.i.in_(6))
    q = s.query(Resolver).set(Resolver.i, 6)
def test_remove_obj():
    s = get_session()
    s.clear_collection(T)
    t = T(i=4)
    s.insert(t)
    assert s.query(T).count() == 1
    s.remove(t)
    assert s.query(T).count() == 0
    t2 = T(i=3)
    s.remove(t2)
예제 #42
0
def test_clone():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3))
    s.insert(T(i=4))
    q = s.query(T)
    q2 = q.clone()
    q.skip(2)
    for count, item in enumerate(q2):
        pass
    assert count == 1
예제 #43
0
def test_self_reference():
    class D(Document):
        d = DocumentField('D', required=False)
        a = IntField()
    d = D(d=D(a=5), a=4)
    assert d.wrap() == { 'd' : { 'a' : 5 }, 'a' : 4 }
    s = get_session()
    s.clear_collection(D)
    s.insert(d)
    d_from_db = s.query(D).one()
    assert d_from_db.d.a == d.d.a
예제 #44
0
def test_remove_expression_error():
    class Page(Document):
        text = StringField()
        page_num = IntField()

    class Book(Document):
        isbn = StringField()
        pages = ListField(DocumentField(Page))

    session = get_session()
    session.query(Book).remove(Book.isbn, Page.page_num == 8)
예제 #45
0
def test_clone():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3))
    s.insert(T(i=4))
    q = s.query(T)
    q2 = q.clone()
    q.skip(2)
    for count, item in enumerate(q2):
        pass
    assert count == 1
예제 #46
0
def test_comparators():
    # once filters have more sanity checking, this test will need to be broken up
    s = get_session()
    query_obj = s.query(T).filter(T.i < 2,
        T.i > 3,
        T.i != 4,
        T.i <= 5,
        T.i >= 6).query

    assert query_obj == {'i': {'$ne': 4, '$gte': 6, '$lte': 5, '$gt': 3, '$lt': 2}}

    s.query(T).filter(T.i == 1).query == { 'i' : 1}
예제 #47
0
def test_self_reference():
    class D(Document):
        d = DocumentField('D', required=False)
        a = IntField()

    d = D(d=D(a=5), a=4)
    assert d.wrap() == {'d': {'a': 5}, 'a': 4}
    s = get_session()
    s.clear_collection(D)
    s.insert(d)
    d_from_db = s.query(D).one()
    assert d_from_db.d.a == d.d.a
예제 #48
0
def test_multi_field():
    class DocMulti(Document):
        a = AutoIncrementField()
        b = AutoIncrementField()

    d = DocMulti()
    s = get_session()
    s.insert(d)
    assert d.a == 1 and d.b == 1

    d = DocMulti()
    s.insert(d)
    assert d.a == 2 and d.b == 2
예제 #49
0
def test_upsert_with_no_changes():
    class D(Document):
        a = IntField()
        c = IntField()
        b = IntField(required=False)

        b_index = Index().ascending('b')

    s = get_session(safe=True)
    s.clear_collection(D)
    # D.b_index.ensure(s.db[D.get_collection_name()])
    s.update(D(a=1, b=4, c=4), id_expression=D.b == 4, upsert=True)
    d = s.query(D).one()
    s.update(d, upsert=True)
예제 #50
0
def qr_test_clone():
    s = get_session()
    s.clear_collection(T)
    s.insert(T(i=3))
    s.insert(T(i=4))
    it = iter(s.query(T))
    next(it)
    next(it)
    it2 = it.clone()
    next(it2)
    next(it2)
    try:
        next(it2)
    except StopIteration:
        pass
예제 #51
0
def test_update():
    s = get_session()
    s.clear_collection(T)

    obj = T(i=3)

    s.insert(obj)

    for o in s.query(T):
        assert o.i == 3

    s.query(T).filter(T.i == 3).set(T.i, 4).execute()

    for o in s.query(T):
        assert o.i == 4
예제 #52
0
def test_field_filter():
    s = get_session()
    s.clear_collection(T, T2)

    # Simple Object
    obj = T(i=3)
    s.insert(obj)
    for t in s.query(T).fields(T.i):
        break
    assert t.i == 3
    # Nested Object
    obj2 = T2(t=obj)
    s.insert(obj2)
    for t2 in s.query(T2).fields(T2.t.i):
        break
    assert t2.t.i == 3
예제 #53
0
def test_update_with_unset():
    class D(Document, DictDoc):
        a = IntField()
        c = IntField()
        b = IntField(required=False)

        b_index = Index().ascending('b')

    s = get_session(safe=True)
    s.clear_collection(D)
    # D.b_index.ensure(s.db[D.get_collection_name()])
    s.update(D(a=1, b=4, c=4), id_expression=D.b == 4, upsert=True)
    d = s.query(D).one()
    del d.c
    s.update(d)
    d = s.query(D).one()
    assert 'c' not in d
예제 #54
0
def test_comparators():
    # once filters have more sanity checking, this test will need to be broken up
    s = get_session()
    query_obj = s.query(T).filter(T.i < 2, T.i > 3, T.i != 4, T.i <= 5,
                                  T.i >= 6).query

    assert query_obj == {
        'i': {
            '$ne': 4,
            '$gte': 6,
            '$lte': 5,
            '$gt': 3,
            '$lt': 2
        }
    }

    s.query(T).filter(T.i == 1).query == {'i': 1}
예제 #55
0
def computed_field_db_test():

    class TestDoc2(Document):
        a = IntField()
        b = IntField()
        @computed_field(IntField(), deps=[a,b])
        def a_plus_b(obj):
            return obj['a'] + obj['b']

    s = get_session()
    s.clear_collection(TestDoc2)
    obj = TestDoc2(a=1, b=2)
    assert obj.a_plus_b == 3, 'Got: %s' % obj.a_plus_b

    s.insert(obj)
    for td in s.query(TestDoc2):
        break
    assert td.a_plus_b == obj.a_plus_b
예제 #56
0
def docfield_test():
    class SuperDoc(Document):
        int1 = IntField()
        sub = DocumentField(TestDoc)

    s = get_session()
    s.clear_collection(TestDoc, SuperDoc)

    doc = TestDoc(int1=3)
    sup = SuperDoc(int1=4, sub=doc)

    s.insert(sup)

    for sd in s.query(SuperDoc):
        break

    assert sd.int1 == sup.int1
    assert sd.sub.int1 == doc.int1