Exemplo n.º 1
0
class AttributionTest(TestCase):
    def setUp(self):
        # Creation Person/Tutor
        Group.objects.create(name="tutors")
        self.person = PersonFactory(global_id="98363454")
        TutorFactory(person=self.person)

        _create_multiple_academic_year()
        self.current_academic_year = create_current_academic_year()

        # Creation Json which will be store on attribution
        attributions = _get_attributions_dict(self.current_academic_year.year)
        self.attrib = AttributionNewFactory(global_id=self.person.global_id,
                                            attributions=attributions)

    def test_get_attribution_list(self):
        attribution_list = attribution.get_attribution_list(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(len(attribution_list), 3)

        previous_year = self.current_academic_year.year - 1
        academic_year_2016 = AcademicYear.objects.get(year=previous_year)
        attribution_list = attribution.get_attribution_list(
            self.person.global_id, academic_year_2016)
        self.assertEqual(len(attribution_list), 1)

        attribution_list = attribution.get_attribution_list(
            self.person.global_id)
        self.assertEqual(len(attribution_list), 3)

    def test_get_attribution_list_empty(self):
        academic_year = AcademicYearFactory(year=1990)
        attribution_list = attribution.get_attribution_list(
            self.person.global_id, academic_year)
        self.assertFalse(attribution_list)

    def test_get_attribution_order(self):
        attribution_list = attribution.get_attribution_list(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(len(attribution_list), 3)
        self.assertEqual(attribution_list[0]['acronym'], "LAGRO1530")
        self.assertEqual(attribution_list[1]['acronym'], "LBIR1200")
        self.assertEqual(attribution_list[2]['acronym'], "LBIR1300")

    def test_computes_volumes_total(self):
        attribution_list = attribution.get_attribution_list(
            self.person.global_id, self.current_academic_year)
        volumes_total = attribution.get_volumes_total(attribution_list)
        self.assertTrue(volumes_total)
        self.assertEqual(
            volumes_total.get(learning_component_year_type.LECTURING),
            Decimal(53.5))
        self.assertEqual(
            volumes_total.get(
                learning_component_year_type.PRACTICAL_EXERCISES),
            Decimal(21.5))

    def test_append_team_and_volume_delcared_vacant(self):
        #  Create Container Year and component
        _create_learning_container_with_components(
            acronym="LBIR1300",
            academic_year=self.current_academic_year,
            volume_lecturing=Decimal(15.5),
            volume_practical_exercices=Decimal(5))
        _create_learning_container_with_components(
            acronym="LBIR1200",
            academic_year=self.current_academic_year,
            volume_lecturing=Decimal(30),
            volume_practical_exercices=Decimal(75))
        _create_learning_container_with_components("LAGRO1530",
                                                   self.current_academic_year,
                                                   Decimal(30), Decimal(30))

        attribution_list = attribution.get_attribution_list(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(attribution_list[0]['acronym'], "LAGRO1530")
        self.assertEqual(attribution_list[0]['volume_lecturing_vacant'],
                         Decimal(30))
        self.assertEqual(
            attribution_list[0]['volume_practical_exercices_vacant'],
            Decimal(30))
        self.assertEqual(attribution_list[0]['team'], False)
        self.assertEqual(attribution_list[1]['acronym'], "LBIR1200")
        self.assertEqual(attribution_list[1]['volume_lecturing_vacant'],
                         Decimal(30))
        self.assertEqual(
            attribution_list[1]['volume_practical_exercices_vacant'],
            Decimal(75))
        self.assertEqual(attribution_list[1]['team'], False)
        self.assertEqual(attribution_list[2]['acronym'], "LBIR1300")
        self.assertEqual(attribution_list[2]['volume_lecturing_vacant'],
                         Decimal(15.5))
        self.assertEqual(
            attribution_list[2]['volume_practical_exercices_vacant'],
            Decimal(5))
        self.assertEqual(attribution_list[2]['team'], False)

    def test_append_start_end_academic_year(self):
        attribution_list = attribution.get_attribution_list(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(attribution_list[0]['acronym'], "LAGRO1530")
        self.assertTrue(attribution_list[0]['start_academic_year'])
        self.assertEqual(attribution_list[0]['start_academic_year'].year, 2015)
        self.assertEqual(attribution_list[0]['end_academic_year'].year,
                         self.current_academic_year.year)

        self.assertEqual(attribution_list[1]['acronym'], "LBIR1200")
        self.assertTrue(attribution_list[1]['start_academic_year'])
        self.assertEqual(attribution_list[1]['start_academic_year'].year, 2013)
        self.assertRaises(
            KeyError,
            lambda: attribution_list[1]['end_academic_year'])  # No end year

        self.assertEqual(attribution_list[2]['acronym'], "LBIR1300")
        self.assertTrue(attribution_list[2]['start_academic_year'])
        self.assertEqual(attribution_list[2]['start_academic_year'].year, 2015)
        self.assertEqual(attribution_list[2]['end_academic_year'].year,
                         self.current_academic_year.year + 1)

    def test_get_attribution_list_about_to_expire(self):
        _create_learning_container_with_components("LAGRO1530",
                                                   self.current_academic_year,
                                                   Decimal(30), Decimal(30))
        next_academic_year = AcademicYear.objects.get(
            year=self.current_academic_year.year + 1)
        _create_learning_container_with_components("LAGRO1530",
                                                   next_academic_year,
                                                   Decimal(30), Decimal(30))
        attribution_list_about_to_expired = attribution.get_attribution_list_about_to_expire(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(len(attribution_list_about_to_expired), 1)
        self.assertTrue(attribution_list_about_to_expired[0]['is_renewable'])
        self.assertIsNone(
            attribution_list_about_to_expired[0]['not_renewable_reason'])

    def test_get_attribution_list_about_to_expire_volume_pratical_lower(self):
        _create_learning_container_with_components("LAGRO1530",
                                                   self.current_academic_year,
                                                   Decimal(30), Decimal(30))
        next_academic_year = AcademicYear.objects.get(
            year=self.current_academic_year.year + 1)
        _create_learning_container_with_components("LAGRO1530",
                                                   next_academic_year,
                                                   Decimal(1), Decimal(30))
        attribution_list_about_to_expired = attribution.get_attribution_list_about_to_expire(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(len(attribution_list_about_to_expired), 1)
        self.assertFalse(attribution_list_about_to_expired[0]['is_renewable'])
        self.assertEqual(
            attribution_list_about_to_expired[0]['not_renewable_reason'],
            _("The vacant volume of the next academic year is lower than the current one"
              ))

    def test_get_attribution_list_about_to_expire_volume_lecturing__lower(
            self):
        _create_learning_container_with_components("LAGRO1530",
                                                   self.current_academic_year,
                                                   Decimal(30), Decimal(30))
        next_academic_year = AcademicYear.objects.get(
            year=self.current_academic_year.year + 1)
        _create_learning_container_with_components("LAGRO1530",
                                                   next_academic_year,
                                                   Decimal(30), Decimal(1))
        attribution_list_about_to_expired = attribution.get_attribution_list_about_to_expire(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(len(attribution_list_about_to_expired), 1)
        self.assertFalse(attribution_list_about_to_expired[0]['is_renewable'])
        self.assertEqual(
            attribution_list_about_to_expired[0]['not_renewable_reason'],
            _("The vacant volume of the next academic year is lower than the current one"
              ))

    def test_get_attribution_list_about_to_expire_volume_zero_is_renewable(
            self):
        self.attrib.attributions = [{
            'year': self.current_academic_year.year,
            'acronym': 'LAGRO1530',
            'title': 'Agrochimie élémentaire',
            'weight': '5.00',
            'LECTURING': '0',
            'PRACTICAL_EXERCISES': '0',
            'function': 'HOLDER',
            'start_year': 2015,
            'end_year': self.current_academic_year.year,
            'is_substitute': False
        }]
        self.attrib.save()
        _create_learning_container_with_components("LAGRO1530",
                                                   self.current_academic_year,
                                                   Decimal(30), Decimal(30))
        next_academic_year = AcademicYear.objects.get(
            year=self.current_academic_year.year + 1)
        _create_learning_container_with_components("LAGRO1530",
                                                   next_academic_year,
                                                   Decimal(30), Decimal(30))
        attribution_list_about_to_expired = attribution.get_attribution_list_about_to_expire(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(len(attribution_list_about_to_expired), 1)
        self.assertTrue(attribution_list_about_to_expired[0]['is_renewable'])
        self.assertEqual(
            attribution_list_about_to_expired[0]['not_renewable_reason'], None)

    def test_get_attribution_list_with_substitute_is_not_renewable(self):
        self.attrib.attributions = [{
            'year': self.current_academic_year.year,
            'acronym': 'LAGRO1530',
            'title': 'Agrochimie élémentaire',
            'weight': '5.00',
            'LECTURING': '0',
            'PRACTICAL_EXERCISES': '0',
            'function': 'HOLDER',
            'start_year': 2015,
            'end_year': self.current_academic_year.year,
            'is_substitute': True
        }]
        self.attrib.save()
        _create_learning_container_with_components("LAGRO1530",
                                                   self.current_academic_year,
                                                   Decimal(30), Decimal(30))
        next_academic_year = AcademicYear.objects.get(
            year=self.current_academic_year.year + 1)
        _create_learning_container_with_components("LAGRO1530",
                                                   next_academic_year,
                                                   Decimal(30), Decimal(30))
        attribution_list_about_to_expired = attribution.get_attribution_list_about_to_expire(
            self.person.global_id, self.current_academic_year)
        self.assertEqual(len(attribution_list_about_to_expired), 1)
        self.assertFalse(attribution_list_about_to_expired[0]['is_renewable'])
        self.assertEqual(
            attribution_list_about_to_expired[0]['not_renewable_reason'],
            _('A substitute can not renew his function of substitute'))

    def test_get_attribution_list_about_to_expire_already_applied(self):
        _create_learning_container_with_components("LAGRO1530",
                                                   self.current_academic_year,
                                                   Decimal(30), Decimal(30))
        next_academic_year = AcademicYear.objects.get(
            year=self.current_academic_year.year + 1)
        _create_learning_container_with_components("LAGRO1530",
                                                   next_academic_year,
                                                   Decimal(30), Decimal(30))
        application = [{
            'remark': 'This is the remarks',
            'course_summary': 'This is the course summary',
            'charge_lecturing_asked': 30,
            'charge_practical_asked': 30,
            'acronym': "LAGRO1530",
            'year': next_academic_year.year,
            'is_substitute': False
        }]
        self.attrib.applications = application
        self.attrib.save()

        attribution_list_about_to_expired = attribution.get_attribution_list_about_to_expire(
            self.person.global_id, self.current_academic_year)

        self.assertEqual(len(attribution_list_about_to_expired), 1)
        self.assertFalse(attribution_list_about_to_expired[0]['is_renewable'])
        self.assertEqual(
            attribution_list_about_to_expired[0]['not_renewable_reason'],
            _('An application has already been submitted'))

    def test_calculate_effective_volume(self):
        vol_tot = 10.0
        planned_classes = 2
        data = {'PLANNED_CLASSES': planned_classes, 'VOLUME_TOTAL': vol_tot}
        self.assertEqual(attribution._calculate_effective_volume(data),
                         vol_tot * planned_classes)

    def test_calculate_effective_volume_incorrect(self):
        vol_tot = 10.0
        planned_classes = -1
        data = {'PLANNED_CLASSES': planned_classes, 'VOLUME_TOTAL': vol_tot}
        self.assertEqual(attribution._calculate_effective_volume(data),
                         attribution.NO_CHARGE)

    def test_calculate_effective_volume_no_volume_provided(self):
        self.assertEqual(
            attribution._calculate_effective_volume({'PLANNED_CLASSES': 1}),
            attribution.NO_CHARGE)

    def test_calculate_effective_volume_case_negative_volume(self):
        vol_tot = -10.0
        planned_classes = 1
        data = {'PLANNED_CLASSES': planned_classes, 'VOLUME_TOTAL': vol_tot}
        self.assertEqual(attribution._calculate_effective_volume(data),
                         attribution.NO_CHARGE)

    def test_get_teachers_parameter_none(self):
        self.assertIsNone(attribution.get_teachers(None, None))
        self.assertIsNone(attribution.get_teachers('LCHM1111', None))
        academic_year = AcademicYearFactory()
        self.assertIsNone(attribution.get_teachers(None, academic_year))

    def test_get_teachers_no_teacher_found(self):
        self.assertIsNone(attribution.get_teachers('LCHM1111', 2017))

    def test_get_2_teachers_in_order(self):
        acronym_LCHM1111 = 'LCHM1111'
        acronym_LDVLP2627 = 'LDVLP2627'
        acronym_LDROI1110 = 'LDROI1110'

        # Create first teacher with one attribution on LCHM1111 and others
        person_first_alphabetical_order = PersonFactory(global_id='12345678',
                                                        last_name='Antonelli')
        attribution_teacher_1_LCHM1111 = {
            'year': 2017,
            'acronym': acronym_LCHM1111
        }
        attributions_teacher1 = [
            attribution_teacher_1_LCHM1111, {
                'year': 2016,
                'acronym': acronym_LDVLP2627
            }, {
                'year': 2017,
                'acronym': acronym_LDVLP2627
            }, {
                'year': 2017,
                'acronym': acronym_LDROI1110
            }
        ]

        AttributionNewFactory(
            global_id=person_first_alphabetical_order.global_id,
            attributions=attributions_teacher1)
        # Create second teacher with one attribution on LCHM1111
        person_second_alphabetical_order = PersonFactory(global_id='987654321',
                                                         last_name='Barnet')
        attribution_teacher_2_LCHM1111 = {
            'year': 2017,
            'acronym': acronym_LCHM1111
        }
        attributions_teacher2 = [attribution_teacher_2_LCHM1111]
        AttributionNewFactory(
            global_id=person_second_alphabetical_order.global_id,
            attributions=attributions_teacher2)
        #
        attributions_result = attribution.get_teachers(acronym_LCHM1111, 2017)
        self.assertEqual(len(attributions_result), 2)
        self.assertEqual(attributions_result[0][attribution.PERSON_KEY],
                         person_first_alphabetical_order)

    def test_update_learning_unit_volume_no_components(self):
        """When no components found on database, the key 'lecturing_vol' / 'practical_exercises_vol' is set to 0.0"""
        l_container = LearningContainerYearFactory(
            acronym='LAGRO1530', academic_year=self.current_academic_year)
        LearningUnitYearFactory(acronym='LAGRO1530',
                                academic_year=self.current_academic_year,
                                learning_container_year=l_container)
        an_attribution = {
            'year': 2017,
            'acronym': 'LAGRO1530',
            'title': 'Chimie complexe',
            'weight': '5.00',
            'LECTURING': '22.5',
            'PRACTICAL_EXERCISES': '5.0',
            'function': 'HOLDER',
            'start_year': 2015,
            'end_year': 2020
        }

        attribution.update_learning_unit_volume(an_attribution,
                                                self.current_academic_year)
        self.assertEqual(an_attribution['lecturing_vol'], Decimal(0.0))
        self.assertEqual(an_attribution['practical_exercises_vol'],
                         Decimal(0.0))

    def test_calculate_component_volume(self):
        an_attribution = {
            'year': 2017,
            'acronym': 'LAGRO1530',
            'title': 'Chimie complexe',
            'weight': '5.00',
            'LECTURING': '22.5',
            'PRACTICAL_EXERCISES': '5.0',
            'function': 'HOLDER',
            'start_year': 2015,
            'end_year': 2020
        }
        l_component_lecturing = LearningComponentYearFactory(
            type=learning_component_year_type.LECTURING)
        l_component_other = LearningComponentYearFactory(type=None)
        components_computed = {
            l_component_lecturing: {
                'VOLUME_TOTAL': Decimal(15),
                'PLANNED_CLASSES': 5
            },
            l_component_other: {
                'VOLUME_TOTAL': Decimal(1),
                'PLANNED_CLASSES': 1
            }
        }
        attribution._calculate_component_volume(an_attribution,
                                                components_computed)
        self.assertEqual(an_attribution['lecturing_vol'],
                         Decimal(75))  # VOLUME_TOTAL * PLANNED_CLASSES
        self.assertRaises(KeyError,
                          lambda: an_attribution['practical_exercises_vol'])

    def test_get_attribution_vacant_none(self):
        learning_container_yr = LearningContainerYearFactory()
        self.assertIsNone(
            attribution.get_attribution_vacant(learning_container_yr))

    def test_format_no_volume(self):
        attributions_without_volume = [{'year': 2018, 'acronym': 'LBIR1200'}]
        attributions_formalized = attribution._format_str_volume_to_decimal(
            attributions_without_volume)
        self.assertEqual(attributions_formalized[0]['LECTURING'], 0.0)
        self.assertEqual(attributions_formalized[0]['PRACTICAL_EXERCISES'],
                         0.0)
Exemplo n.º 2
0
class TestTutorApplicationEpc(TestCase):
    def setUp(self):
        self.academic_year = AcademicYearFactory(year=2017)
        external_id = tutor_application_epc.LEARNING_CONTAINER_YEAR_PREFIX_EXTERNAL_ID + '35654987_2017'
        self.lbir1200 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LBIR1200",
                                                     external_id=external_id)
        self.lagro2630 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LAGRO2630")

        # Creation Person/Tutor
        Group.objects.create(name="tutors")
        person = PersonFactory(global_id="98363454")
        external_id = tutor_application_epc.TUTOR_PREFIX_EXTERNAL_ID + '2089590559'
        self.tutor = TutorFactory(external_id=external_id, person=person)

        # Create two tutor applications
        applications = [_get_application_example(self.lbir1200, '30.5', '40.5'),
                        _get_application_example(self.lagro2630, '12.5', '0')]
        self.attribution = AttributionNewFactory(
            global_id=person.global_id,
            applications=applications
        )

    def test_extract_learning_container_year_epc_info(self):
        learning_container_info = tutor_application_epc._extract_learning_container_year_epc_info('LBIR1200', 2017)
        self.assertIsInstance(learning_container_info, dict)
        self.assertEqual(learning_container_info['reference'], '35654987')
        self.assertEqual(learning_container_info['year'], 2017)

    def test_extract_learning_container_year_epc_info_empty(self):
        learning_container_info = tutor_application_epc._extract_learning_container_year_epc_info('LBIR1250', 2017)
        self.assertIsInstance(learning_container_info, dict)
        self.assertFalse(learning_container_info)

    def test_extract_learning_container_year_epc_info_with_external_id_empty(self):
        self.lbir1200.external_id = None
        self.lbir1200.save()
        learning_container_info = tutor_application_epc._extract_learning_container_year_epc_info('LBIR1200', 2017)
        self.assertIsInstance(learning_container_info, dict)
        self.assertFalse(learning_container_info)

    def test_convert_to_epc_application(self):
        person = self.tutor.person
        application = _get_application_example(self.lbir1200, '30.5', '40.5')

        epc_message = tutor_application_epc._convert_to_epc_application(application=application)

        self.assertTrue(epc_message)
        self.assertEqual(epc_message['remark'], application['remark'])
        self.assertEqual(epc_message['course_summary'], application['course_summary'])
        self.assertEqual(epc_message['lecturing_allocation'], application['charge_lecturing_asked'])
        self.assertEqual(epc_message['practical_allocation'], application['charge_practical_asked'])
        self.assertIsInstance(epc_message['learning_container_year'], dict)
        self.assertEqual(epc_message['learning_container_year']['reference'], '35654987')
        self.assertEqual(epc_message['learning_container_year']['year'], 2017)

    def test_process_message_delete_operation(self):
        person = self.tutor.person
        self.assertEqual(len(self.attribution.applications), 2)
        body = {
            'operation': tutor_application_epc.DELETE_OPERATION,
            'global_id': person.global_id,
            'learning_container_year': {
                'acronym': 'LBIR1200',
                'year': 2017
            }
        }
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_epc.process_message(body_encoded)
        # Check if the application is removed
        self.attribution.refresh_from_db()
        self.assertEqual(len(self.attribution.applications), 1)
        attribution_left = self.attribution.applications[0]
        self.assertEqual(attribution_left['acronym'], self.lagro2630.acronym)

    def test_process_message_update_operation(self):
        person = self.tutor.person
        _set_all_application_in_pending_state(self.attribution.applications)
        self.attribution.save()
        ## Check if all are in pending
        self.assertEqual(len(self.attribution.applications), 2)
        self.assertEqual(self.attribution.applications[0]['pending'], tutor_application_epc.UPDATE_OPERATION)
        self.assertEqual(self.attribution.applications[1]['pending'], tutor_application_epc.UPDATE_OPERATION)

        body = {
            'operation': tutor_application_epc.UPDATE_OPERATION,
            'global_id': person.global_id,
            'learning_container_year': {
                'acronym': 'LBIR1200',
                'year': 2017
            }
        }
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_epc.process_message(body_encoded)
        # Check if the application is removed
        self.attribution.refresh_from_db()
        applications_not_pending = [application for application in self.attribution.applications if
                                    not "pending" in application]
        self.assertEqual(len(applications_not_pending), 1)


    def test_process_message_with_error_operation(self):
        person = self.tutor.person
        _set_all_application_in_pending_state(self.attribution.applications)
        self.attribution.save()
        ## Check if all are in pending
        self.assertEqual(len(self.attribution.applications), 2)
        self.assertEqual(self.attribution.applications[0]['pending'], tutor_application_epc.UPDATE_OPERATION)
        self.assertEqual(self.attribution.applications[1]['pending'], tutor_application_epc.UPDATE_OPERATION)

        body = {
            'operation': tutor_application_epc.UPDATE_OPERATION,
            'global_id': person.global_id,
            'learning_container_year': {
                'acronym': 'LBIR1200',
                'year': 2017
            },
            tutor_application_epc.ERROR_EPC_FIELD: 'An error occured in EPC'
        }
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_epc.process_message(body_encoded)
        # Check if the application is removed
        self.attribution.refresh_from_db()
        applications_not_pending = [application for application in self.attribution.applications if
                                    not "pending" in application]
        self.assertEqual(len(applications_not_pending), 0) # No changed
class TestTutorApplicationEpc(TestCase):
    def setUp(self):
        self.academic_year = AcademicYearFactory(year=2017)
        external_id = tutor_application_osis.LEARNING_CONTAINER_YEAR_PREFIX_EXTERNAL_ID + '35654987_2017'
        self.lbir1200 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LBIR1200",
                                                     external_id=external_id)
        self.lagro2630 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LAGRO2630")
        self.bio = LearningContainerYearFactory(academic_year=self.academic_year, acronym="BIO5213")

        # Creation Person/Tutor
        Group.objects.create(name="tutors")
        person = PersonFactory(global_id="98363454")
        external_id = tutor_application_osis.TUTOR_PREFIX_EXTERNAL_ID + '2089590559'
        self.tutor = TutorFactory(external_id=external_id, person=person)

        # Create two tutor applications
        applications = [_get_application_example(self.lbir1200, '30.5', '40.5'),
                        _get_application_example(self.lagro2630, '12.5', '10.5'),
                        _get_application_example(self.bio, '10.5', '9.5')]
        self.attribution_new = AttributionNewFactory(
            global_id=person.global_id,
            applications=applications
        )

    def test_process_message_update_operation(self):
        person = self.tutor.person
        _set_all_application_in_pending_state(self.attribution_new.applications)
        self.attribution_new.save()
        self.assertEqual(len(self.attribution_new.applications), 3)
        self.assertEqual(self.attribution_new.applications[0]['pending'], tutor_application_osis.UPDATE_OPERATION)
        self.assertEqual(self.attribution_new.applications[1]['pending'], tutor_application_osis.UPDATE_OPERATION)

        body = [{
            'global_id': person.global_id,
            'tutor_applications': [{
                'acronym': 'LBIR1200',
                'year': self.academic_year.year,
                'charge_lecturing_asked': '0',
                'charge_practical_asked': '0',
                'last_changed': "2020-12-08 09:58:57+00:00"
            }]
        }]
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_osis.process_message(body_encoded)
        self.attribution_new.refresh_from_db()
        self.assertEqual(self.attribution_new.applications[0]['charge_lecturing_asked'], '0')

    def test_process_message_no_update_operation(self):
        person = self.tutor.person
        _set_all_application_in_pending_state(self.attribution_new.applications)
        self.attribution_new.save()
        self.assertEqual(len(self.attribution_new.applications), 3)
        self.assertEqual(self.attribution_new.applications[0]['pending'], tutor_application_osis.UPDATE_OPERATION)
        self.assertEqual(self.attribution_new.applications[1]['pending'], tutor_application_osis.UPDATE_OPERATION)

        body = [{
            'global_id': person.global_id,
            'tutor_applications': [{
                'acronym': 'LAGRO2630',
                'year': self.academic_year.year,
                'charge_lecturing_asked': '0',
                'charge_practical_asked': '0',
                'last_changed': "2013-12-08 09:58:57+00:00"
            }]
        }]
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_osis.process_message(body_encoded)
        self.attribution_new.refresh_from_db()
        self.assertEqual(self.attribution_new.applications[0]['charge_lecturing_asked'], '12.5')

    def test_process_message_with_delete_pending(self):
        person = self.tutor.person
        _set_all_application_in_pending_delete(self.attribution_new.applications)
        self.attribution_new.save()
        self.assertEqual(len(self.attribution_new.applications), 3)

        body = [{
            'global_id': person.global_id,
            'tutor_applications': [{
                'acronym': 'LAGRO2630',
                'year': self.academic_year.year,
                'charge_lecturing_asked': '0',
                'charge_practical_asked': '0',
                'last_changed': "2016-12-08 09:58:57+00:00"
            }]
        }]
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_osis.process_message(body_encoded)
        self.attribution_new.refresh_from_db()
        self.assertEqual(len(self.attribution_new.applications), 1)
        self.assertEqual(self.attribution_new.applications[0]['charge_lecturing_asked'], '12.5')

    def test_process_when_attribution_new_not_exist(self):
        body = [{
            'global_id': "321654",
            'tutor_applications': [{
                'acronym': 'LAGRO2630',
                'year': self.academic_year.year,
                'charge_lecturing_asked': '0',
                'charge_practical_asked': '0',
                'last_changed': "2016-12-08 09:58:57+00:00"
            }]
        }]
        self.assertEqual(find_by_global_id("321654"), None)
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_osis.process_message(body_encoded)
        self.assertEqual(find_by_global_id("321654").global_id, "321654")

    def test_process_with_many_change(self):
        person = self.tutor.person
        self.assertEqual(len(self.attribution_new.applications), 3)
        self.attribution_new.applications[0]['pending'] = tutor_application_osis.UPDATE_OPERATION
        self.attribution_new.applications[1]['pending'] = tutor_application_osis.DELETE_OPERATION
        self.attribution_new.save()
        self.attribution_new.refresh_from_db()
        body = [{
            'global_id': person.global_id,
            'tutor_applications': [
                {'acronym': 'LAGRO2630',
                 'year': self.academic_year.year,
                 'charge_lecturing_asked': '0',
                 'charge_practical_asked': '0',
                 'last_changed': "2021-12-08 09:58:57+00:00"
                 },
                {'acronym': 'LBIR1200',
                 'year': self.academic_year.year,
                 'charge_lecturing_asked': '0',
                 'charge_practical_asked': '0',
                 'last_changed': "2013-12-08 09:58:57+00:00"
                 },
                {'acronym': 'BIO5213',
                 'year': self.academic_year.year,
                 'charge_lecturing_asked': '0',
                 'charge_practical_asked': '0',
                 'last_changed': "2013-12-08 09:58:57+00:00"}
            ]
        }]
        body_encoded = bytearray(json.dumps(body), "utf-8")
        tutor_application_osis.process_message(body_encoded)
        self.attribution_new.refresh_from_db()
        self.assertEqual(len(self.attribution_new.applications), 3)
        self.assertEqual(self.attribution_new.applications[0]['charge_lecturing_asked'], '12.5')
        self.assertEqual(self.attribution_new.applications[1]['charge_lecturing_asked'], '30.5')
        self.assertEqual(self.attribution_new.applications[2]['charge_lecturing_asked'], '0')
Exemplo n.º 4
0
class TutorApplicationTest(TestCase):
    def setUp(self):
        # Create academic year
        self.academic_year = AcademicYearFactory(year=2017)
        # Create several learning container year - 2017
        self.lbir1200_2017 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LBIR1200")
        LearningUnitYearFactory(academic_year=self.academic_year, learning_container_year=self.lbir1200_2017)
        self.lbir1250_2017 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LBIR1250")
        LearningUnitYearFactory(academic_year=self.academic_year, learning_container_year=self.lbir1250_2017)
        self.lbir1300_2017 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LBIR1300")
        LearningUnitYearFactory(academic_year=self.academic_year, learning_container_year=self.lbir1300_2017)
        self.lagro1200_2017 = LearningContainerYearFactory(academic_year=self.academic_year, acronym="LAGRO1200")
        LearningUnitYearFactory(academic_year=self.academic_year, learning_container_year=self.lagro1200_2017)

        # Create several learning container year - 2016
        self.academic_year_2016 = AcademicYearFactory(year=2016)
        self.lbir1200_2016 = LearningContainerYearFactory(academic_year=self.academic_year_2016, acronym="LBIR1200")
        LearningUnitYearFactory(academic_year=self.academic_year_2016, learning_container_year=self.lbir1200_2016)
        self.lbir1250_2016 = LearningContainerYearFactory(academic_year=self.academic_year_2016, acronym="LBIR1250")
        LearningUnitYearFactory(academic_year=self.academic_year_2016, learning_container_year=self.lbir1250_2016)

        # Creation Person/Tutor
        Group.objects.create(name="tutors")
        person = PersonFactory(global_id="98363454")
        self.tutor = TutorFactory(person=person)

        applications = [
            _get_application_example(self.lbir1200_2017, '3.5', '35.6'),  # Application 2017
            _get_application_example(self.lbir1300_2017, '7.5', '25'),   # Application 2017
            _get_application_example(self.lbir1200_2016, '2', '30'),  # Application 2016
        ]
        self.attribution = AttributionNewFactory(global_id=person.global_id,
                                                 attributions=_get_attributions_default(),
                                                 applications=applications)

    def test_get_application_list(self):
        global_id = self.tutor.person.global_id
        application_list = tutor_application.get_application_list(global_id, self.academic_year)
        self.assertIsInstance(application_list, list)
        self.assertEqual(len(application_list), 2)

        application_list_in_2016 = tutor_application.get_application_list(global_id, self.academic_year_2016)
        self.assertIsInstance(application_list_in_2016, list)
        self.assertEqual(len(application_list_in_2016), 1)

    def test_get_application_list_order_by(self):
        self.attribution.applications = [
            _get_application_example(self.lbir1200_2017, '3.5', '35.6'),  # Application 2017
            _get_application_example(self.lbir1250_2017, '5', '7', tutor_application_epc.UPDATE_OPERATION),
            _get_application_example(self.lbir1300_2017, '7.5', '25')  # Application 2017
        ]
        self.attribution.save()
        global_id = self.tutor.person.global_id
        application_list = tutor_application.get_application_list(global_id, self.academic_year)
        self.assertIsInstance(application_list, list)
        self.assertEqual(len(application_list), 3)
        # Check order
        self.assertEqual(application_list[0]['acronym'], self.lbir1200_2017.acronym)
        self.assertEqual(application_list[1]['acronym'], self.lbir1300_2017.acronym)
        self.assertEqual(application_list[2]['acronym'], self.lbir1250_2017.acronym)

    def test_get_application(self):
        global_id = self.tutor.person.global_id
        application_searched = tutor_application.get_application(global_id, self.lbir1200_2017)
        self.assertTrue(application_searched)
        self.assertIsInstance(application_searched, dict)
        self.assertEqual(application_searched['acronym'], self.lbir1200_2017.acronym)

    def test_get_application_not_found(self):
        global_id = self.tutor.person.global_id
        application_searched = tutor_application.get_application(global_id, self.lagro1200_2017)
        self.assertFalse(application_searched)

    def test_create_or_update_application_creation_on_existing_user(self):
        application_to_create = _get_application_example(self.lbir1250_2017, '30', '20')
        global_id = self.tutor.person.global_id
        tutor_application.create_or_update_application(global_id, application_to_create)
        application_list = tutor_application.get_application_list(global_id, self.academic_year)
        self.assertEqual(len(application_list), 3) # We should have 3 applications now
        # We should found the newest created
        self.assertTrue(next(app for app in application_list if
                             app.get('acronym') == self.lbir1250_2017.acronym))

    def test_create_or_update_application_creation_on_new_user(self):
        # Create new tutor
        new_tutor = TutorFactory(person=PersonFactory(global_id="59898988"))
        # Submit tutor application
        application_to_create = _get_application_example(self.lbir1250_2017, '30', '20')
        global_id = new_tutor.person.global_id
        tutor_application.create_or_update_application(global_id, application_to_create)
        application_list = tutor_application.get_application_list(global_id, self.academic_year)
        self.assertEqual(len(application_list), 1)  # We should have 1 application

    def test_create_or_update_application_update(self):
        application_to_update = _get_application_example(self.lbir1200_2017, '0', '10')
        global_id = self.tutor.person.global_id
        tutor_application.create_or_update_application(global_id, application_to_update)
        application_list = tutor_application.get_application_list(global_id, self.academic_year)
        self.assertEqual(len(application_list), 2)  # We should have 2 applications
        application_updated = next(app for app in application_list if
                             app.get('acronym') == self.lbir1200_2017.acronym)
        self.assertTrue(application_updated)
        self.assertEqual(application_updated.get('acronym'), self.lbir1200_2017.acronym)
        self.assertEqual(application_updated.get('charge_lecturing_asked'), Decimal(0))
        self.assertEqual(application_updated.get('charge_practical_asked'), Decimal(10))

    def test_get_application_list_with_none_lecturing_charge(self):
        new_tutor = TutorFactory(person=PersonFactory(global_id="59898988"))
        application_to_create = _get_application_example(self.lbir1200_2017, None, "15")
        global_id = new_tutor.person.global_id
        tutor_application.create_or_update_application(global_id, application_to_create)
        application_list = tutor_application.get_application_list(global_id, self.academic_year)

        application_created = application_list[0]
        self.assertTrue(application_created)
        self.assertEqual(application_created.get('acronym'), self.lbir1200_2017.acronym)
        self.assertEqual(application_created.get('charge_lecturing_asked'), Decimal(0))
        self.assertEqual(application_created.get('charge_practical_asked'), Decimal(15))

    def test_get_application_list_with_none_practical_charge(self):
        new_tutor = TutorFactory(person=PersonFactory(global_id="59898988"))
        application_to_create = _get_application_example(self.lbir1200_2017, "15", None)
        global_id = new_tutor.person.global_id
        tutor_application.create_or_update_application(global_id, application_to_create)
        application_list = tutor_application.get_application_list(global_id, self.academic_year)

        application_created = application_list[0]
        self.assertTrue(application_created)
        self.assertEqual(application_created.get('acronym'), self.lbir1200_2017.acronym)
        self.assertEqual(application_created.get('charge_lecturing_asked'), Decimal(15))
        self.assertEqual(application_created.get('charge_practical_asked'), Decimal(0))

    def test_pending_flag(self):
        global_id = self.tutor.person.global_id
        application_searched = tutor_application.get_application(global_id, self.lbir1200_2017)
        self.assertTrue(application_searched)
        tutor_application.set_pending_flag(global_id, application_searched, tutor_application_epc.UPDATE_OPERATION)
        # Research in order to verify that have pending state
        application_searched = tutor_application.get_application(global_id, self.lbir1200_2017)
        self.assertEqual(application_searched['pending'], tutor_application_epc.UPDATE_OPERATION)
        self.assertTrue(application_searched['updated_at'])

    def test_validate_application(self):
        global_id = self.tutor.person.global_id
        application_to_create = _get_application_example(self.lbir1250_2017, '30', '20')
        application_to_create['pending'] = tutor_application_epc.UPDATE_OPERATION
        tutor_application.create_or_update_application(global_id, application_to_create)
        # Check pending state
        application_searched_not_validated = tutor_application.get_application(global_id, self.lbir1250_2017)
        self.assertEqual(application_searched_not_validated['pending'], tutor_application_epc.UPDATE_OPERATION)
        self.assertTrue(application_searched_not_validated['updated_at'])
        sleep(1) # Wait 1 sec for testing updated_at field
        # Validate
        tutor_application.validate_application(global_id, self.lbir1250_2017.acronym,
                                               self.lbir1250_2017.academic_year.year)
        application_searched_validated = tutor_application.get_application(global_id, self.lbir1250_2017)
        self.assertRaises(KeyError, lambda: application_searched_validated['pending'])
        self.assertTrue(application_searched_validated['updated_at'])
        self.assertTrue(application_searched_validated['updated_at'] >
                        application_searched_not_validated['updated_at'])

    def test_delete_application(self):
        global_id = self.tutor.person.global_id
        # Before delete
        application_searched = tutor_application.get_application(global_id, self.lbir1200_2017)
        self.assertTrue(application_searched)
        # Delete process
        acronym_to_delete = self.lbir1200_2017.acronym
        year_to_delete = self.lbir1200_2017.academic_year.year
        tutor_application.delete_application(global_id, acronym_to_delete, year_to_delete)
        # After delete
        application_searched = tutor_application.get_application(global_id, self.lbir1200_2017)
        self.assertFalse(application_searched)