(我正在使用 SQLAlchemy、SQLite3、Flask-SQLAlchemy、Flask 和 Python)
我正在实施一个待办事项列表提要,用户可以在其中创建帖子 ( class Post) 并将任务 ( class Task)附加到每个帖子。每个任务可以有多个帖子。每个帖子可以有多个任务。我在使用 SQLAlchemy 并从表中删除时遇到问题。有趣的是:
task.posts.count() == 0) 时,从数据库中删除成功task.posts.count() > 0) 时,从数据库中删除会引发错误。这是错误:
sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush.
To begin a new transaction with this Session, first issue Session.rollback().
Original exception was: DELETE statement on table 'tasks_posts' expected to delete 1 row(s); Only 0 were matched.
Run Code Online (Sandbox Code Playgroud)
这是 Post & Task …
因此,我有Client一个具有此方法的结构,该方法UserByID向端点发出HTTP请求User。我想对该功能进行单元测试,但也不想在该功能中发出实际的HTTP请求c.Request。我想通过响应和错误控制该功能。
func (c Client) UserByID(id string) (u User, err error) {
v := url.Values{}
v.Set("id", id)
opts := Request{
HTTP: http.Request{
Method: http.MethodGet,
Form: v,
},
URL: 'some/endpoint/users',
}
resp, err := c.Request(opts)
err = json.Unmarshal(resp, &u)
return
}
Run Code Online (Sandbox Code Playgroud)
这是存根的样子:
type mockClient struct {
Client
fakeUser User
fakeError error
}
func (mc mockClient) Request(opts Request) (resp []byte, err error) {
resp, err = json.Marshal(mc.fakeUser)
err = mc.fakeError
return
}
Run Code Online (Sandbox Code Playgroud)
在一个测试中,我有类似以下内容:
client := …Run Code Online (Sandbox Code Playgroud) 说我有字符串str = "ASimpleNoSpaceTitle".我似乎无法理解如何使用正则表达式来分割和提取所有大写单词以便我得到["A", "Simple", "No", "Space", "Title"].
什么是正常表达将完成这项工作?
更新:带有和没有空格/大写的单词串怎么样?像"ASimpleNoSpaceTitle and a subtitle"到["A", "Simple", "No", "Space", "Title", "and", "a", "subtitle"]
flask ×1
go ×1
oop ×1
python ×1
regex ×1
ruby ×1
sqlalchemy ×1
sqlite ×1
testing ×1
unit-testing ×1