- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在编写 Pyramid 单元测试套件时,对执行 SQLAlchemy 调用的 View 进行单元测试的正确或适当方法是什么。示例:
def my_view(request):
DBSession.query(DeclarativeBase).all()
我会使用 Mock()
和 patch
将 DBSession
的范围覆盖到 DummyDB 类吗?
最佳答案
你可以,我很快就会写博客/演讲/举例。这是全新的东西。先睹为快:
import mock
from sqlalchemy.sql import ClauseElement
class MockSession(mock.MagicMock):
def __init__(self, *arg, **kw):
kw.setdefault('side_effect', self._side_effect)
super(MockSession, self).__init__(*arg, **kw)
self._lookup = {}
def _side_effect(self, *arg, **kw):
if self._mock_return_value is not mock.sentinel.DEFAULT:
return self._mock_return_value
else:
return self._generate(*arg, **kw)
def _get_key(self, arg, kw):
return tuple(self._hash(a) for a in arg) + \
tuple((k, self._hash(kw[k])) for k in sorted(kw))
def _hash(self, arg):
if isinstance(arg, ClauseElement):
expr = str(arg.compile(compile_kwargs={"literal_binds": True}))
return expr
else:
assert hash(arg)
return arg
def _generate(self, *arg, **kw):
key = self._get_key(arg, kw)
if key in self._lookup:
return self._lookup[key]
else:
self._lookup[key] = ret = MockSession()
return ret
if __name__ == '__main__':
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Foo(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
x = Column(Integer)
y = Column(Integer)
sess = MockSession()
# write out queries as they would in the code, assign return_value
sess.query(Foo.x).filter(Foo.x == 5).first.return_value = 5
sess.query(Foo.x).filter(Foo.x == 2).first.return_value = 2
sess.query(Foo).filter(Foo.x == 2).filter_by(y=5).all.return_value = [Foo(x=2, y=5)]
sess.query(Foo).filter(Foo.x == 9).all.return_value = [Foo(x=9, y=1), Foo(x=9, y=2)]
# those queries are now replayable and will return what was assigned.
print sess.query(Foo.x).filter(Foo.x == 5).first()
print sess.query(Foo.x).filter(Foo.x == 2).first()
print sess.query(Foo).filter(Foo.x == 2).filter_by(y=5).all()
print sess.query(Foo).filter(Foo.x == 9).all()
我实际上已经将它分配到设置/拆卸中的全局 ScopedSession
中,它的效果非常好:
from myapp.model import MyScopedSession
def setUp(self):
MyScopedSession.registry.set(MockSession())
# set up some queries, we can usually use scoped_session's proxying
MyScopedSession.query(User).filter_by(id=1).first.return_value = User(id=1)
def tearDown(self):
MyScopedSession.remove()
def some_test(self):
# to get at mock methods and accessors, call the scoped_session to get at
# the registry
# ... test some thing
# test a User was added to the session
self.assertEquals(
MyScopedSession().add.mock_calls,
[mock.call(User(name='someuser'))]
)
关于python - 使用 SQLAlchemy DBSession 对 Pyramid View 进行单元测试的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615054/
在构建 Pyramid 应用程序(SQLAlchemy 作为 ORM)时,我遇到了一个我似乎无法弄清楚的问题。 使用 SQLAlchemy 脚手架构建的项目,因此所有设置都是正确的。 当我在 View
这是我的场景:第一个 View 呈现表单,数据进入第二个 View ,我将其存储在数据库(MySQL)中并重定向到第三个 View ,显示写入数据库的内容: Stoing to db: DBS
在这个脚本中第二次调用行 121 http://paste.pocoo.org/show/520040/ ,我收到此错误消息: *** IntegrityError: (IntegrityError)
为了在我的 iOS 应用程序中使用 Dropbox Core API,我点击了以下链接:https://www.dropbox.com/developers/core/start/ios 但是,这都是
在编写 Pyramid 单元测试套件时,对执行 SQLAlchemy 调用的 View 进行单元测试的正确或适当方法是什么。示例: def my_view(request): DBSessio
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
*** LOCAL GEMS *** rails (3.2.8) heroku (2.33.0) heroku-api (0.3.6) taps (0.3.24) 尽管直到今天早上已经工作了几个月,运
我是一名优秀的程序员,十分优秀!