-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathtest_insert.py
More file actions
63 lines (54 loc) · 2.09 KB
/
Copy pathtest_insert.py
File metadata and controls
63 lines (54 loc) · 2.09 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
import sqlalchemy.exc
from asynch.errors import TypeMismatchError
from sqlalchemy import Column, func, text
from clickhouse_sqlalchemy import Table, engines, types
from tests.testcase import AsynchSessionTestCase
class NativeInsertTestCase(AsynchSessionTestCase):
async def test_rowcount_return1(self):
metadata = self.metadata()
table = Table(
'test', metadata,
Column('x', types.UInt32, primary_key=True),
engines.Memory()
)
await self.run_sync(metadata.drop_all)
await self.run_sync(metadata.create_all)
rv = await self.session.execute(
table.insert(),
[{'x': x} for x in range(5)]
)
self.assertEqual(rv.rowcount, 5)
self.assertEqual(
await self.session.run_sync(
lambda sc: sc.query(func.count()).select_from(table).scalar()
),
5
)
rv = await self.session.execute(
text('INSERT INTO test SELECT * FROM system.numbers LIMIT 5')
)
self.assertEqual(rv.rowcount, -1)
async def test_types_check(self):
metadata = self.metadata()
table = Table(
'test', metadata,
Column('x', types.UInt32, primary_key=True),
engines.Memory()
)
await self.run_sync(metadata.drop_all)
await self.run_sync(metadata.create_all)
with self.assertRaises(sqlalchemy.exc.DBAPIError) as ex:
await self.session.execute(
table.insert(),
[{'x': -1}],
execution_options=dict(types_check=True),
)
self.assertTrue(isinstance(ex.exception.orig, TypeMismatchError))
self.assertIn('-1 for column "x"', str(ex.exception))
with self.assertRaises(sqlalchemy.exc.DBAPIError) as ex:
await self.session.execute(table.insert(), {'x': -1})
self.assertTrue(isinstance(ex.exception.orig, TypeMismatchError))
self.assertIn(
'Repeat query with types_check=True for detailed info',
str(ex.exception)
)