def __test_valid_disciplina(self): valid_disci = ValidatorDisci() valid_disci.valideaza_disciplina(self.__disciplina) self.__disci_id_invalid = Disciplina(-10, 'heh', 'ehe') self.__disci_nume_invalid = Disciplina(9, '', 'gabi') self.__disci_nume_prof_invalid = Disciplina(9, 'Fp', '') self.__disci_invalid = Disciplina(-2, '', '') with self.assertRaises(ValidError): valid_disci.valideaza_disciplina(self.__disci_id_invalid) valid_disci.valideaza_disciplina(self.__disci_nume_invalid) valid_disci.valideaza_disciplina(self.__disci_nume_prof_invalid) valid_disci.valideaza_disciplina(self.__disci_invalid)
def __test_repo_note(self): clear_file('test.txt') self.__repo = FileRepo('test.txt', Nota.read_line, Nota.write_line) self.assertEqual(self.__repo.size(), 0) self.__repo.adauga(self.__nota) self.assertEqual(self.__repo.size(), 1) self.assertEqual(self.__repo.get_all(), [self.__nota]) with self.assertRaises(RepoError): self.__repo.adauga(self.__nota) x = self.__repo.cauta(self.__nota) self.assertEqual(x, self.__nota) stud = Student(20, '') disci = Disciplina(20, '', '') self.__nota_inexistenta = Nota(stud, disci, 4) with self.assertRaises(RepoError): x = self.__repo.cauta(self.__nota_inexistenta) self.__nota_to_modify = self.__nota self.__modified_nota = self.__nota self.__modified_nota.set_nota(5) self.__repo.modifica(self.__nota_to_modify, self.__modified_nota) self.assertEqual(self.__nota.get_nota(), 5) with self.assertRaises(RepoError): self.__repo.sterge(self.__nota_inexistenta) self.__repo.sterge(self.__nota) assert (self.__repo.size() == 0)
def get_disci_medie(self): note = self.__repoNote.get_all() situatie = {} for nota in note: if nota.get_disci().get_id() not in situatie: situatie[nota.get_disci().get_id()] = [] situatie[nota.get_disci().get_id()].append(nota.get_nota()) discipline = self.__repoDisci.get_all() for disci in discipline: if disci.get_id() not in situatie: situatie[disci.get_id()] = [0] rez = [] for item in situatie.items(): aux_disci = Disciplina(item[0], '', '') x = self.__repoDisci.cauta(aux_disci) disci = DisciAvgDTO(x.get_nume(), sum(item[1]) / len(item[1])) rez.append(disci) rez.sort(key=lambda x: x.get_medie(), reverse=True) i = 0 j = 0 for i in range(0, len(rez) - 1): for j in range(i + 1, len(rez)): if rez[i].get_medie() == rez[j].get_medie( ) and rez[i].get_nume() > rez[j].get_nume(): aux = rez[i] rez[i] = rez[j] rez[j] = aux return rez[:]
def __test_repo_disciplina(self): clear_file('test.txt') self.__repo = FileRepo('test.txt', Disciplina.read_line, Disciplina.write_line) self.assertEqual(self.__repo.size(), 0) self.__repo.adauga(self.__disciplina) self.assertEqual(self.__repo.size(), 1) self.assertEqual(self.__repo.get_all(), [self.__disciplina]) with self.assertRaises(RepoError): self.__repo.adauga(self.__disciplina) x = self.__repo.cauta(self.__disciplina) self.assertEqual(x, self.__disciplina) self.__disci_inexistenta = Disciplina(1, 'roman', '') with self.assertRaises(RepoError): x = self.__repo.cauta(self.__disci_inexistenta) self.__disci_to_modify = self.__disciplina self.__modified_disci = self.__disciplina self.__modified_disci.set_nume('geogra') self.__modified_disci.set_nume_prof('georgian') self.__repo.modifica(self.__disci_to_modify, self.__modified_disci) self.assertEqual(self.__disciplina.get_nume(), 'geogra') self.assertEqual(self.__disciplina.get_nume_prof(), 'georgian') with self.assertRaises(RepoError): self.__repo.sterge(self.__disci_inexistenta) self.__repo.sterge(self.__disciplina) assert (self.__repo.size() == 0)
def __test_creeaza_nota(self): self.__student = Student(10, 'Bogdan') self.__disciplina = Disciplina(1, 'info', 'neagu') nota = Nota(self.__student, self.__disciplina, 7) self.assertEqual(nota.get_stud(), self.__student) self.assertEqual(nota.get_disci(), self.__disciplina) self.assertEqual(nota.get_nota(), 7) self.assertEqual( str(nota), self.__student.get_nume() + ' are nota 7 la disciplina ' + self.__disciplina.get_nume()) nota2 = Nota(self.__student, self.__disciplina, 9) self.assertFalse(nota == nota2) nota.set_nota(9) self.assertEqual(nota.get_nota(), 9) self.assertTrue(nota == nota2) self.__nota = nota
def modifica_nume_profesor(self, d_id, newname): disci_to_modify = Disciplina(d_id, "search", 'search') self.__valid_disci.valideaza_disciplina(disci_to_modify) x = self.__repoDisci.cauta(disci_to_modify) modified_disci = x modified_disci.set_nume_prof(newname) self.__valid_disci.valideaza_disciplina(modified_disci) self.__repoDisci.modifica(x, modified_disci)
def adauga_nota(self, s_id, d_id, nota): stud_aux = Student(s_id, 'search') stud = self.__repoStud.cauta(stud_aux) disci_aux = Disciplina(d_id, 'search', 'search') disci = self.__repoDisci.cauta(disci_aux) nota = Nota(stud, disci, nota) self.__valid_nota.valideaza_nota(nota) self.__repoNote.adauga(nota)
def __test_create_disciplina(self): disci = Disciplina(10, 'info', 'neagu') self.assertEqual(disci.get_id(), 10) self.assertEqual(disci.get_nume(), 'info') self.assertEqual(disci.get_nume_prof(), 'neagu') disci.set_nume('mate') self.assertEqual(disci.get_nume(), 'mate') disci.set_nume_prof('coada') self.assertEqual(disci.get_nume_prof(), 'coada') disci1 = Disciplina(10, 'info', 'neagu') self.assertTrue(disci == disci1) self.__disciplina = disci
class NoteTests(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def __test_creeaza_nota(self): self.__student = Student(10, 'Bogdan') self.__disciplina = Disciplina(1, 'info', 'neagu') nota = Nota(self.__student, self.__disciplina, 7) self.assertEqual(nota.get_stud(), self.__student) self.assertEqual(nota.get_disci(), self.__disciplina) self.assertEqual(nota.get_nota(), 7) self.assertEqual( str(nota), self.__student.get_nume() + ' are nota 7 la disciplina ' + self.__disciplina.get_nume()) nota2 = Nota(self.__student, self.__disciplina, 9) self.assertFalse(nota == nota2) nota.set_nota(9) self.assertEqual(nota.get_nota(), 9) self.assertTrue(nota == nota2) self.__nota = nota def __test_valideaza_nota(self): valid_nota = ValidatorNota() valid_nota.valideaza_nota(self.__nota) self.__nota_invalida = Nota(self.__student, self.__disciplina, -5) with self.assertRaises(ValidError): valid_nota.valideaza_nota(self.__nota_invalida) def __test_repo_note(self): clear_file('test.txt') self.__repo = FileRepo('test.txt', Nota.read_line, Nota.write_line) self.assertEqual(self.__repo.size(), 0) self.__repo.adauga(self.__nota) self.assertEqual(self.__repo.size(), 1) self.assertEqual(self.__repo.get_all(), [self.__nota]) with self.assertRaises(RepoError): self.__repo.adauga(self.__nota) x = self.__repo.cauta(self.__nota) self.assertEqual(x, self.__nota) stud = Student(20, '') disci = Disciplina(20, '', '') self.__nota_inexistenta = Nota(stud, disci, 4) with self.assertRaises(RepoError): x = self.__repo.cauta(self.__nota_inexistenta) self.__nota_to_modify = self.__nota self.__modified_nota = self.__nota self.__modified_nota.set_nota(5) self.__repo.modifica(self.__nota_to_modify, self.__modified_nota) self.assertEqual(self.__nota.get_nota(), 5) with self.assertRaises(RepoError): self.__repo.sterge(self.__nota_inexistenta) self.__repo.sterge(self.__nota) assert (self.__repo.size() == 0) def run_note_tests(self): self.__test_creeaza_nota() self.__test_valideaza_nota() self.__test_repo_note()
def sterge_disciplina(self, d_id): disci_to_delete = Disciplina(d_id, 'delete', 'delete') self.__valid_disci.valideaza_disciplina(disci_to_delete) self.__repoDisci.sterge(disci_to_delete)
def cauta_disciplina(self, d_id): disci_aux = Disciplina(d_id, 'search', 'search') self.__valid_disci.valideaza_disciplina(disci_aux) return self.__repoDisci.cauta(disci_aux)
def adauga_disciplina(self, d_id, nume, nume_prof): disci_aux = Disciplina(d_id, nume, nume_prof) self.__valid_disci.valideaza_disciplina(disci_aux) self.__repoDisci.adauga(disci_aux)