Ejemplo n.º 1
0
class TestLibrary(TestCase):
    def setUp(self):
        self.library = Library("Test")

    def test_initializing_all_attributes(self):
        self.assertEqual("Test", self.library.name)
        self.assertEqual({}, self.library.books_by_authors)
        self.assertEqual({}, self.library.readers)

        with self.assertRaises(ValueError) as ex:
            self.library.name = ""

        self.assertEqual("Name cannot be empty string!", str(ex.exception))

    def test_add_book(self):
        self.assertDictEqual({}, self.library.books_by_authors)
        self.library.add_book("Ivan", "TestTitle")
        self.assertDictEqual({"Ivan": ["TestTitle"]}, self.library.books_by_authors)

        self.library.add_book("Ivan", "TestTitle2")
        self.assertDictEqual({"Ivan": ["TestTitle", "TestTitle2"]}, self.library.books_by_authors)

    def test_add_reader(self):
        self.assertDictEqual({}, self.library.readers)
        self.library.add_reader("Ivan")
        self.assertDictEqual({"Ivan": []}, self.library.readers)

        result = self.library.add_reader("Ivan")
        self.assertEqual("Ivan is already registered in the Test library.", result)

    def test_rent_book_reader_not_exist(self):
        self.library.add_book("Ivan", "TestTitle")
        result = self.library.rent_book("TestReader", "Ivan", "TestTitle")

        self.assertEqual("TestReader is not registered in the Test Library.", result)

    def test_rent_book_author_not_exist(self):
        self.library.add_reader("TestReader")
        result = self.library.rent_book("TestReader", "Ivan", "TestTitle")

        self.assertEqual("Test Library does not have any Ivan's books.", result)

    def test_rent_book_title_not_exist(self):
        self.library.add_book("Ivan", "TestTitle")
        self.library.add_reader("TestReader")
        result = self.library.rent_book("TestReader", "Ivan", "TitleNotExist")

        self.assertEqual(f"""Test Library does not have Ivan's "TitleNotExist".""", result)

    def test_rent_book(self):
        self.library.add_book("Ivan", "TestTitle")
        self.library.add_reader("TestReader")
        self.library.rent_book("TestReader", "Ivan", "TestTitle")

        self.assertDictEqual({"TestReader": [{"Ivan": "TestTitle"}]}, self.library.readers)
        self.assertDictEqual({"Ivan": []}, self.library.books_by_authors)
Ejemplo n.º 2
0
class TestLibrary(TestCase):
    def setUp(self):
        self.lib = Library("Test")

    def test_init(self):
        self.assertEqual("Test", self.lib.name)
        self.assertEqual({}, self.lib.books_by_authors)
        self.assertEqual({}, self.lib.readers)

    def test_add_invalid_name(self):
        with self.assertRaises(ValueError) as ex:
            self.lib.name("")
        self.assertEqual("Name cannot be empty string!", str(ex.exception))

    def test_add_book_to_dict(self):
        self.assertEqual({}, self.books_by_authors)
        self.lib.add_book("Stephen King", ["It"])
        self.assertEqual({"Stephen King": ["It"]}, self.lib.books_by_authors)
        self.lib.add_book("Stephen King", ["The Green Mile"])
        self.assertEqual({"Stephen King": ["It", "The Green Mile"]}, self.lib.books_by_authors)

    def test_add_reader(self):
        self.assertEqual({}, self.readers)
        self.lib.add_reader("Vako")
        self.assertEqual({"Vako": []}, self.lib.readers)

    def test_add_reader_already_registered(self):
        self.assertEqual({}, self.readers)
        self.lib.add_reader("Vako")
        self.assertEqual({"Vako": []}, self.lib.readers)
        result = self.lib.add_reader("Vako")
        self.assertEqual("Vako is already registered in the 'Test' library.", result)

    def test_rent_a_book_reader_not_in_library(self):
        self.assertEqual({}, self.readers)
        result = self.lib.rent_book("Test_reader_name", "test_book_author", "test_book_title")
        self.assertEqual("Test_reader_name is not registered in the Test Library.", result)

    def test_rent_a_book_author_not_in_library(self):
        self.assertEqual({}, self.books_by_authors)
        result = self.lib.rent_book("Test_reader_name", "test_book_author", "test_book_title")
        self.assertEqual("Test Library does not have any test_book_author's books.", result)

    def test_rent_a_book_title_not_in_library(self):
        self.assertEqual({"Vako": []}, self.readers)
        self.assertEqual({"Stephen King": ["It"]}, self.books_by_authors)
        result = self.lib.rent_book("Vako", "Stephen King", "The Green Mile")
        self.assertEqual("""Test Library does not have Stephen King's "The Green Mile".""", result)

    def test_rent_a_book_successes(self):
        self.assertEqual({"Vako": []}, self.readers)
        self.assertEqual({"Stephen King": ["It"]}, self.books_by_authors)
        self.lib.rent_book("Vako", "Stephen King", "It")
        self.assertEqual({"Vako": [{"Stephen King": "It"}]}, self.readers)
        self.assertEqual({}, self.books_by_authors)
Ejemplo n.º 3
0
class TestLibrary(unittest.TestCase):
    def test__init__(self):
        self.library = Library("Alex")
        self.assertEqual(self.library.name, "Alex")
        self.assertEqual(self.library.books_by_authors, {})
        self.assertEqual(self.library.readers, {})

    def test__init__name_empty_string(self):
        with self.assertRaises(ValueError) as err:
            self.library = Library("")

        msg = "Name cannot be empty string!"
        self.assertEqual(str(err.exception), msg)

    def test_method_add_book_happy_case(self):
        self.library = Library("Alex")
        self.library.add_book("King", "It")
        self.assertEqual(self.library.books_by_authors["King"], ["It"])

    def test_method_add_book_author_already_in_books_by_author(self):
        self.library = Library("Alex")
        self.library.add_book("King", "It")
        self.library.add_book("King", "Shining")
        self.assertEqual(self.library.books_by_authors["King"],
                         ["It", "Shining"])

    def test_add_book_method_books_already_in_books_by_authors(self):
        self.library = Library("Alex")
        self.library.add_book("King", "It")
        self.library.add_book("King", "It")
        self.library.add_book("Jones", "It")
        self.assertEqual(self.library.books_by_authors["King"], ["It"])
        self.assertEqual(self.library.books_by_authors["Jones"], ["It"])

    def test_method_add_reader_happy_case(self):
        self.library = Library("Alex")
        self.library.add_reader("Miro")
        self.assertEqual(self.library.readers, {"Miro": []})

    def test_method_add_reader_username_already_registered(self):
        self.library = Library("Alex")
        self.library.add_reader("Miro")
        self.assertEqual(self.library.add_reader("Miro"),
                         "Miro is already registered in the Alex library.")

    def test_method_rent_book_happy_case(self):
        self.library = Library("Alex")
        self.library.add_reader("Miro")
        self.library.add_book("King", "It")
        # book_index = self.library.books_by_authors["King"].index("It")
        self.library.rent_book("Miro", "King", "It")
        self.assertEqual(self.library.readers, {"Miro": [{"King": "It"}]})
        self.assertEqual(self.library.books_by_authors, {"King": []})

    def test_method_rent_book_reader_not_in_readers(self):
        self.library = Library("Alex")
        self.assertEqual(self.library.rent_book("Miro", "King", "It"),
                         "Miro is not registered in the Alex Library.")

    def test_method_rent_book_author_not_in_books_by_author(self):
        self.library = Library("Alex")
        self.library.add_reader("Miro")
        self.assertEqual(self.library.rent_book("Miro", "King", "It"),
                         "Alex Library does not have any King's books.")

    def test_method_rent_book_book_title_not_in_books_by_author(self):
        self.library = Library("Alex")
        self.library.add_reader("Miro")
        self.library.add_book("King", "It")
        self.assertEqual(self.library.rent_book("Miro", "King", "Shining"),
                         """Alex Library does not have King's "Shining".""")
Ejemplo n.º 4
0
class LibraryTest(TestCase):
    def setUp(self) -> None:
        self.library = Library('AI')

    def test_initialization(self):
        self.assertEqual('AI', self.library.name)
        self.assertEqual({}, self.library.books_by_authors)
        self.assertEqual({}, self.library.readers)

    def test_name_property(self):
        self.library.name = 'Are'
        self.assertEqual('Are', self.library.name)

    def test_name_property_raise(self):
        expected = "Name cannot be empty string!"
        with self.assertRaises(ValueError) as ex:
            self.library.name = ''
        self.assertEqual(expected, str(ex.exception))

    def test_add_book(self):
        self.assertEqual({}, self.library.books_by_authors)
        self.library.add_book('AZ', 'Kniga')
        self.assertEqual({'AZ': ['Kniga']}, self.library.books_by_authors)

    def test_add_reader(self):
        self.assertEqual({}, self.library.readers)
        self.library.add_reader('Pesho')
        self.assertEqual({'Pesho': []}, self.library.readers)

    def test_add_reader_msg(self):
        self.assertEqual({}, self.library.readers)
        self.library.add_reader('Pesho')
        self.assertEqual({'Pesho': []}, self.library.readers)
        expected = "Pesho is already registered in the AI library."
        result = self.library.add_reader('Pesho')
        self.assertEqual(expected, result)

    def test_rent_book_reader_not_there(self):
        expected = "Pesho is not registered in the AI Library."
        result = self.library.rent_book('Pesho', 'AZ', 'Kniga')
        self.assertEqual(expected, result)

    def test_rent_book_author_not_there(self):
        self.library.add_reader('Pesho')
        expected = "AI Library does not have any AZ's books."
        result = self.library.rent_book('Pesho', 'AZ', 'Kniga')
        self.assertEqual(expected, result)

    def test_rent_book_book_not_there(self):
        self.library.add_reader('Pesho')
        self.library.add_book('AZ', 'Kniga')
        expected = """AI Library does not have AZ's "Kniga2"."""
        result = self.library.rent_book('Pesho', 'AZ', 'Kniga2')
        self.assertEqual(expected, result)

    def test_rent_book_working(self):
        self.library.add_reader('Pesho')
        self.library.add_book('AZ', 'Kniga')
        self.assertEqual({'Pesho': []}, self.library.readers)
        self.assertEqual({'AZ': ['Kniga']}, self.library.books_by_authors)
        self.library.rent_book('Pesho', 'AZ', 'Kniga')
        self.assertEqual({'Pesho': [{'AZ': 'Kniga'}]}, self.library.readers)

    def test_rent_book_removing_book(self):
        self.library.add_reader('Pesho')
        self.library.add_book('AZ', 'Kniga')
        self.assertEqual({'Pesho': []}, self.library.readers)
        self.assertEqual({'AZ': ['Kniga']}, self.library.books_by_authors)
        self.library.rent_book('Pesho', 'AZ', 'Kniga')
        self.assertEqual({'Pesho': [{'AZ': 'Kniga'}]}, self.library.readers)
        self.assertEqual({'AZ': []}, self.library.books_by_authors)