def test_bookmarker(self): with test_db.atomic(): number = 6555 title = "Happy Eyeballs test" text = "testing bookmark" category = "Best Current Practice" bookmark = False Data.create( number=number, title=title, text=text, category=category, bookmark=bookmark, ) DataIndex.create(rowid=number, title=title, text=text, category=category) query = Data.select().where(Data.number == 6555) for result in query: self.assertEqual(result.bookmark, False) Data.insert( title=result.title, text=result.text, number=result.number, category=result.category, bookmark=1, ).on_conflict("replace").execute() new = Data.select().where(Data.number == 6555) for result in new: self.assertEqual(result.bookmark, True)
def test_delete_bookmark(self): exists = Data.select().where(Data.bookmark == 1 and Data.number == 7540) for result in exists: self.assertEqual(result.bookmark, True) Data.insert( title=result.title, text=result.text, number=result.number, category=result.category, bookmark=0, ).on_conflict("replace").execute() new = Data.select().where(Data.bookmark == 1 and Data.number == 7540) for x in new: self.assertEqual(x.bookmark, False)
def test_keyword_search_returns_null(self): phrase = "wolf" query = (Data.select().join(DataIndex, on=(Data.number == DataIndex.rowid)).where( DataIndex.match(phrase)).order_by( DataIndex.bm25())) for result in query: print(result.title) self.assertIsNone(result.title) self.assertEqual(result.title, "")
def search_bookmarks(): """Print list of bookmarked RFC's""" print_by_bookmark() print("[*] All Bookmarked RFC's[*]") print() query = Data.select().where(Data.bookmark == 1) for result in query: print(f"\t{Color.OKBLUE}RFC {result.number} - {Color.NOTICE}" f"{result.title[5:]}{Color.END}") search_by_number()
def test_search_by_keyword(self): phrase = "HTTP" query = (Data.select().join(DataIndex, on=(Data.number == DataIndex.rowid)).where( DataIndex.match(phrase)).order_by( DataIndex.bm25())) expected = "Hypertext Transfer Protocol 2 (HTTP/2)" for result in query: self.assertTrue(result.title, phrase) self.assertEqual(result.title, expected) self.assertNotEqual(result.title, "HTTPS") self.assertNotEqual(result.title, "DNS")
def latest(): """Get the most recent RFC's returning ten by default but the user can specify a set number to see. :arg number (default=10) user can set how many to retrieve.""" print_get_latest() query = Data.select().order_by(Data.title.desc()).limit(10) # this is a slow lookup - needs optimisation. for result in query: print(f"\t{Color.OKBLUE}RFC {result.number} - {Color.NOTICE}" f"{result.title[5:]}{Color.END}") search_by_number()
def search_by_keyword(): """User can enter keywords to search for RFC's - only parses the title. Prints all matches with RFC number - user can then enter which RFC number to view, if any, or return to Home Page. """ print_by_keyword() print("[*] Enter Keyword/s [http/2 hpack]") phrase = input(f"{prompt}") phrase = sanitize_inputs(phrase) query = (Data.select().join( DataIndex, on=(Data.number == DataIndex.rowid)).where( DataIndex.match(phrase)).order_by(DataIndex.bm25())) try: for results in query: print( f"{Color.OKBLUE}Matches:{Color.NOTICE} RFC {results.title[:5]}" f"{Color.HEADER}- {results.title[5:]}{Color.END}") print() search_by_number() except OperationalError: print("[!!] Database lookup error! [!!]")
def test_insert_new_rows(self): with test_db.atomic(): number = 1918 title = "Address Allocation for Private Internets" text = """For the purposes of this document, an enterprise is an entity autonomously operating a network using TCP/IP and in particular determining the addressing plan and address assignments within that network.""" category = "Best Current Practice" bookmark = False Data.create( number=number, title=title, text=text, category=category, bookmark=bookmark, ) DataIndex.create(rowid=number, title=title, text=text, category=category) expected_title = [ "Hypertext Transfer Protocol 2 (HTTP/2)", "Address Allocation for Private Internets", ] expected_number = [1918, 7540] actual_title = list() actual_number = list() for row in Data.select(): actual_title.append(row.title) actual_number.append(row.number) self.assertCountEqual(actual_title, expected_title) self.assertCountEqual(actual_number, expected_number) self.assertNotEqual(actual_number, [123, 345])
def update_bookmarks(): """Updates the Bookmark row in database for selected RFC.""" print("[!] Select bookmark to delete [!]") print() query = Data.select().where(Data.bookmark == 1) for result in query: print(f"\t{Color.OKBLUE}RFC {result.number} - {Color.NOTICE}" f"{result.title[5:]}{Color.END}") print() print("[*] Enter Bookmark to delete by number [eg. 8305] [*]") print("[*] OR Press [Enter] for Home Page [*]") choice = input(prompt) if choice.isdigit(): update = Data(number=choice, bookmark=0) update.save() update_bookmarks() print() elif choice == "" or choice == "q": home_page() else: print("\n[!] Please enter a valid number! [!]") print() update_bookmarks()
def test_search_by_number(self): query = Data.select().where(Data.number == 7540) for result in query: self.assertTrue(result, "7540")
def test_number_does_not_exist(self): query = Data.select().where(Data.number == 8305) for result in query: self.assertFalse(result, "7540")
def test_search_by_bookmark(self): query = Data.select().where(Data.bookmark == 1) for result in query: self.assertEqual(result.number, 7540) self.assertNotEqual(result.number, 8305)
def random_rfc(): """Randomly selects a RFC.""" random = Data.select().order_by(fn.Random()).limit(1) for record in random: pager(record.text) bookmarker(random)