def test_edit_multiple_modules_mountings(self):
        '''
            Test that Edit All function can edit multiple modules' mounting(s)
        '''
        test_data = {}

        # Dummy Module 1: Mount in Sem 2
        test_mod_1_code = self.DUMMY_MODULE_CODE_1
        test_mod_1_mounting = True   #mounted
        test_mod_1_quota = 200   #quota added

        test_data[test_mod_1_code+'_isEdited'] = "True"
        test_data[test_mod_1_code+'_Sem2Mounting'] = test_mod_1_mounting   #modified to true
        test_data[test_mod_1_code+'_Sem2Quota'] = test_mod_1_quota

        # Dummy Module 3: Unmount from both sems
        test_mod_2_code = self.DUMMY_MODULE_CODE_3
        test_mod_2_mounting_1 = False   #unmounted
        test_mod_2_quota_1 = False   #quota will be false because unmounted
        test_mod_2_mounting_2 = False   #unmounted
        test_mod_2_quota_2 = False   #quota will be false because unmounted

        test_data[test_mod_2_code+'_isEdited'] = "True"
        # Because mounting is false (unchecked), no mounting value will be returned by the UI
        test_data[test_mod_2_code+'_Sem1Quota'] = test_mod_2_quota_1
        # Because mounting is false (unchecked), no mounting value will be returned by the UI
        test_data[test_mod_2_code+'_Sem2Quota'] = test_mod_2_quota_2

        self.edit_all_handler.POST(test_data)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_mod_1_code,
                                                             self.next_ay+" Sem 2")
        quota = model.get_quota_of_target_tenta_ay_sem(test_mod_1_code,
                                                       self.next_ay+" Sem 2")
        assert_equal(mounting, test_mod_1_mounting)
        assert_equal(quota, test_mod_1_quota)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_mod_2_code,
                                                             self.next_ay+" Sem 1")
        quota = model.get_quota_of_target_tenta_ay_sem(test_mod_2_code,
                                                       self.next_ay+" Sem 1")
        assert_equal(mounting, test_mod_2_mounting_1)
        assert_equal(quota, test_mod_2_quota_1)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_mod_2_code,
                                                             self.next_ay+" Sem 2")
        quota = model.get_quota_of_target_tenta_ay_sem(test_mod_2_code,
                                                       self.next_ay+" Sem 2")
        assert_equal(mounting, test_mod_2_mounting_2)
        assert_equal(quota, test_mod_2_quota_2)

        model.delete_tenta_mounting(test_mod_1_code, self.next_ay+" Sem 2")
        model.add_tenta_mounting(test_mod_2_code, self.next_ay+" Sem 1", self.DUMMY_QUOTA_3)
        model.add_tenta_mounting(test_mod_2_code, self.next_ay+" Sem 2", self.DUMMY_QUOTA_0)
    def test_edit_single_module_quota(self):
        '''
            Test that Edit All function can edit a single module's quota
        '''
        test_module_code = self.DUMMY_MODULE_CODE_1
        test_mounting = True
        test_quota = 100

        # Modifiy Sem 1 quota
        test_data = {}
        test_data[test_module_code+'_isEdited'] = "True"
        test_data[test_module_code+'_Sem1Mounting'] = test_mounting
        test_data[test_module_code+'_Sem1Quota'] = test_quota  #modified

        self.edit_all_handler.POST(test_data)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_module_code,
                                                             self.next_ay+" Sem 1")
        quota = model.get_quota_of_target_tenta_ay_sem(test_module_code,
                                                       self.next_ay+" Sem 1")
        assert_equal(mounting, test_mounting)
        assert_equal(quota, test_quota)

        model.update_quota(test_module_code, self.next_ay+" Sem 1", self.DUMMY_QUOTA_1)

        # Modify Sem 2 quota
        test_module_code = self.DUMMY_MODULE_CODE_2
        test_mounting = True
        test_quota = 200

        test_data = {}
        test_data[test_module_code+'_isEdited'] = "True"
        test_data[test_module_code+'_Sem2Mounting'] = test_mounting
        test_data[test_module_code+'_Sem2Quota'] = test_quota  #modified

        self.edit_all_handler.POST(test_data)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_module_code,
                                                             self.next_ay+" Sem 2")
        quota = model.get_quota_of_target_tenta_ay_sem(test_module_code,
                                                       self.next_ay+" Sem 2")
        assert_equal(mounting, test_mounting)
        assert_equal(quota, test_quota)

        model.update_quota(test_module_code, self.next_ay+" Sem 2", self.DUMMY_QUOTA_2)
    def test_edit_single_module_mounting(self):
        '''
            Test that Edit All function can edit a single module's mounting
        '''
        test_module_code = self.DUMMY_MODULE_CODE_1
        test_mounting = False   #unmounted
        test_quota = False   #quota will be false because unmounted

        # Unmount from Sem 1
        test_data = {}
        test_data[test_module_code+'_isEdited'] = "True"
        # Because mounting is false (unchecked), no mounting value will be returned by the UI
        test_data[test_module_code+'_Sem1Quota'] = test_quota

        self.edit_all_handler.POST(test_data)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_module_code,
                                                             self.next_ay+" Sem 1")
        quota = model.get_quota_of_target_tenta_ay_sem(test_module_code,
                                                       self.next_ay+" Sem 1")
        assert_equal(mounting, test_mounting)
        assert_equal(quota, test_quota)

        model.add_tenta_mounting(test_module_code, self.next_ay+" Sem 1", self.DUMMY_QUOTA_1)

        # Mount in Sem 2
        test_module_code = self.DUMMY_MODULE_CODE_2
        test_mounting = True
        test_quota = 200

        test_data = {}
        test_data[test_module_code+'_isEdited'] = "True"
        test_data[test_module_code+'_Sem2Mounting'] = True   #modified to true
        test_data[test_module_code+'_Sem2Quota'] = test_quota  #modified

        self.edit_all_handler.POST(test_data)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_module_code,
                                                             self.next_ay+" Sem 2")
        quota = model.get_quota_of_target_tenta_ay_sem(test_module_code,
                                                       self.next_ay+" Sem 2")
        assert_equal(mounting, test_mounting)
        assert_equal(quota, test_quota)

        model.delete_tenta_mounting(test_module_code, self.next_ay+" Sem 2")
    def load_mounting_info(self, module_code, ay_sem):
        '''
            Load the mounting status and quota of the target module and AY/Sem
        '''
        fixed_mounting_status = -1
        fixed_quota = None
        is_current_ay = False

        # Get mounting status and quota in current AY
        target_ay = ay_sem[0:8]
        current_ay = model.get_current_ay()
        if target_ay == current_ay:
            is_current_ay = True
            fixed_mounting_status = model.get_mounting_of_target_fixed_ay_sem(
                module_code, ay_sem)
            fixed_quota = model.get_quota_of_target_fixed_ay_sem(
                module_code, ay_sem)
        else:
            target_sem = ay_sem[9:14]
            fixed_mounting_status = model.get_mounting_of_target_fixed_ay_sem(
                module_code, current_ay + " " + target_sem)
            fixed_quota = model.get_quota_of_target_fixed_ay_sem(
                module_code, current_ay + " " + target_sem)

        if fixed_quota is False:
            fixed_quota = '-'

        if is_current_ay:
            if fixed_mounting_status is True:
                self.mounting_status = 1
            else:
                self.mounting_status = -1
            self.quota = fixed_quota

        else:
            # Get mounting status and quota in target (future) AY
            tenta_mounting_status = model.get_mounting_of_target_tenta_ay_sem(
                module_code, ay_sem)
            tenta_quota = model.get_quota_of_target_tenta_ay_sem(
                module_code, ay_sem)

            if tenta_quota is False:
                tenta_quota = '-'
            self.quota = tenta_quota

            if tenta_mounting_status is True:
                self.mounting_status = 1
            else:
                if fixed_mounting_status is True:
                    self.mounting_status = 0
                else:
                    self.mounting_status = -1

        self.is_current_ay = is_current_ay
    def test_edit_multiple_modules_quotas(self):
        '''
            Test that Edit All function can edit multiple modules' quota(s)
        '''
        test_data = {}

        # Modify Dummy Module 1's quota
        test_mod_1_code = self.DUMMY_MODULE_CODE_1
        test_mod_1_mounting = True
        test_mod_1_quota = 100
        test_data[test_mod_1_code+'_isEdited'] = "True"
        test_data[test_mod_1_code+'_Sem1Mounting'] = test_mod_1_mounting
        test_data[test_mod_1_code+'_Sem1Quota'] = test_mod_1_quota

        # Modify Dummy Module 2's quota
        test_mod_2_code = self.DUMMY_MODULE_CODE_2
        test_mod_2_mounting = True
        test_mod_2_quota = 200
        test_data[test_mod_2_code+'_isEdited'] = "True"
        test_data[test_mod_2_code+'_Sem2Mounting'] = test_mod_2_mounting
        test_data[test_mod_2_code+'_Sem2Quota'] = test_mod_2_quota

        self.edit_all_handler.POST(test_data)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_mod_1_code,
                                                             self.next_ay+" Sem 1")
        quota = model.get_quota_of_target_tenta_ay_sem(test_mod_1_code,
                                                       self.next_ay+" Sem 1")
        assert_equal(mounting, test_mod_1_mounting)
        assert_equal(quota, test_mod_1_quota)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_mod_2_code,
                                                             self.next_ay+" Sem 2")
        quota = model.get_quota_of_target_tenta_ay_sem(test_mod_2_code,
                                                       self.next_ay+" Sem 2")
        assert_equal(mounting, test_mod_2_mounting)
        assert_equal(quota, test_mod_2_quota)

        model.update_quota(test_mod_1_code, self.next_ay+" Sem 1", self.DUMMY_QUOTA_1)
        model.update_quota(test_mod_2_code, self.next_ay+" Sem 2", self.DUMMY_QUOTA_2)
    def get_modules_with_modified_quota(self):
        '''
            Get all modules whose quota has been modified in a future AY.
            Return the module code, module name, current AY-Sem, target AY-Sem,
            current AY-Sem's quota, target AY-Sem's quota, and quota change
        '''
        modified_modules = model.get_modules_with_modified_quota()
        modified_modules = [list(module) for module in modified_modules]

        for module in modified_modules:
            current_quota = module[4]
            modified_quota = module[5]
            if current_quota is None:
                quota_change = '+' + str(modified_quota)
            elif modified_quota is None:
                quota_change = str(-current_quota)
            else:
                quota_change = modified_quota - current_quota
                if quota_change > 0:
                    quota_change = '+' + str(quota_change)
                else:
                    quota_change = str(quota_change)
            module.append(quota_change)

        # Include modules with specified quota that have mounting changes
        modules_wth_modified_mountings = self.get_modules_with_modified_mounting(
        )
        for module in modules_wth_modified_mountings:
            mounting_change = module[4]
            if mounting_change == 1:  # Unmounted --> Mounted
                code = module[0]
                name = module[1]
                current_ay = module[2]
                target_ay = module[3]
                quota = model.get_quota_of_target_tenta_ay_sem(code, target_ay)
                if quota is not None and quota > 0:
                    modified_modules.append(
                        (code, name, current_ay, target_ay, "Unmounted", quota,
                         '+' + str(quota)))
            elif mounting_change == 0:  # Mounted --> Unmounted
                code = module[0]
                name = module[1]
                current_ay = module[2]
                target_ay = module[3]
                quota = model.get_quota_of_target_fixed_ay_sem(
                    code, current_ay)
                if quota is not None and quota > 0:
                    modified_modules.append(
                        (code, name, current_ay, target_ay, quota, "Unmounted",
                         '-' + str(quota)))

        return modified_modules
    def test_edit_single_module_multiple_quotas(self):
        '''
            Test that Edit All function can edit a single module's multiple quotas
        '''
        test_module_code = self.DUMMY_MODULE_CODE_3
        test_mounting_1 = True
        test_mounting_2 = True
        test_quota_1 = None
        test_quota_2 = 300

        # Modify Sem 1 and 2 quotas
        test_data = {}
        test_data[test_module_code+'_isEdited'] = "True"
        test_data[test_module_code+'_Sem1Mounting'] = test_mounting_1
        # Because quota is empty, no quota value will be returned by the UI
        test_data[test_module_code+'_Sem2Mounting'] = test_mounting_2
        test_data[test_module_code+'_Sem2Quota'] = test_quota_2    #modified

        self.edit_all_handler.POST(test_data)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_module_code,
                                                             self.next_ay+" Sem 1")
        quota = model.get_quota_of_target_tenta_ay_sem(test_module_code,
                                                       self.next_ay+" Sem 1")
        assert_equal(mounting, test_mounting_1)
        assert_equal(quota, test_quota_1)

        mounting = model.get_mounting_of_target_tenta_ay_sem(test_module_code,
                                                             self.next_ay+" Sem 2")
        quota = model.get_quota_of_target_tenta_ay_sem(test_module_code,
                                                       self.next_ay+" Sem 2")
        assert_equal(mounting, test_mounting_2)
        assert_equal(quota, test_quota_2)

        model.update_quota(test_module_code, self.next_ay+" Sem 1", self.DUMMY_QUOTA_3)
        model.update_quota(test_module_code, self.next_ay+" Sem 2", self.DUMMY_QUOTA_0)
    def test_quota_restore(self):
        '''
            Test if a module's whose quota is modified can be restored
            and if the module will disappear from the table of modules with modified quota
        '''
        # Restore quota to original number
        test_module_code = 'BB3001'
        test_current_quota = 10
        test_target_aysem = self.next_ay+" Sem 1"
        test_modified_quota = 999

        modified_modules = self.modified_modules_handler.get_modules_with_modified_quota()
        is_in_modified_modules = False
        current_quota = -1
        modified_quota = -1

        for module in modified_modules:
            code = module[0]
            if code == test_module_code:
                is_in_modified_modules = True
                current_quota = module[4]
                modified_quota = module[5]
                break

        assert_true(is_in_modified_modules)
        assert_equal(test_current_quota, current_quota)
        assert_equal(test_modified_quota, modified_quota)

        test_post_data = self.TestRestoreModuleData("quota", test_module_code, None,
                                                    test_target_aysem, test_current_quota, None)
        self.module_restore_handler.POST(test_post_data)

        modified_modules = self.modified_modules_handler.get_modules_with_modified_quota()
        is_in_modified_modules = False
        for module in modified_modules:
            code = module[0]
            if code == test_module_code:
                is_in_modified_modules = True
                current_quota = module[3]
                modified_quota = module[4]
                break
        assert_false(is_in_modified_modules)

        restored_quota = model.get_quota_of_target_tenta_ay_sem(test_module_code, test_target_aysem)
        assert_equal(restored_quota, test_current_quota)

        # Restore quota to unspecified
        test_module_code = 'BB3002'
        test_current_quota = None
        test_target_aysem = self.next_ay+" Sem 1"
        test_modified_quota = 999

        modified_modules = self.modified_modules_handler.get_modules_with_modified_quota()
        is_in_modified_modules = False
        current_quota = -1
        modified_quota = -1

        for module in modified_modules:
            code = module[0]
            if code == test_module_code:
                is_in_modified_modules = True
                current_quota = module[4]
                modified_quota = module[5]
                break

        assert_true(is_in_modified_modules)
        assert_equal(test_current_quota, current_quota)
        assert_equal(test_modified_quota, modified_quota)

        test_post_data = self.TestRestoreModuleData("quota", test_module_code, None,
                                                    test_target_aysem, test_current_quota, None)
        self.module_restore_handler.POST(test_post_data)

        modified_modules = self.modified_modules_handler.get_modules_with_modified_quota()
        is_in_modified_modules = False
        for module in modified_modules:
            code = module[0]
            if code == test_module_code:
                is_in_modified_modules = True
                current_quota = module[4]
                modified_quota = module[5]
                break
        assert_false(is_in_modified_modules)

        restored_quota = model.get_quota_of_target_tenta_ay_sem(test_module_code, test_target_aysem)
        assert_equal(restored_quota, test_current_quota)