forked from klen/mixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_peewee.py
80 lines (54 loc) · 1.61 KB
/
test_peewee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import datetime as dt
import pytest
from peewee import *
db = SqliteDatabase(':memory:')
class Person(Model):
name = CharField()
status = CharField(choices=(('user', 'user'), ('moderator', 'moderator'), ('admin', 'admin')))
created = DateTimeField(default=dt.datetime.now)
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = db
class Pet(Model):
owner = ForeignKeyField(Person, related_name='pets')
name = CharField()
animal_type = CharField()
class Meta:
database = db
Person.create_table()
Pet.create_table()
@pytest.fixture
def mixer():
from mixer.backend.peewee import mixer
return mixer
@pytest.fixture(autouse=True)
def clean_tables():
Person.delete().execute()
Pet.delete().execute()
def test_mixer(mixer):
person = mixer.blend(Person)
assert person.name
assert person.id
assert person.birthday
assert person.status in ('user', 'moderator', 'admin')
pet = mixer.blend(Pet)
assert pet.name
assert pet.animal_type
assert pet.owner
with mixer.ctx(commit=False):
person = mixer.blend(Person)
assert person.id
def test_guard(mixer):
person = mixer.blend(Person)
person2 = mixer.guard(Person.name == person.name).blend(Person)
assert person.id == person2.id
def test_reload(mixer):
person = mixer.blend(Person, name='true')
person.name = 'wrong'
person = mixer.reload(person)
assert person.name == 'true'
def test_select(mixer):
person = mixer.blend(Person)
pet = mixer.blend(Pet, owner=mixer.SELECT)
assert person == pet.owner