Exemplo n.º 1
0
    def test_session_key_migration(self):

        conf = config.new(
            {"general": {
                "dbpath": "/dev/null",
                "max-age": "1h"
            }})
        conf.set("general", "session-key", "supersecretkey")

        with sqlite3.connect(self.path) as con:
            con.execute("PRAGMA user_version = 1")
            con.execute("CREATE TABLE threads (id INTEGER PRIMARY KEY)")

        db = SQLite3(self.path, conf)

        self.assertEqual(db.version, SQLite3.MAX_VERSION)
        self.assertEqual(db.preferences.get("session-key"),
                         conf.get("general", "session-key"))

        # try again, now with the session-key removed from our conf
        conf.remove_option("general", "session-key")
        db = SQLite3(self.path, conf)

        self.assertEqual(db.version, SQLite3.MAX_VERSION)
        self.assertEqual(db.preferences.get("session-key"), "supersecretkey")
    def test_wordpress(self):

        xml = join(dirname(__file__), "wordpress.xml")
        xxx = tempfile.NamedTemporaryFile()

        db = SQLite3(xxx.name, conf)
        WordPress(db, xml).migrate()

        self.assertEqual(db.threads["/2014/test/"]["title"], "Hello, World…")
        self.assertEqual(db.threads["/2014/test/"]["id"], 1)

        self.assertEqual(db.threads["/?p=4"]["title"], "...")
        self.assertEqual(db.threads["/?p=4"]["id"], 2)

        self.assertEqual(len(db.execute("SELECT id FROM threads").fetchall()),
                         2)
        self.assertEqual(len(db.execute("SELECT id FROM comments").fetchall()),
                         7)

        first = db.comments.get(1)
        self.assertEqual(first["author"], "Ohai")
        self.assertEqual(first["text"], "Erster!1")
        self.assertEqual(first["remote_addr"], "82.119.20.0")

        second = db.comments.get(2)
        self.assertEqual(second["author"], "Tester")
        self.assertEqual(second["text"], "Zweiter.")

        for i in (3, 4, 5):
            self.assertEqual(db.comments.get(i)["parent"], second["id"])

        last = db.comments.get(6)
        self.assertEqual(last["author"], "Letzter :/")
        self.assertEqual(last["parent"], None)
Exemplo n.º 3
0
    def test_generic(self):
        filepath = join(dirname(__file__), "generic.json")
        tempf = tempfile.NamedTemporaryFile()

        db = SQLite3(tempf.name, conf)
        Generic(db, filepath).migrate()

        self.assertEqual(db.threads["/posts/0001/"]["title"], "Test+post")
        self.assertEqual(db.threads["/posts/0001/"]["id"], 1)

        self.assertEqual(db.threads["/posts/0007/"]["title"], "Nat+%26+Miguel")
        self.assertEqual(db.threads["/posts/0007/"]["id"], 2)

        self.assertEqual(len(db.execute("SELECT id FROM threads").fetchall()),
                         2)
        self.assertEqual(len(db.execute("SELECT id FROM comments").fetchall()),
                         2)

        comment = db.comments.get(1)
        self.assertEqual(comment["author"], "texas holdem")
        self.assertEqual(comment["text"],
                         "Great men can't be ruled. by free online poker")
        self.assertEqual(comment["email"], "")
        self.assertEqual(comment["website"], "http://www.tigerspice.com")
        self.assertEqual(comment["remote_addr"], "0.0.0.0")

        comment = db.comments.get(2)
        self.assertEqual(comment["author"], "Richard Crinshaw")
        self.assertEqual(comment["text"], "Ja-make-a me crazzy mon :)\n")
        self.assertEqual(comment["email"], "*****@*****.**")
        self.assertEqual(comment["website"], "")
        self.assertEqual(comment["remote_addr"], "0.0.0.0")
Exemplo n.º 4
0
def test_generic():
    print(Path(__file__).resolve())
    # filepath = Path(__file__).parent.joinpath('isso', 'tests', 'generic.json')
    filepath = Path(__file__).parent.joinpath('generic.json')

    db = SQLite3(dbpath, conf)
    Generic(db, filepath).migrate()
Exemplo n.º 5
0
    def test_disqus_empty_id_workaround(self):
        """
        Simulate supplying --empty_id to import call to work around empty
        thread ids
        """

        xml = join(dirname(__file__), "disqus.xml")
        xxx = tempfile.NamedTemporaryFile()

        db = SQLite3(xxx.name, conf)
        Disqus(db, xml, empty_id=True).migrate()

        self.assertEqual(
            len(db.execute("SELECT id FROM comments").fetchall()), 3)

        self.assertEqual(db.threads["/"]["title"], "Hello, World!")
        self.assertEqual(db.threads["/"]["id"], 1)

        a = db.comments.get(1)

        self.assertEqual(a["author"], "peter")
        self.assertEqual(a["email"], "*****@*****.**")
        self.assertEqual(a["remote_addr"], "127.0.0.0")

        b = db.comments.get(2)
        self.assertEqual(b["parent"], a["id"])
Exemplo n.º 6
0
    def test_limit_nested_comments(self):

        tree = {
            1: None,
            2: None,
               3: 2,
                  4: 3,
                  7: 3,
               5: 2,
            6: None
        }

        with sqlite3.connect(self.path) as con:
            con.execute("PRAGMA user_version = 2")
            con.execute("CREATE TABLE threads ("
                        "    id INTEGER PRIMARY KEY,"
                        "    uri VARCHAR UNIQUE,"
                        "    title VARCHAR)")
            con.execute("CREATE TABLE comments ("
                        "    tid REFERENCES threads(id),"
                        "    id INTEGER PRIMARY KEY,"
                        "    parent INTEGER,"
                        "    created FLOAT NOT NULL, modified FLOAT,"
                        "    text VARCHAR, email VARCHAR, website VARCHAR,"
                        "    mode INTEGER,"
                        "    remote_addr VARCHAR,"
                        "    likes INTEGER DEFAULT 0,"
                        "    dislikes INTEGER DEFAULT 0,"
                        "    voters BLOB)")

            con.execute("INSERT INTO threads (uri, title) VALUES (?, ?)", ("/", "Test"))
            for (id, parent) in iteritems(tree):
                con.execute("INSERT INTO comments ("
                            "   tid, parent, created)"
                            "VALUEs (?, ?, ?)", (id, parent, id))

        conf = config.new({
            "general": {
                "dbpath": "/dev/null",
                "max-age": "1h"
            }
        })
        SQLite3(self.path, conf)

        flattened = [
            (1, None),
            (2, None),
            (3, 2),
            (4, 2),
            (5, 2),
            (6, None),
            (7, 2)
        ]

        with sqlite3.connect(self.path) as con:
            rv = con.execute("SELECT id, parent FROM comments ORDER BY created").fetchall()
            self.assertEqual(flattened, rv)
Exemplo n.º 7
0
    def test_defaults(self):

        conf = config.new(
            {"general": {
                "dbpath": "/dev/null",
                "max-age": "1h"
            }})
        db = SQLite3(self.path, conf)

        self.assertEqual(db.version, SQLite3.MAX_VERSION)
        self.assertTrue(db.preferences.get("session-key", "").isalnum())
Exemplo n.º 8
0
    def test_disqus(self):

        xml = join(dirname(__file__), "disqus.xml")
        xxx = tempfile.NamedTemporaryFile()

        db = SQLite3(xxx.name, Config.load(None))
        Disqus(db, xml).migrate()

        self.assertEqual(len(db.execute("SELECT id FROM comments").fetchall()), 2)

        self.assertEqual(db.threads["/"]["title"], "Hello, World!")
        self.assertEqual(db.threads["/"]["id"], 1)

        a = db.comments.get(1)

        self.assertEqual(a["author"], "peter")
        self.assertEqual(a["email"], "*****@*****.**")
        self.assertEqual(a["remote_addr"], "127.0.0.0")

        b = db.comments.get(2)
        self.assertEqual(b["parent"], a["id"])
Exemplo n.º 9
0
    def test_disqus_empty_id(self):
        """
        Fails with empty thread id
        """

        xml = join(dirname(__file__), "disqus.xml")
        xxx = tempfile.NamedTemporaryFile()

        db = SQLite3(xxx.name, conf)
        Disqus(db, xml, empty_id=False).migrate()

        # TODO: Convert unittest testcases with assertX to plain pytest
        # asserts, allowing capturing of stdout, like this:
        #
        # def test_disqus_empty_id(self, capfd):
        # [...]
        # out, err = capfd.readouterr()
        # assert out == \
        #     "Isso couldn't import any thread, try again with --empty-id\n"

        self.assertEqual(
            len(db.execute("SELECT id FROM comments").fetchall()), 0)
Exemplo n.º 10
0
    def test_defaults(self):

        db = SQLite3(self.path, Config.load(None))

        self.assertEqual(db.version, SQLite3.MAX_VERSION)
        self.assertTrue(db.preferences.get("session-key", "").isalnum())