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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
from sqlalchemy import testing
from sqlalchemy.testing import eq_
from sqlalchemy.testing.schema import Table, Column
from sqlalchemy.testing import fixtures
from sqlalchemy import Integer, String, ForeignKey, FetchedValue
from sqlalchemy.orm import mapper, Session
from sqlalchemy.testing.assertsql import CompiledSQL
from test.orm import _fixtures
class BulkTest(testing.AssertsExecutionResults):
run_inserts = None
run_define_tables = 'each'
class BulkInsertUpdateTest(BulkTest, _fixtures.FixtureTest):
@classmethod
def setup_mappers(cls):
User, Address = cls.classes("User", "Address")
u, a = cls.tables("users", "addresses")
mapper(User, u)
mapper(Address, a)
def test_bulk_save_return_defaults(self):
User, = self.classes("User",)
s = Session()
objects = [
User(name="u1"),
User(name="u2"),
User(name="u3")
]
assert 'id' not in objects[0].__dict__
with self.sql_execution_asserter() as asserter:
s.bulk_save_objects(objects, return_defaults=True)
asserter.assert_(
CompiledSQL(
"INSERT INTO users (name) VALUES (:name)",
[{'name': 'u1'}]
),
CompiledSQL(
"INSERT INTO users (name) VALUES (:name)",
[{'name': 'u2'}]
),
CompiledSQL(
"INSERT INTO users (name) VALUES (:name)",
[{'name': 'u3'}]
),
)
eq_(objects[0].__dict__['id'], 1)
def test_bulk_save_no_defaults(self):
User, = self.classes("User",)
s = Session()
objects = [
User(name="u1"),
User(name="u2"),
User(name="u3")
]
assert 'id' not in objects[0].__dict__
with self.sql_execution_asserter() as asserter:
s.bulk_save_objects(objects)
asserter.assert_(
CompiledSQL(
"INSERT INTO users (name) VALUES (:name)",
[{'name': 'u1'}, {'name': 'u2'}, {'name': 'u3'}]
),
)
assert 'id' not in objects[0].__dict__
def test_bulk_save_updated_include_unchanged(self):
User, = self.classes("User",)
s = Session(expire_on_commit=False)
objects = [
User(name="u1"),
User(name="u2"),
User(name="u3")
]
s.add_all(objects)
s.commit()
objects[0].name = 'u1new'
objects[2].name = 'u3new'
s = Session()
with self.sql_execution_asserter() as asserter:
s.bulk_save_objects(objects, update_changed_only=False)
asserter.assert_(
CompiledSQL(
"UPDATE users SET name=:name WHERE "
"users.id = :users_id",
[{'users_id': 1, 'name': 'u1new'},
{'users_id': 2, 'name': 'u2'},
{'users_id': 3, 'name': 'u3new'}]
)
)
def test_bulk_update(self):
User, = self.classes("User",)
s = Session(expire_on_commit=False)
objects = [
User(name="u1"),
User(name="u2"),
User(name="u3")
]
s.add_all(objects)
s.commit()
s = Session()
with self.sql_execution_asserter() as asserter:
s.bulk_update_mappings(
User,
[{'id': 1, 'name': 'u1new'},
{'id': 2, 'name': 'u2'},
{'id': 3, 'name': 'u3new'}]
)
asserter.assert_(
CompiledSQL(
"UPDATE users SET name=:name WHERE users.id = :users_id",
[{'users_id': 1, 'name': 'u1new'},
{'users_id': 2, 'name': 'u2'},
{'users_id': 3, 'name': 'u3new'}]
)
)
def test_bulk_insert(self):
User, = self.classes("User",)
s = Session()
with self.sql_execution_asserter() as asserter:
s.bulk_insert_mappings(
User,
[{'id': 1, 'name': 'u1new'},
{'id': 2, 'name': 'u2'},
{'id': 3, 'name': 'u3new'}]
)
asserter.assert_(
CompiledSQL(
"INSERT INTO users (id, name) VALUES (:id, :name)",
[{'id': 1, 'name': 'u1new'},
{'id': 2, 'name': 'u2'},
{'id': 3, 'name': 'u3new'}]
)
)
class BulkUDPostfetchTest(BulkTest, fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table(
'a', metadata,
Column(
'id', Integer,
primary_key=True,
test_needs_autoincrement=True),
Column('x', Integer),
Column('y', Integer, server_default=FetchedValue(), server_onupdate=FetchedValue()))
@classmethod
def setup_classes(cls):
class A(cls.Comparable):
pass
@classmethod
def setup_mappers(cls):
A = cls.classes.A
a = cls.tables.a
mapper(A, a)
def test_insert_w_fetch(self):
A = self.classes.A
s = Session()
a1 = A(x=1)
s.bulk_save_objects([a1])
s.commit()
def test_update_w_fetch(self):
A = self.classes.A
s = Session()
a1 = A(x=1, y=2)
s.add(a1)
s.commit()
eq_(a1.id, 1) # force a load
a1.x = 5
s.expire(a1, ['y'])
assert 'y' not in a1.__dict__
s.bulk_save_objects([a1])
s.commit()
eq_(a1.x, 5)
eq_(a1.y, 2)
class BulkInheritanceTest(BulkTest, fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table(
'people', metadata,
Column(
'person_id', Integer,
primary_key=True,
test_needs_autoincrement=True),
Column('name', String(50)),
Column('type', String(30)))
Table(
'engineers', metadata,
Column(
'person_id', Integer,
ForeignKey('people.person_id'),
primary_key=True),
Column('status', String(30)),
Column('primary_language', String(50)))
Table(
'managers', metadata,
Column(
'person_id', Integer,
ForeignKey('people.person_id'),
primary_key=True),
Column('status', String(30)),
Column('manager_name', String(50)))
Table(
'boss', metadata,
Column(
'boss_id', Integer,
ForeignKey('managers.person_id'),
primary_key=True),
Column('golf_swing', String(30)))
@classmethod
def setup_classes(cls):
class Base(cls.Comparable):
pass
class Person(Base):
pass
class Engineer(Person):
pass
class Manager(Person):
pass
class Boss(Manager):
pass
@classmethod
def setup_mappers(cls):
Person, Engineer, Manager, Boss = \
cls.classes('Person', 'Engineer', 'Manager', 'Boss')
p, e, m, b = cls.tables('people', 'engineers', 'managers', 'boss')
mapper(
Person, p, polymorphic_on=p.c.type,
polymorphic_identity='person')
mapper(Engineer, e, inherits=Person, polymorphic_identity='engineer')
mapper(Manager, m, inherits=Person, polymorphic_identity='manager')
mapper(Boss, b, inherits=Manager, polymorphic_identity='boss')
def test_bulk_save_joined_inh_return_defaults(self):
Person, Engineer, Manager, Boss = \
self.classes('Person', 'Engineer', 'Manager', 'Boss')
s = Session()
objects = [
Manager(name='m1', status='s1', manager_name='mn1'),
Engineer(name='e1', status='s2', primary_language='l1'),
Engineer(name='e2', status='s3', primary_language='l2'),
Boss(
name='b1', status='s3', manager_name='mn2',
golf_swing='g1')
]
assert 'person_id' not in objects[0].__dict__
with self.sql_execution_asserter() as asserter:
s.bulk_save_objects(objects, return_defaults=True)
asserter.assert_(
CompiledSQL(
"INSERT INTO people (name, type) VALUES (:name, :type)",
[{'type': 'manager', 'name': 'm1'}]
),
CompiledSQL(
"INSERT INTO managers (person_id, status, manager_name) "
"VALUES (:person_id, :status, :manager_name)",
[{'person_id': 1, 'status': 's1', 'manager_name': 'mn1'}]
),
CompiledSQL(
"INSERT INTO people (name, type) VALUES (:name, :type)",
[{'type': 'engineer', 'name': 'e1'}]
),
CompiledSQL(
"INSERT INTO people (name, type) VALUES (:name, :type)",
[{'type': 'engineer', 'name': 'e2'}]
),
CompiledSQL(
"INSERT INTO engineers (person_id, status, primary_language) "
"VALUES (:person_id, :status, :primary_language)",
[{'person_id': 2, 'status': 's2', 'primary_language': 'l1'},
{'person_id': 3, 'status': 's3', 'primary_language': 'l2'}]
),
CompiledSQL(
"INSERT INTO people (name, type) VALUES (:name, :type)",
[{'type': 'boss', 'name': 'b1'}]
),
CompiledSQL(
"INSERT INTO managers (person_id, status, manager_name) "
"VALUES (:person_id, :status, :manager_name)",
[{'person_id': 4, 'status': 's3', 'manager_name': 'mn2'}]
),
CompiledSQL(
"INSERT INTO boss (boss_id, golf_swing) VALUES "
"(:boss_id, :golf_swing)",
[{'boss_id': 4, 'golf_swing': 'g1'}]
)
)
eq_(objects[0].__dict__['person_id'], 1)
eq_(objects[3].__dict__['person_id'], 4)
eq_(objects[3].__dict__['boss_id'], 4)
def test_bulk_save_joined_inh_no_defaults(self):
Person, Engineer, Manager, Boss = \
self.classes('Person', 'Engineer', 'Manager', 'Boss')
s = Session()
with self.sql_execution_asserter() as asserter:
s.bulk_save_objects([
Manager(
person_id=1,
name='m1', status='s1', manager_name='mn1'),
Engineer(
person_id=2,
name='e1', status='s2', primary_language='l1'),
Engineer(
person_id=3,
name='e2', status='s3', primary_language='l2'),
Boss(
person_id=4, boss_id=4,
name='b1', status='s3', manager_name='mn2',
golf_swing='g1')
],
)
# the only difference here is that common classes are grouped together.
# at the moment it doesn't lump all the "people" tables from
# different classes together.
asserter.assert_(
CompiledSQL(
"INSERT INTO people (person_id, name, type) VALUES "
"(:person_id, :name, :type)",
[{'person_id': 1, 'type': 'manager', 'name': 'm1'}]
),
CompiledSQL(
"INSERT INTO managers (person_id, status, manager_name) "
"VALUES (:person_id, :status, :manager_name)",
[{'status': 's1', 'person_id': 1, 'manager_name': 'mn1'}]
),
CompiledSQL(
"INSERT INTO people (person_id, name, type) VALUES "
"(:person_id, :name, :type)",
[{'person_id': 2, 'type': 'engineer', 'name': 'e1'},
{'person_id': 3, 'type': 'engineer', 'name': 'e2'}]
),
CompiledSQL(
"INSERT INTO engineers (person_id, status, primary_language) "
"VALUES (:person_id, :status, :primary_language)",
[{'person_id': 2, 'status': 's2', 'primary_language': 'l1'},
{'person_id': 3, 'status': 's3', 'primary_language': 'l2'}]
),
CompiledSQL(
"INSERT INTO people (person_id, name, type) VALUES "
"(:person_id, :name, :type)",
[{'person_id': 4, 'type': 'boss', 'name': 'b1'}]
),
CompiledSQL(
"INSERT INTO managers (person_id, status, manager_name) "
"VALUES (:person_id, :status, :manager_name)",
[{'status': 's3', 'person_id': 4, 'manager_name': 'mn2'}]
),
CompiledSQL(
"INSERT INTO boss (boss_id, golf_swing) VALUES "
"(:boss_id, :golf_swing)",
[{'boss_id': 4, 'golf_swing': 'g1'}]
)
)
def test_bulk_insert_joined_inh_return_defaults(self):
Person, Engineer, Manager, Boss = \
self.classes('Person', 'Engineer', 'Manager', 'Boss')
s = Session()
with self.sql_execution_asserter() as asserter:
s.bulk_insert_mappings(
Boss,
[
dict(
name='b1', status='s1', manager_name='mn1',
golf_swing='g1'
),
dict(
name='b2', status='s2', manager_name='mn2',
golf_swing='g2'
),
dict(
name='b3', status='s3', manager_name='mn3',
golf_swing='g3'
),
], return_defaults=True
)
asserter.assert_(
CompiledSQL(
"INSERT INTO people (name) VALUES (:name)",
[{'name': 'b1'}]
),
CompiledSQL(
"INSERT INTO people (name) VALUES (:name)",
[{'name': 'b2'}]
),
CompiledSQL(
"INSERT INTO people (name) VALUES (:name)",
[{'name': 'b3'}]
),
CompiledSQL(
"INSERT INTO managers (person_id, status, manager_name) "
"VALUES (:person_id, :status, :manager_name)",
[{'person_id': 1, 'status': 's1', 'manager_name': 'mn1'},
{'person_id': 2, 'status': 's2', 'manager_name': 'mn2'},
{'person_id': 3, 'status': 's3', 'manager_name': 'mn3'}]
),
CompiledSQL(
"INSERT INTO boss (boss_id, golf_swing) VALUES "
"(:boss_id, :golf_swing)",
[{'golf_swing': 'g1', 'boss_id': 1},
{'golf_swing': 'g2', 'boss_id': 2},
{'golf_swing': 'g3', 'boss_id': 3}]
)
)
|