def test_iterations(self): self._list = [1, 2, 3, 4, 5] self._struct = Structure(self._list) self.assertEqual(len(self._struct), len(self._list)) self.assertEqual(self._struct[0], self._list[0]) #self.assertEqual(self._struct, self._list) ???????????????????? self.assertEqual(next(self._struct), 2) self.assertEqual(next(self._struct), 3) self.assertEqual(next(self._struct), 4) self.assertEqual(next(self._struct), 5) self.assertRaises(StopIteration, next, self._struct) del self._struct[0] self.assertEqual(self._struct[0], 2) self.assertEqual(len(self._struct), len(self._list)) self.assertIn(5, self._struct) if 6 not in self._struct: assert True for i in self._struct: print(i) self._struct = Structure([Student(1, 'Test'), Student(2, 'Test2')]) if Student(1, 'T') not in self._struct: assert False
def test_update_student(self): self.repository.update_item(Student(1, "D", 2)) updated_student = self.repository.get_by_id(1) self.assertEqual(updated_student.get_name(), "D") # check exception case try: self.repository.update_item(Student(4, "D", 4)) except CustomException as e: self.assertEqual(e.get_msg(), "No item for update found")
def setUp(self): # setUp method will be executed before every test TestCase.setUp(self) self.repository = Repository() # add mock students to repo stud1 = Student(1, "A", 1) stud2 = Student(2, "B", 2) stud3 = Student(3, "C", 3) self.repository.add_item(stud1) self.repository.add_item(stud2) self.repository.add_item(stud3)
def testGr(self): stud = Student("42", "Mihai Adamescu", "913") stud2 = Student ("12", "Adfs Aop", "915") ass = Assignment("22", "imi plac caii", "20.4") ass2 = Assignment("29", "nu imi plac caii", "20.9") gr = Grade("23", ass, stud, "7") self.assertTrue(gr.getId() == "23") gr.setStud(stud2) gr.setAss(ass2) gr.setGrade("9") self.assertTrue(gr.getAss() == ass2) self.assertTrue(gr.getStud() == stud2) self.assertTrue(gr.getGrade() == "9")
def testName(self): "Student ID must be positive" discipline = Student(-1, "Ana") try: self.__validator.validate(discipline) except StudentValidatorException: pass "Student name cannot be empty" discipline = Student(1, "") try: self.__validator.validate(discipline) except StudentValidatorException: pass
def store(self, sid, name, group): ''' store Function to store a student Arguments: sid {string} -- id of student name {strinf} -- name of studebt group {string} -- group of student Raises: StudentException: ID must be a number StudentException: ID already exists StudentException: Group must be a number Returns: student -- stored student ''' if not sid.isdigit(): raise StudentException('ID must be a number') sid = int(sid) elem = self._repo.find(sid) if elem is not None: raise StudentException('The ID already exists') if not group.isdigit(): raise StudentException('Group must be a number') group = int(group) stud = Student(sid, name, group) self._repo.store(stud) redo = FunctionCall(self._repo.store, stud) undo = FunctionCall(self._repo.delete, sid) op = Operation(undo, redo) self._undo.recordOperation(op) return stud
def test_student_repository(self): stud_repo = StudentRepository() self.assertIsInstance(stud_repo, StudentRepository, 'Wrong instance') self.assertIsInstance(stud_repo, Repository, 'Wrong instance') self.assertEqual(len(stud_repo._list), 0) stud_repo.store(Student('marcel')) stud_repo.store(Student('marcelutz')) self.assertEqual(stud_repo._list[-1].name, 'marcelutz') stud_repo.remove(0) self.assertEqual(stud_repo._list[-1].name, 'marcelutz') with self.assertRaises(RepoErr): stud_repo.remove(20) stud_repo.update(-1, 'nomoremarcel') self.assertEqual(stud_repo._list[-1].name, 'nomoremarcel') with self.assertRaises(RepoErr): stud_repo.update(20, 'gaga') self.assertEqual(stud_repo.get_list(), stud_repo._list)
def testName(self): self._repo.store(self._student) self.assertEqual(len(self._repo), 1) s = Student("1", "Gigi") self._repo.update(s) self.assertEqual(self._repo.getById("1").getId, "1") self._repo.remove("1") self.assertEqual(len(self._repo), 0)
def _loadDatabase(self): current = self._connection.cursor() current.execute("SELECT * FROM grades") rows = current.fetchall() for row in rows: self._objects.append( Grade(Student(row[1], '', ''), Assignment(row[2], '', ''), row[0], row[3]))
def generateStudents(): undoController.newOperation(), students.store(Student( 123, 'Andrei Stefan')) undoController.newOperation(), students.store( Student(124, 'Cosmin Iordache')) undoController.newOperation(), students.store(Student( 125, 'Simon Claudiu')) undoController.newOperation(), students.store(Student(126, 'Arthur Mihai')) undoController.newOperation(), students.store(Student(127, 'George Ionut')) undoController.newOperation(), students.store( Student(128, 'Beatrice Chelaru')) undoController.newOperation(), students.store(Student(129, 'Iosif Emil')) undoController.newOperation(), students.store(Student(130, 'Cret Vasile')) undoController.newOperation(), students.store( Student(131, 'Popovici Tudor')) undoController.newOperation(), students.store( Student(132, 'Ciprian Teodorescu'))
def testRepo(self): self.assertEqual(len(self._repo), 0) x = Student("1","Pop Catalin","916") self._repo.store(x) self.assertEqual(len(self._repo), 1) self._repo.delete("1") self.assertEqual(len(self._repo), 0) self._repo.store(x)
def testValidateStud(self): validator = StudentValidator() stud = Student("42", "Mihai Adamescu", "913") try: validator.validate(stud) self.assertTrue(True) except: pass
def testRepo(self): ''' Testing store ''' self.assertEqual(len(self._repo), 0) s = Student("1", "Nei") self._repo.store(s) self.assertEqual(len(self._repo), 1) self.assertRaises(RepositoryException, self._repo.store, s) s = Student("2", "Ker") self._repo.store(s) self.assertEqual(len(self._repo), 2) ''' Testing remove from repo ''' self._repo.remove("1") self.assertEqual(len(self._repo), 1) self.assertRaises(RepositoryException, self._repo.remove, "1")
def _load_file(self): with open(self._path, 'r') as fp: data = json.load(fp) for obj in data['students']: id = obj['_id'] name = obj['name'] obj = Student.create_from_data(name, id) self._list.append(obj) fp.close()
def update(self, studentID, newName): try: studentID = int(studentID) except ValueError: raise StudentControllerException("Student ID must be an integer") if not newName.isaplha(): raise StudentControllerException("Name must contain only letters") newSt = Student(studentID, newName) self.__validator.validate(newSt) self.__studentRepo.update(newSt)
def testValidateGr(self): validator = GradeValidator() stud = Student("42", "Mihai Adamescu", "913") ass = Assignment("22", "imi plac caii", "20.4") gr = Grade("1", ass, stud,"5") try: validator.validate(gr) self.assertTrue(True) except: pass
def add(self, name): """ Makes validations, creates the object and calls the repo Add :raises: IOErr """ if not name.isalpha(): raise IOErr("name must be literal") obj = Student(name) self._repo.store(obj) return obj
def add(self, studentID, studentName): try: studentID = int(studentID) except ValueError: raise StudentControllerException("Student ID must be an integer") if not studentName.isalpha(): raise StudentControllerException("Name must contain only letters") student = Student(studentID, studentName) self.__validator.validate(student) self.__studentRepo.add(student)
def _loadFile(self): f = open(self._file, 'r') for x in f: a = x if a == '': break a = a.strip() a = a.split(',') self.store(Grade(Student(int(a[0]), '', ''), Assignment(int(a[1]), '', ''), int(a[2]), int(a[3]))) f.close()
def reading(self): file = open("/Users/ecaterinacarazan/PycharmProjects/files/student.txt", "r") lines = file.read().splitlines() for line in lines: list = line.split(", ") stud = Student(int(list[0]), list[1], int(list[2])) for s in Repository.get_all(self): if s.get_id() == int(list[0]): raise ControllerException("Can not add student with same id") Repository.add_item(self, stud) file.close()
def _load_file(self): file = open(self._path, 'r') line = file.readline() while line: line = line.strip().split() name = line[0] id = line[1] obj = Student.create_from_data(name, id) self._list.append(obj) line = file.readline() file.close()
def __readStudent(self): ''' Functionality: Try to read a Student object parameters, if fail print invalid input INPUT: - OUTPUT: an object Student ''' print("Please insert the student ID:") ID = self.__getNumber() name = input("Student name: ") return Student(ID, name)
def _loadFile(self): with open(self._file) as f: try: data = json.load(f) except Exception: f.close() return for i in data.keys(): data[i] = data[i].split(',') self.store(Student(int(data[i][0]), data[i][1], int(data[i][2]))) f.close()
def _loadFile(self): l = [] f = open(self._file, 'r') for x in f: if x == '': break a = x a = a.strip() a = a.split(',') self.store(Student(int(a[0]), a[1], int(a[2]))) f.close()
def reading(self): file = open("/Users/AndiD//Documents/Eclipse/lab57/student.txt", "r") lines = file.read().splitlines() for line in lines: list = line.split(", ") stud = Student(int(list[0]), list[1], int(list[2])) for s in Repository.get_all(self): if s.get_id() == int(list[0]): raise ControllerException( "Can not add student with same id") Repository.add_item(self, stud) file.close()
def addStudent(self, ident, name): ''' Saves a student with an id and a name to the student repository Input : ident - the student's id name - the student's name Output : the student Error : if the data entered is invalid ''' s = Student(ident, name) self._studentValidator.validate(s) self._studentRepo.store(s) return s
def searchForName(self, name): ''' Partial string matching(search for a student after his name, the search is case insensitive) name - the name to search for ( can't be empty ) ''' _entity = [] s = Student(None, name) self._studentValidator.validate(s) for student in self._studentRepo.getAll(): if name.lower() in student.name.lower(): _entity.append(str(student)) return _entity
def update_student(self, stud_id, name, group): ''' Updates a student from the repository after validating its new properties Input: :param stud_id :param name :param group ''' stud = Student(stud_id, name, group) self.__stud_validator.validate_student(stud) self.__stud_repository.update_student(stud)
def add_student(self, stud_id, name, group): ''' Adds a new student to the repository after validating its properties Input: :param stud_id :param name :param group ''' stud = Student(stud_id, name, group) self.__stud_validator.validate_student(stud) self.__stud_repository.add_student(stud)
def update_student(self, student_id, name, group): """ Finds the student with the same id and changes his/her name and/or group :param student_id: positive integer :param name: syring :param group: positive integer :return: """ student = Student(student_id, name, group) self._validator.validate_student(student) self._repository.update_student(student)