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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
|
from sqlalchemy.testing import assert_raises, assert_raises_message
import sqlalchemy as sa
from sqlalchemy import testing
from sqlalchemy import Integer, String, ForeignKey, \
select
from sqlalchemy.testing.schema import Table, Column
from sqlalchemy.orm import mapper, relationship, \
CompositeProperty, aliased
from sqlalchemy.orm import composite, Session, configure_mappers
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
class PointTest(fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table('graphs', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('name', String(30)))
Table('edges', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('graph_id', Integer,
ForeignKey('graphs.id')),
Column('x1', Integer),
Column('y1', Integer),
Column('x2', Integer),
Column('y2', Integer),
)
@classmethod
def setup_mappers(cls):
graphs, edges = cls.tables.graphs, cls.tables.edges
class Point(cls.Comparable):
def __init__(self, x, y):
self.x = x
self.y = y
def __composite_values__(self):
return [self.x, self.y]
__hash__ = None
def __eq__(self, other):
return isinstance(other, Point) and \
other.x == self.x and \
other.y == self.y
def __ne__(self, other):
return not isinstance(other, Point) or \
not self.__eq__(other)
class Graph(cls.Comparable):
pass
class Edge(cls.Comparable):
def __init__(self, *args):
if args:
self.start, self.end = args
mapper(Graph, graphs, properties={
'edges':relationship(Edge)
})
mapper(Edge, edges, properties={
'start':sa.orm.composite(Point, edges.c.x1, edges.c.y1),
'end': sa.orm.composite(Point, edges.c.x2, edges.c.y2)
})
def _fixture(self):
Graph, Edge, Point = (self.classes.Graph,
self.classes.Edge,
self.classes.Point)
sess = Session()
g = Graph(id=1, edges=[
Edge(Point(3, 4), Point(5, 6)),
Edge(Point(14, 5), Point(2, 7))
])
sess.add(g)
sess.commit()
return sess
def test_early_configure(self):
# test [ticket:2935], that we can call a composite
# expression before configure_mappers()
Edge = self.classes.Edge
Edge.start.__clause_element__()
def test_round_trip(self):
Graph, Point = self.classes.Graph, self.classes.Point
sess = self._fixture()
g1 = sess.query(Graph).first()
sess.close()
g = sess.query(Graph).get(g1.id)
eq_(
[(e.start, e.end) for e in g.edges],
[
(Point(3, 4), Point(5, 6)),
(Point(14, 5), Point(2, 7)),
]
)
def test_detect_change(self):
Graph, Edge, Point = (self.classes.Graph,
self.classes.Edge,
self.classes.Point)
sess = self._fixture()
g = sess.query(Graph).first()
g.edges[1].end = Point(18, 4)
sess.commit()
e = sess.query(Edge).get(g.edges[1].id)
eq_(e.end, Point(18, 4))
def test_not_none(self):
Graph, Edge, Point = (self.classes.Graph,
self.classes.Edge,
self.classes.Point)
# current contract. the composite is None
# when hasn't been populated etc. on a
# pending/transient object.
e1 = Edge()
assert e1.end is None
sess = Session()
sess.add(e1)
# however, once it's persistent, the code as of 0.7.3
# would unconditionally populate it, even though it's
# all None. I think this usage contract is inconsistent,
# and it would be better that the composite is just
# created unconditionally in all cases.
# but as we are just trying to fix [ticket:2308] and
# [ticket:2309] without changing behavior we maintain
# that only "persistent" gets the composite with the
# Nones
sess.flush()
assert e1.end is not None
def test_eager_load(self):
Graph, Point = self.classes.Graph, self.classes.Point
sess = self._fixture()
g = sess.query(Graph).first()
sess.close()
def go():
g2 = sess.query(Graph).\
options(sa.orm.joinedload('edges')).\
get(g.id)
eq_(
[(e.start, e.end) for e in g2.edges],
[
(Point(3, 4), Point(5, 6)),
(Point(14, 5), Point(2, 7)),
]
)
self.assert_sql_count(testing.db, go, 1)
def test_comparator(self):
Graph, Edge, Point = (self.classes.Graph,
self.classes.Edge,
self.classes.Point)
sess = self._fixture()
g = sess.query(Graph).first()
assert sess.query(Edge).\
filter(Edge.start == Point(3, 4)).one() is \
g.edges[0]
assert sess.query(Edge).\
filter(Edge.start != Point(3, 4)).first() is \
g.edges[1]
eq_(
sess.query(Edge).filter(Edge.start == None).all(),
[]
)
def test_comparator_aliased(self):
Graph, Edge, Point = (self.classes.Graph,
self.classes.Edge,
self.classes.Point)
sess = self._fixture()
g = sess.query(Graph).first()
ea = aliased(Edge)
assert sess.query(ea).\
filter(ea.start != Point(3, 4)).first() is \
g.edges[1]
def test_get_history(self):
Edge = self.classes.Edge
Point = self.classes.Point
from sqlalchemy.orm.attributes import get_history
e1 = Edge()
e1.start = Point(1,2)
eq_(
get_history(e1, 'start'),
([Point(x=1, y=2)], (), [Point(x=None, y=None)])
)
eq_(
get_history(e1, 'end'),
((), [Point(x=None, y=None)], ())
)
def test_query_cols_legacy(self):
Edge = self.classes.Edge
sess = self._fixture()
eq_(
sess.query(Edge.start.clauses, Edge.end.clauses).all(),
[(3, 4, 5, 6), (14, 5, 2, 7)]
)
def test_query_cols(self):
Edge = self.classes.Edge
Point = self.classes.Point
sess = self._fixture()
start, end = Edge.start, Edge.end
eq_(
sess.query(start, end).filter(start == Point(3, 4)).all(),
[(Point(3, 4), Point(5, 6))]
)
def test_query_cols_labeled(self):
Edge = self.classes.Edge
Point = self.classes.Point
sess = self._fixture()
start, end = Edge.start, Edge.end
row = sess.query(start.label('s1'), end).filter(start == Point(3, 4)).first()
eq_(row.s1.x, 3)
eq_(row.s1.y, 4)
eq_(row.end.x, 5)
eq_(row.end.y, 6)
def test_delete(self):
Point = self.classes.Point
Graph, Edge = self.classes.Graph, self.classes.Edge
sess = self._fixture()
g = sess.query(Graph).first()
e = g.edges[1]
del e.end
sess.flush()
eq_(
sess.query(Edge.start, Edge.end).all(),
[
(Point(x=3, y=4), Point(x=5, y=6)),
(Point(x=14, y=5), Point(x=None, y=None))
]
)
def test_save_null(self):
"""test saving a null composite value
See google groups thread for more context:
http://groups.google.com/group/sqlalchemy/browse_thread/thread/0c6580a1761b2c29
"""
Graph, Edge = self.classes.Graph, self.classes.Edge
sess = Session()
g = Graph(id=1)
e = Edge(None, None)
g.edges.append(e)
sess.add(g)
sess.commit()
g2 = sess.query(Graph).get(1)
assert g2.edges[-1].start.x is None
assert g2.edges[-1].start.y is None
def test_expire(self):
Graph, Point = self.classes.Graph, self.classes.Point
sess = self._fixture()
g = sess.query(Graph).first()
e = g.edges[0]
sess.expire(e)
assert 'start' not in e.__dict__
assert e.start == Point(3, 4)
def test_default_value(self):
Edge = self.classes.Edge
e = Edge()
eq_(e.start, None)
class PrimaryKeyTest(fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table('graphs', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('version_id', Integer, primary_key=True,
nullable=True),
Column('name', String(30)))
@classmethod
def setup_mappers(cls):
graphs = cls.tables.graphs
class Version(cls.Comparable):
def __init__(self, id, version):
self.id = id
self.version = version
def __composite_values__(self):
return (self.id, self.version)
__hash__ = None
def __eq__(self, other):
return isinstance(other, Version) and \
other.id == self.id and \
other.version == self.version
def __ne__(self, other):
return not self.__eq__(other)
class Graph(cls.Comparable):
def __init__(self, version):
self.version = version
mapper(Graph, graphs, properties={
'version':sa.orm.composite(Version, graphs.c.id,
graphs.c.version_id)})
def _fixture(self):
Graph, Version = self.classes.Graph, self.classes.Version
sess = Session()
g = Graph(Version(1, 1))
sess.add(g)
sess.commit()
return sess
def test_get_by_col(self):
Graph = self.classes.Graph
sess = self._fixture()
g = sess.query(Graph).first()
g2 = sess.query(Graph).get([g.id, g.version_id])
eq_(g.version, g2.version)
def test_get_by_composite(self):
Graph, Version = self.classes.Graph, self.classes.Version
sess = self._fixture()
g = sess.query(Graph).first()
g2 = sess.query(Graph).get(Version(g.id, g.version_id))
eq_(g.version, g2.version)
@testing.fails_on('mssql', 'Cannot update identity columns.')
def test_pk_mutation(self):
Graph, Version = self.classes.Graph, self.classes.Version
sess = self._fixture()
g = sess.query(Graph).first()
g.version = Version(2, 1)
sess.commit()
g2 = sess.query(Graph).get(Version(2, 1))
eq_(g.version, g2.version)
@testing.fails_on_everything_except("sqlite")
def test_null_pk(self):
Graph, Version = self.classes.Graph, self.classes.Version
sess = Session()
# test pk with one column NULL
# only sqlite can really handle this
g = Graph(Version(2, None))
sess.add(g)
sess.commit()
g2 = sess.query(Graph).filter_by(version=Version(2, None)).one()
eq_(g.version, g2.version)
class DefaultsTest(fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table('foobars', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('x1', Integer, default=2),
Column('x2', Integer),
Column('x3', Integer, server_default="15"),
Column('x4', Integer)
)
@classmethod
def setup_mappers(cls):
foobars = cls.tables.foobars
class Foobar(cls.Comparable):
pass
class FBComposite(cls.Comparable):
def __init__(self, x1, x2, x3, x4):
self.goofy_x1 = x1
self.x2 = x2
self.x3 = x3
self.x4 = x4
def __composite_values__(self):
return self.goofy_x1, self.x2, self.x3, self.x4
__hash__ = None
def __eq__(self, other):
return other.goofy_x1 == self.goofy_x1 and \
other.x2 == self.x2 and \
other.x3 == self.x3 and \
other.x4 == self.x4
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "FBComposite(%r, %r, %r, %r)" % (
self.goofy_x1, self.x2, self.x3, self.x4
)
mapper(Foobar, foobars, properties=dict(
foob=sa.orm.composite(FBComposite,
foobars.c.x1,
foobars.c.x2,
foobars.c.x3,
foobars.c.x4)
))
def test_attributes_with_defaults(self):
Foobar, FBComposite = self.classes.Foobar, self.classes.FBComposite
sess = Session()
f1 = Foobar()
f1.foob = FBComposite(None, 5, None, None)
sess.add(f1)
sess.flush()
eq_(f1.foob, FBComposite(2, 5, 15, None))
f2 = Foobar()
sess.add(f2)
sess.flush()
eq_(f2.foob, FBComposite(2, None, 15, None))
def test_set_composite_values(self):
Foobar, FBComposite = self.classes.Foobar, self.classes.FBComposite
sess = Session()
f1 = Foobar()
f1.foob = FBComposite(None, 5, None, None)
sess.add(f1)
sess.flush()
eq_(f1.foob, FBComposite(2, 5, 15, None))
class MappedSelectTest(fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table('descriptions', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('d1', String(20)),
Column('d2', String(20)),
)
Table('values', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('description_id', Integer,
ForeignKey('descriptions.id'),
nullable=False),
Column('v1', String(20)),
Column('v2', String(20)),
)
@classmethod
def setup_mappers(cls):
values, descriptions = cls.tables.values, cls.tables.descriptions
class Descriptions(cls.Comparable):
pass
class Values(cls.Comparable):
pass
class CustomValues(cls.Comparable, list):
def __init__(self, *args):
self.extend(args)
def __composite_values__(self):
return self
desc_values = select(
[values, descriptions.c.d1, descriptions.c.d2],
descriptions.c.id == values.c.description_id
).alias('descriptions_values')
mapper(Descriptions, descriptions, properties={
'values': relationship(Values, lazy='dynamic'),
'custom_descriptions': composite(
CustomValues,
descriptions.c.d1,
descriptions.c.d2),
})
mapper(Values, desc_values, properties={
'custom_values': composite(CustomValues,
desc_values.c.v1,
desc_values.c.v2),
})
def test_set_composite_attrs_via_selectable(self):
Values, CustomValues, values, Descriptions, descriptions = (self.classes.Values,
self.classes.CustomValues,
self.tables.values,
self.classes.Descriptions,
self.tables.descriptions)
session = Session()
d = Descriptions(
custom_descriptions = CustomValues('Color', 'Number'),
values =[
Values(custom_values = CustomValues('Red', '5')),
Values(custom_values=CustomValues('Blue', '1'))
]
)
session.add(d)
session.commit()
eq_(
testing.db.execute(descriptions.select()).fetchall(),
[(1, 'Color', 'Number')]
)
eq_(
testing.db.execute(values.select()).fetchall(),
[(1, 1, 'Red', '5'), (2, 1, 'Blue', '1')]
)
class ManyToOneTest(fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table('a',
metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('b1', String(20)),
Column('b2_id', Integer, ForeignKey('b.id'))
)
Table('b', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('data', String(20))
)
@classmethod
def setup_mappers(cls):
a, b = cls.tables.a, cls.tables.b
class A(cls.Comparable):
pass
class B(cls.Comparable):
pass
class C(cls.Comparable):
def __init__(self, b1, b2):
self.b1, self.b2 = b1, b2
def __composite_values__(self):
return self.b1, self.b2
def __eq__(self, other):
return isinstance(other, C) and \
other.b1 == self.b1 and \
other.b2 == self.b2
mapper(A, a, properties={
'b2':relationship(B),
'c':composite(C, 'b1', 'b2')
})
mapper(B, b)
def test_early_configure(self):
# test [ticket:2935], that we can call a composite
# expression before configure_mappers()
A = self.classes.A
A.c.__clause_element__()
def test_persist(self):
A, C, B = (self.classes.A,
self.classes.C,
self.classes.B)
sess = Session()
sess.add(A(c=C('b1', B(data='b2'))))
sess.commit()
a1 = sess.query(A).one()
eq_(a1.c, C('b1', B(data='b2')))
def test_query(self):
A, C, B = (self.classes.A,
self.classes.C,
self.classes.B)
sess = Session()
b1, b2 = B(data='b1'), B(data='b2')
a1 = A(c=C('a1b1', b1))
a2 = A(c=C('a2b1', b2))
sess.add_all([a1, a2])
sess.commit()
eq_(
sess.query(A).filter(A.c == C('a2b1', b2)).one(),
a2
)
def test_query_aliased(self):
A, C, B = (self.classes.A,
self.classes.C,
self.classes.B)
sess = Session()
b1, b2 = B(data='b1'), B(data='b2')
a1 = A(c=C('a1b1', b1))
a2 = A(c=C('a2b1', b2))
sess.add_all([a1, a2])
sess.commit()
ae = aliased(A)
eq_(
sess.query(ae).filter(ae.c == C('a2b1', b2)).one(),
a2
)
class ConfigurationTest(fixtures.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table('edge', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('x1', Integer),
Column('y1', Integer),
Column('x2', Integer),
Column('y2', Integer),
)
@classmethod
def setup_mappers(cls):
class Point(cls.Comparable):
def __init__(self, x, y):
self.x = x
self.y = y
def __composite_values__(self):
return [self.x, self.y]
def __eq__(self, other):
return isinstance(other, Point) and \
other.x == self.x and \
other.y == self.y
def __ne__(self, other):
return not isinstance(other, Point) or \
not self.__eq__(other)
class Edge(cls.Comparable):
pass
def _test_roundtrip(self):
Edge, Point = self.classes.Edge, self.classes.Point
e1 = Edge(start=Point(3, 4), end=Point(5, 6))
sess = Session()
sess.add(e1)
sess.commit()
eq_(
sess.query(Edge).one(),
Edge(start=Point(3, 4), end=Point(5, 6))
)
def test_columns(self):
edge, Edge, Point = (self.tables.edge,
self.classes.Edge,
self.classes.Point)
mapper(Edge, edge, properties={
'start':sa.orm.composite(Point, edge.c.x1, edge.c.y1),
'end': sa.orm.composite(Point, edge.c.x2, edge.c.y2)
})
self._test_roundtrip()
def test_attributes(self):
edge, Edge, Point = (self.tables.edge,
self.classes.Edge,
self.classes.Point)
m = mapper(Edge, edge)
m.add_property('start', sa.orm.composite(Point, Edge.x1, Edge.y1))
m.add_property('end', sa.orm.composite(Point, Edge.x2, Edge.y2))
self._test_roundtrip()
def test_strings(self):
edge, Edge, Point = (self.tables.edge,
self.classes.Edge,
self.classes.Point)
m = mapper(Edge, edge)
m.add_property('start', sa.orm.composite(Point, 'x1', 'y1'))
m.add_property('end', sa.orm.composite(Point, 'x2', 'y2'))
self._test_roundtrip()
def test_deferred(self):
edge, Edge, Point = (self.tables.edge,
self.classes.Edge,
self.classes.Point)
mapper(Edge, edge, properties={
'start':sa.orm.composite(Point, edge.c.x1, edge.c.y1,
deferred=True, group='s'),
'end': sa.orm.composite(Point, edge.c.x2, edge.c.y2,
deferred=True)
})
self._test_roundtrip()
def test_check_prop_type(self):
edge, Edge, Point = (self.tables.edge,
self.classes.Edge,
self.classes.Point)
mapper(Edge, edge, properties={
'start': sa.orm.composite(Point, (edge.c.x1,), edge.c.y1),
})
assert_raises_message(
sa.exc.ArgumentError,
# note that we also are checking that the tuple
# renders here, so the "%" operator in the string needs to
# apply the tuple also
r"Composite expects Column objects or mapped "
"attributes/attribute names as "
"arguments, got: \(Column",
configure_mappers
)
class ComparatorTest(fixtures.MappedTest, testing.AssertsCompiledSQL):
__dialect__ = 'default'
@classmethod
def define_tables(cls, metadata):
Table('edge', metadata,
Column('id', Integer, primary_key=True,
test_needs_autoincrement=True),
Column('x1', Integer),
Column('y1', Integer),
Column('x2', Integer),
Column('y2', Integer),
)
@classmethod
def setup_mappers(cls):
class Point(cls.Comparable):
def __init__(self, x, y):
self.x = x
self.y = y
def __composite_values__(self):
return [self.x, self.y]
def __eq__(self, other):
return isinstance(other, Point) and \
other.x == self.x and \
other.y == self.y
def __ne__(self, other):
return not isinstance(other, Point) or \
not self.__eq__(other)
class Edge(cls.Comparable):
def __init__(self, start, end):
self.start = start
self.end = end
def __eq__(self, other):
return isinstance(other, Edge) and \
other.id == self.id
def _fixture(self, custom):
edge, Edge, Point = (self.tables.edge,
self.classes.Edge,
self.classes.Point)
if custom:
class CustomComparator(sa.orm.CompositeProperty.Comparator):
def near(self, other, d):
clauses = self.__clause_element__().clauses
diff_x = clauses[0] - other.x
diff_y = clauses[1] - other.y
return diff_x * diff_x + diff_y * diff_y <= d * d
mapper(Edge, edge, properties={
'start': sa.orm.composite(Point, edge.c.x1, edge.c.y1,
comparator_factory=CustomComparator),
'end': sa.orm.composite(Point, edge.c.x2, edge.c.y2)
})
else:
mapper(Edge, edge, properties={
'start': sa.orm.composite(Point, edge.c.x1, edge.c.y1),
'end': sa.orm.composite(Point, edge.c.x2, edge.c.y2)
})
def test_comparator_behavior_default(self):
self._fixture(False)
self._test_comparator_behavior()
def test_comparator_behavior_custom(self):
self._fixture(True)
self._test_comparator_behavior()
def _test_comparator_behavior(self):
Edge, Point = (self.classes.Edge,
self.classes.Point)
sess = Session()
e1 = Edge(Point(3, 4), Point(5, 6))
e2 = Edge(Point(14, 5), Point(2, 7))
sess.add_all([e1, e2])
sess.commit()
assert sess.query(Edge).\
filter(Edge.start==Point(3, 4)).one() is \
e1
assert sess.query(Edge).\
filter(Edge.start!=Point(3, 4)).first() is \
e2
eq_(
sess.query(Edge).filter(Edge.start==None).all(),
[]
)
def test_default_comparator_factory(self):
self._fixture(False)
Edge = self.classes.Edge
start_prop = Edge.start.property
assert start_prop.comparator_factory is CompositeProperty.Comparator
def test_custom_comparator_factory(self):
self._fixture(True)
Edge, Point = (self.classes.Edge,
self.classes.Point)
edge_1, edge_2 = Edge(Point(0, 0), Point(3, 5)), \
Edge(Point(0, 1), Point(3, 5))
sess = Session()
sess.add_all([edge_1, edge_2])
sess.commit()
near_edges = sess.query(Edge).filter(
Edge.start.near(Point(1, 1), 1)
).all()
assert edge_1 not in near_edges
assert edge_2 in near_edges
near_edges = sess.query(Edge).filter(
Edge.start.near(Point(0, 1), 1)
).all()
assert edge_1 in near_edges and edge_2 in near_edges
def test_order_by(self):
self._fixture(False)
Edge = self.classes.Edge
s = Session()
self.assert_compile(
s.query(Edge).order_by(Edge.start, Edge.end),
"SELECT edge.id AS edge_id, edge.x1 AS edge_x1, "
"edge.y1 AS edge_y1, edge.x2 AS edge_x2, edge.y2 AS edge_y2 "
"FROM edge ORDER BY edge.x1, edge.y1, edge.x2, edge.y2"
)
def test_order_by_aliased(self):
self._fixture(False)
Edge = self.classes.Edge
s = Session()
ea = aliased(Edge)
self.assert_compile(
s.query(ea).order_by(ea.start, ea.end),
"SELECT edge_1.id AS edge_1_id, edge_1.x1 AS edge_1_x1, "
"edge_1.y1 AS edge_1_y1, edge_1.x2 AS edge_1_x2, "
"edge_1.y2 AS edge_1_y2 "
"FROM edge AS edge_1 ORDER BY edge_1.x1, edge_1.y1, "
"edge_1.x2, edge_1.y2"
)
def test_clause_expansion(self):
self._fixture(False)
Edge = self.classes.Edge
from sqlalchemy.orm import configure_mappers
configure_mappers()
self.assert_compile(
select([Edge]).order_by(Edge.start),
"SELECT edge.id, edge.x1, edge.y1, edge.x2, edge.y2 FROM edge "
"ORDER BY edge.x1, edge.y1"
)
|