def finding_database_control(fixture): """The correct DatabaseControl will be found from the entry point reahl.component.databasecontrols for a given reahlsystem.connection_uri config setting based on its control_matching_regex. """ fixture.config.reahlsystem.connection_uri = 'myprefix://thedb:theuser:thepasswd:thehost:123' system_control = SystemControl(fixture.config) vassert(isinstance(system_control.db_control, TestDatabaseControl)) fixture.config.reahlsystem.connection_uri = 'wrongprefix://thedb:theuser:thepasswd:thehost:123' with expected(CouldNotFindDatabaseControlException): SystemControl(fixture.config)
def create_context(self, config_directory): try: self.context = ExecutionContext.for_config_directory(config_directory) except DistributionNotFound as ex: ex.args = ('%s (In development? Did you forget to do a "reahl setup -- develop -N"?)' % ex.args[0],) raise self.context.install() self.context.system_control = SystemControl(self.context.config)
def test_minimum_required_database_control_settings(dbcontrol_fixture): """The minimum an uri should contain is a database name. """ fixture = dbcontrol_fixture fixture.config.reahlsystem.connection_uri = 'myprefix:///' with expected( ProgrammerError, test='Please specify a database name in reahlsystem.connection_uri' ): SystemControl(fixture.config)
def database_control_settings(fixture): """DatabaseControl settings are read from the reahlsystem.connection_uri config setting based on its uri_regex_string which should be a regex with the correct named groups. """ fixture.config.reahlsystem.connection_uri = 'myprefix://thedb:theuser:thepasswd:thehost:123' system_control = SystemControl(fixture.config) vassert(system_control.db_control.database_name == 'thedb') vassert(system_control.db_control.user_name == 'theuser') vassert(system_control.db_control.password == 'thepasswd') vassert(system_control.db_control.host == 'thehost') vassert(system_control.db_control.port == 123) fixture.config.reahlsystem.connection_uri = 'myprefix://stuffstuffstuff' with expected(InvalidConnectionURIException): SystemControl(fixture.config)
def test_database_control_settings(dbcontrol_fixture, uri_scenario): """DatabaseControl settings are read from the reahlsystem.connection_uri config setting parsed as an RFC1808 URI """ fixture = dbcontrol_fixture fixture.config.reahlsystem.connection_uri = uri_scenario.uri system_control = SystemControl(fixture.config) assert system_control.db_control.database_name == uri_scenario.database_name assert system_control.db_control.user_name == uri_scenario.user_name assert system_control.db_control.password == uri_scenario.password assert system_control.db_control.host == uri_scenario.host assert system_control.db_control.port == uri_scenario.port
def test_database_control_url_safe_parts(dbcontrol_fixture): """URL parts are unquoted, as they may contain URL quoted characters. """ fixture = dbcontrol_fixture def quote(values): return [urllib.parse.quote(i) for i in values] expected_parts \ = [expected_user_name, expected_password, expected_host, expected_db] \ = ['usêrname', 'p#=sword', 'hõst', 'd~t∀b^s∊'] uri = 'myprefix://%s:%s@%s:456/%s' % tuple(quote(expected_parts)) assert not set(expected_parts).intersection(quote(expected_parts)) fixture.config.reahlsystem.connection_uri = uri system_control = SystemControl(fixture.config) assert system_control.db_control.user_name == expected_user_name assert system_control.db_control.password == expected_password assert system_control.db_control.host == expected_host assert system_control.db_control.port == 456 assert system_control.db_control.database_name == expected_db
def test_database_control_url_safe_parts(dbcontrol_fixture): """URL parts are unquoted, as they may contain URL quoted characters. """ fixture = dbcontrol_fixture def quote(values): return [urllib_parse.quote(i) for i in values] if six.PY2: expected_parts \ = [expected_user_name, expected_password, expected_host, expected_db] \ = [u'usêrname', u'p#=sword', u'hõst', u'd~t∀b^s∊'] #force the str, else it somehow becomes unicode in some interpreters: string is required for quote/unquote utf_encoded_expected_parts = [ i.encode('utf-8') for i in expected_parts ] uri = str('myprefix://%s:%s@%s:456/%s' % tuple(quote(utf_encoded_expected_parts))) assert not set(expected_parts).intersection( quote(utf_encoded_expected_parts)) else: expected_parts \ = [expected_user_name, expected_password, expected_host, expected_db] \ = ['usêrname', 'p#=sword', 'hõst', 'd~t∀b^s∊'] uri = 'myprefix://%s:%s@%s:456/%s' % tuple(quote(expected_parts)) assert not set(expected_parts).intersection(quote(expected_parts)) fixture.config.reahlsystem.connection_uri = uri system_control = SystemControl(fixture.config) assert system_control.db_control.user_name == expected_user_name assert system_control.db_control.password == expected_password assert system_control.db_control.host == expected_host assert system_control.db_control.port == 456 assert system_control.db_control.database_name == expected_db
def new_system_control(self): """The :class:`~reahl.component.dbutils.SystemControl` with which you can control the underlying database. """ return SystemControl(self.config)
import sys from reahl.component.config import StoredConfiguration from reahl.component.dbutils import SystemControl from reahl.sqlalchemysupport import Session, metadata from reahl.component.context import ExecutionContext from reahl.doc.examples.tutorial.migrationexamplebootstrap.migrationexamplebootstrap import Address config = StoredConfiguration(sys.argv[1]) config.configure() context = ExecutionContext().install() context.config = config context.system_control = SystemControl(config) try: context.system_control.orm_control.connect() metadata.create_all() Session.add(Address(name='John Doe', email_address='*****@*****.**')) Session.add( Address(name='Jane Johnson', email_address='*****@*****.**')) Session.add(Address(name='Jack Black', email_address='*****@*****.**')) context.system_control.orm_control.commit() finally: context.system_control.orm_control.disconnect()
def new_system_control(self): return SystemControl(self.config)