def test_default_repr(self): from alchemist import db uri = 'sqlite:///:memory:' with settings(self.app, DATABASES={'default': uri}): assert repr(db.engine) == repr(db.engine['default'])
def test_repr(self): from alchemist import db config = {'default': 'sqlite:///:memory:'} with settings(self.app, DATABASES=config), self.app.app_context(): text = '<Session(bind=%r)>' % db.engine['default'] assert repr(db.session) == text
def test_missing(self): from alchemist import db, exceptions with settings(self.app): with raises(exceptions.ImproperlyConfigured): db.engine['default']
def test_component(self): self.app = Flask('alchemist.tests.a') with settings(self.app, COMPONENTS=['alchemist.tests.a.b']): alchemist.configure(self.app) assert self.app.config.get('B_SETTING', 1) assert self.app.config.get('A_SETTING', 5)
def test_lookup(self): from alchemist import db uri = 'sqlite:///:memory:' with settings(self.app, DATABASES={'default': uri}): assert 'connect' in dir(db.engine)
def test_default(self): from alchemist import db uri = 'sqlite:///:memory:' with settings(self.app, DATABASES={'default': uri}): assert db.engine.url.drivername == 'sqlite' assert db.engine.url.database == ':memory:'
def test_missing_database(self): from alchemist import db, exceptions uri = 'sqlite:///:memory:' with settings(self.app, DATABASES={'other': uri}): with raises(exceptions.ImproperlyConfigured): db.engine['default']
def test_alternative(self): from alchemist import db uri = 'sqlite:///:memory:' with settings(self.app, DATABASES={'other': uri}): assert db.engine['other'].url.drivername == 'sqlite' assert db.engine['other'].url.database == ':memory:'
def test_missing_project(self): """ Should succeed and not raise if the project does not contain a settings file. """ self.app = Flask('alchemist') with settings(self.app): alchemist.configure(self.app)
def test_usage(self): from alchemist import db uri = 'sqlite:///:memory:' with settings(self.app, DATABASES={'default': uri}): with contextlib.closing(db.engine.connect()) as connection: result = connection.execute("BEGIN") assert isinstance(result, ResultProxy)
def test_shell_sqlite3(self): from alchemist import db with settings(self.app, DATABASES={'default': 'sqlite:///:memory:'}): target = mock.MagicMock() with mock.patch('os.execvp', target): db.shell() target.assert_called_with('sqlite3', ['sqlite3', ':memory:'])
def test_expanded(self): from alchemist import db config = { 'engine': 'sqlite', 'name': 'name', } with settings(self.app, DATABASES={'default': config}): assert db.engine.url.drivername == 'sqlite'
def test_shell_mysql(self): from alchemist import db uri = 'mysql+oursql://user:pass@host:55/name' with settings(self.app, DATABASES={'default': uri}): target = mock.MagicMock() with mock.patch('os.execvp', target): db.shell() target.assert_called_with('mysql', [ 'mysql', '--user=user', '--password=pass', '--host=host', '--port=55', 'name'])
def test_project_as_component(self): """ Should not merge the project settings in twice even if it is listed in the COMPONENTS array. """ self.app = Flask('alchemist.tests.a') components = ['alchemist.tests.a.b', 'alchemist.tests.a'] with settings(self.app, COMPONENTS=components): alchemist.configure(self.app) assert self.app.config.get('B_SETTING', 1) assert self.app.config.get('A_SETTING', 5)
def test_shell_mysql_socket(self): from alchemist import db uri = ('mysql+oursql://user:pass@localhost/name?' 'unix_socket=/some/file/over/there.socket') with settings(self.app, DATABASES={'default': uri}): target = mock.MagicMock() with mock.patch('os.execvp', target): db.shell() target.assert_called_with('mysql', [ 'mysql', '--user=user', '--password=pass', '--socket=/some/file/over/there.socket', 'name'])
def test_shell_mysql(self): from alchemist import db uri = 'mysql+oursql://user:pass@host:55/name' with settings(self.app, DATABASES={'default': uri}): target = mock.MagicMock() with mock.patch('os.execvp', target): db.shell() target.assert_called_with('mysql', [ 'mysql', '--user=user', '--password=pass', '--host=host', '--port=55', 'name' ])
def test_shell_mysql_socket(self): from alchemist import db uri = ('mysql+oursql://user:pass@localhost/name?' 'unix_socket=/some/file/over/there.socket') with settings(self.app, DATABASES={'default': uri}): target = mock.MagicMock() with mock.patch('os.execvp', target): db.shell() target.assert_called_with('mysql', [ 'mysql', '--user=user', '--password=pass', '--socket=/some/file/over/there.socket', 'name' ])
def test_project_env(self): """ Should override settings from the file specified in the <project>_SETTINGS_MODULE environ variable. """ filename = path.join(path.dirname(__file__), '../a/b/settings.py') os.environ['TESTS_A_SETTINGS_MODULE'] = filename self.app = Flask('alchemist.tests.a') with settings(self.app): alchemist.configure(self.app) assert self.app.config.get('B_SETTING', 1) assert self.app.config.get('A_SETTING', 5) del os.environ['TESTS_A_SETTINGS_MODULE']
def test_override(self): config = { 'SERVER_HOST': 'app.me', 'SERVER_PORT': 5000, 'SERVER_RELOAD': False, 'SERVER_PROCESSES': 10, 'SERVER_THREADED': True, 'SERVER_DEBUG': False } with test.settings(self.app, **config): target = self._run(['run']) target.assert_called_with(self.app, passthrough_errors=False, host=config['SERVER_HOST'], port=config['SERVER_PORT'], use_reloader=config['SERVER_RELOAD'], use_debugger=config['SERVER_DEBUG'], processes=config['SERVER_PROCESSES'], threaded=config['SERVER_THREADED'])
def test_override(self): config = { 'SERVER_HOST': 'app.me', 'SERVER_PORT': 5000, 'SERVER_RELOAD': False, 'SERVER_PROCESSES': 10, 'SERVER_THREADED': True, 'SERVER_DEBUG': False } with test.settings(self.app, **config): target = self._run(['run']) target.assert_called_with( self.app, passthrough_errors=False, host=config['SERVER_HOST'], port=config['SERVER_PORT'], use_reloader=config['SERVER_RELOAD'], use_debugger=config['SERVER_DEBUG'], processes=config['SERVER_PROCESSES'], threaded=config['SERVER_THREADED'])
def test_project(self): self.app = Flask('alchemist.tests.a') with settings(self.app): alchemist.configure(self.app) assert self.app.config.get('A_SETTING', 1)
def test_acquire_default(self): from alchemist import db config = {'default': 'sqlite:///:memory:'} with settings(self.app, DATABASES=config), self.app.app_context(): assert db.session