99######################################################################
1010from __future__ import annotations
1111
12+ import json
1213import os
1314
1415import pytest
@@ -32,30 +33,30 @@ def setup(self, sqlite_account_info_factory, account_info_default_data_schema_0)
3233
3334 def test_upgrade_1_default_allowed (self ):
3435 """The 'allowed' field should be the default for upgraded databases."""
35- old_account_info = self .sqlite_account_info_factory (schema_0 = True )
36+ old_account_info = self .sqlite_account_info_factory (last_upgrade_to_run = 0 )
3637 old_account_info .set_auth_data_with_schema_0_for_test (** self .account_info_default_data )
3738 new_account_info = self .sqlite_account_info_factory (file_name = old_account_info .filename )
3839
3940 assert AbstractAccountInfo .DEFAULT_ALLOWED == new_account_info .get_allowed ()
4041
4142 def test_upgrade_2_default_app_key (self ):
4243 """The 'application_key_id' field should default to the account ID."""
43- old_account_info = self .sqlite_account_info_factory (schema_0 = True )
44+ old_account_info = self .sqlite_account_info_factory (last_upgrade_to_run = 0 )
4445 old_account_info .set_auth_data_with_schema_0_for_test (** self .account_info_default_data )
4546 new_account_info = self .sqlite_account_info_factory (file_name = old_account_info .filename )
4647
4748 assert 'account_id' == new_account_info .get_application_key_id ()
4849
4950 def test_upgrade_3_default_s3_api_url (self ):
5051 """The 's3_api_url' field should be set."""
51- old_account_info = self .sqlite_account_info_factory (schema_0 = True )
52+ old_account_info = self .sqlite_account_info_factory (last_upgrade_to_run = 0 )
5253 old_account_info .set_auth_data_with_schema_0_for_test (** self .account_info_default_data )
5354 new_account_info = self .sqlite_account_info_factory (file_name = old_account_info .filename )
5455
5556 assert '' == new_account_info .get_s3_api_url ()
5657
5758 def test_migrate_to_4 (self ):
58- old_account_info = self .sqlite_account_info_factory (schema_0 = True )
59+ old_account_info = self .sqlite_account_info_factory (last_upgrade_to_run = 0 )
5960 old_account_info .set_auth_data_with_schema_0_for_test (** self .account_info_default_data )
6061 new_account_info = self .sqlite_account_info_factory (file_name = old_account_info .filename )
6162
@@ -82,19 +83,48 @@ def test_migrate_to_4(self):
8283 ),
8384 ],
8485 )
85- @pytest .mark .apiver (2 )
86- def test_migrate_to_5_v2 (
87- self , v2_sqlite_account_info_factory , account_info_default_data , allowed
86+ @pytest .mark .apiver (to_ver = 2 )
87+ @pytest .mark .parametrize (
88+ ('start_version' , 'end_version' ),
89+ [
90+ (4 , 5 ),
91+ (5 , 6 ),
92+ ],
93+ )
94+ def test_migrate_to_5_prior_to_v3 (
95+ self ,
96+ sqlite_account_info_factory ,
97+ account_info_default_data : dict ,
98+ allowed : dict ,
99+ start_version : int ,
100+ end_version : int ,
88101 ):
89- old_account_info = v2_sqlite_account_info_factory ()
90- account_info_default_data .update ({'allowed' : allowed })
102+ old_account_info = sqlite_account_info_factory (last_upgrade_to_run = start_version )
91103 old_account_info .set_auth_data (** account_info_default_data )
104+
105+ allowed_text = json .dumps (allowed )
106+ with old_account_info ._get_connection () as conn :
107+ conn .execute (f"UPDATE account SET allowed = ('{ allowed_text } ');" )
108+
109+ assert old_account_info ._get_update_count (start_version ) == 1
110+ assert old_account_info ._get_update_count (end_version ) == 0
111+
92112 assert old_account_info .get_allowed () == allowed
93113
94- new_account_info = self .sqlite_account_info_factory (file_name = old_account_info .filename )
114+ new_account_info = self .sqlite_account_info_factory (
115+ file_name = old_account_info .filename , last_upgrade_to_run = end_version
116+ )
95117
118+ assert new_account_info ._get_update_count (end_version ) == 1
96119 assert new_account_info .get_allowed () == allowed
97120
121+ with new_account_info ._get_connection () as conn :
122+ new_allowed = json .loads (conn .execute ('SELECT allowed FROM account;' ).fetchone ()[0 ])
123+
124+ assert 'buckets' in new_allowed
125+ assert 'bucketId' not in new_allowed
126+ assert 'bucketName' not in new_allowed
127+
98128 @pytest .mark .parametrize (
99129 ('old_allowed' , 'exp_allowed' ),
100130 [
@@ -126,22 +156,40 @@ def test_migrate_to_5_v2(
126156 ),
127157 ],
128158 )
159+ @pytest .mark .parametrize (
160+ ('start_version' , 'end_version' ),
161+ [
162+ (4 , 5 ),
163+ (5 , 6 ),
164+ ],
165+ )
129166 @pytest .mark .apiver (from_ver = 3 )
130167 def test_migrate_to_5_v3 (
131168 self ,
132- v2_sqlite_account_info_factory ,
133- account_info_default_data ,
134- old_allowed ,
135- exp_allowed ,
169+ sqlite_account_info_factory ,
170+ account_info_default_data : dict ,
171+ old_allowed : dict ,
172+ exp_allowed : dict ,
173+ start_version : int ,
174+ end_version : int ,
136175 ):
137- old_account_info = v2_sqlite_account_info_factory ()
138- account_info_default_data .update ({'allowed' : old_allowed })
176+ old_account_info = sqlite_account_info_factory (last_upgrade_to_run = start_version )
139177 old_account_info .set_auth_data (** account_info_default_data )
178+
179+ allowed_text = json .dumps (old_allowed )
180+ with old_account_info ._get_connection () as conn :
181+ conn .execute (f"UPDATE account SET allowed = ('{ allowed_text } ');" )
182+
140183 assert old_account_info .get_allowed () == old_allowed
184+ assert old_account_info ._get_update_count (start_version ) == 1
185+ assert old_account_info ._get_update_count (end_version ) == 0
141186
142- new_account_info = self .sqlite_account_info_factory (file_name = old_account_info .filename )
187+ new_account_info = self .sqlite_account_info_factory (
188+ file_name = old_account_info .filename , last_upgrade_to_run = end_version
189+ )
143190
144191 assert new_account_info .get_allowed () == exp_allowed
192+ assert new_account_info ._get_update_count (end_version ) == 1
145193
146194 @pytest .mark .apiver (2 )
147195 def test_multi_bucket_key_error_apiver_v2 (
0 commit comments