def test_functional_echo_but_no_url(self): with Replacer() as r: # make sure there's no DB_URL r.replace('os.environ', dict()) # hoover up the logging ;-) with OutputCapture(): register_session(echo=True)
def test_subsecond_accuracy(self): Base = declarative_base() class Sucktastic(Base): __tablename__ = "test" id = Column(Integer, primary_key=True) value = Column(DateTime) register_session(metadata=Base.metadata) with transaction.manager: session = get_session() session.add(Sucktastic(value=datetime(2001, 1, 1, 10, 1, 2, 3))) session = get_session() suck = session.query(Sucktastic).one() if session.bind.name == "mysql": # whoops, there goes our sub-second info self.assertEqual(suck.value, datetime(2001, 1, 1, 10, 1, 2)) else: self.assertEqual(suck.value, datetime(2001, 1, 1, 10, 1, 2, 3)) session.rollback()
def test_url_from_environment(self): self.r.replace('os.environ', dict(DB_URL='x://')) register_session() compare([ ('realRegisterSession', ('x://', u'', None, False, True, True, None, None), {}), ], self.m.method_calls)
def test_subsecond_accuracy(self): Base = declarative_base() class Sucktastic(Base): __tablename__ = 'test' id = Column(Integer, primary_key=True) value = Column(DateTime) register_session(metadata=Base.metadata) with transaction.manager: session = get_session() session.add(Sucktastic(value=datetime(2001,1,1,10,1,2,3))) session = get_session() suck = session.query(Sucktastic).one() if session.bind.name=='mysql': # whoops, there goes our sub-second info self.assertEqual(suck.value,datetime(2001,1,1,10,1,2)) else: self.assertEqual(suck.value,datetime(2001,1,1,10,1,2,3)) session.rollback()
def test_engine_passed(self): engine = object() register_session(engine=engine, ) compare([ ('realRegisterSession', (None, u'', engine, False, True, True, None, None), {}), ], self.m.method_calls)
def test_extension(self): engine = object() extension = object() register_session(engine=engine, extension=extension) compare([ ('realRegisterSession', (None, u'', engine, False, True, True, None, extension), {}), ], self.m.method_calls)
def test_engine_overrides_environment(self): self.r.replace('os.environ', dict(DB_URL='x://')) engine = object() register_session(engine=engine) compare([ ('realRegisterSession', (None, u'', engine, False, True, True, None, None), {}), ], self.m.method_calls)
def test_extension(self): engine = object() extension = object() register_session(engine=engine,extension=extension) compare([ ('realRegisterSession', (None, u'', engine, False, True, True, None, extension), {}), ],self.m.method_calls)
def test_engine_passed(self): engine = object() register_session( engine=engine, ) compare([ ('realRegisterSession', (None, u'', engine, False, True, True, None), {}), ],self.m.method_calls)
def test_url_from_environment(self): self.r.replace('os.environ',dict( DB_URL = 'x://' )) register_session() compare([ ('realRegisterSession', ('x://', u'', None, False, True, True, None), {}), ],self.m.method_calls)
def test_engine_overrides_environment(self): self.r.replace('os.environ',dict( DB_URL = 'x://' )) engine = object() register_session(engine=engine) compare([ ('realRegisterSession', (None, u'', engine, False, True, True, None), {}), ],self.m.method_calls)
def test_specified_params(self): register_session( url='x://', name='foo', echo=True, transactional=False, scoped=False, ) compare([ ('realRegisterSession', ('x://', u'foo', None, True, False, False, None, None), {}), ], self.m.method_calls)
def test_specified_params(self): register_session( url='x://', name='foo', echo=True, transactional=False, scoped=False, ) compare([ ('realRegisterSession', ('x://', u'foo', None, True, False, False, None), {}), ],self.m.method_calls)
def test_echo_but_no_url(self): # make sure there's no DBURL self.r.replace('os.environ',dict()) register_session(echo=True) compare([ ('create_engine', ('sqlite://',), {'poolclass': StaticPool, 'echo': True}), ('realRegisterSession', (None, u'', self.m.create_engine.return_value, False, True, True, None), {}), ],self.m.method_calls)
def test_empty_environment_url(self): self.r.replace('os.environ', dict(DB_URL='')) register_session() compare([ ('create_engine', ('sqlite://', ), { 'poolclass': StaticPool, 'echo': False }), ('realRegisterSession', ('', u'', self.m.create_engine.return_value, False, True, True, None, None), {}), ], self.m.method_calls)
def test_default_params(self): # ie: no DB_URL! self.r.replace('os.environ',dict()) register_session() compare([ ('create_engine', ('sqlite://',), {'poolclass': StaticPool, 'echo': False}), ('realRegisterSession', (None, u'', self.m.create_engine.return_value, False, True, True, None), {}), ],self.m.method_calls)
def test_functional_metadata(self): Base = sa_declarative_base() class Model(Base): __tablename__ = 'model' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session(transactional=False, metadata=Base.metadata) session = get_session() session.add(Model(name='foo')) session.commit()
def test_functional(self): Base = sa_declarative_base() class Model(Base): __tablename__ = 'model' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session( transactional=False, config=Config(Source(Model.__table__))) session = get_session() session.add(Model(name='foo')) session.commit()
def test_echo_but_no_url(self): # make sure there's no DBURL self.r.replace('os.environ', dict()) register_session(echo=True) compare([ ('create_engine', ('sqlite://', ), { 'poolclass': StaticPool, 'echo': True }), ('realRegisterSession', (None, u'', self.m.create_engine.return_value, False, True, True, None, None), {}), ], self.m.method_calls)
def test_default_params(self): # ie: no DB_URL! self.r.replace('os.environ', dict()) register_session() compare([ ('create_engine', ('sqlite://', ), { 'poolclass': StaticPool, 'echo': False }), ('realRegisterSession', (None, u'', self.m.create_engine.return_value, False, True, True, None, None), {}), ], self.m.method_calls)
def test_empty_environment_url(self): self.r.replace('os.environ',dict( DB_URL = '' )) register_session() compare([ ('create_engine', ('sqlite://',), {'poolclass': StaticPool, 'echo': False}), ('realRegisterSession', ('', u'', self.m.create_engine.return_value, False, True, True, None), {}), ],self.m.method_calls)
def test_tricky_to_delete(self): # respect any DB_URL set here so that # we sure the real db here to make sure # delete works across all our DB types... db_path = ( os.environ.get('DB_URL', '').strip() or 'sqlite:///'+os.path.join(self.dir.path, 'test.db') ) Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column(Integer, primary_key=True) model2_id = Column(Integer, ForeignKey('model2.id')) model2 = relationship("Model2") class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) # create in one session register_session(db_path, name='create', transactional=False, metadata=Base.metadata) m1 = Model1() m2 = Model2() m1.model2 = m2 session = get_session('create') if db_path.startswith('sqlite:'): session.execute('PRAGMA foreign_keys = ON') session.add(m1) session.add(m2) session.commit() compare(session.query(Model1).count(), 1) compare(session.query(Model2).count(), 1) session.rollback() # now register another session which should # blow the above away register_session(db_path,name='read', transactional=False, metadata=Base.metadata) session = get_session('read') compare(session.query(Model1).count(), 0) compare(session.query(Model2).count(), 0) session.rollback()
def test_tricky_to_delete(self): # respect any DB_URL set here so that # we sure the real db here to make sure # delete works across all our DB types... db_path = (os.environ.get('DB_URL', '').strip() or 'sqlite:///' + os.path.join(self.dir.path, 'test.db')) Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column(Integer, primary_key=True) model2_id = Column(Integer, ForeignKey('model2.id')) model2 = relationship("Model2") class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) # create in one session register_session(db_path, name='create', transactional=False, metadata=Base.metadata) m1 = Model1() m2 = Model2() m1.model2 = m2 session = get_session('create') if db_path.startswith('sqlite:'): session.execute('PRAGMA foreign_keys = ON') session.add(m1) session.add(m2) session.commit() compare(session.query(Model1).count(), 1) compare(session.query(Model2).count(), 1) session.rollback() # now register another session which should # blow the above away register_session(db_path, name='read', transactional=False, metadata=Base.metadata) session = get_session('read') compare(session.query(Model1).count(), 0) compare(session.query(Model2).count(), 0) session.rollback()
def test_only_some_packages(self): Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session(transactional=False, config=Config(Source(Model1.__table__))) # only table1 should have been created! compare([u'model1'], Inspector.from_engine(get_session().bind).get_table_names())
def setUp(self): register_session(transactional=False) self.Base = declarative_base() class Model(Common, self.Base): id = Column(Integer, primary_key=True) value = Column(Integer) class AnotherModel(Common, self.Base): id = Column(Integer, primary_key=True) attr = Column(Integer) other_id = Column(Integer, ForeignKey('model.id')) other = relationship("Model", backref='another') self.Model = Model self.AnotherModel = AnotherModel self.session = get_session() self.addCleanup(self.session.rollback) self.Base.metadata.create_all(self.session.bind)
def test_only_some_packages(self): Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session( transactional=False, config=Config(Source(Model1.__table__))) # only table1 should have been created! compare( [u'model1'], Inspector.from_engine(get_session().bind).get_table_names() )
def setUp(self): register_session(transactional=False) self.session = get_session() self.addCleanup(self.session.rollback) Base.metadata.create_all(self.session.bind)
def setUpClass(cls): register_session(transactional=False)