forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_choices.py
More file actions
65 lines (58 loc) · 1.89 KB
/
test_choices.py
File metadata and controls
65 lines (58 loc) · 1.89 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
from tests.base import DBTestCase
from tests.example_app.tables import Shirt
class TestChoices(DBTestCase):
def _insert_shirts(self):
Shirt.insert(
Shirt(size=Shirt.Size.small),
Shirt(size=Shirt.Size.medium),
Shirt(size=Shirt.Size.large),
).run_sync()
def test_save(self):
"""
Make sure saving works, when setting a value as an Enum.
"""
shirt = Shirt(size=Shirt.Size.large)
shirt.save().run_sync()
def test_default(self):
"""
Make sure the default works correctly, when the default is an Enum.
"""
Shirt().save().run_sync()
shirt = Shirt.objects().first().run_sync()
self.assertEqual(shirt.size, "l")
def test_update(self):
"""
Make sure rows can be updated using Enums.
"""
self._insert_shirts()
Shirt.update({Shirt.size: Shirt.Size.large}).where(
Shirt.size == Shirt.Size.small
).run_sync()
shirts = (
Shirt.select(Shirt.size)
.output(as_list=True)
.order_by(Shirt.id)
.run_sync()
)
self.assertEqual(shirts, ["l", "m", "l"])
def test_select_where(self):
"""
Make sure Enums can be used in the where clause of select queries.
"""
self._insert_shirts()
shirts = (
Shirt.select(Shirt.size)
.where(Shirt.size == Shirt.Size.small)
.run_sync()
)
self.assertEqual(shirts, [{"size": "s"}])
def test_objects_where(self):
"""
Make sure Enums can be used in the where clause of objects queries.
"""
self._insert_shirts()
shirts = (
Shirt.objects().where(Shirt.size == Shirt.Size.small).run_sync()
)
self.assertEqual(len(shirts), 1)
self.assertEqual(shirts[0].size, "s")