Created
January 28, 2020 15:55
-
-
Save pmg103/5d09be56be97b8959518ebb7d1e6fcf2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.apps import apps | |
from django.test import TestCase | |
from django.db.migrations.executor import MigrationExecutor | |
from django.db import connection | |
from django.db.models import F | |
from model_mommy import mommy | |
class TestMigrations(TestCase): | |
@property | |
def app(self): | |
return apps.get_containing_app_config(type(self).__module__).name | |
# migrate_from = None | |
# migrate_to = None | |
def setUp(self): | |
assert self.migrate_from and self.migrate_to, \ | |
"TestCase '{}' must define migrate_from and migrate_to properties".format(type(self).__name__) | |
# self.migrate_from = [(self.app, self.migrate_from)] | |
# self.migrate_to = [(self.app, self.migrate_to)] | |
self.migrate_from = [('assets', self.migrate_from)] | |
self.migrate_to = [('assets', self.migrate_to)] | |
executor = MigrationExecutor(connection) | |
old_apps = executor.loader.project_state(self.migrate_from).apps | |
# Reverse to the original migration | |
executor.migrate(self.migrate_from) | |
self.setUpBeforeMigration(old_apps) | |
# Run the migration to test | |
executor = MigrationExecutor(connection) | |
executor.loader.build_graph() # reload. | |
executor.migrate(self.migrate_to) | |
self.apps = executor.loader.project_state(self.migrate_to).apps | |
def setUpBeforeMigration(self, apps): | |
pass | |
class RemoveDemographicFieldsTestCase(TestMigrations): | |
migrate_from = '0408_auto_20200113_1059' | |
migrate_to = '0409_auto_20200121_1424' | |
def dump(self): | |
print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') | |
for p in self.products: | |
print(p.name) | |
for pv in p.productversion_set.all(): | |
print(' Version') | |
for qc in pv.questioncollection_set.all(): | |
print(' QC #', qc.position) | |
for q in qc.questions.all(): | |
print(' Q #', q.code, q.position) | |
def setUpBeforeMigration(self, apps): | |
Product = apps.get_model('assets', 'Product') | |
ProductVersion = apps.get_model('assets', 'ProductVersion') | |
QuestionCollection = apps.get_model('assets', 'QuestionCollection') | |
Question = apps.get_model('assets', 'Question') | |
TranslatableText = apps.get_model('assets', 'TranslatableText') | |
p1 = mommy.make(Product, name="360", type='THREE_SIXTY') | |
p1v1 = mommy.make(ProductVersion, product=p1) | |
p1v1qc1 = mommy.make(QuestionCollection, product_version=p1v1) | |
questions = [ | |
Question(code='p1v1qc1 q%d' % i, question_code='p1v1qc1 q%d' % i, question_collection=p1v1qc1, position=i, translatable_text=mommy.make(TranslatableText)) | |
for i in range(10) | |
] | |
Question.objects.bulk_create(questions) | |
p1v2 = mommy.make(ProductVersion, product=p1) | |
p1v2qc1 = mommy.make(QuestionCollection, product_version=p1v2) | |
questions = [ | |
Question(code='p1v2qc1 q%d' % i, question_code='p1v2qc1 q%d' % i, question_collection=p1v2qc1, position=i, translatable_text=mommy.make(TranslatableText)) | |
for i in range(10) | |
] | |
Question.objects.bulk_create(questions) | |
p2 = mommy.make(Product, name="PP", type='PSYCAP_POTENTIAL') | |
p2v1 = mommy.make(ProductVersion, product=p2) | |
p2v1qc3 = mommy.make(QuestionCollection, product_version=p2v1) | |
questions = [ | |
Question(code='p2v1qc3 q%d' % i, question_code='p2v1qc3 q%d' % i, question_collection=p2v1qc3, position=i, translatable_text=mommy.make(TranslatableText)) | |
for i in range(10) | |
] | |
Question.objects.bulk_create(questions) | |
p2v1qc2 = mommy.make(QuestionCollection, product_version=p2v1) | |
questions = [ | |
Question(code='p2v1qc2 q%d' % i, question_code='p2v1qc2 q%d' % i, question_collection=p2v1qc2, position=i, translatable_text=mommy.make(TranslatableText)) | |
for i in range(10) | |
] | |
Question.objects.bulk_create(questions) | |
p2v1qc1 = mommy.make(QuestionCollection, product_version=p2v1) | |
questions = [ | |
Question(code='p2v1qc1 q%d' % i, question_code='p2v1qc1 q%d' % i, question_collection=p2v1qc1, position=i, translatable_text=mommy.make(TranslatableText)) | |
for i in range(10) | |
] | |
Question.objects.bulk_create(questions) | |
self.products = [p1, p2] | |
self.question_collections = QuestionCollection.objects.all() | |
self.dump() | |
Question.objects.update(position=F('position') + 100) | |
self.dump() | |
def test_demographic_fields_migrated(self): | |
self.dump() | |
for qc in self.question_collections: | |
positions = qc.questions.values_list('position', flat=True) | |
self.assertEqual( | |
len(positions), | |
len(set(positions)), | |
'Duplicate position values found in qc! %s' % qc.id | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment