summaryrefslogtreecommitdiff
path: root/tests/test_bulk_insert.py
blob: 1ad5b967465602b6b4a0a2e5a8bc3043bf6489ed (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import text
from sqlalchemy.sql import column
from sqlalchemy.sql import table
from sqlalchemy.types import TypeEngine

from alembic import op
from alembic.migration import MigrationContext
from alembic.testing import assert_raises_message
from alembic.testing import config
from alembic.testing import eq_
from alembic.testing.fixtures import op_fixture
from alembic.testing.fixtures import TestBase


class BulkInsertTest(TestBase):
    def _table_fixture(self, dialect, as_sql):
        context = op_fixture(dialect, as_sql)
        t1 = table(
            "ins_table",
            column("id", Integer),
            column("v1", String()),
            column("v2", String()),
        )
        return context, t1

    def _big_t_table_fixture(self, dialect, as_sql):
        context = op_fixture(dialect, as_sql)
        t1 = Table(
            "ins_table",
            MetaData(),
            Column("id", Integer, primary_key=True),
            Column("v1", String()),
            Column("v2", String()),
        )
        return context, t1

    def _test_bulk_insert(self, dialect, as_sql):
        context, t1 = self._table_fixture(dialect, as_sql)

        op.bulk_insert(
            t1,
            [
                {"id": 1, "v1": "row v1", "v2": "row v5"},
                {"id": 2, "v1": "row v2", "v2": "row v6"},
                {"id": 3, "v1": "row v3", "v2": "row v7"},
                {"id": 4, "v1": "row v4", "v2": "row v8"},
            ],
        )
        return context

    def _test_bulk_insert_single(self, dialect, as_sql):
        context, t1 = self._table_fixture(dialect, as_sql)

        op.bulk_insert(t1, [{"id": 1, "v1": "row v1", "v2": "row v5"}])
        return context

    def _test_bulk_insert_single_bigt(self, dialect, as_sql):
        context, t1 = self._big_t_table_fixture(dialect, as_sql)

        op.bulk_insert(t1, [{"id": 1, "v1": "row v1", "v2": "row v5"}])
        return context

    def test_bulk_insert(self):
        context = self._test_bulk_insert("default", False)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) VALUES (:id, :v1, :v2)"
        )

    def test_bulk_insert_wrong_cols(self):
        context = op_fixture("postgresql")
        t1 = table(
            "ins_table",
            column("id", Integer),
            column("v1", String()),
            column("v2", String()),
        )
        op.bulk_insert(t1, [{"v1": "row v1"}])
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (%(id)s, %(v1)s, %(v2)s)"
        )

    def test_bulk_insert_no_rows(self):
        context, t1 = self._table_fixture("default", False)

        op.bulk_insert(t1, [])
        context.assert_()

    def test_bulk_insert_pg(self):
        context = self._test_bulk_insert("postgresql", False)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (%(id)s, %(v1)s, %(v2)s)"
        )

    def test_bulk_insert_pg_single(self):
        context = self._test_bulk_insert_single("postgresql", False)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (%(id)s, %(v1)s, %(v2)s)"
        )

    def test_bulk_insert_pg_single_as_sql(self):
        context = self._test_bulk_insert_single("postgresql", True)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) VALUES (1, 'row v1', 'row v5')"
        )

    def test_bulk_insert_pg_single_big_t_as_sql(self):
        context = self._test_bulk_insert_single_bigt("postgresql", True)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (1, 'row v1', 'row v5')"
        )

    def test_bulk_insert_mssql(self):
        context = self._test_bulk_insert("mssql", False)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) VALUES (:id, :v1, :v2)"
        )

    def test_bulk_insert_inline_literal_as_sql(self):
        context = op_fixture("postgresql", True)

        class MyType(TypeEngine):
            pass

        t1 = table("t", column("id", Integer), column("data", MyType()))

        op.bulk_insert(
            t1,
            [
                {"id": 1, "data": op.inline_literal("d1")},
                {"id": 2, "data": op.inline_literal("d2")},
            ],
        )
        context.assert_(
            "INSERT INTO t (id, data) VALUES (1, 'd1')",
            "INSERT INTO t (id, data) VALUES (2, 'd2')",
        )

    def test_bulk_insert_as_sql(self):
        context = self._test_bulk_insert("default", True)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (1, 'row v1', 'row v5')",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (2, 'row v2', 'row v6')",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (3, 'row v3', 'row v7')",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (4, 'row v4', 'row v8')",
        )

    def test_bulk_insert_as_sql_pg(self):
        context = self._test_bulk_insert("postgresql", True)
        context.assert_(
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (1, 'row v1', 'row v5')",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (2, 'row v2', 'row v6')",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (3, 'row v3', 'row v7')",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (4, 'row v4', 'row v8')",
        )

    def test_bulk_insert_as_sql_mssql(self):
        context = self._test_bulk_insert("mssql", True)
        # SQL server requires IDENTITY_INSERT
        # TODO: figure out if this is safe to enable for a table that
        # doesn't have an IDENTITY column
        context.assert_(
            "SET IDENTITY_INSERT ins_table ON",
            "GO",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (1, 'row v1', 'row v5')",
            "GO",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (2, 'row v2', 'row v6')",
            "GO",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (3, 'row v3', 'row v7')",
            "GO",
            "INSERT INTO ins_table (id, v1, v2) "
            "VALUES (4, 'row v4', 'row v8')",
            "GO",
            "SET IDENTITY_INSERT ins_table OFF",
            "GO",
        )

    def test_bulk_insert_from_new_table(self):
        context = op_fixture("postgresql", True)
        t1 = op.create_table(
            "ins_table",
            Column("id", Integer),
            Column("v1", String()),
            Column("v2", String()),
        )
        op.bulk_insert(
            t1,
            [
                {"id": 1, "v1": "row v1", "v2": "row v5"},
                {"id": 2, "v1": "row v2", "v2": "row v6"},
            ],
        )
        context.assert_(
            "CREATE TABLE ins_table (id INTEGER, v1 VARCHAR, v2 VARCHAR)",
            "INSERT INTO ins_table (id, v1, v2) VALUES "
            "(1, 'row v1', 'row v5')",
            "INSERT INTO ins_table (id, v1, v2) VALUES "
            "(2, 'row v2', 'row v6')",
        )

    def test_invalid_format(self):
        context, t1 = self._table_fixture("sqlite", False)
        assert_raises_message(
            TypeError, "List expected", op.bulk_insert, t1, {"id": 5}
        )

        assert_raises_message(
            TypeError,
            "List of dictionaries expected",
            op.bulk_insert,
            t1,
            [(5,)],
        )


class RoundTripTest(TestBase):
    __only_on__ = "sqlite"

    def setUp(self):
        self.conn = config.db.connect()
        with self.conn.begin():
            self.conn.execute(
                text(
                    """
                create table foo(
                    id integer primary key,
                    data varchar(50),
                    x integer
                )
            """
                )
            )
        context = MigrationContext.configure(self.conn)
        self.op = op.Operations(context)
        self.t1 = table("foo", column("id"), column("data"), column("x"))

        self.trans = self.conn.begin()

    def tearDown(self):
        self.trans.rollback()
        with self.conn.begin():
            self.conn.execute(text("drop table foo"))
        self.conn.close()

    def test_single_insert_round_trip(self):
        self.op.bulk_insert(self.t1, [{"data": "d1", "x": "x1"}])

        eq_(
            self.conn.execute(text("select id, data, x from foo")).fetchall(),
            [(1, "d1", "x1")],
        )

    def test_bulk_insert_round_trip(self):
        self.op.bulk_insert(
            self.t1,
            [
                {"data": "d1", "x": "x1"},
                {"data": "d2", "x": "x2"},
                {"data": "d3", "x": "x3"},
            ],
        )

        eq_(
            self.conn.execute(text("select id, data, x from foo")).fetchall(),
            [(1, "d1", "x1"), (2, "d2", "x2"), (3, "d3", "x3")],
        )

    def test_bulk_insert_inline_literal(self):
        class MyType(TypeEngine):
            pass

        t1 = table("foo", column("id", Integer), column("data", MyType()))

        self.op.bulk_insert(
            t1,
            [
                {"id": 1, "data": self.op.inline_literal("d1")},
                {"id": 2, "data": self.op.inline_literal("d2")},
            ],
            multiinsert=False,
        )

        eq_(
            self.conn.execute(text("select id, data from foo")).fetchall(),
            [(1, "d1"), (2, "d2")],
        )

    def test_bulk_insert_from_new_table(self):
        t1 = self.op.create_table(
            "ins_table",
            Column("id", Integer),
            Column("v1", String()),
            Column("v2", String()),
        )
        self.op.bulk_insert(
            t1,
            [
                {"id": 1, "v1": "row v1", "v2": "row v5"},
                {"id": 2, "v1": "row v2", "v2": "row v6"},
            ],
        )
        eq_(
            self.conn.execute(
                text("select id, v1, v2 from ins_table order by id")
            ).fetchall(),
            [(1, "row v1", "row v5"), (2, "row v2", "row v6")],
        )