def initialize(context): enable_log() if os.environ.get('ZOPETESTCASE') is None: pwManager = getUtility(IPasswordManager, 'pg') dbHostname = os.environ.get('PG_HOSTNAME', 'localhost') connString = 'postgres://%s@%s/gites_wallons' % \ (pwManager.getLoginPassWithSeparator(':'), dbHostname) createSAWrapper(connString, forZope=True, echo=False, session_options={'query_cls': query_callable(regions)}, engine_options={'convert_unicode': True, 'encoding': 'utf-8'}, encoding='utf-8', name='gites_wallons', model='GitesMappings') content_types, constructors, ftis = process_types( listTypes(PROJECTNAME), PROJECTNAME) cmfutils.ContentInit( PROJECTNAME + ' Content', content_types=content_types, permission=DEFAULT_ADD_CONTENT_PERMISSION, extra_constructors=constructors, fti=ftis).initialize(context)
def initialize(context): pwManager = getUtility(IPasswordManager, 'pg') connString = 'postgresql://%s@localhost/cenforsoc' % pwManager.getLoginPassWithSeparator(':') createSAWrapper(connString, forZope=True, echo=False, engine_options={'convert_unicode': True}, name='cenforsoc', model='cenforsocMappings')
def initialize(context): pwManager = getUtility(IPasswordManager, 'pg') connString = 'postgres://%s@localhost/clpsbw' % pwManager.getLoginPassWithSeparator( ':') createSAWrapper(connString, forZope=True, echo=False, engine_options={'convert_unicode': True}, name='clpsbw', model='clpsbwMappings')
def setUpZope(self, app, configurationContext): import gdw.stats xmlconfig.file('ftesting.zcml', gdw.stats, context=configurationContext) connString = 'postgresql://*****:*****@localhost:5432/gites_wallons_test' createSAWrapper(connString, forZope=True, #echo=True, name='gites_wallons', model='GitesMappings')
def main(): import gites.calendar.scripts parseZCML(gites.calendar.scripts, 'calendar.zcml') pg = PGDB('jfroche', 'xxxxxxx', 'localhost', 5432, 'gites_wallons') connString = 'postgres://*****:*****@localhost/gites_wallons' createSAWrapper(connString, forZope=True, engine_options = {'convert_unicode': True, 'encoding': 'utf-8'}, encoding='utf-8', name='gites_wallons', model='GitesMappings') exporter = ExportCalendarActivity(pg) exporter.connect() exporter.export()
def testDeclarativeWithModel(self): def getModel(metadata): model = Model() Base = declarative_base(metadata=metadata) class Foo(Base): __tablename__ = "foo" id = Column("id", Integer, primary_key=True) name = Column("name", String(50)) model.add("foo", mapper_class=Foo) Base.metadata.create_all() return model db = createSAWrapper(self.dsn, model=getModel) session = db.session Foo = db.getMapper("foo") session.add(Foo(id=1, name="Andreas Jung")) session.add(Foo(id=2, name="Peter Becker")) session.flush() rows = session.query(Foo).all() self.assertEqual(len(rows), 2)
def afterSetUp(self): self.folder_path = '/' + self.folder.absolute_url(1) self.dsn = os.environ.get('TEST_DSN', 'sqlite:///test') wrapper = createSAWrapper(self.dsn) metadata.bind = wrapper.engine metadata.create_all() self.session = wrapper.session
def testDeclarativeWithModel(self): def getModel(metadata): model = Model() Base = declarative_base(metadata=metadata) class Foo(Base): __tablename__ = 'foo' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) model.add('foo', mapper_class=Foo) Base.metadata.create_all() return model db = createSAWrapper(self.dsn, model=getModel) session = db.session Foo = db.getMapper('foo') session.add(Foo(id=1, name='Andreas Jung')) session.add(Foo(id=2, name='Peter Becker')) session.flush() rows = session.query(Foo).all() self.assertEqual(len(rows), 2)
def _create_sa_wrapper(self): """.""" try: wrapper = getSAWrapper(self.util_id) except ValueError: try: if self.util_id is None: self._new_utilid() wrapper = createSAWrapper( self.dsn, forZope=True, transactional=True, extension_options={'initial_state': 'invalidated'}, engine_options=self.engine_options_dict, name=self.util_id) register_z3c(self.id, wrapper) except ValueError: try: wrapper = getSAWrapper(self.util_id) except LookupError: wrapper = lookup_entry(self.id) except Exception: wrapper = None return wrapper
def testCheckConnection(self): """ Check access to low-level connection """ db = createSAWrapper(self.dsn) conn = db.connection cursor = conn.cursor() cursor.execute("select * from users") rows = cursor.fetchall() self.assertEqual(len(rows), 0)
def testModelNonExistingTables(self): M = Model() M.add("non_existing_table") db = createSAWrapper(self.dsn, model=M) try: db.getMapper("non_existing_table") except exc.NoSuchTableError: pass
def afterSetUp(self): from z3c.sqlalchemy import createSAWrapper self.folder_path = '/' + self.folder.absolute_url(1) self.dsn = os.environ.get('TEST_DSN', 'sqlite:///sqlitetestdb2') wrapper = createSAWrapper(self.dsn) metadata.bind = wrapper.engine metadata.create_all() self.session = wrapper.session
def testCheckConnection(self): """ Check access to low-level connection """ db = createSAWrapper(self.dsn) conn = db.connection cursor = conn.cursor() cursor.execute('select * from users') rows = cursor.fetchall() self.assertEqual(len(rows), 0)
def testModelNonExistingTables(self): M = Model() M.add('non_existing_table') db = createSAWrapper(self.dsn, model=M) try: db.getMapper('non_existing_table') except exc.NoSuchTableError: pass
def testMapperWithCustomModel(self): class myUser(MappedClassBase): pass M = Model() M.add("users", mapper_class=myUser) db = createSAWrapper(self.dsn, model=M) User = db.getMapper("users") self.assertEqual(User, myUser)
def initialize(context): pwManager = getUtility(IPasswordManager, 'pg') connString = 'postgresql://%s@%s/ipplf' % (pwManager.getLoginPassWithSeparator(':'), host()) wr = createSAWrapper(connString, forZope=True, echo=False, engine_options={'convert_unicode': True}, name='ipplf', model='ipplfMappings') return wr
def testMapperWithCustomModel(self): class myUser(MappedClassBase): pass M = Model() M.add('users', mapper_class=myUser) db = createSAWrapper(self.dsn, model=M) User = db.getMapper('users') self.assertEqual(User, myUser)
def _wrapper(self): if self.dsn: try: return getSAWrapper(self.util_id) except ValueError: return createSAWrapper(self.dsn, forZope=True, transactional=self.transactional, name=self.util_id) return None
def afterSetUp(self): self.dsn = os.environ.get('TEST_DSN', 'sqlite:///test') wrapper = createSAWrapper(self.dsn) metadata.bind = wrapper.engine metadata.create_all() session = wrapper.session t1 = Test(id=1, utext=u'Hello world', text='hello world') t2 = Test(id=2, utext=u'foo', text='far') session.add(t1) session.add(t2)
def _wrapper(self): if self.dsn: try: return getSAWrapper(self.util_id) except ValueError: return createSAWrapper(self.dsn, forZope=True, transactional=self.transactional, extension_options={'initial_state': 'invalidated'}, engine_options=self.engine_options, name=self.util_id) return None
def getDBServer(self): if not self.dsn: return None self._v_thread_local.dbconns = getattr(self._v_thread_local, 'dbconns', {}) dbserver = self._v_thread_local.dbconns.get(self.dsn, None) if dbserver is None or not dbserver.adaptor.conn.is_valid: try: wrapper = getSAWrapper(self.dsn) except ValueError: wrapper = createSAWrapper(dsn=self.dsn, name=self.dsn) #TODO: manage OperationalError on connection dbserver = DBServer(wrapper.connection, __import__( self.getDriver() )) self._v_thread_local.dbconns[self.dsn] = dbserver return dbserver
def _wrapper(self): if self.dsn: try: return getSAWrapper(self.util_id) except ValueError: return createSAWrapper( self.dsn, forZope=True, transactional=self.transactional, extension_options={'initial_state': 'invalidated'}, engine_options=self.engine_options, name=self.util_id) return None
def _wrapper(self): if self.dsn: try: return getSAWrapper(self.util_id) except ValueError: return createSAWrapper(self.dsn, forZope=True, transactional=self.transactional, engine_options={ 'convert_unicode': self.convert_unicode, 'encoding': self.encoding }, name=self.util_id) return None
def getDBServer(self): if not self.dsn: return None self._v_thread_local.dbconns = getattr(self._v_thread_local, 'dbconns', {}) dbserver = self._v_thread_local.dbconns.get(self.dsn, None) if dbserver is None or not dbserver.adaptor.conn.is_valid: try: wrapper = getSAWrapper(self.dsn) except ValueError: wrapper = createSAWrapper(dsn=self.dsn, name=self.dsn) #TODO: manage OperationalError on connection dbserver = DBServer(wrapper.connection, __import__(self.getDriver())) self._v_thread_local.dbconns[self.dsn] = dbserver return dbserver
def _supply_z3c_sa_wrapper(self): """ Look up or create the underlying z3c.sqlalchemy `ZopeWrapper`. """ if not self.dsn: return None else: try: wrapper = getSAWrapper(self.util_id) except ValueError: try: if self.util_id is None: # the z3c.sqlalchemy registration doesn't register None values # of util_id; we need something that will stick. self._new_utilid() wrapper = createSAWrapper( self.dsn, forZope=True, transactional=self.transactional, extension_options={'initial_state': 'invalidated'}, engine_options=self.engine_options, name=self.util_id) register_sa_wrapper(self.id, wrapper) except ValueError as e: # ...weird...could this be a timing issue during startup? # We've seen log messages that look like this: # "ValueError: SAWrapper '1435583419.58.0.991532919015' already registered. # You can not register a wrapper twice under the same name." # This makes little sense because we just tried a lookup under that # name and did not find it. Wrapper did not exist in # component registry, but did exist in the z3c.sqlalchemy # registeredWrappers dict registry. Try recovering by using # the module dict lookup. logger.warning("Unexpected failure to create SAWrapper: " + str(e)) try: wrapper = getSAWrapper(self.util_id) except LookupError as e: logger.warning( "SAWrapper lookup falling back to SQLAlchemyDA registry:" + str(e)) wrapper = lookup_sa_wrapper(self.id) except Exception: logger.exception( "No z3c.sqlalchemy ZopeWrapper found or created!") wrapper = None return wrapper
def setUp(self): self.dsn = os.environ.get('TEST_DSN') self.tempfile = None if not self.dsn: self.tempfile = tempfile.mktemp() self.dsn = 'sqlite:///%s' % self.tempfile self.db = wrapper = createSAWrapper(self.dsn) metadata = MetaData(bind=wrapper.engine) Table('users', metadata, Column('id', Integer, primary_key=True), Column('firstname', String(255)), Column('lastname', String(255))) Table('skills', metadata, Column('user_id', Integer, primary_key=True), Column('name', String(255))) metadata.create_all()
def testSimplePopulation(self): db = createSAWrapper(self.dsn) # obtain mapper for table 'users' User = db.getMapper("users") session = db.session rows = session.query(User).all() self.assertEqual(len(rows), 0) session.add(User(id=1, firstname="udo", lastname="juergens")) session.add(User(id=2, firstname="heino", lastname="n/a")) session.flush() rows = session.query(User).order_by(User.id).all() self.assertEqual(len(rows), 2) row1 = rows[0] d = row1.asDict() self.assertEqual(d, {"firstname": "udo", "lastname": "juergens", "id": 1})
def setupDatabase(self): configurationContext = self['configurationContext'] xmlconfig.file('testing.zcml', self.package, context=configurationContext) configurationContext.execute_actions() schema_file = os.path.join(CURRENT_DIR, 'tests', 'gites_wallons.sql') self.db = gocept.testdb.PostgreSQL(encoding='UTF8', db_template='gites_wallons_testing', schema_path=schema_file) self.db.create() wr = createSAWrapper(self.db.dsn, forZope=self.forZope, echo=self.logging, engine_options={'convert_unicode': True}, name=self.dbName, model=self.model) self['%s_wrapper' % self.dbPrefix] = self.wrapper = wr self.engine = self.wrapper.engine wrapper = getSAWrapper('gites_wallons') wrapper.metadata.create_all()
def _supply_z3c_sa_wrapper(self): """ Look up or create the underlying z3c.sqlalchemy `ZopeWrapper`. """ if not self.dsn: return None else: try: wrapper = getSAWrapper(self.util_id) except ValueError: try: if self.util_id is None: # the z3c.sqlalchemy registration doesn't register None values # of util_id; we need something that will stick. self._new_utilid() wrapper = createSAWrapper(self.dsn, forZope=True, transactional=self.transactional, extension_options={'initial_state': 'invalidated'}, engine_options=self.engine_options, name=self.util_id) register_sa_wrapper(self.id, wrapper) except ValueError as e: # ...weird...could this be a timing issue during startup? # We've seen log messages that look like this: # "ValueError: SAWrapper '1435583419.58.0.991532919015' already registered. # You can not register a wrapper twice under the same name." # This makes little sense because we just tried a lookup under that # name and did not find it. Wrapper did not exist in # component registry, but did exist in the z3c.sqlalchemy # registeredWrappers dict registry. Try recovering by using # the module dict lookup. logger.warning("Unexpected failure to create SAWrapper: " + str(e)) try: wrapper = getSAWrapper(self.util_id) except LookupError as e: logger.warning("SAWrapper lookup falling back to SQLAlchemyDA registry:" + str(e)) wrapper = lookup_sa_wrapper(self.id) except Exception: logger.exception("No z3c.sqlalchemy ZopeWrapper found or created!") wrapper = None return wrapper
def testDeclarative(self): db = createSAWrapper(self.dsn) session = db.session metadata = db.metadata Base = declarative_base(metadata=metadata) class Foo(Base): __tablename__ = 'foo' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) Base.metadata.create_all(db._engine) session.add(Foo(id=1, name='Andreas Jung')) session.add(Foo(id=2, name='Peter Becker')) session.flush() rows = session.query(Foo).all() self.assertEqual(len(rows), 2)
def testDeclarative(self): db = createSAWrapper(self.dsn) session = db.session metadata = db.metadata Base = declarative_base(metadata=metadata) class Foo(Base): __tablename__ = "foo" id = Column("id", Integer, primary_key=True) name = Column("name", String(50)) Base.metadata.create_all(db._engine) session.add(Foo(id=1, name="Andreas Jung")) session.add(Foo(id=2, name="Peter Becker")) session.flush() rows = session.query(Foo).all() self.assertEqual(len(rows), 2)
def setUp(self): self.dsn = os.environ.get("TEST_DSN") self.tempfile = None if not self.dsn: self.tempfile = tempfile.mktemp() self.dsn = "sqlite:///%s" % self.tempfile self.db = wrapper = createSAWrapper(self.dsn) metadata = MetaData(bind=wrapper.engine) Table( "users", metadata, Column("id", Integer, primary_key=True), Column("firstname", String(255)), Column("lastname", String(255)), ) Table("skills", metadata, Column("user_id", Integer, primary_key=True), Column("name", String(255))) metadata.create_all()
def afterSetUp(self): self.dsn = os.environ.get('TEST_DSN', 'sqlite:///test') wrapper = createSAWrapper(self.dsn) metadata = MetaData(bind=wrapper.engine) test_table = Table('test', metadata, Column('id', Integer, primary_key=True), Column('utext', Unicode(255)), Column('text', String(255))) class Test(MappedClassBase): pass mapper(Test, test_table) metadata.create_all() session = wrapper.session t1 = Test(id=1, utext=u'Hello world', text='hello world') t2 = Test(id=2, utext=u'foo', text='far') session.save(t1) session.save(t2)
def testSimplePopulation(self): db = createSAWrapper(self.dsn) # obtain mapper for table 'users' User = db.getMapper('users') session = db.session rows = session.query(User).all() self.assertEqual(len(rows), 0) session.add(User(id=1, firstname='udo', lastname='juergens')) session.add(User(id=2, firstname='heino', lastname='n/a')) session.flush() rows = session.query(User).order_by(User.id).all() self.assertEqual(len(rows), 2) row1 = rows[0] d = row1.asDict() self.assertEqual(d, { 'firstname': 'udo', 'lastname': 'juergens', 'id': 1 })
def testWrapperDirectRegistration(self): wrapper = createSAWrapper(self.dsn, name='test.wrapper3') wrapper2 = getSAWrapper('test.wrapper3') self.assertEqual(wrapper, wrapper2)
for sanerValidator in sanerValidators: validation.register(sanerValidator) #Open up our connection to the database and do it early from z3c.sqlalchemy import createSAWrapper, Model from z3c.sqlalchemy.mapper import MappedClassBase from sqlalchemy.orm import mapper, synonym from sqlalchemy import Table class EhsBookingMapper(MappedClassBase): pass #We're creating our SQLAlchemy wrapper and mapping our table here to the #class above. All fields are our primary key because the view doesn't have #a primary key defined. Go Oracle! wrapper = createSAWrapper(config.EHS_BOOKING_DB_CONNECTION_STRING, name=config.EHS_BOOKING_DB_CONNECTOR) table = Table(config.EHS_BOOKING_TABLE_NAME, wrapper.metadata, schema=config.EHS_BOOKING_DB_SCHEMA, autoload=True, ) ehs_mapper = mapper(EhsBookingMapper, table, primary_key=table.c._data.values(), properties={}) wrapper.registerMapper(ehs_mapper, name=config.EHS_BOOKING_ABSOLUTE_NAME) #Create our cancellations file with headings if it doesn't exist already if not os.path.exists(config.EHS_CANCELLATION_LOG): csv_log = open(config.EHS_CANCELLATION_LOG, 'a') writer = csv.writer(csv_log, quotechar='"', quoting=csv.QUOTE_MINIMAL) writer.writerow(config.EHS_CANCELLATION_LOG_CSV_FORMAT) csv_log.close() #
def testGetMappers(self): db = createSAWrapper(self.dsn) db.getMapper("users") db.getMapper("skills") db.getMappers("users", "skills")
def testWrapperRegistration(self): wrapper = createSAWrapper(self.dsn) registerSAWrapper(wrapper, "test.wrapper1") wrapper2 = getSAWrapper("test.wrapper1") self.assertEqual(wrapper, wrapper2)
def testWrapperDirectRegistration(self): wrapper = createSAWrapper(self.dsn, name="test.wrapper3") wrapper2 = getSAWrapper("test.wrapper3") self.assertEqual(wrapper, wrapper2)
from zope.i18nmessageid import MessageFactory from z3c.sqlalchemy import createSAWrapper import jpype from tdh.metadata import config #We're creating our SQLAlchemy wrapper and mapping tables in our database. if hasattr(config.configuration, 'product_config'): for db_connection in config.DB_CONNECTIONS: connection = config.DB_CONNECTIONS[db_connection] createSAWrapper(connection['db-connection-string'], name=connection['db-connector-id']) # Set up the i18n message factory for our package MessageFactory = MessageFactory('tdh.metadata') # Initialise our JVM for use with RIF-CS and do it early. jpype.startJVM(config.JVM_PATH, '-Djava.class.path=%s' % config.RIFCS_API_LOCATION)
def testWrapperRegistrationFailing(self): createSAWrapper(self.dsn) self.assertRaises(ValueError, getSAWrapper, "test.wrapperNonExistant")
def testWrapperDoubleRegistrationFailing(self): wrapper = createSAWrapper(self.dsn) registerSAWrapper(wrapper, "test.wrapper2") self.assertRaises(ValueError, registerSAWrapper, wrapper, "test.wrapper2")
def testWrapperDoubleRegistrationFailing(self): wrapper = createSAWrapper(self.dsn) registerSAWrapper(wrapper, 'test.wrapper2') self.assertRaises(ValueError, registerSAWrapper, wrapper, 'test.wrapper2')
def testWrapperRegistrationFailing(self): createSAWrapper(self.dsn) self.assertRaises(ValueError, getSAWrapper, 'test.wrapperNonExistant')
def testWrapperRegistration(self): wrapper = createSAWrapper(self.dsn) registerSAWrapper(wrapper, 'test.wrapper1') wrapper2 = getSAWrapper('test.wrapper1') self.assertEqual(wrapper, wrapper2)
def testGetMappers(self): db = createSAWrapper(self.dsn) db.getMapper('users') db.getMapper('skills') db.getMappers('users', 'skills')