Пример #1
0
def test_limit_and_offset(persons):
    assert len(list(Person.limit(2))) == 2
    assert len(list(Person.offset(4).limit(2))) == 1
    records = Person.order('age').offset(1).limit(2).records()
    assert len(records) == 2
    assert records[0].name == 'Henry'
    assert records[1].name == 'Henry'
Пример #2
0
def test_order(persons):
    assert Person.order('age',
                        asc=True).records() == sorted(Person.all(),
                                                      key=lambda p: p.age)
    assert Person.order('age',
                        desc=True).records() == sorted(Person.all(),
                                                       key=lambda p: -p.age)
Пример #3
0
def records():
    p = Person.create(name='jjy', age=25)
    p2 = Person.create(name='simon', age=45)
    p.pets.create(name='pipi', age=2)
    p.pets.create(name='cola', age=1)
    p2.pets.create(name='penguin', age=1)
    yield
    Person.delete_all()
    Pet.delete_all()
Пример #4
0
def test_where_in(persons):
    assert sorted(list(Person.where('age IN :ages', ages=(30, 80))),
                  key=lambda p: p.name) == sorted(
                      [p for p in Person.all() if p.age in [30, 80]],
                      key=lambda p: p.name)
    assert sorted(
        list(Person.where('name NOT IN :names', names=('Hari Seldon', 'jjy'))),
        key=lambda p: p.name) == sorted(
            [p for p in Person.all() if p.name not in ('Hari Seldon', 'jjy')],
            key=lambda p: p.name)
Пример #5
0
def test_group(persons):
    import itertools
    assert sorted(Person.group('name').count()) == sorted([
        (name, len(list(group)))
        for (name,
             group) in itertools.groupby(Person.all(), key=lambda p: p.name)
    ])
    assert Person.group('name').having('count(*) > 1').count() == [('Henry', 2)
                                                                   ]
    assert ('Henry', 2) not in Person.group('name').having('count(*) = :x',
                                                           x=1).count()
Пример #6
0
def test_where_chain(persons):
    assert Person.where(name='A girl has no name').first() is None
    henrys = Person.where(name='Henry')
    assert len(list(henrys)) == 2
    assert list(henrys.where(age=30)) == list(
        Person.where(name='Henry', age=30))
    assert list(henrys.where(age=27)) == list(
        Person.where(name='Henry', age=27))
    with pytest.raises(RecordNotFound):
        henrys.find_by(age=42)
    for p in Person.all():
        assert Person.where('name=:name and age=:age', name=p.name,
                            age=p.age).first() == p
Пример #7
0
def records():
    Person.create(name='jjy', age=25)
    Person.create(name='Hari Seldon', age=60)
    Person.create(name='Anders', age=80)
    Person.create(name='Henry', age=30)
    Person.create(name='Henry', age=27)
    yield
    Person.delete_all()
Пример #8
0
def test_first_or_create(records):
    p = Person.where(name='jjy', age=25).first_or_create()
    p2 = Person.where(name='jjy', age=35).first_or_create()
    assert not p.is_new_record()
    assert not p2.is_new_record() and p2.name == 'jjy' and p2.age == 35
Пример #9
0
def test_first_or_build(records):
    p = Person.where(name='jjy', age=25).first_or_build()
    p2 = Person.where(name='jjy', age=35).first_or_build()
    assert not p.is_new_record()
    assert p2.is_new_record()
Пример #10
0
def test_build(records):
    p = Person.where(name='jjy', age=25).build()
    p2 = Person.where(name='jjy').where(age=25).build()
    assert p.is_new_record()
    assert p.age == 25 and p.name == 'jjy'
    assert p == p2
Пример #11
0
def test_query(records):
    person = Person.find_by(name='jjy')
    assert sorted(list(person.pets), key=lambda p: p.name) == sorted(list(Pet.where(person_id=person.id)),
                                                                     key=lambda p: p.name)
    assert person.pets.count() == 2
Пример #12
0
def test_count(persons):
    assert Person.count() == len(Person.records())
    assert Person.where(name='Henry').count() == 2
Пример #13
0
def test_find_by(persons):
    p = Person.where(name='jjy').first()
    assert p == Person.find_by(age=p.age)
    with pytest.raises(RecordNotFound):
        Person.find_by(name='A girl has no name')
Пример #14
0
def test_find(persons):
    p = Person.where(name='jjy').first()
    assert p == Person.find(p.id)
    assert p.age == 25
    with pytest.raises(RecordNotFound):
        Person.find(0)