Пример #1
0
class LibLabriryTest(unittest.TestCase):
    def setUp(self):
        self.library = Library('Lib_Test', ':memory:')


    def test_add_user__list_all_users__remove_user(self):
        self.library.add_user("Alex Test", 'RG_123123', 'Moscow, Lenin Prospect 232 - flat 17', '+755555-4444')
        self.library.add_user('Mickle O`Conner II', '12FE2322F', 'UK, London Soho Green St. 12', '+3888-2423-2432')
        self.library.add_user('I. A. Ivanov-Petrov', 'WE1231234', 'Moscow Pererva St. 12 - flat 288', '+7(915)555-22-33')
        users = self.library.list_all_users()
        self.assertEqual(3, len(users))
        pasports_id = []
        list_of_id = []
        for user in users:
            pasports_id.append(user.passport_id)
            list_of_id.append(user.user_id)
        self.assertEqual(sorted(pasports_id), sorted(['RG_123123', '12FE2322F', 'WE1231234']))
        # Check removing by ID:
        id_for_removing = list_of_id.pop()
        self.library.remove_user(id_for_removing)
        users = self.library.list_all_users()
        list_of_id_after_removing_one_user = []
        for user in users:
            list_of_id_after_removing_one_user.append(user.user_id)
        self.assertEqual(sorted(list_of_id), sorted(list_of_id_after_removing_one_user))


    def test_add_user_and_test_list_all_books(self):
        self.library.add_book('Maugli', "R. Kipling", 'RT-12342342', 'KT-12', 1882)
        self.library.add_book('Tom Sawyer', "Mark Twain", 'RT-128822', 'KT-86', 1832)
        l = self.library.list_all_books()
        # Check authors:
        self.assertEqual(sorted(['Maugli', 'Tom Sawyer']), sorted([l[0].title, l[1].title]))
        # Check year of publishing:
        self.assertEqual(sorted([1882, 1832]), sorted([l[0].year_of_publishing, l[1].year_of_publishing]))
        # Check total number of books in the library:
        self.assertEqual(2, len(l))


    def tearDown(self):
        del self.library
Пример #2
0
class Menu:

    def __init__(self):
        self.library = Library('Lib01')
        self.choice = { '1': self.show_all_books,
                        '2': self.add_book,
                        '3': self.search_by_attribute,
                        '4': self.hand_out_book,
                        '5': self.return_book,
                        '6': self.add_user,
                        '7': self.remove_user,
                        'q': self.quit,
                      }
                      
    def display_menu(self):
        print('\n1 - Show books.\n2 - Add book.\n' +
              '3 - Search book by attribute \n4 - Hand out a book\n'
              '5 - Return a book \n6 - Add user\n'
              '7 - Delete user \nq - Quit')
                      
    def run(self):
        while True:
            self.display_menu()
            choice = str(input('What is your choice? '))
            if choice in self.choice.keys():
                action = self.choice.get(choice)
                action()
            else:
                print('Can`t find such choice.')
                
    def print_list_of_books(self, list_of_books):
        if not list_of_books:
            print('- list is empty')
        else:
            print('Result(s):')
        for book in list_of_books:
            print (('Book Title: {0}\n Book author: {1}\n' + 
                   'Book Id: {2}\n Publishing year: {3}\n').format(book.title, 
                    book.author, book.book_id, book.year_of_publishing) +
                    '-'*20)
    
    def show_all_books(self):
        print('\nList of all books in the library:')
        self.print_list_of_books(self.library.list_all_books())
                    
    def add_book(self):
        title = input('Title: ')
        author = input('Author: ')
        book_code = input('Book code: ')
        group_code = input('Group code: ')
        year = input('Year of publishing: ')
        self.library.add_book(title, author, book_code, group_code, year)
        
    def search_by_attribute(self):
        print ('Available attributes to search: ' +
               'title, author, rang, year')
        attribute = input('Attribute name: ')
        attr_value = input('Attribute value: ')
        suitable_books = self.library.find_books(attribute, attr_value)
        if suitable_books == 'Err':
            print('Search parameter is incorrect!')
        else:
            print('')
            self.print_list_of_books(suitable_books)

    def add_user(self):
        name = input('Name: ')
        passport_id = input('Passport ID: ')
        address = input('Address: ')
        phone = input('Phone number: ')
        self.library.add_user(name, passport_id, address, phone)

    def remove_user(self):
        user_id = input('User id: ')
        try:
            remove_result = self.library.remove_user(user_id)
            if remove_result:
                print('User with ID ' + user_id + ' was successfully removed')
            else:
                print('Can`t remove such ID.')
        except Exception as e:
            print('Error was detected: ' + str(Exception) + ' info: ' + str(e))

    def hand_out_book(self):
        book_id = input('Book ID (for rent a book): ')
        user_id = input('User ID (renting a book):   ')
        print(self.library.hand_out_book(book_id, user_id))

    def return_book(self):
        book_id = input('Book ID (returned book): ')
        user_id = input('User ID (user who returns book): ')
        result = self.library.return_book(book_id, user_id)
        if result == 0:
             print('Book with ID: {0} was successfully returned by user with ID: {1}) . '.format(book_id, user_id))
        elif result == 1:
            print('Can`t find user with ID {0}'.format(user_id))
        elif result == 2:
            print('User with ID {0} did not lend out the book with ID {1}'.format(user_id, book_id))
        else:
            print ('Error: unknown return of function')



    def quit(self):
        print('Library closed.')
        sys.exit(0)