class TestUpdate(TestCase): def setUp(self): self.current_academic_year = create_current_academic_year() self.start_date_ay_1 = self.current_academic_year.start_date.replace( year=self.current_academic_year.year + 1) self.end_date_ay_1 = self.current_academic_year.end_date.replace( year=self.current_academic_year.year + 2) self.previous_academic_year = AcademicYearFactory( year=self.current_academic_year.year - 1) academic_year_1 = AcademicYearFactory.build( start_date=self.start_date_ay_1, end_date=self.end_date_ay_1, year=self.current_academic_year.year + 1) academic_year_1.save() self.start_date_ay_2 = self.current_academic_year.start_date.replace( year=self.current_academic_year.year + 2) self.end_date_ay_2 = self.current_academic_year.end_date.replace( year=self.current_academic_year.year + 3) academic_year_2 = AcademicYearFactory.build( start_date=self.start_date_ay_2, end_date=self.end_date_ay_2, year=self.current_academic_year.year + 2) academic_year_2.save() self.education_group_year = GroupFactory() EntityVersionFactory( entity=self.education_group_year.management_entity, start_date=self.education_group_year.academic_year.start_date) EntityVersionFactory( entity=self.education_group_year.administration_entity, start_date=self.education_group_year.academic_year.start_date) AuthorizedRelationshipFactory( parent_type=self.education_group_year.education_group_type, child_type=self.education_group_year.education_group_type) self.url = reverse(update_education_group, kwargs={ "root_id": self.education_group_year.pk, "education_group_year_id": self.education_group_year.pk }) self.person = CentralManagerFactory() PersonEntityFactory(person=self.person, entity=self.education_group_year.management_entity) self.client.force_login(self.person.user) permission = Permission.objects.get(codename='change_educationgroup') self.person.user.user_permissions.add(permission) self.perm_patcher = mock.patch( "base.business.education_groups.perms._is_eligible_certificate_aims", return_value=True) self.mocked_perm = self.perm_patcher.start() self.an_training_education_group_type = EducationGroupTypeFactory( category=education_group_categories.TRAINING) self.previous_training_education_group_year = TrainingFactory( academic_year=self.previous_academic_year, education_group_type=self.an_training_education_group_type, education_group__start_year=1968) EntityVersionFactory( entity=self.previous_training_education_group_year. management_entity, start_date=self.previous_training_education_group_year. academic_year.start_date) EntityVersionFactory( entity=self.previous_training_education_group_year. administration_entity, start_date=self.previous_training_education_group_year. academic_year.start_date) self.training_education_group_year = TrainingFactory( academic_year=self.current_academic_year, education_group_type=self.an_training_education_group_type, education_group__start_year=1968) self.training_education_group_year_1 = TrainingFactory( academic_year=academic_year_1, education_group_type=self.an_training_education_group_type, education_group=self.training_education_group_year.education_group) self.training_education_group_year_2 = TrainingFactory( academic_year=academic_year_2, education_group_type=self.an_training_education_group_type, education_group=self.training_education_group_year.education_group) AuthorizedRelationshipFactory( parent_type=self.an_training_education_group_type, child_type=self.an_training_education_group_type, ) EntityVersionFactory( entity=self.training_education_group_year.management_entity, start_date=self.education_group_year.academic_year.start_date) EntityVersionFactory( entity=self.training_education_group_year.administration_entity, start_date=self.education_group_year.academic_year.start_date) self.training_url = reverse(update_education_group, args=[ self.training_education_group_year.pk, self.training_education_group_year.pk ]) PersonEntityFactory( person=self.person, entity=self.training_education_group_year.management_entity) self.domains = [DomainFactory() for x in range(10)] self.a_mini_training_education_group_type = EducationGroupTypeFactory( category=education_group_categories.MINI_TRAINING) self.mini_training_education_group_year = MiniTrainingFactory( academic_year=self.current_academic_year, education_group_type=self.a_mini_training_education_group_type) self.mini_training_url = reverse( update_education_group, args=[ self.mini_training_education_group_year.pk, self.mini_training_education_group_year.pk ]) PersonEntityFactory( person=self.person, entity=self.mini_training_education_group_year.management_entity) EntityVersionFactory( entity=self.mini_training_education_group_year.management_entity, start_date=self.education_group_year.academic_year.start_date) def tearDown(self): self.perm_patcher.stop() def test_login_required(self): self.client.logout() response = self.client.get(self.url) self.assertRedirects(response, '/login/?next={}'.format(self.url)) def test_template_used(self): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "education_group/update_groups.html") def test_response_context(self): response = self.client.get(self.url) form_education_group_year = response.context[ "form_education_group_year"] self.assertIsInstance(form_education_group_year, GroupYearModelForm) def test_post(self): new_entity_version = MainEntityVersionFactory() PersonEntityFactory(person=self.person, entity=new_entity_version.entity) self.education_group_year.management_entity = new_entity_version.entity self.education_group_year.save() data = { 'title': 'Cours au choix', 'title_english': 'deaze', 'education_group_type': self.education_group_year.education_group_type.id, 'credits': 42, 'acronym': 'CRSCHOIXDVLD', 'partial_acronym': 'LDVLD101R', 'management_entity': new_entity_version.pk, 'main_teaching_campus': "", 'academic_year': self.education_group_year.academic_year.pk, "constraint_type": "", } response = self.client.post(self.url, data=data) self.assertEqual(response.status_code, 302) self.education_group_year.refresh_from_db() self.assertEqual(self.education_group_year.title, 'Cours au choix') self.assertEqual(self.education_group_year.title_english, 'deaze') self.assertEqual(self.education_group_year.credits, 42) self.assertEqual(self.education_group_year.acronym, 'CRSCHOIXDVLD') self.assertEqual(self.education_group_year.partial_acronym, 'LDVLD101R') self.assertEqual(self.education_group_year.management_entity, new_entity_version.entity) def test_template_used_for_training(self): response = self.client.get(self.training_url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "education_group/update_trainings.html") def test_template_used_for_certificate_edition(self): faculty_managers_group = Group.objects.get(name='faculty_managers') self.faculty_user = UserFactory() self.faculty_user.groups.add(faculty_managers_group) self.faculty_person = PersonFactory(user=self.faculty_user) self.client.force_login(self.faculty_user) permission = Permission.objects.get(codename='change_educationgroup') self.faculty_user.user_permissions.add(permission) response = self.client.get( reverse(update_education_group, args=[ self.previous_training_education_group_year.pk, self.previous_training_education_group_year.pk ])) self.assertEqual(response.status_code, 200) self.assertTemplateUsed( response, "education_group/blocks/form/training_certificate.html") certificate_aims = [ CertificateAimFactory(code=code) for code in range(100, 103) ] response = self.client.post( reverse(update_education_group, args=[ self.previous_training_education_group_year.pk, self.previous_training_education_group_year.pk ]), data={'certificate_aims': str(certificate_aims[0].id)}) self.assertEqual(response.status_code, 302) def test_post_training(self): old_domain = DomainFactory() EducationGroupYearDomainFactory( education_group_year=self.training_education_group_year, domain=old_domain) new_entity_version = MainEntityVersionFactory() PersonEntityFactory(person=self.person, entity=new_entity_version.entity) list_domains = [domain.pk for domain in self.domains] isced_domain = DomainIscedFactory() data = { 'title': 'Cours au choix', 'title_english': 'deaze', 'education_group_type': self.an_training_education_group_type.pk, 'credits': 42, 'acronym': 'CRSCHOIXDVLD', 'partial_acronym': 'LDVLD101R', 'management_entity': new_entity_version.pk, 'administration_entity': new_entity_version.pk, 'main_teaching_campus': "", 'academic_year': self.training_education_group_year.academic_year.pk, 'secondary_domains': [ '|' + ('|'.join([str(domain.pk) for domain in self.domains])) + '|' ], 'isced_domain': isced_domain.pk, 'active': ACTIVE, 'schedule_type': DAILY, "internship": internship_presence.NO, "primary_language": LanguageFactory().pk, "start_year": 2010, "constraint_type": "", "diploma_printing_title": "Diploma Title", } response = self.client.post(self.training_url, data=data) self.assertEqual(response.status_code, 302) self.training_education_group_year.refresh_from_db() self.assertEqual(self.training_education_group_year.title, 'Cours au choix') self.assertEqual(self.training_education_group_year.title_english, 'deaze') self.assertEqual(self.training_education_group_year.credits, 42) self.assertEqual(self.training_education_group_year.acronym, 'CRSCHOIXDVLD') self.assertEqual(self.training_education_group_year.partial_acronym, 'LDVLD101R') self.assertEqual(self.training_education_group_year.management_entity, new_entity_version.entity) self.assertEqual( self.training_education_group_year.administration_entity, new_entity_version.entity) self.assertEqual(self.training_education_group_year.isced_domain, isced_domain) self.assertCountEqual( list( self.training_education_group_year.secondary_domains. values_list('id', flat=True)), list_domains) self.assertNotIn(old_domain, self.education_group_year.secondary_domains.all()) def test_post_mini_training(self): old_domain = DomainFactory() EducationGroupYearDomainFactory( education_group_year=self.mini_training_education_group_year, domain=old_domain) new_entity_version = MainEntityVersionFactory() PersonEntityFactory(person=self.person, entity=new_entity_version.entity) data = { 'title': 'Cours au choix', 'title_english': 'deaze', 'education_group_type': self.a_mini_training_education_group_type.pk, 'credits': 42, 'acronym': 'CRSCHOIXDVLD', 'partial_acronym': 'LDVLD101R', 'management_entity': new_entity_version.pk, 'main_teaching_campus': "", 'academic_year': self.mini_training_education_group_year.academic_year.pk, 'active': ACTIVE, 'schedule_type': DAILY, "primary_language": LanguageFactory().pk, "start_year": 2010, "constraint_type": "", "diploma_printing_title": "Diploma Title", } response = self.client.post(self.mini_training_url, data=data) self.assertEqual(response.status_code, HttpResponseRedirect.status_code) self.mini_training_education_group_year.refresh_from_db() self.assertEqual(self.mini_training_education_group_year.title, 'Cours au choix') self.assertEqual(self.mini_training_education_group_year.title_english, 'deaze') self.assertEqual(self.mini_training_education_group_year.credits, 42) self.assertEqual(self.mini_training_education_group_year.acronym, 'CRSCHOIXDVLD') self.assertEqual( self.mini_training_education_group_year.partial_acronym, 'LDVLD101R') self.assertEqual( self.mini_training_education_group_year.management_entity, new_entity_version.entity) def test_post_training_with_end_year(self): new_entity_version = MainEntityVersionFactory() PersonEntityFactory(person=self.person, entity=new_entity_version.entity) data = { 'title': 'Cours au choix', 'title_english': 'deaze', 'education_group_type': self.an_training_education_group_type.pk, 'credits': 42, 'acronym': 'CRSCHOIXDVLD', 'partial_acronym': 'LDVLD101R', 'management_entity': new_entity_version.pk, 'administration_entity': new_entity_version.pk, 'main_teaching_campus': "", 'academic_year': self.training_education_group_year.academic_year.pk, 'secondary_domains': [ '|' + ('|'.join([str(domain.pk) for domain in self.domains])) + '|' ], 'active': ACTIVE, 'schedule_type': DAILY, "internship": internship_presence.NO, "primary_language": LanguageFactory().pk, "start_year": 2010, "end_year": 2018, "constraint_type": "", "diploma_printing_title": "Diploma Title", } response = self.client.post(self.training_url, data=data) messages = [m.message for m in get_messages(response.wsgi_request)] self.assertEqual( messages[1], _("Education group year %(acronym)s (%(academic_year)s) successfuly deleted." ) % { "acronym": self.training_education_group_year_1.acronym, "academic_year": self.training_education_group_year_1.academic_year, }) self.assertEqual( messages[2], _("Education group year %(acronym)s (%(academic_year)s) successfuly deleted." ) % { "acronym": self.training_education_group_year_2.acronym, "academic_year": self.training_education_group_year_2.academic_year, })
class TestEducationGroupConstraintEndYearOn2M(TestCase): @classmethod def setUpTestData(cls): cls.academic_year_2018 = AcademicYearFactory(year=2018) cls.academic_year_2019 = AcademicYearFactory(year=cls.academic_year_2018.year + 1) cls.academic_year_2020 = AcademicYearFactory(year=cls.academic_year_2018.year + 2) def setUp(self): # Create 2m pgrm structure as # 2M # |--FINALITY_LIST # |--2MS # |--2MD self.master_120 = TrainingFactory( academic_year=self.academic_year_2018, education_group_type__name=TrainingType.PGRM_MASTER_120.name, education_group__end_year=None ) self.finality_group = GroupFactory( academic_year=self.academic_year_2018, education_group_type__name=GroupType.FINALITY_120_LIST_CHOICE.name, education_group__end_year=None ) GroupElementYearFactory(parent=self.master_120, child_branch=self.finality_group) self.master_120_specialized = GroupFactory( academic_year=self.academic_year_2018, education_group_type__name=TrainingType.MASTER_MS_120.name, education_group__end_year=None ) GroupElementYearFactory(parent=self.finality_group, child_branch=self.master_120_specialized) self.master_120_didactic = GroupFactory( academic_year=self.academic_year_2018, education_group_type__name=TrainingType.MASTER_MD_120.name, education_group__end_year=self.academic_year_2019 ) GroupElementYearFactory(parent=self.finality_group, child_branch=self.master_120_didactic) def test_check_end_year_constraints_case_2m_end_year_is_greater_or_equals_than_finalities(self): """ In this test, we ensure that a root 2M can have end date which are greater or equals than his finalities """ self.master_120_specialized.education_group.end_year = self.academic_year_2019 self.master_120_specialized.education_group.save() self.master_120_specialized.refresh_from_db() self.master_120.education_group._check_end_year_constraints_on_2m() def test_check_end_year_constraints_case_2m_end_year_is_lower_than_finalities(self): """ In this test, we ensure that a root 2M CANNOT have end date which are lower than at least one end year of his finalities """ # Set root 2M to 2019 self.master_120.education_group.end_year = self.academic_year_2019 self.master_120.education_group.save() self.master_120.refresh_from_db() with self.assertRaises(ValidationError): self.master_120.education_group._check_end_year_constraints_on_2m() def test_check_end_year_constraints_case_finality_end_year_greater_than_2m(self): for edy in [self.master_120_didactic, self.master_120_specialized, self.master_120]: edy.education_group.end_year = self.academic_year_2019 edy.education_group.save() edy.education_group.refresh_from_db() self.master_120_didactic.education_group._check_end_year_constraints_on_2m() self.master_120_didactic.education_group.end_year = self.academic_year_2020 self.master_120_didactic.education_group.save() self.master_120_didactic.education_group.refresh_from_db() with self.assertRaises(ValidationError): self.master_120_didactic.education_group._check_end_year_constraints_on_2m()
class TestUpdate(TestCase): def setUp(self): self.current_academic_year = create_current_academic_year() self.education_group_year = GroupFactory() EntityVersionFactory( entity=self.education_group_year.management_entity, start_date=self.education_group_year.academic_year.start_date) EntityVersionFactory( entity=self.education_group_year.administration_entity, start_date=self.education_group_year.academic_year.start_date) AuthorizedRelationshipFactory( parent_type=self.education_group_year.education_group_type, child_type=self.education_group_year.education_group_type) self.url = reverse( update_education_group, args=[self.education_group_year.pk, self.education_group_year.pk]) self.person = PersonFactory() self.client.force_login(self.person.user) permission = Permission.objects.get(codename='change_educationgroup') self.person.user.user_permissions.add(permission) self.perm_patcher = mock.patch( "base.business.education_groups.perms.is_eligible_to_change_education_group", return_value=True) self.mocked_perm = self.perm_patcher.start() self.an_training_education_group_type = EducationGroupTypeFactory( category=education_group_categories.TRAINING) self.training_education_group_year = TrainingFactory( academic_year=self.current_academic_year, education_group_type=self.an_training_education_group_type) AuthorizedRelationshipFactory( parent_type=self.an_training_education_group_type, child_type=self.an_training_education_group_type, ) EntityVersionFactory( entity=self.training_education_group_year.management_entity, start_date=self.education_group_year.academic_year.start_date) EntityVersionFactory( entity=self.training_education_group_year.administration_entity, start_date=self.education_group_year.academic_year.start_date) self.training_url = reverse(update_education_group, args=[ self.training_education_group_year.pk, self.training_education_group_year.pk ]) self.domains = [DomainFactory() for x in range(10)] def tearDown(self): self.perm_patcher.stop() def test_login_required(self): self.client.logout() response = self.client.get(self.url) self.assertRedirects(response, '/login/?next={}'.format(self.url)) def test_permission_required(self): response = self.client.get(self.url) self.mocked_perm.assert_called_once_with(self.person, self.education_group_year, raise_exception=True) def test_template_used(self): response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "education_group/update_groups.html") def test_response_context(self): response = self.client.get(self.url) form_education_group_year = response.context[ "form_education_group_year"] self.assertIsInstance(form_education_group_year, GroupYearModelForm) def test_post(self): new_entity_version = MainEntityVersionFactory() data = { 'title': 'Cours au choix', 'title_english': 'deaze', 'education_group_type': self.education_group_year.education_group_type.id, 'credits': 42, 'acronym': 'CRSCHOIXDVLD', 'partial_acronym': 'LDVLD101R', 'management_entity': new_entity_version.pk, 'main_teaching_campus': "", 'academic_year': self.education_group_year.academic_year.pk, "constraint_type": "", } response = self.client.post(self.url, data=data) self.assertEqual(response.status_code, 302) self.education_group_year.refresh_from_db() self.assertEqual(self.education_group_year.title, 'Cours au choix') self.assertEqual(self.education_group_year.title_english, 'deaze') self.assertEqual(self.education_group_year.credits, 42) self.assertEqual(self.education_group_year.acronym, 'CRSCHOIXDVLD') self.assertEqual(self.education_group_year.partial_acronym, 'LDVLD101R') self.assertEqual(self.education_group_year.management_entity, new_entity_version.entity) def test_template_used_for_training(self): response = self.client.get(self.training_url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, "education_group/update_trainings.html") def test_post_training(self): old_domain = DomainFactory() EducationGroupYearDomainFactory( education_group_year=self.training_education_group_year, domain=old_domain) new_entity_version = MainEntityVersionFactory() list_domains = [domain.pk for domain in self.domains] data = { 'title': 'Cours au choix', 'title_english': 'deaze', 'education_group_type': self.an_training_education_group_type.pk, 'credits': 42, 'acronym': 'CRSCHOIXDVLD', 'partial_acronym': 'LDVLD101R', 'management_entity': new_entity_version.pk, 'administration_entity': new_entity_version.pk, 'main_teaching_campus': "", 'academic_year': self.training_education_group_year.academic_year.pk, 'secondary_domains': [ '|' + ('|'.join([str(domain.pk) for domain in self.domains])) + '|' ], 'active': ACTIVE, 'schedule_type': DAILY, "internship": internship_presence.NO, "primary_language": LanguageFactory().pk, "start_year": 2010, "constraint_type": "", } response = self.client.post(self.training_url, data=data) self.assertEqual(response.status_code, 302) self.training_education_group_year.refresh_from_db() self.assertEqual(self.training_education_group_year.title, 'Cours au choix') self.assertEqual(self.training_education_group_year.title_english, 'deaze') self.assertEqual(self.training_education_group_year.credits, 42) self.assertEqual(self.training_education_group_year.acronym, 'CRSCHOIXDVLD') self.assertEqual(self.training_education_group_year.partial_acronym, 'LDVLD101R') self.assertEqual(self.training_education_group_year.management_entity, new_entity_version.entity) self.assertEqual( self.training_education_group_year.administration_entity, new_entity_version.entity) self.assertListEqual( list( self.training_education_group_year.secondary_domains. values_list('id', flat=True)), list_domains) self.assertNotIn(old_domain, self.education_group_year.secondary_domains.all())