def test_case_6(): shutil.copy(test4_db, test_new_db) db_config.init_db(test_new_db) l1_data = get_ln_data(1) auth.Auth.login(l1_data["login"], l1_data["password"]) p1 = user_manager.User.get( user_manager.User.name == patrons_data["p1"]["name"]) p2 = user_manager.User.get( user_manager.User.name == patrons_data["p2"]["name"]) p3 = user_manager.User.get( user_manager.User.name == patrons_data["p3"]["name"]) s = user_manager.User.get( user_manager.User.name == patrons_data["s"]["name"]) v = user_manager.User.get( user_manager.User.name == patrons_data["v"]["name"]) d3 = doc_manager.Book.get( doc_manager.Book.title == docs_data["books"]["d3"]["title"]) bsystem = booking_system.Booking_system(l1_data["login"]) bsystem.check_out(d3, p1) bsystem.check_out(d3, p2) bsystem.check_out(d3, s) bsystem.check_out(d3, v) bsystem.check_out(d3, p3) access_error = False try: bsystem.outstanding_request(d3, p3) except auth.AccessError: access_error = True assert access_error
def test_case_1(self): prepare_database() # Sample group group1 = group_manager.Group.add({ "name": "Group", "book_ct": 2, "book_bestseller_ct": 1, "journal_ct": 2, "av_ct": 2 }) # Sample user user1 = user_manager.User.add({ "name": "User", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group1 }) # Sample book book1 = doc_manager.Book.add({ "title": "title", "author": "author", "cost": 300, "keywords": "keywords", "edition": "edition", "publisher": "publisher", "year": 2000 }) # Book's copies b1_copy1 = doc_manager.Copy.add(book1) b1_copy2 = doc_manager.Copy.add(book1) # Checkout first copy to a user bsystem = booking_system.Booking_system() bsystem.check_out(user1, b1_copy1, "Librarian") # Get copies and make sure there are only two of them b1_copies = book1.get_document_copies() self.assertTrue(len(b1_copies) == 2) b1_copy1_after = b1_copies[0] b1_copy2_after = b1_copies[1] # Make sure that user's story contains only one entry user1_history = bsystem.get_user_history(user1) self.assertTrue(len(user1_history) == 1) # Make sure that copy given to user and this is one of the existing copies uh = user1_history[0] self.assertTrue(uh.user.card_id == user1.card_id) self.assertTrue((b1_copy1_after.CopyID == uh.copy.CopyID) or (b1_copy2_after.CopyID == uh.copy.CopyID)) self.assertTrue((b1_copy1_after.CopyID != uh.copy.CopyID) or (b1_copy2_after.CopyID != uh.copy.CopyID))
def test_case_8(self): self.test_case_1() p1 = user_manager.User.get_by_id(1000) b1 = doc_manager.Book.get_by_id(1) b2 = doc_manager.Book.get_by_id(2) p2 = user_manager.User.get_by_id(1001) av1 = doc_manager.AVMaterial.get_by_id(1) bs = booking_system.Booking_system() h1 = bs.check_out(p1, b2.get_document_copies()[0], 'Mr. Libro')[1] h1.date_check_out = '2018-02-02' h1.save() h3 = bs.check_out(p2, b1.get_document_copies()[1], 'Mr. Libro')[1] h3.date_check_out = '2018-02-05' h3.save() h2 = bs.check_out(p1, b1.get_document_copies()[0], 'Mr. Libro')[1] h2.date_check_out = '2018-02-09' h2.save() h4 = bs.check_out(p2, av1.get_document_copies()[0], 'Mr. Libro')[1] h4.date_check_out = '2018-02-17' h4.save() p1 = user_manager.User.get_by_id(1000) p2 = user_manager.User.get_by_id(1001) ans1 = [] ans2 = [] p1_history = bs.get_user_history(p1) for entry in p1_history: if (bs.check_overdue(entry) > 0): t = (entry.copy.get_doc().title, bs.check_overdue(entry) // 100) ans1.append(t) p2_history = bs.get_user_history(p2) for entry in p2_history: if (bs.check_overdue(entry) > 0): t = (entry.copy.get_doc().title, bs.check_overdue(entry) // 100) ans2.append(t) today = datetime.date.today() self.assertEqual(ans1[0][1], min(3 + (today - datetime.date(2018, 3, 5)).days, 3000)) self.assertEqual(ans1[0][0], b2.title) self.assertEqual(ans2[0][1], min(7 + (today - datetime.date(2018, 3, 5)).days, 3000)) self.assertEqual(ans2[0][0], b1.title) self.assertEqual(ans2[1][1], min(2 + (today - datetime.date(2018, 3, 5)).days, 3000)) self.assertEqual(ans2[1][0], av1.title)
def test_case_9(self): prepare_database() faculty = group_manager.Group.add({ "name": "Faculty", "book_ct": 4, "book_bestseller_ct": 4, "journal_ct": 2, "av_ct": 2 }) students = group_manager.Group.add({ "name": "Students", "book_ct": 3, "book_bestseller_ct": 2, "journal_ct": 2, "av_ct": 2 }) f = user_manager.User.add({ "name": "Faculty", "surname": "Member", "address": "Moscow, MSU", "phone": 84959391000, "group": faculty }) s = user_manager.User.add({ "name": "Faculty", "surname": "Member", "address": "Moscow, MSU, ", "phone": 88005553535, "group": students }) b = doc_manager.Book.add({ "title": "Book", "author": "author", "cost": 300, "keywords": "keywords, best seller", "edition": "edition", "publisher": "publisher", "year": 2000 }) bc = doc_manager.Copy.add(b) bsystem = booking_system.Booking_system() bsystem.check_out(f, bc, "Librarian") fh = bsystem.get_user_history(f) self.assertTrue(len(fh) == 1) fh = fh[0] return_time = datetime.datetime.strptime( bsystem.get_max_return_time(fh), "%Y-%m-%d").date() print((return_time - datetime.date.today()).days) self.assertTrue((return_time - datetime.date.today()).days >= 7 * 2)
def test_case_10(self): prepare_database() # Sample group group = group_manager.Group.add({ "name": "Group", "book_ct": 2, "book_bestseller_ct": 1, "journal_ct": 2, "av_ct": 2 }) # Sample user user = user_manager.User.add({ "name": "User1", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group }) a = doc_manager.Book.add({ "title": "BookA", "author": "author", "cost": 300, "keywords": "keywords", "edition": "edition", "publisher": "publisher", "year": 2000 }) a_copy = doc_manager.Copy.add(a) b = doc_manager.Book.add({ "title": "BookB", "author": "author", "cost": 300, "keywords": "keywords, reference", "edition": "edition", "publisher": "publisher", "year": 2000 }) b_copy = doc_manager.Copy.add(b) bsystem = booking_system.Booking_system() res1, history1 = bsystem.check_out(user, a_copy, "Librarian") res2, history2 = bsystem.check_out(user, b_copy, "Librarian") self.assertEqual(res1, 0) self.assertEqual(res2, 2) self.assertNotEqual(history1, None) self.assertEqual(history2, None)
def test_case_6(self): self.test_case_2() bsystem = booking_system.Booking_system() p1 = user_manager.User.get_by_id(1000) p3 = user_manager.User.get_by_id(1002) b1_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.b1) if x.active and not x.checked_out] res = bsystem.check_out(p1, b1_not_checked_copies[0], "Librarian") # successfully checked out b1 to p1 self.assertEqual(res[0], 0) b1_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.b1) if not x.checked_out] b2_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.b2) if not x.checked_out] res = bsystem.check_out(p3, b1_not_checked_copies[0], "Librarian") # error while checking out b1 to p3 self.assertEqual(res[0], 3) res = bsystem.check_out(p3, b2_not_checked_copies[0], "Librarian") # successfully checked out b2 to p3 self.assertEqual(res[0], 0) self.assertEqual(p1.name + " " + self.p1.surname, "Sergey Afonso") self.assertEqual(p1.address, "Via Margutta, 3") self.assertEqual(p1.phone, 30001) self.assertEqual(p1.card_id, 1000) self.assertEqual(p1.group, self.g_f) self.assertEqual(len(p1.operations), 1) p1_h = p1.operations[0] p1_d = p1_h.copy.get_doc() self.assertEqual(p1_d.DocumentID, self.b1.DocumentID) ret_time = bsystem.get_max_return_time(p1_h) delay = (datetime.datetime.strptime(ret_time, "%Y-%m-%d").date() - datetime.date.today()).days self.assertEqual(delay, 28) p3 = user_manager.User.get_by_id(1002) self.assertEqual(p3.name + " " + self.p3.surname, "Elvira Espindola") self.assertEqual(p3.address, "Via del Corso, 22") self.assertEqual(p3.phone, 30003) self.assertEqual(p3.card_id, 1002) self.assertEqual(p3.group, self.g_s) self.assertEqual(len(p3.operations), 1) p3_h = p3.operations[0] p3_d = p3_h.copy.get_doc() self.assertEqual(p3_d.DocumentID, self.b2.DocumentID) ret_time = bsystem.get_max_return_time(p3_h) delay = (datetime.datetime.strptime(ret_time, "%Y-%m-%d").date() - datetime.date.today()).days self.assertEqual(delay, 14)
def test_case_7(): global notified notified = False def check_notification(code, msg): global notified notified = True shutil.copy(test4_db, test_new_db) db_config.init_db(test_new_db) l3_data = get_ln_data(3) auth.Auth.login(l3_data["login"], l3_data["password"]) p1 = user_manager.User.get( user_manager.User.name == patrons_data["p1"]["name"]) p2 = user_manager.User.get( user_manager.User.name == patrons_data["p2"]["name"]) p3 = user_manager.User.get( user_manager.User.name == patrons_data["p3"]["name"]) s = user_manager.User.get( user_manager.User.name == patrons_data["s"]["name"]) v = user_manager.User.get( user_manager.User.name == patrons_data["v"]["name"]) d3 = doc_manager.Book.get( doc_manager.Book.title == docs_data["books"]["d3"]["title"]) bsystem = booking_system.Booking_system(l3_data["login"]) bsystem.check_out(d3, p1) bsystem.check_out(d3, p2) bsystem.check_out(d3, s) bsystem.check_out(d3, v) bsystem.check_out(d3, p3) d3_c = d3.get_document_copies() access_error = False try: res = bsystem.outstanding_request(d3, p3) except auth.AccessError: access_error = True assert not access_error sleep(1) event_manager.register_listener("task_ended", check_notification) task_manager.timer_function() assert notified
def test_case_7(self): group1 = group_manager.Group.add({ "name": "Group", "book_ct": 2, "book_bestseller_ct": 1, "journal_ct": 2, "av_ct": 2 }) # Sample users p1 = user_manager.User.add({ "name": "User1", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group1 }) p2 = user_manager.User.add({ "name": "User2", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group1 }) # Sample book b1 = doc_manager.Book.add({ "title": "title", "author": "author", "cost": 300, "keywords": "keywords", "edition": "edition", "publisher": "publisher", "year": 2000 }) b1_copy1 = doc_manager.Copy.add(b1) b1_copy2 = doc_manager.Copy.add(b1) bsystem = booking_system.Booking_system() res1, history1 = bsystem.check_out(p1, b1_copy1, "Librarian") res2, history2 = bsystem.check_out(p2, b1_copy2, "Librarian") self.assertEqual(res1, 0) self.assertEqual(res2, 0) self.assertNotEqual(history1, None) self.assertNotEqual(history2, None)
def test_case_6(self): prepare_database() # Sample group group = group_manager.Group.add({ "name": "Group", "book_ct": 2, "book_bestseller_ct": 1, "journal_ct": 2, "av_ct": 2 }) # Sample user user = user_manager.User.add({ "name": "User1", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group }) book = doc_manager.Book.add({ "title": "Book", "author": "author", "cost": 300, "keywords": "keywords", "edition": "edition", "publisher": "publisher", "year": 2000 }) copy1 = doc_manager.Copy.add(book) copy2 = doc_manager.Copy.add(book) bsystem = booking_system.Booking_system() # Copy successfuly checked out res, history = bsystem.check_out(user, copy1, "Librarian") self.assertEqual(res, 0) # Cannot check out another copy res, history = bsystem.check_out(user, copy2, "Librarian") self.assertEqual(res, 6)
def test_case_1(self): #db init values db_config.init_db('test1.db') db_config.drop_tables() db_config.create_tables() patron = group_manager.Group.create(name = 'patrons', book_ct = 3, book_bestseller_ct = 2, journal_ct = 2, av_ct = 2) user = user_manager.User.add(name = 'user_name', surname = 'user_surname', address = 'user_address', phone = 11111111111, group = patron) book = doc_manager.Book.create(title = 'Wonderful book', author = 'Nice Author', cost = 500, keywords = 'key', edition = 1, publisher = 'Good publisher', year = 2018) copy2 = doc_manager.Copy.add(book) copy1 = doc_manager.Copy.add(book) #test operations bs = booking_system.Booking_system() bs.check_out(user, copy1, 'mr. Librarian') #check values checked_out_copies = 0 not_checked_out_copies = 0 for entry in bs.get_document_copies(book): if (entry.checked_out): checked_out_copies += 1 else: not_checked_out_copies += 1 self.assertEqual(checked_out_copies, 1) self.assertEqual(not_checked_out_copies, 1)
def test_case_5(self): prepare_database() # Sample group group1 = group_manager.Group.add({ "name": "Group", "book_ct": 2, "book_bestseller_ct": 1, "journal_ct": 2, "av_ct": 2 }) # Sample users users = [] users.append( user_manager.User.add({ "name": "User1", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group1 })) users.append( user_manager.User.add({ "name": "User2", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group1 })) users.append( user_manager.User.add({ "name": "User3", "surname": "Userovich", "address": "Pushkin street, Kolotushkin building", "phone": 88005553535, "group": group1 })) book = doc_manager.Book.add({ "title": "Book", "author": "author", "cost": 300, "keywords": "keywords", "edition": "edition", "publisher": "publisher", "year": 2000 }) doc_manager.Copy.add(book) doc_manager.Copy.add(book) b_copies = book.get_document_copies() bsystem = booking_system.Booking_system() for i, user in enumerate(users): b_not_checked_copies = [x for x in b_copies if not x.checked_out] print(b_not_checked_copies) if i != 2: # still have copies self.assertTrue(len(b_not_checked_copies) != 0) # checkout one of the copies to a user copy = b_not_checked_copies[0] bsystem.check_out(user, copy, "Librarian") else: # there is no free copies self.assertTrue(len(b_not_checked_copies) == 0)
def test_case_7(self): self.test_case_1() bsystem = booking_system.Booking_system() p1 = user_manager.User.get_by_id(1001) b1_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.b1) if not x.checked_out] b2_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.b2) if not x.checked_out] b3_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.b3) if not x.checked_out] av1_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.av1) if not x.checked_out] av2_not_checked_copies = [x for x in doc_manager.Book.get_document_copies(self.av2) if not x.checked_out] res = bsystem.check_out(self.p1, b1_not_checked_copies[0], "Librarian") self.assertEqual(res[0], 0) res = bsystem.check_out(self.p1, b2_not_checked_copies[0], "Librarian") self.assertEqual(res[0], 0) # Cannot checkout: is reference res = bsystem.check_out(self.p1, b3_not_checked_copies[0], "Librarian") self.assertEqual(res[0], 2) res = bsystem.check_out(self.p1, av1_not_checked_copies[0], "Librarian") self.assertEqual(res[0], 0) res = bsystem.check_out(self.p2, b1_not_checked_copies[1], "Librarian") self.assertEqual(res[0], 0) res = bsystem.check_out(self.p2, b2_not_checked_copies[1], "Librarian") self.assertEqual(res[0], 0) res = bsystem.check_out(self.p2, av2_not_checked_copies[0], "Librarian") self.assertEqual(res[0], 0) p1 = user_manager.User.get_by_id(1000) self.assertEqual(p1.name + " " + self.p1.surname, "Sergey Afonso") self.assertEqual(p1.address, "Via Margutta, 3") self.assertEqual(p1.phone, 30001) self.assertEqual(p1.card_id, 1000) self.assertEqual(p1.group, self.g_f) p1_operations = list(p1.operations) p1_docs = {self.b1: 4, self.b2: 4, self.av1: 2} # documents which user should have for p1_shd in p1_docs.keys(): # documents which user actually have have = False for p1_h in p1_operations: p1_d = p1_h.copy.get_doc() if type(p1_d) == type(p1_shd) and p1_d.DocumentID == p1_shd.DocumentID: self.assertFalse(have) ret_time = bsystem.get_max_return_time(p1_h) delay = datetime.datetime.strptime(ret_time, "%Y-%m-%d").date() - datetime.date.today() self.assertEqual(delay.days, 7*p1_docs[p1_shd]) have = True self.assertTrue(have) p2 = user_manager.User.get_by_id(1001) self.assertEqual(p2.name + " " + self.p2.surname, "Nadia Teixeira") self.assertEqual(p2.address, "Via Sacra, 13") self.assertEqual(p2.phone, 30002) self.assertEqual(p2.card_id, 1001) self.assertEqual(p2.group, self.g_s) p2_operations = list(p2.operations) p2_docs = {self.b1: 3, self.b2: 2, self.av2: 2} # documents which user should have for p2_shd in p2_docs.keys(): # documents which user actually have have = False for p2_h in p2_operations: p2_d = p2_h.copy.get_doc() if type(p2_d) == type(p2_shd) and p2_d.DocumentID == p2_shd.DocumentID: self.assertFalse(have) ret_time = bsystem.get_max_return_time(p2_h) delay = datetime.datetime.strptime(ret_time, "%Y-%m-%d").date() - datetime.date.today() datetime.datetime.strptime(ret_time, "%Y-%m-%d").date() self.assertEqual(delay.days, 7*p2_docs[p2_shd]) have = True self.assertTrue(have)
def test_case_5(self): self.test_case_2() bsystem = booking_system.Booking_system() p2 = user_manager.User.get_by_id(1001) res = bsystem.check_out(p2, self.b1c1, "Librarian") self.assertEqual(res, (4, None))
def prepare_database(self): db_fname = "tests/test_database.db" db_config.init_db(db_fname) db_config.drop_tables() db_config.create_tables() self.g_f = group_manager.Group.get( group_manager.Group.name == 'Faculty') self.g_s = group_manager.Group.get( group_manager.Group.name == 'Students') self.g_v = group_manager.Group.get( group_manager.Group.name == 'Visiting professors') self.d1 = doc_manager.Book.add({ "title": "Introduction to Algorithms", "author": "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein", "cost": 5000, "keywords": "", "edition": "Third edition", "publisher": "MIT Press", "year": 2009 }) self.d2 = doc_manager.Book.add({ "title": "Design Patterns: Elements of Reusable Object-Oriented Software", "author": "Erich Gamma, Ralph Johnson, John Vlissides, Richard Helm", "cost": 1700, "keywords": "best seller", "edition": "First edition", "publisher": "Addison-Wesley Professional", "year": 2003 }) self.d3 = doc_manager.AVMaterial.add({ "title": "Null References: The Billion Dollar Mistake", "author": "Tony Hoare", "cost": 700, "keywords": "", }) self.p1 = user_manager.User.add({ "name": "Sergey", "surname": "Afonso", "address": "Via Margutta, 3", "phone": 30001, "group": self.g_f, "email": "*****@*****.**" }) self.p2 = user_manager.User.add({ "name": "Nadia", "surname": "Teixeira", "address": "Via Sacra, 13", "phone": 30002, "group": self.g_f, "email": "*****@*****.**" }) self.p3 = user_manager.User.add({ "name": "Elvira", "surname": "Espindola", "address": "Via del Corso, 22", "phone": 30003, "group": self.g_f, "email": "*****@*****.**" }) self.s = user_manager.User.add({ "name": "Andrey", "surname": "Velo", "address": "Avenida Mazatlan 250", "phone": 30004, "group": self.g_s, "email": "*****@*****.**" }) self.v = user_manager.User.add({ "name": "Veronika", "surname": "Rama", "address": "Stret Atocha, 27", "phone": 30005, "group": self.g_v, "email": "*****@*****.**" }) self.d1c1 = doc_manager.Copy.add(self.d1) self.d1c2 = doc_manager.Copy.add(self.d1) self.d1c3 = doc_manager.Copy.add(self.d1) self.d2c1 = doc_manager.Copy.add(self.d2) self.d2c2 = doc_manager.Copy.add(self.d2) self.d2c3 = doc_manager.Copy.add(self.d2) self.d3c1 = doc_manager.Copy.add(self.d3) self.d3c2 = doc_manager.Copy.add(self.d3) self.bsystem = booking_system.Booking_system()