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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
import pytest
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import close_all_sessions
from sqlalchemy_utils import (
CompositeType,
Currency,
CurrencyType,
i18n,
NumericRangeType,
register_composites,
remove_composite_listeners
)
from sqlalchemy_utils.types import pg_composite
from sqlalchemy_utils.types.range import intervals
@pytest.mark.usefixtures('postgresql_dsn')
class TestCompositeTypeWithRegularTypes:
@pytest.fixture
def Account(self, Base):
class Account(Base):
__tablename__ = 'account'
id = sa.Column(sa.Integer, primary_key=True)
balance = sa.Column(
CompositeType(
'money_type',
[
sa.Column('currency', sa.String),
sa.Column('amount', sa.Integer)
]
)
)
return Account
@pytest.fixture
def init_models(self, Account):
pass
def test_parameter_processing(self, session, Account):
account = Account(
balance=('USD', 15)
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == 'USD'
assert account.balance.amount == 15
def test_non_ascii_chars(self, session, Account):
account = Account(
balance=('ääöö', 15)
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == 'ääöö'
assert account.balance.amount == 15
def test_dict_input(self, session, Account):
account = Account(
balance={'currency': 'USD', 'amount': 15}
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == 'USD'
assert account.balance.amount == 15
def test_incomplete_dict(self, session, Account):
"""
Postgres doesn't allow non-nullabe fields in Composite Types:
"no constraints (such as NOT NULL) can presently be included"
(https://www.postgresql.org/docs/10/rowtypes.html)
So this should be allowed.
"""
account = Account(
balance={'amount': 15}
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency is None
assert account.balance.amount == 15
@pytest.mark.skipif('i18n.babel is None')
@pytest.mark.usefixtures('postgresql_dsn')
class TestCompositeTypeWithTypeDecorators:
@pytest.fixture
def Account(self, Base):
class Account(Base):
__tablename__ = 'account'
id = sa.Column(sa.Integer, primary_key=True)
balance = sa.Column(
CompositeType(
'money_type',
[
sa.Column('currency', CurrencyType),
sa.Column('amount', sa.Integer)
]
)
)
return Account
@pytest.fixture
def init_models(self, Account):
i18n.get_locale = lambda: i18n.babel.Locale('en')
def test_result_set_processing(self, session, Account):
account = Account(
balance=('USD', 15)
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == Currency('USD')
assert account.balance.amount == 15
def test_parameter_processing(self, session, Account):
account = Account(
balance=(Currency('USD'), 15)
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == Currency('USD')
assert account.balance.amount == 15
def test_dict_input(self, session, Account):
account = Account(
balance={'currency': Currency('USD'), 'amount': 15}
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == 'USD'
assert account.balance.amount == 15
@pytest.mark.skipif('i18n.babel is None')
@pytest.mark.usefixtures('postgresql_dsn')
class TestCompositeTypeInsideArray:
@pytest.fixture
def type_(self):
return CompositeType(
'money_type',
[
sa.Column('currency', CurrencyType),
sa.Column('amount', sa.Integer)
]
)
@pytest.fixture
def Account(self, Base, type_):
class Account(Base):
__tablename__ = 'account'
id = sa.Column(sa.Integer, primary_key=True)
balances = sa.Column(
ARRAY(type_, dimensions=1)
)
return Account
@pytest.fixture
def init_models(self, Account):
i18n.get_locale = lambda: i18n.babel.Locale('en')
def test_parameter_processing(self, session, type_, Account):
account = Account(
balances=[
type_.type_cls(Currency('USD'), 15),
type_.type_cls(Currency('AUD'), 20)
]
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balances[0].currency == Currency('USD')
assert account.balances[0].amount == 15
assert account.balances[1].currency == Currency('AUD')
assert account.balances[1].amount == 20
def test_dict_input(self, session, type_, Account):
account = Account(
balances=[
{'currency': Currency('USD'), 'amount': 15},
{'currency': Currency('AUD'), 'amount': 20}
]
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balances[0].currency == Currency('USD')
assert account.balances[0].amount == 15
assert account.balances[1].currency == Currency('AUD')
assert account.balances[1].amount == 20
@pytest.mark.skipif('intervals is None')
@pytest.mark.usefixtures('postgresql_dsn')
class TestCompositeTypeWithRangeTypeInsideArray:
@pytest.fixture
def type_(self):
return CompositeType(
'category',
[
sa.Column('scale', NumericRangeType),
sa.Column('name', sa.String)
]
)
@pytest.fixture
def Account(self, Base, type_):
class Account(Base):
__tablename__ = 'account'
id = sa.Column(sa.Integer, primary_key=True)
categories = sa.Column(
ARRAY(type_, dimensions=1)
)
return Account
@pytest.fixture
def init_models(self, Account):
pass
def test_parameter_processing_with_named_tuple(
self,
session,
type_,
Account
):
account = Account(
categories=[
type_.type_cls(
intervals.DecimalInterval([15, 18]),
'bad'
),
type_.type_cls(
intervals.DecimalInterval([18, 20]),
'good'
)
]
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert (
account.categories[0].scale == intervals.DecimalInterval([15, 18])
)
assert account.categories[0].name == 'bad'
assert (
account.categories[1].scale == intervals.DecimalInterval([18, 20])
)
assert account.categories[1].name == 'good'
def test_parameter_processing_with_tuple(self, session, Account):
account = Account(
categories=[
(intervals.DecimalInterval([15, 18]), 'bad'),
(intervals.DecimalInterval([18, 20]), 'good')
]
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert (
account.categories[0].scale == intervals.DecimalInterval([15, 18])
)
assert account.categories[0].name == 'bad'
assert (
account.categories[1].scale == intervals.DecimalInterval([18, 20])
)
assert account.categories[1].name == 'good'
def test_parameter_processing_with_nulls_as_composite_fields(
self,
session,
Account
):
account = Account(
categories=[
(None, 'bad'),
(intervals.DecimalInterval([18, 20]), None)
]
)
session.add(account)
session.commit()
assert account.categories[0].scale is None
assert account.categories[0].name == 'bad'
assert (
account.categories[1].scale == intervals.DecimalInterval([18, 20])
)
assert account.categories[1].name is None
def test_parameter_processing_with_nulls_as_composites(
self,
session,
Account
):
account = Account(
categories=[
(None, None),
None
]
)
session.add(account)
session.commit()
assert account.categories[0].scale is None
assert account.categories[0].name is None
assert account.categories[1] is None
@pytest.mark.usefixtures('postgresql_dsn')
class TestCompositeTypeWhenTypeAlreadyExistsInDatabase:
@pytest.fixture
def Account(self, Base):
pg_composite.registered_composites = {}
type_ = CompositeType(
'money_type',
[
sa.Column('currency', sa.String),
sa.Column('amount', sa.Integer)
]
)
class Account(Base):
__tablename__ = 'account'
id = sa.Column(sa.Integer, primary_key=True)
balance = sa.Column(type_)
return Account
@pytest.fixture
def session(self, request, engine, connection, Base, Account):
sa.orm.configure_mappers()
Session = sessionmaker(bind=connection)
session = Session()
session.execute(
sa.text("CREATE TYPE money_type AS (currency VARCHAR, amount INTEGER)")
)
session.execute(
sa.text("""CREATE TABLE account (
id SERIAL, balance MONEY_TYPE, PRIMARY KEY(id)
)""")
)
def teardown():
session.execute(sa.text('DROP TABLE account'))
session.execute(sa.text('DROP TYPE money_type'))
session.commit()
close_all_sessions()
connection.close()
remove_composite_listeners()
engine.dispose()
register_composites(connection)
request.addfinalizer(teardown)
return session
def test_parameter_processing(self, session, Account):
account = Account(
balance=('USD', 15),
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == 'USD'
assert account.balance.amount == 15
@pytest.mark.usefixtures('postgresql_dsn')
class TestCompositeTypeWithMixedCase:
@pytest.fixture
def Account(self, Base):
pg_composite.registered_composites = {}
type_ = CompositeType(
'MoneyType',
[
sa.Column('currency', sa.String),
sa.Column('amount', sa.Integer)
]
)
class Account(Base):
__tablename__ = 'account'
id = sa.Column(sa.Integer, primary_key=True)
balance = sa.Column(type_)
return Account
@pytest.fixture
def session(self, request, engine, connection, Base, Account):
sa.orm.configure_mappers()
Session = sessionmaker(bind=connection)
try:
# Enable sqlalchemy 2.0 behavior
session = Session(future=True)
except TypeError:
# sqlalchemy 1.3
session = Session()
session.execute(
sa.text('CREATE TYPE "MoneyType" AS (currency VARCHAR, amount INTEGER)')
)
session.execute(sa.text(
"""CREATE TABLE account (
id SERIAL, balance "MoneyType", PRIMARY KEY(id)
)"""
))
def teardown():
session.execute(sa.text('DROP TABLE account'))
session.execute(sa.text('DROP TYPE "MoneyType"'))
session.commit()
close_all_sessions()
connection.close()
remove_composite_listeners()
engine.dispose()
register_composites(connection)
request.addfinalizer(teardown)
return session
def test_parameter_processing(self, session, Account):
account = Account(
balance=('USD', 15),
)
session.add(account)
session.commit()
account = session.query(Account).first()
assert account.balance.currency == 'USD'
assert account.balance.amount == 15
|