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
6
3
from sqlalchemy .testing import fixtures
7
- from sqlalchemy .ext . associationproxy import association_proxy
4
+ from sqlalchemy .testing . schema import Column , Table
8
5
9
6
__all__ = ()
10
7
11
8
12
9
class 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."""
14
11
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"
22
17
23
18
@classmethod
24
19
def setup_classes (cls ):
@@ -37,114 +32,138 @@ class Address(Base):
37
32
class Thing (Base ):
38
33
pass
39
34
40
-
41
35
@classmethod
42
36
def setup_mappers (cls ):
43
37
User , users = cls .classes .User , cls .tables .users
44
38
UserInfo , user_infos = cls .classes .UserInfo , cls .tables .user_infos
45
39
Address , addresses = cls .classes .Address , cls .tables .addresses
46
40
Thing , things = cls .classes .Thing , cls .tables .things
47
41
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
+ )
59
68
mapper (Address , addresses )
60
69
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
+ )
64
82
65
83
configure_mappers ()
66
84
67
85
@classmethod
68
86
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
+ )
105
96
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
+ )
106
134
107
135
@classmethod
108
136
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 ),
116
144
),
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" ),
123
150
),
124
-
125
- addresses = (
126
- ('id' , 'user_id' , 'email_address' ),
151
+ "addresses" : (
152
+ ("id" , "user_id" , "email_address" ),
127
153
128
154
129
155
130
156
131
-
157
+
132
158
),
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" ),
143
162
(7 , 1 ),
144
- (8 , 1 ), # include a duplicate intentionally
145
163
(8 , 1 ),
164
+ (8 , 1 ), # include a duplicate intentionally
146
165
(10 , 2 ),
147
166
(9 , 2 ),
148
167
(10 , 3 ),
149
168
),
150
- )
169
+ }
0 commit comments