FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-spanner-orm/spanner_orm/tests/migrations_test.py at master · NeoTim/python-spanner-orm · GitHub
NeoTim
/
python-spanner-orm
Public
forked from
google/python-spanner-orm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-spanner-orm
/
spanner_orm
/
tests
/
migrations_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
262 lines (218 loc) · 10.2 KB
Breadcrumbs
python-spanner-orm
/
spanner_orm
/
tests
/
migrations_test.py
Copy path
File metadata and controls
262 lines (218 loc) · 10.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
logging
import
os
import
shutil
import
stat
import
tempfile
import
unittest
from
unittest
import
mock
from
spanner_orm
import
error
from
spanner_orm
.
admin
import
migration
from
spanner_orm
.
admin
import
migration_executor
from
spanner_orm
.
admin
import
migration_manager
from
spanner_orm
.
admin
import
update
class
MigrationsTest
(
unittest
.
TestCase
):
TEST_DIR
=
tempfile
.
mkdtemp
()
TEST_MIGRATIONS_DIR
=
os
.
path
.
join
(
TEST_DIR
,
'migrations'
)
def
test_retrieve
(
self
):
testdata_filename
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'migrations'
)
manager
=
migration_manager
.
MigrationManager
(
testdata_filename
)
migrations
=
manager
.
migrations
self
.
assertEqual
(
len
(
migrations
),
3
)
self
.
assertEqual
(
migrations
[
2
].
prev_migration_id
,
migrations
[
1
].
migration_id
)
self
.
assertEqual
(
migrations
[
1
].
prev_migration_id
,
migrations
[
0
].
migration_id
)
def
test_generate
(
self
):
testdata_filename
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'migrations'
)
shutil
.
rmtree
(
self
.
TEST_MIGRATIONS_DIR
)
shutil
.
copytree
(
testdata_filename
,
self
.
TEST_MIGRATIONS_DIR
)
os
.
chmod
(
self
.
TEST_MIGRATIONS_DIR
,
stat
.
S_IRWXO
|
stat
.
S_IRWXU
)
for
f
in
os
.
listdir
(
self
.
TEST_MIGRATIONS_DIR
):
file_path
=
os
.
path
.
join
(
self
.
TEST_MIGRATIONS_DIR
,
f
)
if
not
os
.
path
.
isdir
(
file_path
):
os
.
chmod
(
file_path
,
stat
.
S_IROTH
|
stat
.
S_IWOTH
|
stat
.
S_IRUSR
|
stat
.
S_IWUSR
)
manager
=
migration_manager
.
MigrationManager
(
self
.
TEST_MIGRATIONS_DIR
)
path
=
manager
.
generate
(
'test migration'
)
try
:
migration_
=
manager
.
_migration_from_file
(
path
)
self
.
assertIsNotNone
(
migration_
.
migration_id
)
self
.
assertIsNotNone
(
migration_
.
prev_migration_id
)
self
.
assertIsInstance
(
migration_
.
upgrade
(),
update
.
NoUpdate
)
self
.
assertIsInstance
(
migration_
.
downgrade
(),
update
.
NoUpdate
)
finally
:
shutil
.
rmtree
(
self
.
TEST_MIGRATIONS_DIR
)
def
test_order_migrations
(
self
):
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
migrations
=
[
third
,
first
,
second
]
expected_order
=
[
first
,
second
,
third
]
manager
=
migration_manager
.
MigrationManager
(
self
.
TEST_MIGRATIONS_DIR
)
self
.
assertEqual
(
manager
.
_order_migrations
(
migrations
),
expected_order
)
def
test_order_migrations_with_no_none
(
self
):
first
=
migration
.
Migration
(
'2'
,
'1'
)
second
=
migration
.
Migration
(
'3'
,
'2'
)
third
=
migration
.
Migration
(
'4'
,
'3'
)
migrations
=
[
third
,
first
,
second
]
expected_order
=
[
first
,
second
,
third
]
manager
=
migration_manager
.
MigrationManager
(
self
.
TEST_MIGRATIONS_DIR
)
self
.
assertEqual
(
manager
.
_order_migrations
(
migrations
),
expected_order
)
def
test_order_migrations_error_on_unclear_successor
(
self
):
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'1'
)
migrations
=
[
third
,
first
,
second
]
manager
=
migration_manager
.
MigrationManager
(
self
.
TEST_MIGRATIONS_DIR
)
with
self
.
assertRaisesRegex
(
error
.
SpannerError
,
'unclear successor'
):
manager
.
_order_migrations
(
migrations
)
def
test_order_migrations_error_on_unclear_start_migration
(
self
):
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'3'
,
'2'
)
migrations
=
[
first
,
second
]
manager
=
migration_manager
.
MigrationManager
(
self
.
TEST_MIGRATIONS_DIR
)
with
self
.
assertRaisesRegex
(
error
.
SpannerError
,
'no valid previous'
):
manager
.
_order_migrations
(
migrations
)
def
test_order_migrations_error_on_circular_dependency
(
self
):
first
=
migration
.
Migration
(
'1'
,
'3'
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
migrations
=
[
third
,
first
,
second
]
manager
=
migration_manager
.
MigrationManager
(
self
.
TEST_MIGRATIONS_DIR
)
with
self
.
assertRaisesRegex
(
error
.
SpannerError
,
'No valid migration'
):
manager
.
_order_migrations
(
migrations
)
def
test_order_migrations_error_on_no_successor
(
self
):
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'3'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
migrations
=
[
third
,
first
,
second
]
manager
=
migration_manager
.
MigrationManager
(
self
.
TEST_MIGRATIONS_DIR
)
with
self
.
assertRaisesRegex
(
error
.
SpannerError
,
'no successor'
):
manager
.
_order_migrations
(
migrations
)
def
test_filter_migrations
(
self
):
connection
=
mock
.
Mock
()
executor
=
migration_executor
.
MigrationExecutor
(
connection
,
self
.
TEST_MIGRATIONS_DIR
)
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
migrations
=
[
first
,
second
,
third
]
migrated
=
{
'1'
:
True
,
'2'
:
False
,
'3'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
filtered
=
executor
.
_filter_migrations
(
migrations
,
False
,
None
)
self
.
assertEqual
(
filtered
, [
second
,
third
])
filtered
=
executor
.
_filter_migrations
(
migrations
,
False
,
'2'
)
self
.
assertEqual
(
filtered
, [
second
])
filtered
=
executor
.
_filter_migrations
(
reversed
(
migrations
),
True
,
'1'
)
self
.
assertEqual
(
filtered
, [
first
])
def
test_filter_migrations_error_on_bad_last_migration
(
self
):
connection
=
mock
.
Mock
()
executor
=
migration_executor
.
MigrationExecutor
(
connection
,
self
.
TEST_MIGRATIONS_DIR
)
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
migrations
=
[
first
,
second
,
third
]
migrated
=
{
'1'
:
True
,
'2'
:
False
,
'3'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
with
self
.
assertRaises
(
error
.
SpannerError
):
executor
.
_filter_migrations
(
migrations
,
False
,
'1'
)
with
self
.
assertRaises
(
error
.
SpannerError
):
executor
.
_filter_migrations
(
migrations
,
False
,
'4'
)
def
test_validate_migrations
(
self
):
connection
=
mock
.
Mock
()
executor
=
migration_executor
.
MigrationExecutor
(
connection
,
self
.
TEST_MIGRATIONS_DIR
)
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
with
mock
.
patch
.
object
(
executor
,
'migrations'
)
as
migrations
:
migrations
.
return_value
=
[
first
,
second
,
third
]
migrated
=
{
'1'
:
True
,
'2'
:
False
,
'3'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
executor
.
_validate_migrations
()
migrated
=
{
'1'
:
False
,
'2'
:
False
,
'3'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
executor
.
_validate_migrations
()
def
test_validate_migrations_error_on_unmigrated_after_migrated
(
self
):
connection
=
mock
.
Mock
()
executor
=
migration_executor
.
MigrationExecutor
(
connection
,
self
.
TEST_MIGRATIONS_DIR
)
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
with
mock
.
patch
.
object
(
executor
,
'migrations'
)
as
migrations
:
migrations
.
return_value
=
[
first
,
second
,
third
]
migrated
=
{
'1'
:
False
,
'2'
:
True
,
'3'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
with
self
.
assertRaises
(
error
.
SpannerError
):
executor
.
_validate_migrations
()
migrated
=
{
'1'
:
False
,
'2'
:
False
,
'3'
:
True
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
with
self
.
assertRaises
(
error
.
SpannerError
):
executor
.
_validate_migrations
()
def
test_validate_migrations_error_on_unmigrated_first
(
self
):
connection
=
mock
.
Mock
()
executor
=
migration_executor
.
MigrationExecutor
(
connection
,
self
.
TEST_MIGRATIONS_DIR
)
first
=
migration
.
Migration
(
'2'
,
'1'
)
with
mock
.
patch
.
object
(
executor
,
'migrations'
)
as
migrations
:
migrations
.
return_value
=
[
first
]
migrated
=
{
'1'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
with
self
.
assertRaises
(
error
.
SpannerError
):
executor
.
_validate_migrations
()
migrated
=
{}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
with
self
.
assertRaises
(
error
.
SpannerError
):
executor
.
_validate_migrations
()
def
test_migrate
(
self
):
connection
=
mock
.
Mock
()
executor
=
migration_executor
.
MigrationExecutor
(
connection
,
self
.
TEST_MIGRATIONS_DIR
)
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
with
mock
.
patch
.
object
(
executor
,
'migrations'
)
as
migrations
:
migrations
.
return_value
=
[
first
,
second
,
third
]
migrated
=
{
'1'
:
True
,
'2'
:
False
,
'3'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
executor
.
migrate
()
self
.
assertEqual
(
migrated
, {
'1'
:
True
,
'2'
:
True
,
'3'
:
True
})
def
test_rollback
(
self
):
connection
=
mock
.
Mock
()
executor
=
migration_executor
.
MigrationExecutor
(
connection
,
self
.
TEST_MIGRATIONS_DIR
)
first
=
migration
.
Migration
(
'1'
,
None
)
second
=
migration
.
Migration
(
'2'
,
'1'
)
third
=
migration
.
Migration
(
'3'
,
'2'
)
with
mock
.
patch
.
object
(
executor
,
'migrations'
)
as
migrations
:
migrations
.
return_value
=
[
first
,
second
,
third
]
migrated
=
{
'1'
:
True
,
'2'
:
False
,
'3'
:
False
}
with
mock
.
patch
.
object
(
executor
,
'_migration_status_map'
,
migrated
):
executor
.
rollback
(
'1'
)
self
.
assertEqual
(
migrated
, {
'1'
:
False
,
'2'
:
False
,
'3'
:
False
})
@
classmethod
def
tearDownClass
(
cls
):
super
().
tearDownClass
()
shutil
.
rmtree
(
MigrationsTest
.
TEST_DIR
)
if
__name__
==
'__main__'
:
logging
.
basicConfig
()
unittest
.
main
()
Back
|
FazBrowse Home
|
New Git URL