@@ -93,55 +93,61 @@ def test_database_restore_data(backup_path: Path):
93
93
settings = get_app_settings ()
94
94
backup_v2 = BackupV2 (settings .DB_URL )
95
95
96
- assert backup_path .exists ()
97
- backup_v2 .restore (backup_path )
98
-
99
- # make sure migrations populated data successfully
100
- with session_context () as session :
101
- session = cast (Session , session )
102
-
103
- groups = session .query (Group ).all ()
104
- recipes = session .query (RecipeModel ).all ()
105
- shopping_lists = session .query (ShoppingList ).all ()
106
- labels = session .query (MultiPurposeLabel ).all ()
107
-
108
- foods = session .query (IngredientFoodModel ).all ()
109
- units = session .query (IngredientUnitModel ).all ()
110
-
111
- # 2023-02-14-20.45.41_5ab195a474eb_add_normalized_search_properties
112
- for recipe in recipes :
113
- if recipe .name :
114
- assert recipe .name_normalized
115
- if recipe .description :
116
- assert recipe .description_normalized
117
-
118
- for ingredient in recipe .recipe_ingredient :
119
- if ingredient .note :
120
- assert ingredient .note_normalized
121
- if ingredient .original_text :
122
- assert ingredient .original_text_normalized
123
-
124
- # 2023-02-21-22.03.19_b04a08da2108_added_shopping_list_label_settings
125
- for shopping_list in shopping_lists :
126
- group_labels = [label for label in labels if label .group_id == shopping_list .group_id ]
127
- assert len (shopping_list .label_settings ) == len (group_labels )
128
- for label_setting , label in zip (
129
- sorted (shopping_list .label_settings , key = lambda x : x .label .id ),
130
- sorted (group_labels , key = lambda x : x .id ),
131
- strict = True ,
132
- ):
133
- assert label_setting .label == label
134
-
135
- # 2023-08-06-21.00.34_04ac51cbe9a4_added_group_slug
136
- for group in groups :
137
- assert group .slug
138
-
139
- # 2023-09-01-14.55.42_0341b154f79a_added_normalized_unit_and_food_names
140
- for food in foods :
141
- if food .name :
142
- assert food .name_normalized
143
-
144
- for unit in units :
145
- assert unit .name_normalized
146
- if unit .abbreviation :
147
- assert unit .abbreviation_normalized
96
+ # create a backup of the existing data so we can restore it later
97
+ original_data_backup = backup_v2 .backup ()
98
+
99
+ try :
100
+ assert backup_path .exists ()
101
+ backup_v2 .restore (backup_path )
102
+
103
+ # make sure migrations populated data successfully
104
+ with session_context () as session :
105
+ session = cast (Session , session )
106
+
107
+ groups = session .query (Group ).all ()
108
+ recipes = session .query (RecipeModel ).all ()
109
+ shopping_lists = session .query (ShoppingList ).all ()
110
+ labels = session .query (MultiPurposeLabel ).all ()
111
+
112
+ foods = session .query (IngredientFoodModel ).all ()
113
+ units = session .query (IngredientUnitModel ).all ()
114
+
115
+ # 2023-02-14-20.45.41_5ab195a474eb_add_normalized_search_properties
116
+ for recipe in recipes :
117
+ if recipe .name :
118
+ assert recipe .name_normalized
119
+ if recipe .description :
120
+ assert recipe .description_normalized
121
+
122
+ for ingredient in recipe .recipe_ingredient :
123
+ if ingredient .note :
124
+ assert ingredient .note_normalized
125
+ if ingredient .original_text :
126
+ assert ingredient .original_text_normalized
127
+
128
+ # 2023-02-21-22.03.19_b04a08da2108_added_shopping_list_label_settings
129
+ for shopping_list in shopping_lists :
130
+ group_labels = [label for label in labels if label .group_id == shopping_list .group_id ]
131
+ assert len (shopping_list .label_settings ) == len (group_labels )
132
+ for label_setting , label in zip (
133
+ sorted (shopping_list .label_settings , key = lambda x : x .label .id ),
134
+ sorted (group_labels , key = lambda x : x .id ),
135
+ strict = True ,
136
+ ):
137
+ assert label_setting .label == label
138
+
139
+ # 2023-08-06-21.00.34_04ac51cbe9a4_added_group_slug
140
+ for group in groups :
141
+ assert group .slug
142
+
143
+ # 2023-09-01-14.55.42_0341b154f79a_added_normalized_unit_and_food_names
144
+ for food in foods :
145
+ if food .name :
146
+ assert food .name_normalized
147
+
148
+ for unit in units :
149
+ assert unit .name_normalized
150
+ if unit .abbreviation :
151
+ assert unit .abbreviation_normalized
152
+ finally :
153
+ backup_v2 .restore (original_data_backup )
0 commit comments