Esempio n. 1
0
 def _test_repository_pushremove(self):
     '''
     Tests if the functions push and remove from the repository work
     '''
     repository = Repository()
     Repository.push(repository, "5", 0)
     Repository.push(repository, 2)        
     Repository.push(repository, True, 3)
     Repository.push(repository, 7.5)
     
     assert Repository.getList(repository) == ["5", 2, 7.5]
     
     Repository.remove(repository, 3)
     Repository.remove(repository, 1)
     
     assert Repository.getList(repository) == ["5", 7.5]
     
     Repository.remove(repository, 0)
     
     assert Repository.getList(repository) == [7.5]
     
     Repository.remove(repository, 0)
     Repository.remove(repository, 0)
     
     assert Repository.getList(repository) == []
Esempio n. 2
0
    def testRepositoryClients(self):
        repoClient = Repository()
        client = Client(1, "Name")
        client1 = Client(2, "Name1")

        self.assertTrue(repoClient.size() == 0)
        repoClient.add(client)
        self.assertTrue(repoClient.size() == 1)
        repoClient.add(client1)

        self.assertTrue(repoClient.existsById(1))
        self.assertTrue(repoClient.existsById(2))
        self.assertTrue(repoClient.getById(1).getName() == "Name")

        repoClient.removeById(1)
        self.assertTrue(repoClient.size() == 1)
        repoClient.remove(client1)
        self.assertTrue(repoClient.size() == 0)

        self.assertTrue(repoClient.existsById(1) == False)

        repoClient.add(client)
        repoClient.update(Client(1, "Updated Name"))

        self.assertTrue(repoClient.findById(1) == 0)
        self.assertTrue(repoClient.getById(1).getName() == "Updated Name")

        self.assertTrue(str(repoClient.getById(1)) == "CLIENT ID: " + str(
            repoClient.getById(1).getId()) + "| Name: " + repoClient.getById(1).getName())
Esempio n. 3
0
class TestRepository(unittest.TestCase):
    def setUp(self):
        self.repo = Repository()
        self.c = Client(12, 'alex')
        self.c1 = Client(121, 'ana')
        self.repo2 = FileRepository('testclient.txt',
                                    Client.readClientFromLine,
                                    Client.writeClientToLine)
        self.repo3 = FileRepository('testclient4.txt',
                                    Client.readClientFromLine,
                                    Client.writeClientToLine)

    def test_repo(self):
        self.assertEqual(len(self.repo), 0)
        self.repo.add(self.c)
        self.assertEqual(str(self.repo), '012. alex\n\n')
        self.assertEqual(len(self.repo), 1)
        self.assertRaises(RepositoryException, self.repo.add, self.c)
        self.assertEqual(len(self.repo.getAll()), 1)
        self.assertEqual(self.repo.findByID(12), 12)
        self.assertEqual(self.repo.findByID(13), -1)
        self.assertEqual(self.repo.get(12), self.c)
        self.assertRaises(RepositoryException, self.repo.get, 13)
        self.assertRaises(RepositoryException, self.repo.remove, 13)
        self.assertRaises(RepositoryException, self.repo.update, 13)
        self.repo.update(self.c)
        self.repo.remove(12)
        self.assertEqual(len(self.repo), 0)
        self.repo2.add(self.c)
        self.repo2.upd(self.c)
        self.repo2.rem(self.c.getID())
        self.assertEqual(len(self.repo2), 1)
 def remove(self, id):
     Repository.remove(self, id)
     self.writeAllToFile()
Esempio n. 5
0
class PickleRepository(Repository):
    def __init__(self, fileName):
        self.__fileName = fileName
        self.__repo = Repository()
        self.__clear(fileName)
        self.__loadFromFile()

    def add(self, elem):
        self.__repo.add(elem)
        self.__storeToFile()

    def remove(self, elem):
        self.__repo.remove(elem)
        self.__storeToFile()

    def removeById(self, id):
        self.__repo.removeById(id)
        self.__storeToFile()

    def update(self, elem):
        self.__repo.update(elem)
        self.__storeToFile()

    def __len__(self):
        return self.__repo.size()

    def getById(self, id):
        return self.__repo.getById(id)

    def findById(self, id):
        return self.__repo.findById(id)

    def getAll(self):
        return self.__repo.getAll()

    def existsById(self, id):
        return self.__repo.existsById(id)

    def size(self):
        return self.__repo.size()

    def __storeToFile(self):
        f = open(self.__fileName, "wb")
        pickle.dump(self.__repo.getAll(), f)
        f.close()

    def __loadFromFile(self):
        f = open(self.__fileName, "rb")
        try:
            self.__repo._elems = pickle.load(f)
        except EOFError:
            return []
        f.close()

    def __clear(self, fileName):
        """
        Clear the file
        :param fileName:
        :return: None
        """
        open(fileName, "w").close()
Esempio n. 6
0
    def testRepositoryBooks(self):
        repoBooks = Repository()
        repoText = CSVRepository("bookRepo.csv", Book(1, "e", "s", "w"))
        repoBinary = PickleRepository("binaryBookRepo.pickle")
        repoCustom = CustomRepository(Book)

        book = Book(1, "Title", "Description", "Author")
        book1 = Book(2, "Title1", "Description1", "Author1")

        self.assertEqual(repoBooks.size(), 0)
        self.assertEqual(repoBinary.size(), 0)
        self.assertEqual(repoText.size(), 0)
        self.assertEqual(repoCustom.size(), 0)

        repoBooks.add(book)
        repoBooks.add(book1)
        repoText.add(book)
        repoText.add(book1)
        repoBinary.add(book)
        repoBinary.add(book1)
        repoCustom.add(book)
        repoCustom.add(book1)

        self.assertTrue(repoBooks.existsById(1))
        self.assertTrue(repoBooks.size() == 2)
        self.assertTrue(repoBooks.getById(1) == book)
        self.assertTrue(repoBooks.existsById(2))
        self.assertTrue(repoBooks.getById(2) == book1)

        self.assertTrue(repoText.existsById(1))
        self.assertTrue(repoText.size() == 2)
        self.assertTrue(repoText.getById(1) == book)
        self.assertTrue(repoText.existsById(2))
        self.assertTrue(repoText.getById(2) == book1)

        self.assertTrue(repoBinary.existsById(1))
        self.assertTrue(repoBinary.size() == 2)
        self.assertTrue(repoBinary.getById(1) == book)
        self.assertTrue(repoBinary.existsById(2))
        self.assertTrue(repoBinary.getById(2) == book1)

        self.assertTrue(repoCustom.existsById(1))
        self.assertTrue(repoCustom.size() == 2)
        self.assertTrue(repoCustom.getById(1) == book)
        self.assertTrue(repoCustom.existsById(2))
        self.assertTrue(repoCustom.getById(2) == book1)

        repoBooks.removeById(1)
        repoBooks.remove(book1)
        repoText.removeById(1)
        repoText.remove(book1)
        repoBinary.removeById(1)
        repoBinary.remove(book1)
        repoCustom.removeById(1)
        repoCustom.remove(book1)

        self.assertTrue(repoBooks.size() == 0)
        self.assertTrue(repoBooks.existsById(1) == False)
        self.assertTrue(repoText.size() == 0)
        self.assertTrue(repoText.existsById(1) == False)
        self.assertTrue(repoBinary.size() == 0)
        self.assertTrue(repoBinary.existsById(1) == False)
        self.assertTrue(repoCustom.size() == 0)
        self.assertTrue(repoCustom.existsById(1) == False)

        repoBooks.add(book)
        repoBooks.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        repoText.add(book)
        repoText.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        repoBinary.add(book)
        repoBinary.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        repoCustom.add(book)
        repoCustom.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        self.assertTrue(repoBooks.findById(1) == 0)
        self.assertTrue(repoText.findById(1) == 0)
        self.assertTrue(repoBinary.findById(1) == 0)
        self.assertTrue(repoCustom.findById(1) == 0)

        self.assertTrue(repoBooks.getById(1).getTitle() == "Updated Title")
        self.assertTrue(repoText.getById(1).getTitle() == "Updated Title")
        self.assertTrue(repoBinary.getById(1).getTitle() == "Updated Title")
        self.assertTrue(repoCustom.getById(1).getTitle() == "Updated Title")

        self.assertTrue(repoBooks.getById(1).getDescription() == "Updated Desc")
        self.assertTrue(repoText.getById(1).getDescription() == "Updated Desc")
        self.assertTrue(repoBinary.getById(1).getDescription() == "Updated Desc")
        self.assertTrue(repoCustom.getById(1).getDescription() == "Updated Desc")

        self.assertTrue(repoBooks.getById(1).getAuthor() == "Updated Author")
        self.assertTrue(repoText.getById(1).getAuthor() == "Updated Author")
        self.assertTrue(repoBinary.getById(1).getAuthor() == "Updated Author")
        self.assertTrue(repoCustom.getById(1).getAuthor() == "Updated Author")

        self.assertTrue(str(repoBooks.getById(1)) == "BOOK ID: " + str(repoBooks.getById(1).getId()) + "| Title: " + \
                        repoBooks.getById(1).getTitle() + "| Description: " + \
                        repoBooks.getById(1).getDescription() + \
                        "| Author: " + repoBooks.getById(1).getAuthor())
        self.assertTrue(str(repoText.getById(1)) == "BOOK ID: " + str(repoText.getById(1).getId()) + "| Title: " + \
                        repoText.getById(1).getTitle() + "| Description: " + \
                        repoText.getById(1).getDescription() + \
                        "| Author: " + repoText.getById(1).getAuthor())
        self.assertTrue(str(repoBinary.getById(1)) == "BOOK ID: " + str(repoBinary.getById(1).getId()) + "| Title: " + \
                        repoBinary.getById(1).getTitle() + "| Description: " + \
                        repoBinary.getById(1).getDescription() + \
                        "| Author: " + repoBinary.getById(1).getAuthor())
        self.assertTrue(str(repoCustom.getById(1)) == "BOOK ID: " + str(repoCustom.getById(1).getId()) + "| Title: " + \
                        repoCustom.getById(1).getTitle() + "| Description: " + \
                        repoCustom.getById(1).getDescription() + \
                        "| Author: " + repoCustom.getById(1).getAuthor())
class CSVRepository(Repository):
    def __init__(self, fileName, objectType):
        self.__fileName = fileName
        self.__repo = Repository()
        self.__clear(fileName)
        self.type = objectType
        self.__loadFromFile(objectType)

    def __clear(self, fileName):
        """
        Clear the file
        :param fileName:
        :return: None
        """
        open(fileName, "w").close()

    def add(self, elem):
        """
        Write the updated list to the file
        :param elem: elem to be added
        :return:
        """
        self.__repo.add(elem)
        self.__storeToFile(elem)

    def remove(self, elem):
        """
        Remove elem from the repo
        :param elem:
        :return:
        """
        self.__repo.remove(elem)
        self.__storeToFile(elem)

    def removeById(self, id):
        """
        :param id: int
        :return: The element with the id id
        """
        self.__repo.removeById(id)
        self.__storeToFile(self.type)  #TODO

    def update(self, elem):
        """
        Update an element
        :param elem:
        :return: None
        """
        self.__repo.update(elem)
        self.__storeToFile(elem)

    def __len__(self):
        return len(self.__repo)

    def getById(self, id):
        """
        :param id: int
        :return: The element with the id id
        """
        return self.__repo.getById(id)

    def findById(self, id):
        """

        :param id: int
        :return: the index of the elem with the id id
        """
        return self.__repo.findById(id)

    def getAll(self):
        """

        :return: a list of all elements
        """
        return self.__repo.getAll()

    def existsById(self, id):
        """

        :param id: int
        :return: True if the element with the id is in the repo
        """
        return self.__repo.existsById(id)

    def size(self):
        """
        :return: The number of elements in the repo
        """
        return self.__repo.size()

    def __storeToFile(self, type):
        """
        :param elemSample: element
        :return: None
        """
        f = open(self.__fileName, "w")

        # handle each type of object accordingly
        if type == Book:
            for book in self.__repo.getAll():
                output = str(book.getId()) + ","
                output += book.getTitle() + ","
                output += book.getDescription() + ";"
                output += book.getAuthor()
                output += "\n"
                f.write(output)

        if type == Client:
            for client in self.__repo.getAll():
                output = str(client.getId()) + ","
                output += client.getName()
                output += "\n"
                f.write(output)

        if type == Rental:
            for rental in self.__repo.getAll():
                output = str(rental.getRentalId()) + ","
                output += str(rental.getBookId()) + ","
                output += str(rental.getClientId()) + ","
                output += str(rental.getRentedDate()) + ","
                output += str(rental.getDueDate()) + ","
                output += str(rental.getReturnDate())
                output += "\n"
                f.write(output)

        f.close()

    def __loadFromFile(self, type):
        """
        :param sample: Element
        :return: None
        :raises RepositoryException when the file cannot be read for some reason
        """
        try:
            f = open(self.__fileName, "r")
            line = f.readline().strip()
            while line != "":
                attrs = line.split(",")

                if type == Book:
                    book = Book(int(attrs[0]), attrs[1], attrs[2], attrs[3])
                    self.__repo.add(book)
                    line = f.readline().strip()

                if type == Client:
                    client = Client(int(attrs[0]), attrs[1])
                    self.__repo.add(client)
                    line = f.readline().strip()

                if type == Rental:
                    rental = Rental(
                        int(attrs[0]), int(attrs[1]), int(attrs[2]),
                        createDateFromString(self.__reverseDate(attrs[3])),
                        createDateFromString(self.__reverseDate(attrs[4])),
                        createDateFromString(self.__reverseDate(attrs[5])))
                    self.__repo.add(rental)
                    line = f.readline().strip()
        except IOError:
            raise RepositoryException()
        finally:
            f.close()
Esempio n. 8
0
 def remove(self, ident):
     Repository.remove(self, ident)
     self.__writeToFile()