You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The way MigratorTestCase is implemented it is only possible to setup test data in the prepare function which is called during setUp. Because of that there is no way to setup different data for different test functions.
In order to be able to support multiple test functions MigratorTestCase could be implemented this way:
@tag(MIGRATION_TEST_MARKER)classMigratorTestCase(TransactionTestCase):
"""Used when using raw ``unitest`` library for test."""database_name: ClassVar[Optional[str]] =Noneold_state: ProjectStatenew_state: ProjectState#: Part of the end-user API. Used to tell what migrations we are using.migrate_from: ClassVar[MigrationSpec]
migrate_to: ClassVar[MigrationSpec]
auto_migrate: bool=True# <-------------- Add an option to disable automatic migrationdefsetUp(self) ->None:
""" Regular ``unittest`` styled setup case. What it does? - It starts with defining the initial migration state - Then it allows to run custom method to prepare some data before the migration will happen - Then it applies the migration and saves all states """super().setUp()
self._migrator=Migrator(self.database_name)
self.old_state=self._migrator.apply_initial_migration(
self.migrate_from,
)
self.prepare()
ifself.auto_migrate: # <-------------- check if auto_migrate is activatedself.migrate() # <-------------- call the extracted the method# v-------------- do the migrationdefmigrate(self) ->None:
assertnothasattr(self, "new_state"), "migrate must be called only once"self.new_state=self._migrator.apply_tested_migration(self.migrate_to)
it would allow to add multiple test scenarios like this:
classTest1234(MigratorTestCase):
migrate_from= ...
migrate_to= ...
auto_migrate=Falsedefprepare(self):
... # prepare common stuff heredeftest_scenario_1(self):
... # instantiate stuff here using old stateself.migrate()
... # validate migration here using new statedeftest_scenario_2(self):
... # instantiate stuff here using old stateself.migrate()
... # validate migration here using new state
The text was updated successfully, but these errors were encountered:
The workaround for the moment is to create a custom MigratorTestCase.
fromdjango_test_migrations.contrib.unittest_caseimportMigratorTestCasefromdjango_test_migrations.migratorimportMigratorclassManualMigratorTestCase(MigratorTestCase):
defsetUp(self) ->None:
self._migrator=Migrator(self.database_name)
self.old_state=self._migrator.apply_initial_migration(
self.migrate_from,
)
self.prepare()
defmigrate(self) ->None:
assertnothasattr(self, "new_state"), "migrate must be called only once"self.new_state=self._migrator.apply_tested_migration(self.migrate_to)
The way
MigratorTestCase
is implemented it is only possible to setup test data in theprepare
function which is called duringsetUp
. Because of that there is no way to setup different data for different test functions.In order to be able to support multiple test functions
MigratorTestCase
could be implemented this way:it would allow to add multiple test scenarios like this:
The text was updated successfully, but these errors were encountered: