Пример #1
0
 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)
Пример #2
0
    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()
Пример #3
0
 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)
Пример #4
0
    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()
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
 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)
Пример #8
0
 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)
Пример #9
0
 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)
Пример #10
0
 def test_engine_passed(self):
     engine = object()
     register_session(
         engine=engine,
         )
     compare([
             ('realRegisterSession',
              (None, u'', engine, False, True, True, None), {}),
             ],self.m.method_calls)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
 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)
Пример #15
0
 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)
Пример #16
0
 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)
Пример #17
0
 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)
Пример #18
0
    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()
Пример #19
0
 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()
Пример #20
0
 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)
Пример #21
0
 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)
Пример #22
0
 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)
Пример #23
0
    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()
Пример #24
0
    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()
Пример #25
0
    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())
Пример #26
0
    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)
Пример #27
0
    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)
Пример #28
0
    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()
            )
Пример #29
0
 def setUp(self):
     register_session(transactional=False)
     self.session = get_session()
     self.addCleanup(self.session.rollback)
     Base.metadata.create_all(self.session.bind)
Пример #30
0
 def setUp(self):
     register_session(transactional=False)
     self.session = get_session()
     self.addCleanup(self.session.rollback)
     Base.metadata.create_all(self.session.bind)
Пример #31
0
 def setUpClass(cls):
     register_session(transactional=False)