Esempio n. 1
0
 def test_charset_option(self):
     uri = URI(os.environ["STORM_MYSQL_URI"])
     uri.options["charset"] = "ascii"
     database = create_database(uri)
     connection = database.connect()
     result = connection.execute("SELECT @@character_set_client")
     self.assertEquals(result.get_one(), ("ascii",))
Esempio n. 2
0
    def _create_uri(self, dbname=None):
        # postgres is a special database which is always present,
        # it was added in 8.1 which is thus our requirement'
        dbname = dbname or 'postgres'

        # Do not output the password in the logs
        if not self.first:
            log.info('connecting to %s' % self._build_dsn(
                dbname, filter_password=True))
            self.first = False

        dsn = self._build_dsn(dbname, filter_password=False)
        uri = URI(dsn)
        uri.options['isolation'] = 'read-committed'
        return uri
Esempio n. 3
0
 def setUp(self):
     super(PostgresDisconnectionTestWithPGBouncerBase, self).setUp()
     database_uri = URI(os.environ["STORM_POSTGRES_HOST_URI"])
     database_user = database_uri.username or os.environ['USER']
     database_dsn = make_dsn(database_uri)
     # Create a pgbouncer fixture.
     self.pgbouncer = pgbouncer.fixture.PGBouncerFixture()
     self.pgbouncer.databases[database_uri.database] = database_dsn
     self.pgbouncer.users[database_user] = "trusted"
     self.pgbouncer.admin_users = [database_user]
     self.useFixture(self.pgbouncer)
     # Create a Database that uses pgbouncer.
     pgbouncer_uri = database_uri.copy()
     pgbouncer_uri.host = self.pgbouncer.host
     pgbouncer_uri.port = self.pgbouncer.port
     self.database = create_database(pgbouncer_uri)
Esempio n. 4
0
    def _create_uri(self, dbname=None):
        # postgres is a special database which is always present,
        # it was added in 8.1 which is thus our requirement'
        dbname = dbname or 'postgres'

        # Do not output the password in the logs
        if not self.first:
            log.info('connecting to %s' % self._build_dsn(
                dbname, filter_password=True))
            self.first = False

        dsn = self._build_dsn(dbname, filter_password=False)
        uri = URI(dsn)
        uri.options['isolation'] = 'read-committed'

        if uri.host == "":
            pair = test_local_database()
            if pair is None:
                raise DatabaseError(
                    _("Could not find a database server on this computer"))
            uri.host = pair[0]
            uri.port = int(pair[1])

        return uri
Esempio n. 5
0
 def get_uri(self):
     """Return URI instance with a defined host and port."""
     if not self.environment_variable or not self.default_port:
         raise RuntimeError("Define at least %s.environment_variable and "
                            "%s.default_port" % (type(self).__name__,
                                                 type(self).__name__))
     uri_str = os.environ.get(self.host_environment_variable)
     if uri_str:
         uri = URI(uri_str)
         if not uri.host:
             raise RuntimeError("The URI in %s must include a host." %
                                self.host_environment_variable)
         if not uri.port:
             uri.port = self.default_port
         return uri
     else:
         uri_str = os.environ.get(self.environment_variable)
         if uri_str:
             uri = URI(uri_str)
             if uri.host:
                 if not uri.port:
                     uri.port = self.default_port
                 return uri
     return None
Esempio n. 6
0
 def test_journal(self):
     journal_values = {
         "DELETE": u'delete',
         "TRUNCATE": u'truncate',
         "PERSIST": u'persist',
         "MEMORY": u'memory',
         "WAL": u'wal',
         "OFF": u'off'
     }
     for value in journal_values:
         database = SQLite(
             URI("sqlite:%s?journal_mode=%s" % (self.get_path(), value)))
         connection = database.connect()
         result = connection.execute("PRAGMA journal_mode").get_one()[0]
         assert result == journal_values[value]
Esempio n. 7
0
    def _create_uri(self, dbname=None):
        # postgres is a special database which is always present,
        # it was added in 8.1 which is thus our requirement'
        dbname = dbname or 'postgres'

        # Do not output the password in the logs
        if not self.first:
            log.info('connecting to %s' % self._build_dsn(
                dbname, filter_password=True))
            self.first = False

        dsn = self._build_dsn(dbname, filter_password=False)
        uri = URI(dsn)
        uri.options['isolation'] = 'read-committed'

        if uri.host == "":
            pair = test_local_database()
            if pair is None:
                raise DatabaseError(
                    _("Could not find a database server on this computer"))
            uri.host = pair[0]
            uri.port = int(pair[1])

        return uri
Esempio n. 8
0
    def test_exception_when_unsupported(self):

        # Install a directory in front of the search path.
        module_dir = self.make_path()
        os.mkdir(module_dir)
        sys.path.insert(0, module_dir)

        # Copy the real module over to a new place, since the old one is
        # already using the real module, if it's available.
        db_module = __import__("storm.databases."+self.db_module_name,
                               None, None, [""])
        db_module_filename = db_module.__file__
        if db_module_filename.endswith(".pyc"):
            db_module_filename = db_module_filename[:-1]
        shutil.copyfile(db_module_filename,
                        os.path.join(module_dir, "_fake_.py"))

        dbapi_modules = {}
        for dbapi_module_name in self.dbapi_module_names:

            # If the real module is available, remove it from sys.modules.
            dbapi_module = sys.modules.pop(dbapi_module_name, None)
            if dbapi_module is not None:
                dbapi_modules[dbapi_module_name] = dbapi_module

            # Create a module which raises ImportError when imported, to fake
            # a missing module.
            dirname = self.make_path(path=os.path.join(module_dir,
                                                       dbapi_module_name))
            os.mkdir(dirname)
            self.make_path("raise ImportError",
                           os.path.join(module_dir, dbapi_module_name,
                                        "__init__.py"))

        # Finally, test it.
        import _fake_
        uri = URI("_fake_://db")

        try:
            with pytest.raises(DatabaseModuleError):
                _fake_.create_from_uri(uri)
        finally:
            # Unhack the environment.
            del sys.path[0]
            del sys.modules["_fake_"]

            sys.modules.update(dbapi_modules)
Esempio n. 9
0
 def test_journal_persistency_to_rollback(self):
     journal_values = {
         "DELETE": u'delete',
         "TRUNCATE": u'truncate',
         "PERSIST": u'persist',
         "MEMORY": u'memory',
         "WAL": u'wal',
         "OFF": u'off'
     }
     for value in journal_values:
         database = SQLite(
             URI("sqlite:%s?journal_mode=%s" % (self.get_path(), value)))
         connection = database.connect()
         connection.execute("CREATE TABLE test (id INTEGER PRIMARY KEY)")
         connection.rollback()
         result = connection.execute("PRAGMA journal_mode").get_one()[0]
         self.assertEqual(result, journal_values[value])
Esempio n. 10
0
def create_database(uri):
    """Create a database instance.

    @param uri: An URI instance, or a string describing the URI. Some examples:
        - "sqlite:" An in memory sqlite database.
        - "sqlite:example.db" A SQLite database called example.db
        - "postgres:test" The database 'test' from the local postgres server.
        - "postgres://*****:*****@host/test" The database test on machine host
          with supplied user credentials, using postgres.
        - "anything:..." Where 'anything' has previously been registered
          with L{register_scheme}.
    """
    if isinstance(uri, six.string_types):
        uri = URI(uri)
    if uri.scheme in _database_schemes:
        factory = _database_schemes[uri.scheme]
    else:
        module = __import__("%s.databases.%s" % (storm.__name__, uri.scheme),
                            None, None, [""])
        factory = module.create_from_uri
    return factory(uri)
Esempio n. 11
0
 def get_uri(self):
     """Return URI instance with a defined host and port."""
     if not self.environment_variable or not self.default_port:
         raise RuntimeError("Define at least %s.environment_variable and "
                            "%s.default_port" %
                            (type(self).__name__, type(self).__name__))
     uri_str = os.environ.get(self.host_environment_variable)
     if uri_str:
         uri = URI(uri_str)
         if not uri.host:
             raise RuntimeError("The URI in %s must include a host." %
                                self.host_environment_variable)
         if not uri.port:
             uri.port = self.default_port
         return uri
     else:
         uri_str = os.environ.get(self.environment_variable)
         if uri_str:
             uri = URI(uri_str)
             if uri.host:
                 if not uri.port:
                     uri.port = self.default_port
                 return uri
     return None
Esempio n. 12
0
def getCreationSQL(store):
    connType = store._connection.__class__.__name__
    return {
        'PostgresConnection': {
            'tableExists':
            lambda s, t: bool(
                s.execute(
                    "SELECT count(*) FROM pg_class where relname = ?::text",
                    (unicode(t), )).get_one()[0]),
            'creations': [
                ('warp_avatar', """
                CREATE TABLE warp_avatar (
                    id SERIAL NOT NULL PRIMARY KEY,
                    email VARCHAR,
                    password VARCHAR,
                    UNIQUE(email))"""),
                ('warp_session', """
                CREATE TABLE warp_session (
                    uid BYTEA NOT NULL PRIMARY KEY,
                    isPersistent BOOLEAN NOT NULL DEFAULT FALSE,
                    touched INTEGER,
                    avatar_id INTEGER REFERENCES warp_avatar(id) ON DELETE CASCADE)"""
                 ),
                ('warp_avatar_role', """
                CREATE TABLE warp_avatar_role (
                    id SERIAL NOT NULL PRIMARY KEY,
                    avatar_id INTEGER NOT NULL REFERENCES warp_avatar(id) ON DELETE CASCADE,
                    role_name BYTEA NOT NULL,
                    position SERIAL NOT NULL)"""),
                ('warp_fulltext',
                 ("""CREATE TABLE warp_fulltext (
                    model VARCHAR(128) NOT NULL,
                    doc_id INTEGER NOT NULL,
                    fulltext tsvector,
                    PRIMARY KEY (model, doc_id))""",
                  """CREATE INDEX fulltext_idx ON warp_fulltext USING gin(fulltext)"""
                  )),
            ],
        },
        'SQLiteConnection': {
            'tableExists':
            lambda s, t: bool(
                s.execute(
                    "SELECT count(*) FROM sqlite_master where name = '%s'" % t)
                .get_one()[0]),
            'creations': [
                ('warp_avatar', """
                CREATE TABLE warp_avatar (
                    id INTEGER NOT NULL PRIMARY KEY,
                    email VARCHAR,
                    password VARCHAR,
                    UNIQUE(email))"""),
                ('warp_session', """
                CREATE TABLE warp_session (
                    uid BYTEA NOT NULL PRIMARY KEY,
                    avatar_id INTEGER REFERENCES warp_avatar(id) ON DELETE CASCADE)"""
                 ),
                ('warp_avatar_role', """
                CREATE TABLE warp_avatar_role (
                    id INTEGER NOT NULL PRIMARY KEY,
                    avatar_id INTEGER NOT NULL REFERENCES warp_avatar(id) ON DELETE CASCADE,
                    role_name BYTEA NOT NULL,
                    position INTEGER NOT NULL DEFAULT 0)"""),
            ],
        },
        'MySQLConnection': {
            'tableExists':
            lambda s, t: bool(
                s.execute(
                    """
                   SELECT count(*) FROM information_schema.tables
                   WHERE table_schema = ? AND table_name=?""",
                    (URI(config['db']).database, t)).get_one()[0]),
            'creations': [
                ('warp_avatar', """
                CREATE TABLE warp_avatar (
                    id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    email VARCHAR(64),
                    password VARCHAR(32),
                    UNIQUE(email)
                  ) engine=InnoDB, charset=utf8"""),
                ('warp_session', """
                CREATE TABLE warp_session (
                    uid VARBINARY(32) NOT NULL PRIMARY KEY,
                    avatar_id INTEGER REFERENCES warp_avatar(id) ON DELETE CASCADE
                  ) engine=InnoDB, charset=utf8"""),
                ('warp_avatar_role', """
                CREATE TABLE warp_avatar_role (
                    id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    avatar_id INTEGER NOT NULL REFERENCES warp_avatar(id) ON DELETE CASCADE,
                    role_name VARBINARY(32) NOT NULL,
                    position INTEGER NOT NULL
                  ) engine=InnoDB, charset=utf8"""),
            ],
        },
    }[connType]
Esempio n. 13
0
def test_parse_just_relative_database():
    uri = URI("scheme:d%61ta/base")
    assert uri.scheme == "scheme"
    assert uri.database == "data/base"
Esempio n. 14
0
def test_parse_host():
    uri = URI("scheme://ho%73t")
    assert uri.scheme == "scheme"
    assert uri.host == "host"
Esempio n. 15
0
def test_parse_username():
    uri = URI("scheme://user%6eame@")
    assert uri.scheme == "scheme"
    assert uri.username == "username"
    assert uri.host == None
Esempio n. 16
0
 def create_database(self):
     self.database = SQLite(
         URI("sqlite:%s?synchronous=OFF&timeout=0" % self.get_path()))
Esempio n. 17
0
def test_parse_username_password_host():
    uri = URI("scheme://user%6eame:pass%77ord@ho%73t")
    assert uri.scheme == "scheme"
    assert uri.username == "username"
    assert uri.password == "password"
    assert uri.host == "host"
Esempio n. 18
0
 def str(self, uri):
     self.assertEquals(str(URI(uri)), uri)
Esempio n. 19
0
 def test_parse_just_colon(self):
     uri = URI("scheme:")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.database, None)
Esempio n. 20
0
 def test_copy(self):
     uri = URI("scheme:///db?opt=value")
     uri_copy = uri.copy()
     self.assertTrue(uri_copy is not uri)
     self.assertTrue(uri_copy.__dict__ == uri.__dict__)
     self.assertTrue(uri_copy.options is not uri.options)
Esempio n. 21
0
 def test_parse_host_database_options(self):
     uri = URI("scheme://ho%73t/d%61tabase?a%62c=d%65f&ghi=jkl")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.host, "host")
     self.assertEquals(uri.database, "database")
     self.assertEquals(uri.options, {"abc": "def", "ghi": "jkl"})
Esempio n. 22
0
 def test_parse_options(self):
     uri = URI("scheme:?a%62c=d%65f&ghi=jkl")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.host, None)
     self.assertEquals(uri.database, None)
     self.assertEquals(uri.options, {"abc": "def", "ghi": "jkl"})
Esempio n. 23
0
            del self[key]
        except KeyError, k:
            raise AttributeError, k

    def __repr__(self):
        return '<Storage ' + dict.__repr__(self) + '>'

    def __getstate__(self):
        return dict(self)

    def __setstate__(self, value):
        for (k, v) in value.items():
            self[k] = v

def randomStr(length, num=True):
    """
    Returns a random a mixed lowercase, uppercase, alfanumerical (if num True)
    string long length
    """
    chars = string.ascii_lowercase + string.ascii_uppercase
    if num:
        chars += string.digits
    return ''.join(random.choice(chars) for x in range(length))

from oonib import config

database = SQLite(URI(config.main.database_uri))
db_threadpool = ThreadPool(0, config.main.db_threadpool_size)
db_threadpool.start()
transactor = Transactor(db_threadpool)
Esempio n. 24
0
 def create_database(self):
     self.database = SQLite(
         URI("sqlite:%s?synchronous=OFF" % self.make_path()))
Esempio n. 25
0
 def test_parse_just_relative_database(self):
     uri = URI("scheme:d%61ta/base")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.database, "data/base")
Esempio n. 26
0
def test_parse_username_host():
    uri = URI("scheme://user%6eame@ho%73t")
    assert uri.scheme == "scheme"
    assert uri.username == "username"
    assert uri.host == "host"
Esempio n. 27
0
def test_parse_just_colon():
    uri = URI("scheme:")
    assert uri.scheme == "scheme"
    assert uri.database == None
Esempio n. 28
0
 def test_parse_just_absolute_database(self):
     uri = URI("scheme:/d%61ta/base")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.database, "/data/base")
Esempio n. 29
0
 def test_copy(self):
     uri = URI("scheme:///db?opt=value")
     uri_copy = uri.copy()
     self.assertTrue(uri_copy is not uri)
     self.assertTrue(uri_copy.__dict__ == uri.__dict__)
     self.assertTrue(uri_copy.options is not uri.options)
Esempio n. 30
0
 def test_parse_username(self):
     uri = URI("scheme://user%6eame@")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.username, "username")
     self.assertEquals(uri.host, None)
Esempio n. 31
0
 def test_create_database_with_uri(self):
     uri = URI("db_module:db")
     create_database(uri)
     self.assertTrue(self.uri is uri)
Esempio n. 32
0
 def test_parse_username_host(self):
     uri = URI("scheme://user%6eame@ho%73t")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.username, "username")
     self.assertEquals(uri.host, "host")
Esempio n. 33
0
 def test_get_uri(self):
     dummy = DummyModel()
     self.assertEqual(dummy.get_uri().scheme, URI('sqlite:').scheme)
Esempio n. 34
0
 def test_parse_username_password_host(self):
     uri = URI("scheme://user%6eame:pass%77ord@ho%73t")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.username, "username")
     self.assertEquals(uri.password, "password")
     self.assertEquals(uri.host, "host")
Esempio n. 35
0
 def test_parse_host(self):
     uri = URI("scheme://ho%73t")
     self.assertEquals(uri.scheme, "scheme")
     self.assertEquals(uri.host, "host")
Esempio n. 36
0
def test_parse_just_absolute_database():
    uri = URI("scheme:/d%61ta/base")
    assert uri.scheme == "scheme"
    assert uri.database == "/data/base"