File: test_tuple.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 (180 lines) | stat: -rw-r--r-- 5,438 bytes parent folder | download | duplicates (2)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from datetime import date

from clickhouse_driver import errors
from tests.testcase import BaseTestCase
from tests.util import require_server_version


class TupleTestCase(BaseTestCase):
    def entuple(self, lst):
        return tuple(
            self.entuple(x) if isinstance(x, list) else x for x in lst
        )

    def test_simple(self):
        columns = 'a Tuple(Int32, String)'
        data = [((1, 'a'), ), ((2, 'b'), )]

        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, "(1,'a')\n(2,'b')\n")

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

    def test_tuple_single_element(self):
        columns = 'a Tuple(Int32)'
        data = [((1, ), ), ((2, ), )]

        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, "(1)\n(2)\n")

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

    def test_nullable(self):
        data = [
            ((1, 'a'),),
            ((2, None),), ((None, None),), ((None, 'd'),),
            ((5, 'e'),)
        ]

        with self.create_table('a Tuple(Nullable(Int32), Nullable(String))'):
            self.client.execute(
                'INSERT INTO test (a) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)
            self.assertEqual(
                inserted,
                "(1,'a')\n"
                "(2,NULL)\n(NULL,NULL)\n(NULL,'d')\n"
                "(5,'e')\n"
            )

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

    def test_nested_tuple_with_common_types(self):
        columns = 'a Tuple(String, Tuple(Int32, String), String)'
        data = [
            (('one', (1, 'a'), 'two'),),
            (('three', (2, 'b'), 'four'),)
        ]

        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,
                "('one',(1,'a'),'two')\n"
                "('three',(2,'b'),'four')\n"
            )

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

    def test_tuple_of_tuples(self):
        columns = (
            "a Tuple("
            "Tuple(Int32, String),"
            "Tuple(Enum8('hello' = 1, 'world' = 2), Date)"
            ")"
        )
        data = [
            (((1, 'a'), (1, date(2020, 3, 11))),),
            (((2, 'b'), (2, date(2020, 3, 12))),)
        ]

        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,
                "((1,'a'),('hello','2020-03-11'))\n"
                "((2,'b'),('world','2020-03-12'))\n"
            )

            inserted = self.client.execute(query)
            self.assertEqual(
                inserted, [
                    (((1, 'a'), ('hello', date(2020, 3, 11))), ),
                    (((2, 'b'), ('world', date(2020, 3, 12))), )
                ]
            )

    def test_tuple_of_arrays(self):
        data = [(([1, 2, 3],),), (([4, 5, 6],),)]

        with self.create_table('a Tuple(Array(Int32))'):
            self.client.execute(
                'INSERT INTO test (a) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)
            self.assertEqual(
                inserted,
                "([1,2,3])\n([4,5,6])\n"
            )

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

    # Bug in Array of Tuple handing before 19.16.13:
    # DESCRIBE TABLE test
    #
    # | a.1  | Array(UInt8) |
    # | a.2  | Array(UInt8) |
    # | a.3  | Array(UInt8) |
    # https://github.com/ClickHouse/ClickHouse/pull/8866
    @require_server_version(19, 16, 13)
    def test_array_of_tuples(self):
        data = [
            ([(1, 2, 3), (4, 5, 6)],),
            ([(7, 8, 9)],),
        ]

        with self.create_table('a Array(Tuple(UInt8, UInt8, UInt8))'):
            self.client.execute(
                'INSERT INTO test (a) VALUES', data
            )

            query = 'SELECT * FROM test'
            inserted = self.emit_cli(query)
            self.assertEqual(
                inserted,
                "[(1,2,3),(4,5,6)]\n"
                "[(7,8,9)]\n"
            )

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

    def test_type_mismatch_error(self):
        columns = 'a Tuple(Int32)'
        data = [('test', )]

        with self.create_table(columns):
            with self.assertRaises(errors.TypeMismatchError):
                self.client.execute('INSERT INTO test (a) VALUES', data)