e2e: add apply_default_data to load data from migrations after tables have been truncated
This commit is contained in:
		
							
								
								
									
										35
									
								
								e2e/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								e2e/utils.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| """passbook e2e testing utilities""" | ||||
|  | ||||
| from glob import glob | ||||
| from inspect import getmembers, isfunction | ||||
| from importlib.util import spec_from_file_location, module_from_spec | ||||
| from django.apps import apps | ||||
| from django.db import connection, transaction | ||||
| from django.db.utils import IntegrityError | ||||
|  | ||||
| def apply_default_data(): | ||||
|     """apply objects created by migrations after tables have been truncated""" | ||||
|     # Find all migration files | ||||
|     # load all functions | ||||
|     migration_files = glob("**/migrations/*.py", recursive=True) | ||||
|     matches = [] | ||||
|     for migration in migration_files: | ||||
|         with open(migration, 'r+') as migration_file: | ||||
|             # Check if they have a `RunPython` | ||||
|             if "RunPython" in migration_file.read(): | ||||
|                 matches.append(migration) | ||||
|  | ||||
|     with connection.schema_editor() as schema_editor: | ||||
|         for match in matches: | ||||
|             # Load module from file path | ||||
|             spec = spec_from_file_location("", match) | ||||
|             migration_module = module_from_spec(spec) | ||||
|             # pyright: reportGeneralTypeIssues=false | ||||
|             spec.loader.exec_module(migration_module) | ||||
|             # Call all functions from module | ||||
|             for _, func in getmembers(migration_module, isfunction): | ||||
|                 with transaction.atomic(): | ||||
|                     try: | ||||
|                         func(apps, schema_editor) | ||||
|                     except IntegrityError: | ||||
|                         pass | ||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer