22 files changed

+682
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from functools import reduce
2+
3+
4+
class ClientValidator:
5+
'''
6+
Class which purpose is to validate data needed to create a Client object
7+
'''
8+
9+
@staticmethod
10+
def validate(clientId, name):
11+
'''
12+
Static function that validate each field and raises an error with a list of messages
13+
:param clientId: integer
14+
:param name: string // cannot be empty
15+
:return: True if the number was validated correctly, raises errors otherwise
16+
'''
17+
errors = []
18+
try:
19+
clientId = int(clientId.strip())
20+
except ValueError:
21+
errors.append("Client ID should not be string, expected integer!")
22+
if len(name.strip()) == 0:
23+
errors.append("Client Name should not pe empty!")
24+
if ',' in name:
25+
errors.append("Name cannot contain ',' character")
26+
27+
if len(errors) > 0:
28+
errorMessage = reduce(lambda x, y: str(x) + '\n' + str(y), errors)
29+
errorMessage = "Errors:\n" + errorMessage
30+
raise ValueError(errorMessage)
31+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from Models import Movie
2+
3+
4+
class MovieValidator:
5+
'''
6+
Class which purpose is to validate data needed to create a Movie object
7+
'''
8+
9+
@staticmethod
10+
def validate(movieId, title, description, genre):
11+
'''
12+
Static function that validate each field and raises an error with a list of messages
13+
:param movieId: integer
14+
:param title: string
15+
:param description: string
16+
:param genre: one of MOVIE_GENRE
17+
:return: True if the number was validated correctly, raises errors otherwise
18+
'''
19+
errors = []
20+
try:
21+
movieId = int(movieId.strip())
22+
except ValueError:
23+
errors.append("Movie ID should not be string, expected integer")
24+
if len(title.strip()) == 0:
25+
errors.append(" Movie title should not pe empty")
26+
if ',' in title:
27+
errors.append(" Movie title cannot contain ',' character")
28+
if ',' in description:
29+
errors.append(" Movie description cannot contain ',' character")
30+
if ',' in genre:
31+
errors.append(" Movie genre cannot contain ',' character")
32+
if len(description.strip()) == 0:
33+
errors.append(" Movie description should not pe empty")
34+
if genre.strip().lower() not in Movie.MOVIE_GENRE:
35+
errors.append(" Movie Genre isn't valid")
36+
37+
if len(errors) > 0:
38+
errorMessage = "Errors:\n "
39+
for error in errors:
40+
errorMessage += error + "\n"
41+
raise ValueError(errorMessage)
42+
return True
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from Models import Movie
2+
from functools import reduce
3+
from datetime import date
4+
5+
6+
class RentalValidator:
7+
@staticmethod
8+
def validate(rentalId, movieId, clientId, rentedDate, dueDate, returnedDate):
9+
errors = []
10+
dateRent = -1
11+
dateDue = -1
12+
try:
13+
movieId = int(movieId.strip())
14+
except ValueError:
15+
errors.append("Movie ID should not be string, expected integer")
16+
try:
17+
clientId = int(clientId.strip())
18+
except ValueError:
19+
errors.append("Client ID should not be string, expected integer")
20+
try:
21+
rentalId = int(rentalId.strip())
22+
except ValueError:
23+
errors.append("Rental ID should not be string, expected integer")
24+
if len(rentedDate) == 10:
25+
try:
26+
year, month, day = map(int, rentedDate.split('-'))
27+
dateRent = date(year, month, day)
28+
except Exception as e:
29+
errors.append("Rented Date is not in the correct format")
30+
else:
31+
errors.append("Rented date is not in the correct format")
32+
33+
if int(returnedDate) != -1:
34+
if len(returnedDate) == 10:
35+
try:
36+
year, month, day = map(int, returnedDate.split('-'))
37+
datet = date(year, month, day)
38+
except Exception as e:
39+
errors.append("Returned date is not in the correct format")
40+
else:
41+
errors.append("Returned date is not in the correct format")
42+
43+
if len(dueDate) == 10:
44+
try:
45+
year, month, day = map(int, dueDate.split('-'))
46+
dateDue = date(year, month, day)
47+
except Exception as e:
48+
errors.append("Due date is not in the correct format")
49+
else:
50+
errors.append("Due date is not in the correct format")
51+
if isinstance(dateDue, date) and isinstance(dateRent, date):
52+
if dateDue < dateRent:
53+
errors.append("Due date cannot be smaller than rent date")
54+
if len(errors) > 0:
55+
errorMessage = "Errors:\n "
56+
for error in errors:
57+
errorMessage += error + "\n"
58+
59+
raise ValueError(errorMessage)
60+
return True
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from Models.Client import Client
2+
from Repositories.FileRepository import FileRepository
3+
from Tests.TestRepo import RepoTestCase
4+
5+
6+
class FileRepoTestCase(RepoTestCase):
7+
def setUp(self):
8+
RepoTestCase.setUp(self)
9+
self.fileRepo = FileRepository("../Tests/testFileRepo", "Repo for test", Client.clientToFile,
10+
Client.clientFromFile)
11+
with open("../Tests/testFileRepo", "w") as f:
12+
f.write("3, Birhan\n")
13+
14+
def tearDown(self):
15+
open("testFileRepo", "w").close()
16+
17+
def test_FileRepo(self):
18+
self.test_Repo()
19+
self.assertEqual(self.fileRepo.getItemById(3), Client(3, "Birhan"))
20+
c = Client(4, "numeFaaaain")
21+
self.fileRepo.createItem(c)
22+
self.assertEqual(self.fileRepo.getItemById(4), c)
23+
self.fileRepo.updateItemById(4, Client(4, "lapteeee"))
24+
self.fileRepo.deleteItemById(4)
25+
self.assertEqual(self.fileRepo.getItemById(4), False)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from Models.Rental import Rental
2+
from Repositories.JsonRepository import JsonRepo
3+
from Tests.TestFileRepo import FileRepoTestCase
4+
5+
6+
class JsonRepoTestCase(FileRepoTestCase):
7+
def setUp(self):
8+
FileRepoTestCase.setUp(self)
9+
self.jsonRepo = JsonRepo("../Tests/testJsonRepo", "Repo for test", Rental.rentalFromJson)
10+
open("../Tests/testJsonRepo", "w").close()
11+
12+
def tearDown(self):
13+
open("testFileRepo", "w").close()
14+
15+
def test_JsonRepo(self):
16+
self.test_FileRepo()
17+
c = Rental(4, 4, 4, "2017-12-10", "2017-12-20", "2017-12-14")
18+
self.jsonRepo.createItem(c)
19+
self.assertEqual(self.jsonRepo.getItemById(4), c)
20+
self.jsonRepo.updateItemById(4, Rental(4, 4, 4, "2017-12-12", "2017-12-22", "2017-12-24"))
21+
self.jsonRepo.deleteItemById(4)
22+
self.assertEqual(self.jsonRepo.getItemById(4), False)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
3+
from Models.Client import Client
4+
from Repositories.BaseRepository import Repository
5+
from Repositories.RepositoryException import RepositoryException
6+
7+
8+
class RepoTestCase(unittest.TestCase):
9+
def setUp(self):
10+
self.__c0 = Client(1, "Ghita")
11+
self.__c1 = Client(2, "Marcel")
12+
self.__c2 = Client(3, "Birhan")
13+
self.__c3 = Client("4", "Erik")
14+
self.__c4 = Client(1, "Bbf")
15+
16+
def test_Repo(self):
17+
self.r = Repository()
18+
self.r.create(self.__c0)
19+
self.assertEqual(self.r.getAll(), [self.__c0])
20+
self.assertEqual(len(self.r), 1)
21+
self.assertEqual("1, Ghita\n", str(self.r))
22+
self.assertEqual(self.__c0, self.__c0)
23+
self.r.create(self.__c1)
24+
self.assertEqual(self.r.find(10), False)
25+
self.r.update(self.__c4)
26+
self.assertEqual(self.__c4, self.r.find(self.__c4.id))
27+
self.assertRaises(RepositoryException, self.r.update, self.__c3)
28+
self.assertRaises(RepositoryException, self.r.create, self.__c0)
29+
self.assertRaises(RepositoryException, self.r.delete, 77)
30+
31+
c = self.__c4
32+
self.r.delete(c.id)
33+
self.assertEqual(self.r.find(1), False)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)