Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
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!")
Ejemplo n.º 4
0
    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])