1- from sqlalchemy import MetaData , Integer , String , ForeignKey , Text
2- from sqlalchemy import util , desc
3- from sqlalchemy .testing .schema import Table
4- from sqlalchemy .testing .schema import Column
5- from sqlalchemy .orm import attributes , mapper , relationship , backref , configure_mappers , create_session
1+ from sqlalchemy import ForeignKey , Integer , String , Text , desc
2+ from sqlalchemy .orm import backref , configure_mappers , mapper , relationship
63from sqlalchemy .testing import fixtures
7- from sqlalchemy .ext . associationproxy import association_proxy
4+ from sqlalchemy .testing . schema import Column , Table
85
96__all__ = ()
107
118
129class FixtureTest (fixtures .MappedTest ):
13- """A MappedTest pre-configured with a common set of fixtures.
10+ """A MappedTest pre-configured with a common set of fixtures."""
1411
15- """
16-
17- run_define_tables = 'once'
18- run_setup_classes = 'once'
19- run_setup_mappers = 'each'
20- run_inserts = 'each'
21- run_deletes = 'each'
12+ run_define_tables = "once"
13+ run_setup_classes = "once"
14+ run_setup_mappers = "each"
15+ run_inserts = "each"
16+ run_deletes = "each"
2217
2318 @classmethod
2419 def setup_classes (cls ):
@@ -37,114 +32,138 @@ class Address(Base):
3732 class Thing (Base ):
3833 pass
3934
40-
4135 @classmethod
4236 def setup_mappers (cls ):
4337 User , users = cls .classes .User , cls .tables .users
4438 UserInfo , user_infos = cls .classes .UserInfo , cls .tables .user_infos
4539 Address , addresses = cls .classes .Address , cls .tables .addresses
4640 Thing , things = cls .classes .Thing , cls .tables .things
4741
48- mapper (User , users , properties = {
49- 'addresses' : relationship (
50- Address ,
51- backref = backref ('user' , lazy = "bulk" ),
52- lazy = "bulk" ,
53- order_by = [desc (addresses .c .email_address )]
54- ),
55- 'children' : relationship (User , backref = backref ('parent' , remote_side = [users .c .id ], lazy = "bulk" ), lazy = "bulk" ),
56- 'user_info' : relationship (UserInfo , lazy = "bulk" , backref = backref ('user' , lazy = "bulk" ), uselist = False ),
57- 'things' : relationship (Thing , secondary = cls .tables .user_to_things , lazy = "bulk" ),
58- })
42+ mapper (
43+ User ,
44+ users ,
45+ properties = {
46+ "addresses" : relationship (
47+ Address ,
48+ backref = backref ("user" , lazy = "bulk" ),
49+ lazy = "bulk" ,
50+ order_by = [desc (addresses .c .email_address )],
51+ ),
52+ "children" : relationship (
53+ User ,
54+ backref = backref ("parent" , remote_side = [users .c .id ], lazy = "bulk" ),
55+ lazy = "bulk" ,
56+ ),
57+ "user_info" : relationship (
58+ UserInfo ,
59+ lazy = "bulk" ,
60+ backref = backref ("user" , lazy = "bulk" ),
61+ uselist = False ,
62+ ),
63+ "things" : relationship (
64+ Thing , secondary = cls .tables .user_to_things , lazy = "bulk"
65+ ),
66+ },
67+ )
5968 mapper (Address , addresses )
6069 mapper (UserInfo , user_infos )
61- mapper (Thing , things , properties = {
62- 'users' : relationship (User , secondary = cls .tables .user_to_things , lazy = "bulk" ),
63- })
70+ mapper (
71+ Thing ,
72+ things ,
73+ properties = {
74+ "users" : relationship (
75+ User ,
76+ secondary = cls .tables .user_to_things ,
77+ lazy = "bulk" ,
78+ overlaps = "things" ,
79+ ),
80+ },
81+ )
6482
6583 configure_mappers ()
6684
6785 @classmethod
6886 def define_tables (cls , metadata ):
69- Table ('users' , metadata ,
70- Column ('id' , Integer , primary_key = True ,
71- test_needs_autoincrement = True ),
72- Column ('name' , String (30 ), nullable = False ),
73- Column ('parent_id' , None , ForeignKey ('users.id' )),
74- test_needs_acid = True ,
75- test_needs_fk = True )
76-
77- Table ('user_infos' , metadata ,
78- Column ('id' , Integer , primary_key = True ,
79- test_needs_autoincrement = True ),
80- Column ('details' , Text ),
81- Column ('user_id' , None , ForeignKey ('users.id' )),
82- test_needs_acid = True ,
83- test_needs_fk = True )
84-
85- Table ('addresses' , metadata ,
86- Column ('id' , Integer , primary_key = True ,
87- test_needs_autoincrement = True ),
88- Column ('user_id' , None , ForeignKey ('users.id' )),
89- Column ('email_address' , String (50 ), nullable = False ),
90- test_needs_acid = True ,
91- test_needs_fk = True )
92-
93- Table ('things' , metadata ,
94- Column ('id' , Integer , primary_key = True ,
95- test_needs_autoincrement = True ),
96- Column ('name' , String (30 ), nullable = False ),
97- test_needs_acid = True ,
98- test_needs_fk = True )
99-
100- Table ('user_to_things' , metadata ,
101- Column ('user_id' , None , ForeignKey ('users.id' )),
102- Column ('thing_id' , None , ForeignKey ('things.id' )),
103- test_needs_acid = True ,
104- test_needs_fk = True )
87+ Table (
88+ "users" ,
89+ metadata ,
90+ Column ("id" , Integer , primary_key = True , test_needs_autoincrement = True ),
91+ Column ("name" , String (30 ), nullable = False ),
92+ Column ("parent_id" , None , ForeignKey ("users.id" )),
93+ test_needs_acid = True ,
94+ test_needs_fk = True ,
95+ )
10596
97+ Table (
98+ "user_infos" ,
99+ metadata ,
100+ Column ("id" , Integer , primary_key = True , test_needs_autoincrement = True ),
101+ Column ("details" , Text ),
102+ Column ("user_id" , None , ForeignKey ("users.id" )),
103+ test_needs_acid = True ,
104+ test_needs_fk = True ,
105+ )
106+
107+ Table (
108+ "addresses" ,
109+ metadata ,
110+ Column ("id" , Integer , primary_key = True , test_needs_autoincrement = True ),
111+ Column ("user_id" , None , ForeignKey ("users.id" )),
112+ Column ("email_address" , String (50 ), nullable = False ),
113+ test_needs_acid = True ,
114+ test_needs_fk = True ,
115+ )
116+
117+ Table (
118+ "things" ,
119+ metadata ,
120+ Column ("id" , Integer , primary_key = True , test_needs_autoincrement = True ),
121+ Column ("name" , String (30 ), nullable = False ),
122+ test_needs_acid = True ,
123+ test_needs_fk = True ,
124+ )
125+
126+ Table (
127+ "user_to_things" ,
128+ metadata ,
129+ Column ("user_id" , None , ForeignKey ("users.id" )),
130+ Column ("thing_id" , None , ForeignKey ("things.id" )),
131+ test_needs_acid = True ,
132+ test_needs_fk = True ,
133+ )
106134
107135 @classmethod
108136 def fixtures (cls ):
109- return dict (
110- users = (
111- ('id' , ' name' , ' parent_id' ),
112- (7 , ' jack' , None ),
113- (8 , ' jack jr' , 7 ),
114- (9 , ' fred' , 7 ),
115- (10 , ' jack jr jr' , 8 ),
137+ return {
138+ " users" : (
139+ ("id" , " name" , " parent_id" ),
140+ (7 , " jack" , None ),
141+ (8 , " jack jr" , 7 ),
142+ (9 , " fred" , 7 ),
143+ (10 , " jack jr jr" , 8 ),
116144 ),
117-
118- user_infos = (
119- ('id' , 'user_id' , 'details' ),
120- (1 , 7 , 'is cool' ),
121- (2 , 8 , 'is not cool' ),
122- (3 , 10 , 'is moderately cool' ),
145+ "user_infos" : (
146+ ("id" , "user_id" , "details" ),
147+ (1 , 7 , "is cool" ),
148+ (2 , 8 , "is not cool" ),
149+ (3 , 10 , "is moderately cool" ),
123150 ),
124-
125- addresses = (
126- ('id' , 'user_id' , 'email_address' ),
151+ "addresses" : (
152+ ("id" , "user_id" , "email_address" ),
127153128154129155130156131- 157+ 132158 ),
133-
134- things = (
135- ('id' , 'name' ),
136- (1 , 'dog' ),
137- (2 , 'lamp' ),
138- (3 , 'chair' ),
139- ),
140-
141- user_to_things = (
142- ('user_id' , 'thing_id' ),
159+ "things" : (("id" , "name" ), (1 , "dog" ), (2 , "lamp" ), (3 , "chair" )),
160+ "user_to_things" : (
161+ ("user_id" , "thing_id" ),
143162 (7 , 1 ),
144- (8 , 1 ), # include a duplicate intentionally
145163 (8 , 1 ),
164+ (8 , 1 ), # include a duplicate intentionally
146165 (10 , 2 ),
147166 (9 , 2 ),
148167 (10 , 3 ),
149168 ),
150- )
169+ }
0 commit comments