forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_shortcuts.py
51 lines (38 loc) · 1.36 KB
/
tests_shortcuts.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
import unittest
from peewee import *
from playhouse.shortcuts import case
db = SqliteDatabase(':memory:')
class TestModel(Model):
name = CharField()
number = IntegerField()
class Meta:
database = db
class CaseShortcutTestCase(unittest.TestCase):
values = (
('alpha', 1),
('beta', 2),
('gamma', 3))
expected = [
{'name': 'alpha', 'number_string': 'one'},
{'name': 'beta', 'number_string': 'two'},
{'name': 'gamma', 'number_string': '?'},
]
def setUp(self):
TestModel.drop_table(True)
TestModel.create_table()
for name, number in self.values:
TestModel.create(name=name, number=number)
def test_predicate(self):
query = (TestModel
.select(TestModel.name, case(TestModel.number, (
(1, "one"),
(2, "two")), "?").alias('number_string'))
.order_by(TestModel.id))
self.assertEqual(list(query.dicts()), self.expected)
def test_no_predicate(self):
query = (TestModel
.select(TestModel.name, case(None, (
(TestModel.number == 1, "one"),
(TestModel.number == 2, "two")), "?").alias('number_string'))
.order_by(TestModel.id))
self.assertEqual(list(query.dicts()), self.expected)