def test_validate_grade(self): ok_grade = Grade(1, 2, 9) not_ok_grade = Grade(-1, 2, 9) self.elems() self._valid_grade.validate_grade(ok_grade) try: self._valid_grade.validate_grade(not_ok_grade) self.assertTrue(False) except ValidationError as error: self.assertEqual(str(error), "invalid student id!\n") except Exception: self.assertTrue(False) not_ok_grade = Grade(-1, -2, 9) try: self._valid_grade.validate_grade(not_ok_grade) self.assertTrue(False) except ValidationError as error: self.assertEqual(str(error), "invalid student id!\ninvalid assignment id!\n") except Exception: self.assertTrue(False)
def _test_search_grade(self): self._repo = Repository([]) assert self._repo.size() == 0 self._repo.add(Grade(1, 3, 5)) assert self._repo.size() == 1 key_grade = Grade(1, 3, None) found_grade = self._repo.search(key_grade) assert found_grade.get_grade() == 5
def _test_validate_grade(self): ok_grade = Grade(12, 45, 8) vld_grade = ValidationGrade() try: vld_grade.validate_grade(ok_grade) except ValidationError: assert False not_ok_grade = Grade(-1, -1, -1) try: vld_grade.validate_grade(not_ok_grade) assert False except ValidationError as error: assert str( error ) == "invalid student id!\ninvalid assignment id!\ninvalid grade!\n"
def _load(self): """ we will load into the memory repos the data from the files """ try: file = open(self._filename, "r") self._data = json.load(file) # we check which file was opened to enter data correctly // we could have done 3 repo's instead of this one for item in self._data[self._entity]: if self._entity == 'Student': super().add_object( Student(item[self._atr1], item[self._atr2], item[self._atr3])) if self._entity == 'Assignment': super().add_object( Assignment(item[self._atr1], item[self._atr2], item[self._atr3])) if self._entity == 'Grade': super().add_object( Grade(item[self._atr1], item[self._atr2], item[self._atr3])) # file.close() except JSONDecodeError: self._data[self._entity] = [] with open(self._filename, 'w') as outfile: json.dump(self._data, outfile) outfile.close()
def give_assignment(self, student_id, assignment_id, student_grade=None): """ We give an assingment to a student and we set the grade to None :param student_id: the student id :param assignment_id: the assignment id """ self.validators.find_student_by_id(self.student_list, student_id) self.validatora.find_assignment(self.assignment_list, assignment_id) for grade in self.grade_list: if grade.student_id == student_id and grade.assignment_ID == assignment_id: raise gradeServiceException( "Invalid student and assignment id. The student already has this assignment!...\n" ) # the grade when we assign an assignment is None new_grade = Grade(student_id, assignment_id, None) # we can now make the undo/redo for giving an assignment to a student #-------------------------- undo_function = FunctionCall(self.grade_list.remove_object, new_grade) redo_function = FunctionCall(self.grade_list.add_object, new_grade) operation = OperationCall(undo_function, redo_function) self._undo_service.record(operation) #---------------------------------------------------------- self.grade_list.add_object(new_grade)
def _load(self): """ We load the data from Data base """ try: # we try here to make the connection to the data base connection_string = psycopg2.connect( "host=localhost dbname=postgres user=postgres password=polopolo" ) current_connector = connection_string.cursor() # if we get to this line it means that the connection was succesfully done! # now according to the value stored in self._entity we will select all data from the corresponding table current_connector.execute(self.get_selectquery) # after that we will store it in the list for row in current_connector: if self._entity == 'Student': super().add_object( Student(str(row[0]), str(row[1]), str(row[2]))) if self._entity == 'Assignment': super().add_object( Assignment(str(row[0]), str(row[1]), str(row[2]))) if self._entity == 'Grade': super().add_object( Grade(str(row[0]), str(row[1]), str(row[2]))) current_connector.close() connection_string.close() except Exception as ve: raise RepositoryException(ve)
def _load(self): """ We load the data from file """ try: file = open(self._filename, "rt") lines = file.readlines() file.close() for line in lines: line = line.split(";") if line[2] == "None\n": super().add_object(Grade(line[0], line[1], None)) else : super().add_object(Grade(line[0], line[1], str(int(line[2])))) except FileNotFoundError: raise RepositoryException(f"File {self._filename} does not exist!...\n") except EOFError: pass except IOError as ve: raise RepositoryException("An error occured - " + str(ve))
def give_assignment_to_group(self, group, assignment_id, student_grade=None): """ We give an assingment to a student and we set the grade to None :param group: the group of students where we will assign that assignment :param assignment_id: the assignment id """ # in order to do the undo we need to use a cascade operation because we need to delete all the grades # given to a group when we undo/ add when we do redo self.validatora.find_assignment(self.assignment_list, assignment_id) Cascaded_Operation = [] for index in range(len(self.student_list)): if self.student_list[index].student_group == group: done = False for grade in self.grade_list: if grade.student_id == self.student_list[ index].student_id and grade.assignment_ID == assignment_id: done = True if done == False: new_grade = Grade(self.student_list[index].student_id, assignment_id, None) # here the undo of the assignments began----------------- undo_function = FunctionCall(self.grade_list.remove_object, new_grade) redo_function = FunctionCall(self.grade_list.add_object, new_grade) operation = OperationCall(undo_function, redo_function) Cascaded_Operation.append(operation) # undo/redo ends here ------------------------------------ self.grade_list.add_object(new_grade) # If the length of the cascaded operation is not 0 we can append the undo/redo # OTHER wise we can print an error message that all the students in that group already have that assignment if len(Cascaded_Operation) != 0: Cascaded_Operation = tuple(Cascaded_Operation) Cascaded_Operation = CascadedOperation(*Cascaded_Operation) self._undo_service.record(Cascaded_Operation) else: raise gradeServiceException( f"All the students in group {group} have the assignment {assignment_id} " )
def generate_random_grades(self): grades = [None,'1','2','3','4','5','6','7','8','9','10'] i = 0 while i < 100: done = False random_stud_id = random.randint(0, len(self.llist_s) - 1) random_assign_id = random.randint(0, len(self.llist_a) - 1) if len(self._grades_list) > 0: for grade in self._grades_list: if grade.student_id == self.llist_s.student_list_service[random_stud_id].student_id and grade.assignment_ID == self.llist_a.list_of_assignments[random_assign_id].assignment_ID: done = True break if done == False: rand_grade_index = random.randint(0,10) grade = Grade(self.llist_s.student_list_service[random_stud_id].student_id, self.llist_a.list_of_assignments[random_assign_id].assignment_ID, grades[rand_grade_index]) i += 1 self._grades_list.grade_list.add_object(grade)
def test_grade(self): grade_val = Grade_Validator() grade_val.validate(Grade('124', '124', '10')) grade = Grade('124', '124', '10') try: grade_val.check_if_grade_exist(self.grade_list,'124','124','10') except GradeExceptions: self.assertTrue('ok') st = str(grade) try: grade_val.validate(Grade('124', '124', '11')) self.assertFalse('not ok') except GradeExceptions: self.assertTrue('ok') try: grade_val.validate(Grade('124', '12a', '11')) self.assertFalse('not ok') except GradeExceptions: self.assertTrue('ok') try: grade_val.validate(Grade('12a', '124', '11')) self.assertFalse('not ok') except GradeExceptions: self.assertTrue('ok') try: grade_val.validate(Grade('124', '124', '1a')) self.assertFalse('not ok') except GradeExceptions: self.assertTrue('ok') try: val = self.grade_list.return_list_assignments('124') self.assertFalse('not ok') except gradeServiceException: self.assertTrue('ok')
def create_grade(student_id, assignment_id, grade): return Grade(int(student_id), int(assignment_id), int(grade))
def create_grade(student_id, assignment_id, grade): return Grade(int(student_id), int(assignment_id), int(grade)) parser = ConfigParser() parser.read("settings.properties") repository = parser.get('settings', 'repository') students = parser.get('settings', 'students') assignments = parser.get('settings', 'assignments') grades = parser.get('settings', 'grades') ui = parser.get('settings', 'ui') if repository == 'inmemory': students_repo = Repository(stud_list.get_list()) assignments_repo = Repository(assignments_list.get_list()) grade_repo = Repository([Grade(1, 1, 0), Grade(1, 2, 5), Grade(2, 1, 10)]) elif repository == 'textfile': students_repo = TextRepository(students, create_student, []) assignments_repo = TextRepository(assignments, create_assignment, []) grade_repo = TextRepository(grades, create_grade, []) elif repository == 'binaryfile': students_repo = BinaryRepository(students, []) assignments_repo = BinaryRepository(assignments, []) grade_repo = BinaryRepository(grades, []) elif repository == 'json': students_repo = JsonRepository(students, create_student, [], ["student_id", "name", "group"]) assignments_repo = JsonRepository(
def _test_create_grade(self): test_grade = Grade(12, 45, 8) assert test_grade.get_assignment_id() == 45 assert test_grade.get_student_id() == 12 assert test_grade.get_grade() == 8
def test_create_grade(self): test_grade = Grade(1, 2, 9) self.assertEqual(test_grade.get_student_id(), 1) self.assertEqual(test_grade.get_assignment_id(), 2) test_grade.set_grade(6) self.assertEqual(test_grade.get_grade(), 6)