Example #1
0
def db_conn():
    """Set up chimedb.core for testing with a local dummy DB."""
    (fd, rcfile) = tempfile.mkstemp(text=True)
    os.close(fd)

    # Tell chimedb where the database connection config is
    assert os.path.isfile(rcfile), "Could not find {}.".format(rcfile)

    os.environ["CHIMEDB_TEST_SQLITE"] = rcfile
    # Make sure we don't write to the actual chime database
    os.environ["CHIMEDB_TEST_ENABLE"] = "Yes, please."

    db.connect()
    db.orm.create_tables(["chimedb.dataflag.opinion"])

    # insert a user
    pwd = ":B:0000ffff:e989651ffffcb5bf9b9abedfdab58460"
    db.mediawiki.MediaWikiUser.get_or_create(user_id=user_id,
                                             user_name=user,
                                             user_password=pwd)

    # insert a user with a password hash we don't understand
    pwd = "1 2 3 4"
    db.mediawiki.MediaWikiUser.get_or_create(user_id=1,
                                             user_name=fail_user,
                                             user_password=pwd)
    db.close()

    yield

    # tear down
    os.remove(rcfile)
    def test_chimedb_test_rc(self):
        # Create an empty on-disk sqlite database
        (fd, dbfile) = tempfile.mkstemp(text=True)
        os.close(fd)

        # Create a rcfile
        (fd, rcfile) = tempfile.mkstemp(text=True)
        with os.fdopen(fd, "a") as rc:
            rc.write("""\
chimedb:
    db_type: sqlite
    db: {0}
""".format(dbfile))

        # This should be ignored
        os.environ["CHIMEDB_TEST_RC"] = rcfile

        db.test_enable()

        db.connect(read_write=True)
        db.proxy.create_tables([TestTable])
        TestTable.create(datum=datum_value)

        # Did that work?
        self.assertEqual(
            TestTable.select(TestTable.datum).scalar(), datum_value)
        db.close()

        # The on-disk sqlite database should not be empty
        stat = os.stat(dbfile)
        self.assertNotEqual(stat.st_size, 0)

        os.unlink(rcfile)
        os.unlink(dbfile)
    def test_atomic_autocommit(self):
        @db.atomic(read_write=True)
        def inside_atomic():
            TestTable.update(datum=datum_value + 1).execute()

        # Execute
        inside_atomic()

        # Check
        db.close()
        db.connect()
        self.assertEqual(
            TestTable.select(TestTable.datum).scalar(), datum_value + 1)
Example #4
0
def test_user_authentication(db_conn):
    with pytest.raises(db.exceptions.ValidationError):
        db.mediawiki.MediaWikiUser.authenticate(fail_user, password)

    with pytest.raises(UserWarning):
        db.mediawiki.MediaWikiUser.authenticate("unknown_user", password)

    with pytest.raises(UserWarning):
        db.mediawiki.MediaWikiUser.authenticate(user, "wrong_password")

    assert db.mediawiki.MediaWikiUser.authenticate(user, password) is not None

    db.close()
    def test_atomic_raise(self):
        @db.atomic(read_write=True)
        def inside_atomic():
            TestTable.update(datum=datum_value + 1).execute()

            raise RuntimeError

        # Execute
        with self.assertRaises(RuntimeError):
            inside_atomic()

        # Check
        db.close()
        db.connect()
        self.assertEqual(
            TestTable.select(TestTable.datum).scalar(), datum_value)
    def test_chimedb_test_sqlite(self):
        # Create an empty on-disk sqlite database
        (fd, dbfile) = tempfile.mkstemp(text=True)
        os.close(fd)

        os.environ["CHIMEDB_TEST_SQLITE"] = dbfile

        db.test_enable()

        db.connect(read_write=True)
        db.proxy.create_tables([TestTable])
        TestTable.create(datum=datum_value)

        # Did that work?
        self.assertEqual(
            TestTable.select(TestTable.datum).scalar(), datum_value)
        db.close()

        # The on-disk sqlite database should not be empty anymore
        stat = os.stat(dbfile)
        self.assertNotEqual(stat.st_size, 0)
Example #7
0
 def tearDown(self):
     self.patched_env.stop()
     db.close()
     os.remove(self.dbfile)
Example #8
0
 def __del__(self):
     """Stop the archiver."""
     chimedb.close()