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 setUp(self): """Setup the database and create one entry, including the sqlite3 full-text search. Database is peewee ORM.""" test_db.bind([Data, DataIndex], bind_refs=False, bind_backrefs=False) test_db.connect() test_db.create_tables([Data, DataIndex]) with test_db.atomic(): number = 7540 title = "Hypertext Transfer Protocol 2 (HTTP/2)" text = """This specification describes an optimized expression of the semantics of the Hypertext Transfer Protocol (HTTP), referred to as HTTP version 2 (HTTP/2). HTTP/2 enables a more efficient use of network""" category = "Standards Track" bookmark = True Data.create( number=number, title=title, text=text, category=category, bookmark=bookmark, ) DataIndex.create(rowid=number, title=title, text=text, category=category)
def write_to_db(): """Write the contents of files to sqlite database. function will run each time the database is updated. Relies on RFC number as the Primary Key to issue Unique Key Constraint which prohibits duplicate RFC's being written to DB. Writes the following to models.Data (and its Virtual Table; DataIndex) :arg number: RFC number taken from filename <rfc1918.txt> :arg title: RFC Title taken from rfc-index.txt and mapped against number :arg text: body of the document parsed for reading in terminal :arg category: category type taken from document :arg bookmark: boolean, if bookmarked returns 1 (True), default=0 Removes folder containing all text files post write. """ create_tables() print("..Beginning database writes..") title_list = get_title_list() for file in strip_extensions(): with open(os.path.join(Config.STORAGE_PATH, file), errors="ignore") as f: f = f.read().strip() try: number = file.strip(".txt").strip("rfc") title = map_title_from_list(number, title_list) body = f category = get_categories(f) bookmark = False with db.atomic(): Data.create( number=number, title=title, text=body, category=category, bookmark=bookmark, ) DataIndex.create(rowid=number, title=title, text=body, category=category) except IntegrityError as e: logging.debug(f"Integrity Error: {e} Raised at {number}") pass except AttributeError or ValueError as e: logging.debug(f"{e}: hit at RFC {file}") pass else: remove_rfc_files() print("Successfully finished importing all files to database.") print("Now removing unnecessary files from disk....") print("...Done!")
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 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 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 test_tables_exist(self): self.assertTrue(Data.table_exists()) self.assertTrue(DataIndex.table_exists())