File: test_enum.py

package info (click to toggle)
python-clickhouse-driver 0.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,516 kB
  • sloc: python: 10,950; pascal: 42; makefile: 31; sh: 3
file content (151 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download | duplicates (3)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from enum import IntEnum

from tests.testcase import BaseTestCase
from clickhouse_driver import errors


class A(IntEnum):
    hello = -1
    world = 2


class B(IntEnum):
    foo = -300
    bar = 300


class EnumTestCase(BaseTestCase):
    def test_simple(self):
        columns = (
            "a Enum8('hello' = -1, 'world' = 2), "
            "b Enum16('foo' = -300, 'bar' = 300)"
        )

        data = [(A.hello, B.bar), (A.world, B.foo), (-1, 300), (2, -300)]
        with self.create_table(columns):
            self.client.execute(
                'INSERT INTO test (a, b) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)
            self.assertEqual(
                inserted, (
                    'hello\tbar\n'
                    'world\tfoo\n'
                    'hello\tbar\n'
                    'world\tfoo\n'
                )
            )

            inserted = self.client.execute(query)
            self.assertEqual(
                inserted, [
                    ('hello', 'bar'), ('world', 'foo'),
                    ('hello', 'bar'), ('world', 'foo')
                ]
            )

    def test_enum_by_string(self):
        columns = "a Enum8('hello' = 1, 'world' = 2)"
        data = [('hello', ), ('world', )]
        with self.create_table(columns):
            self.client.execute(
                'INSERT INTO test (a) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)
            self.assertEqual(
                inserted, (
                    'hello\n'
                    'world\n'
                )
            )

            inserted = self.client.execute(query)
            self.assertEqual(inserted, data)

    def test_errors(self):
        columns = "a Enum8('test' = 1, 'me' = 2)"
        data = [(A.world, )]
        with self.create_table(columns):
            with self.assertRaises(errors.LogicalError):
                self.client.execute(
                    'INSERT INTO test (a) VALUES', data
                )

        columns = "a Enum8('test' = 1, 'me' = 2)"
        data = [(3, )]
        with self.create_table(columns):
            with self.assertRaises(errors.LogicalError):
                self.client.execute(
                    'INSERT INTO test (a) VALUES', data
                )

    def test_quote_in_name(self):
        columns = "a Enum8(' \\' t = ' = -1, 'test' = 2)"
        data = [(-1, ), (" ' t = ", )]
        with self.create_table(columns):
            self.client.execute(
                'INSERT INTO test (a) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)
            self.assertEqual(
                inserted, (
                    " \\' t = \n"
                    " \\' t = \n"
                )
            )

            inserted = self.client.execute(query)
            self.assertEqual(inserted, [(" ' t = ", ), (" ' t = ", )])

    def test_comma_and_space_in_name(self):
        columns = "a Enum8('one' = 1, 'two_with_comma, ' = 2, 'three' = 3)"
        data = [(2, ), ('two_with_comma, ', )]
        with self.create_table(columns):
            self.client.execute(
                'INSERT INTO test (a) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)

            self.assertEqual(
                inserted, (
                    'two_with_comma, \n'
                    'two_with_comma, \n'
                )
            )

            inserted = self.client.execute(query)
            self.assertEqual(
                inserted, [('two_with_comma, ',), ('two_with_comma, ',)]
            )

    def test_nullable(self):
        columns = "a Nullable(Enum8('hello' = -1, 'world' = 2))"

        data = [(None, ), (A.hello, ), (None, ), (A.world, )]
        with self.create_table(columns):
            self.client.execute(
                'INSERT INTO test (a) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)
            self.assertEqual(
                inserted, (
                    '\\N\nhello\n\\N\nworld\n'
                )
            )

            inserted = self.client.execute(query)
            self.assertEqual(
                inserted, [
                    (None, ), ('hello', ), (None, ), ('world', ),
                ]
            )