From 131c45e1c7830c82a975bdd39f8840fdb58191a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Tue, 5 Sep 2017 14:16:59 +0200 Subject: [PATCH] Add tests to check data migrations of supportBDS. Run 'bash provisioning/check_migrations_supportBDS.sh' from the vagrant VM. This will do the following: - delete the existing database 'cof_gestion', if applicable, - apply migrations to render the database as pre-supportBDS, - the last pre-supportBDS migration of 'cof' app creates some instances using old database schema, - supportBDS migrations are applied, - finally, the 'check_olddata' command of 'cof' app ensures data has been correctly migrated. This commit should be reverted before reaching production stage. --- cof/management/commands/check_olddata.py | 93 +++++++++++++++++++++ cof/migrations/0008_py3.py | 87 +++++++++++++++++++ provisioning/check_migrations_supportBDS.sh | 40 +++++++++ 3 files changed, 220 insertions(+) create mode 100644 cof/management/commands/check_olddata.py create mode 100644 provisioning/check_migrations_supportBDS.sh diff --git a/cof/management/commands/check_olddata.py b/cof/management/commands/check_olddata.py new file mode 100644 index 00000000..8fdac0e4 --- /dev/null +++ b/cof/management/commands/check_olddata.py @@ -0,0 +1,93 @@ +from django.contrib.auth.models import User, Group +from django.core.management import BaseCommand + +from gestion.models import ( + Association, Club, ClubUser, Event, EventCommentField, EventCommentValue, + EventRegistration, Location, +) + + +class Command(BaseCommand): + + def handle(self, *args, **options): + self.check_cof_assoc() + self.check_users() + self.check_clubs() + self.check_events() + + self.stdout.write("All good, gg wp! :-)") + + def check_cof_assoc(self): + self.stdout.write("* COF assoc... ", ending='') + + self.assoc = Association.objects.get(name='COF') + self.g_staff = Group.objects.get(name='cof_buro') + self.g_members = Group.objects.get(name='cof_members') + assert self.assoc.staff_group == self.g_staff + assert self.assoc.members_group == self.g_members + + self.stdout.write("OK") + + def check_users(self): + self.stdout.write("* Utilisateurs... ", ending='') + + self.u1 = User.objects.get(username='user1') + assert self.u1.profile.login_clipper == 'user1' + assert self.g_staff in self.u1.groups.all() + assert self.g_members in self.u1.groups.all() + assert self.u1.has_perm('cof.buro') + assert self.u1.has_perm('cof.member') + + self.u2 = User.objects.get(username='user2') + assert self.u2.profile.login_clipper == 'user2' + assert self.g_staff not in self.u2.groups.all() + assert self.g_members in self.u2.groups.all() + assert not self.u2.has_perm('cof.buro') + assert self.u2.has_perm('cof.member') + + self.u3 = User.objects.get(username='user3') + assert self.u3.profile.login_clipper == 'user3' + assert self.g_staff not in self.u3.groups.all() + assert self.g_members not in self.u3.groups.all() + assert not self.u3.has_perm('cof.buro') + assert not self.u3.has_perm('cof.member') + + self.stdout.write("OK") + + def check_clubs(self): + self.stdout.write("* Clubs... ", ending='') + + c1 = Club.objects.get(name='Club 1') + m1_1 = ClubUser.objects.get(user=self.u1, club=c1) + assert not m1_1.is_respo + m1_2 = ClubUser.objects.get(user=self.u2, club=c1) + assert m1_2.is_respo + + c2 = Club.objects.get(name='Club 2') + assert c2.members.count() == 0 + + self.stdout.write("OK") + + def check_events(self): + self.stdout.write("* Évènements... ", ending='') + + Location.objects.get(name='Location 1') + assert Location.objects.count() == 1 + + e1 = Event.objects.get(title='Event 1') + + assert e1.associations.count() == 1 + assert e1.commentfields.count() == 2 + + er1 = e1.eventregistration_set.all() + assert len(er1) == 2 + + er1_1 = EventRegistration.objects.get(user=self.u2, event=e1) + assert er1_1.filledcomments.count() == 1 + + er1_2 = EventRegistration.objects.get(user=self.u3, event=e1) + assert er1_2.filledcomments.count() == 1 + + Event.objects.get(title='Event 2') + + self.stdout.write("OK") diff --git a/cof/migrations/0008_py3.py b/cof/migrations/0008_py3.py index 612d06ac..99aa2f9f 100644 --- a/cof/migrations/0008_py3.py +++ b/cof/migrations/0008_py3.py @@ -9,6 +9,92 @@ def forwards(apps, schema_editor): Profile.objects.update(comments="") +def load_user(apps, username, is_cof=False): + User = apps.get_model('auth', 'User') + CofProfile = apps.get_model('cof', 'CofProfile') + + u = User.objects.create( + username=username, + first_name=username, + last_name=username, + ) + + p = CofProfile.objects.create(user=u) + p.login_clipper = username + p.is_cof = is_cof + p.save() + + return u + + +def load_olddata(apps, schema_editor): + print( + ">>> Insertion de données utilisant le schéma de DB pré-supportBDS...", + end=' ', + ) + + # Setup users. + u1 = load_user(apps, 'user1', is_cof=True) + u1.profile.is_buro = True + u1.profile.save() + + u2 = load_user(apps, 'user2', is_cof=True) + + u3 = load_user(apps, 'user3') + + # Setup clubs. + Club = apps.get_model('cof', 'Club') + + c1 = Club.objects.create(name='Club 1') + c2 = Club.objects.create(name='Club 2') + + c1.membres.add(u1, u2) + c1.respos.add(u2) + + # Setup events. + Event = apps.get_model('cof', 'Event') + + e1 = Event.objects.create( + title='Event 1', + location='Location 1', + ) + e2 = Event.objects.create( + title='Event 2', + location='Location 1', + ) + + EventRegistration = apps.get_model('cof', 'EventRegistration') + + er1_1 = EventRegistration.objects.create(user=u2, event=e1) + er1_2 = EventRegistration.objects.create(user=u3, event=e1) + + EventCommentField = apps.get_model('cof', 'EventCommentField') + + ecf1_1 = EventCommentField.objects.create( + name='Comment field 1', + event=e1, + ) + ecf1_2 = EventCommentField.objects.create( + name='Comment field 2', + event=e1, + ) + + EventCommentValue = apps.get_model('cof', 'EventCommentValue') + + EventCommentValue.objects.create( + commentfield=ecf1_1, + registration=er1_1, + content='', + ) + EventCommentValue.objects.create( + commentfield=ecf1_2, + registration=er1_2, + content='', + ) + + print("DONE") + + class Migration(migrations.Migration): dependencies = [ @@ -250,4 +336,5 @@ class Migration(migrations.Migration): field=models.CharField(verbose_name='Question', max_length=200), ), migrations.RunPython(forwards, migrations.RunPython.noop), + migrations.RunPython(load_olddata), ] diff --git a/provisioning/check_migrations_supportBDS.sh b/provisioning/check_migrations_supportBDS.sh new file mode 100644 index 00000000..2ab79657 --- /dev/null +++ b/provisioning/check_migrations_supportBDS.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +function mysql_cmd { + mysql -uroot -p$DBPASSWD -e "$*" +} + +# Recreate database. +mysql_cmd DROP DATABASE $DBNAME >/dev/null 2>&1 +mysql_cmd CREATE DATABASE $DBNAME >/dev/null 2>&1 + +function dj-migrate { + python manage.py migrate -v0 $* +} + +echo ">>> Mise en place de la BDD utilisant le schéma pré-supportBDS..." + +dj-migrate auth +dj-migrate sites +dj-migrate contenttypes +dj-migrate admin +dj-migrate sessions + +dj-migrate django_cas_ng +dj-migrate custommail + +dj-migrate bda 0010 +dj-migrate cof 0008 +dj-migrate kfet 0047 + +echo "DONE." + +echo ">>> Application des migrations supportBDS..." + +dj-migrate + +echo "DONE." + +echo ">>> Les migrations supportBDS ont-elles fait du bon travail ?" + +python manage.py check_olddata