forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
94 lines (67 loc) · 2.48 KB
/
Copy pathtest_utils.py
File metadata and controls
94 lines (67 loc) · 2.48 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import functools
from .base import ModelTestCase
from .base import TestModel
from peewee import *
from playhouse.test_utils import assert_query_count
from playhouse.test_utils import count_queries
class Data(TestModel):
key = CharField()
class Meta:
order_by = ('key',)
class DataItem(TestModel):
data = ForeignKeyField(Data, backref='items')
value = CharField()
class Meta:
order_by = ('value',)
class TestQueryCounter(ModelTestCase):
requires = [DataItem, Data]
def test_count(self):
with count_queries() as count:
Data.create(key='k1')
Data.create(key='k2')
self.assertEqual(count.count, 2)
with count_queries() as count:
items = [item.key for item in Data.select().order_by(Data.key)]
self.assertEqual(items, ['k1', 'k2'])
Data.get(Data.key == 'k1')
Data.get(Data.key == 'k2')
self.assertEqual(count.count, 3)
def test_only_select(self):
with count_queries(only_select=True) as count:
for i in range(10):
Data.create(key=str(i))
items = [item.key for item in Data.select()]
Data.get(Data.key == '0')
Data.get(Data.key == '9')
Data.delete().where(
Data.key << ['1', '3', '5', '7', '9']).execute()
items = [item.key for item in Data.select().order_by(Data.key)]
self.assertEqual(items, ['0', '2', '4', '6', '8'])
self.assertEqual(count.count, 4)
def test_assert_query_count_decorator(self):
@assert_query_count(2)
def will_fail_under():
Data.create(key='x')
@assert_query_count(2)
def will_fail_over():
for i in range(3):
Data.create(key=str(i))
@assert_query_count(4)
def will_succeed():
for i in range(4):
Data.create(key=str(i + 100))
will_succeed()
self.assertRaises(AssertionError, will_fail_under)
self.assertRaises(AssertionError, will_fail_over)
def test_assert_query_count_ctx_mgr(self):
with assert_query_count(3):
for i in range(3):
Data.create(key=str(i))
def will_fail():
with assert_query_count(2):
Data.create(key='x')
self.assertRaises(AssertionError, will_fail)
@assert_query_count(3)
def test_only_three(self):
for i in range(3):
Data.create(key=str(i))