forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_base.py
More file actions
104 lines (82 loc) · 2.64 KB
/
test_base.py
File metadata and controls
104 lines (82 loc) · 2.64 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
95
96
97
98
99
100
101
102
103
104
from enum import Enum
from unittest import TestCase
from piccolo.columns.choices import Choice
from piccolo.columns.column_types import Integer, Varchar
from piccolo.table import Table
class MyTable(Table):
name = Varchar()
class TestCopy(TestCase):
def test_copy(self):
"""
Try copying columns.
"""
column = MyTable.name
new_column = column.copy()
self.assertNotEqual(id(column), id(new_column))
self.assertNotEqual(id(column._meta), id(new_column._meta))
self.assertNotEqual(
id(column._meta.call_chain), id(new_column._meta.call_chain)
)
class TestHelpText(TestCase):
def test_help_text(self):
"""
Test adding help text to a column.
"""
help_text = "This is some important help text for users."
column = Varchar(help_text=help_text)
self.assertTrue(column._meta.help_text == help_text)
class TestChoices(TestCase):
def test_choices(self):
"""
Test adding choices to a column.
"""
class Title(Enum):
mr = 1
mrs = 2
column = Integer(choices=Title)
self.assertEqual(column._meta.choices, Title)
def test_invalid_types(self):
"""
Test adding choices to a column, which are the wrong type.
"""
class Title(Enum):
mr = 1
mrs = 2
with self.assertRaises(ValueError):
Varchar(choices=Title)
def test_get_choices_dict(self):
"""
Test ``get_choices_dict``.
"""
class Title(Enum):
mr = 1
mrs = 2
column = Integer(choices=Title)
self.assertEqual(
column._meta.get_choices_dict(),
{
"mr": {"display_name": "Mr", "value": 1},
"mrs": {"display_name": "Mrs", "value": 2},
},
)
def test_get_choices_dict_without_choices(self):
"""
Test ``get_choices_dict``, with no choices set.
"""
column = Integer()
self.assertEqual(column._meta.get_choices_dict(), None)
def test_get_choices_dict_with_custom_names(self):
"""
Test ``get_choices_dict``, when ``Choice`` is used.
"""
class Title(Enum):
mr = Choice(value=1, display_name="Mr.")
mrs = Choice(value=2, display_name="Mrs.")
column = Integer(choices=Title)
self.assertEqual(
column._meta.get_choices_dict(),
{
"mr": {"display_name": "Mr.", "value": 1},
"mrs": {"display_name": "Mrs.", "value": 2},
},
)