def testService(): ''' testeaza functiile din clasa Service ''' valid=ValidJucator() repo=FileRepo("testServ.txt") file=open("testServ.txt",'w') file.close() serv=Service(repo, valid) try: serv.adauga('','',0,'asd') assert False except ValidError: assert True jucator1=Jucator('Mihali', 'Teo', 172, 'pivot') jucator2=Jucator('Alex','Pop',180,'extrema') jucator3=Jucator('Mihali', 'Teo', 172, 'pivot') assert len(serv.getAll())==0 serv.adauga('Mihali', 'Teo', 172, 'pivot') assert len(serv.getAll())==1 serv.adauga('Alex','Pop',180,'extrema') assert len(serv.getAll())==2 try: serv.adauga('Mihali', 'Teo', 172, 'pivot') assert False except RepoError: assert True # serv.update("Mihali", "Teo", 160) # assert jucator1.get_inaltime()==160 jucator4=Jucator('Navickas','Gediminas',190,'fundas') jucator5=Jucator('Nikolic','Niksa',192,'fundas') jucator6=Jucator('Dragusin','Ionut',220,'pivot') jucator7=Jucator('Graham','Paul',200,'extrema') jucator8=Jucator('Dumitrescu','Tudor',186,'extrema') serv.adauga('Navickas','Gediminas',190,'fundas') serv.adauga('Nikolic','Niksa',192,'fundas') serv.adauga('Dragusin','Ionut',220,'pivot') serv.adauga('Graham','Paul',200,'extrema') serv.adauga('Dumitrescu','Tudor',186,'extrema') echipa=[] echipa=serv.echipa() assert jucator4 in echipa assert jucator5 in echipa assert jucator6 in echipa assert jucator7 in echipa assert jucator8 in echipa # assert echipa==[jucator4,jucator5,jucator6,jucator7,jucator8] # print(echipa) f=open("testImporta.txt",'w') #repo2=FileRepo("testImporta.txt") line="ala bala\n" f.write(line) f.close() serv.importa("testImporta.txt") assert len(serv.getAll())==8
def importa(self, filename): try: f=open(filename,'r') except IOError: print("Nu s-a gasit fisierul!") return posturi=['extrema','fundas','pivot'] jucatori2=self.__repo.getAll() jucatori=[] for line in f: if line.strip()=="": continue params=line.split(" ") nume=params[0].strip() prenume=params[1].strip() inaltime=random.randint(1,230) i=random.randint(0,2) post=posturi[i] jucator=Jucator(nume,prenume,inaltime,post) #(jucator) jucatori.append(jucator) f.close() for i in jucatori: if i not in jucatori2: self.__repo.adauga(i)
def __createJucatorFromLine(self, line): '''creeaza un obiect de tip Jucator din linia line si il returneaza ''' params = line.split(" ") jucator = Jucator(params[0].strip(), params[1].strip(), int(params[2].strip()), params[3].strip()) return jucator
def testValid(): validator = ValidJucator() jucator = Jucator('', '', 0, 'asd') try: validator.valideazaJucator(jucator) assert False except ValidError as msg: assert len(msg.get_errors()) == 4 jucator2 = Jucator('Teo', 'Mihali', -3, '') try: validator.valideazaJucator(jucator2) assert False except ValidError as msg: assert len(msg.get_errors()) == 2
def testRepoFile(): ''' testeaza functiile din clasa FileRepo ''' filename = "test.txt" file = open(filename, 'w') #face fisierul gol fileRepo = FileRepo(filename) assert len(fileRepo.getAll()) == 0 jucator = Jucator("Teo", "Mihali", 172, "extrema") fileRepo.adauga(jucator) assert len(fileRepo.getAll()) == 1 file.close()
def update(self, nume, prenume, inaltime): '''updateaza inaltimea jucatorului cu numele si prenumele dat cu inaltimea data ca si parametru arunca RepoError jucatorul nu exitsa postcond: inaltimea nr nat strict pozitiv ''' jucatori = self.__loadFromFile() jucator = Jucator(nume, prenume, 0, 'pivot') if jucator not in jucatori: raise RepoError("Jucator inexistent!") for i in jucatori: if i == jucator: i.set_inaltime(inaltime) # self.__storeToFile(jucatori) self.__storeToFile(jucatori)
def adauga(self, nume, prenume,inaltime,post): jucator=Jucator(nume, prenume, inaltime, post) self.__valid.valideazaJucator(jucator) self.__repo.adauga(jucator)