示例#1
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)
示例#2
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
示例#3
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
示例#4
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()
示例#5
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
示例#6
0
def test_count(persons):
    assert Person.count() == len(Person.records())
    assert Person.where(name='Henry').count() == 2
示例#7
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')
示例#8
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)