From e417ff2b4dd06e2dc83384109d2a8612f221e168 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 4 Feb 2021 00:44:37 +1100 Subject: [PATCH] Test migrations for build app --- InvenTree/InvenTree/helpers.py | 6 +- .../build/migrations/0018_build_reference.py | 2 +- InvenTree/build/test_migrations.py | 118 ++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 InvenTree/build/test_migrations.py diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 5cb6cc18dd..33136d76ce 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -517,7 +517,7 @@ def getMigrationFileNames(app): return migration_files -def getOldestMigrationFile(app, exclude_extension=True): +def getOldestMigrationFile(app, exclude_extension=True, ignore_initial=True): """ Return the filename associated with the oldest migration """ @@ -526,6 +526,10 @@ def getOldestMigrationFile(app, exclude_extension=True): oldest_file = None for f in getMigrationFileNames(app): + + if ignore_initial and f.startswith('0001_initial'): + continue + num = int(f.split('_')[0]) if oldest_file is None or num < oldest_num: diff --git a/InvenTree/build/migrations/0018_build_reference.py b/InvenTree/build/migrations/0018_build_reference.py index bcf4f9b9d4..e8fd938b6a 100644 --- a/InvenTree/build/migrations/0018_build_reference.py +++ b/InvenTree/build/migrations/0018_build_reference.py @@ -9,7 +9,7 @@ def add_default_reference(apps, schema_editor): Best we can do is use the PK of the build order itself. """ - Build = apps.get_model('build', 'Build') + Build = apps.get_model('build', 'build') count = 0 diff --git a/InvenTree/build/test_migrations.py b/InvenTree/build/test_migrations.py new file mode 100644 index 0000000000..1e95cfb54e --- /dev/null +++ b/InvenTree/build/test_migrations.py @@ -0,0 +1,118 @@ +""" +Tests for the build model database migrations +""" + +from django_test_migrations.contrib.unittest_case import MigratorTestCase + +from InvenTree import helpers + + +class TestForwardMigrations(MigratorTestCase): + """ + Test entire schema migration sequence for the build app + """ + + migrate_from = ('build', helpers.getOldestMigrationFile('build')) + migrate_to = ('build', helpers.getNewestMigrationFile('build')) + + def prepare(self): + """ + Create initial data! + """ + + Part = self.old_state.apps.get_model('part', 'part') + + buildable_part = Part.objects.create( + name='Widget', + description='Buildable Part', + active=True, + ) + + with self.assertRaises(TypeError): + # Cannot set the 'assembly' field as it hasn't been added to the db schema + Part.objects.create( + name='Blorb', + description='ABCDE', + assembly=True + ) + + Build = self.old_state.apps.get_model('build', 'build') + + Build.objects.create( + part=buildable_part, + title='A build of some stuff', + quantity=50 + ) + + def test_items_exist(self): + + Part = self.new_state.apps.get_model('part', 'part') + + self.assertEqual(Part.objects.count(), 1) + + Build = self.new_state.apps.get_model('build', 'build') + + self.assertEqual(Build.objects.count(), 1) + + # Check that the part object now has an assembly field + part = Part.objects.all().first() + part.assembly = True + part.save() + part.assembly = False + part.save() + + +class TestReferenceMigration(MigratorTestCase): + """ + Test custom migration which adds 'reference' field to Build model + """ + + migrate_from = ('build', helpers.getOldestMigrationFile('build')) + migrate_to = ('build', '0018_build_reference') + + def prepare(self): + """ + Create some builds + """ + + Part = self.old_state.apps.get_model('part', 'part') + + part = Part.objects.create( + name='Part', + description='A test part' + ) + + Build = self.old_state.apps.get_model('build', 'build') + + Build.objects.create( + part=part, + title='My very first build', + quantity=10 + ) + + Build.objects.create( + part=part, + title='My very second build', + quantity=10 + ) + + Build.objects.create( + part=part, + title='My very third build', + quantity=10 + ) + + # Ensure that the builds *do not* have a 'reference' field + for build in Build.objects.all(): + with self.assertRaises(AttributeError): + print(build.reference) + + def test_build_reference(self): + + Build = self.new_state.apps.get_model('build', 'build') + + self.assertEqual(Build.objects.count(), 3) + + # Check that the build reference is properly assigned + for build in Build.objects.all(): + self.assertEqual(str(build.reference), str(build.pk))