gpt4 book ai didi

python - 测试 Flask-Restful 应用程序时随机出现 'werkzeug.routing.BuildError'

转载 作者:行者123 更新时间:2023-12-02 00:04:42 25 4
gpt4 key购买 nike

所以,我在 flask-restfulfields.Url 中遇到了一些奇怪的错误,我无法真正弄清楚为什么会出现这种错误,因为它是随机发生的。当正常运行我的应用程序时,它似乎不会发生,并且只会在测试中随机失败。

值得注意的是,它仅发生在 POST 请求上,并且适用于 GET 请求。

作为引用,所有代码位于 https://github.com/Tehnix/cred-server (应用了临时修复)。

错误

在编码我的数据对象时,URL 字段的创建似乎失败了。完整的错误位于问题的末尾。

File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/werkzeug/routing.py", line 1678, in build
raise BuildError(endpoint, values, method)
werkzeug.routing.BuildError: ('events_item', {'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x102df6d68>}, None)

罪魁祸首

端点定义为,

# FILE: cred/routes.py
api.add_resource(EventsItem, '/events/<int:event_id>', endpoint='events_item')

我正在使用以下内容来编码我的 SQLAlchemy 对象,

# FILE: cred/resources/events.py
simple_event_fields = {
'id': flask.ext.restful.fields.Integer(attribute='event_id'),
'uri': flask.ext.restful.fields.Url('events_item', absolute=True),
}

其中 fields.Url 似乎由于某种原因失败。最后是处理 POST 请求的资源,

# FILE: cred/resources/events.py
class Events(flask.ext.restful.Resource):
"""Methods going to the /events route."""

def post(self):
"""Create a new event and return the id and uri of the event."""
# Parse the POST args using the parser from flask-restful into event_args
# ...
# Create the event in the database
event = EventModel(
self.client,
event_args['name'],
event_args['location'],
event_args['action'],
event_args['value']
)
# Save the event in the database
db.session.add(event)
db.session.commit()
# Finally, convert the event object into our format by marshalling it,
# and returning the JSON object
return {
'status': 201,
'message': 'Created Event',
'event': marshal(event, simple_event_fields)
}, 201

正如一开始所说,它只在运行测试时发生(而且只是有时),所以它本身并不是关键,但不必重新运行一堆测试仍然很好多次只是为了看看它是否会消失而其他所有的都会过去。

测试用例

我正在使用 Flask-Testing,但除此之外它几乎是单元测试,

# FILE: cred/test/test_events.py
test_event = {
'event': {
'name': 'Temperature',
'location': 'Living Room',
'action': 'Temperature Above Setting',
'value': '5'
}
}

class BaseTestCase(flask.ext.testing.TestCase):
SQLALCHEMY_DATABASE_URI = "sqlite://"
TESTING = True

def create_app(self):
return app

def setUp(self):
"""Create a SQLite database for quick testing."""
cred.database.init_db(cred.database.db)
self.session_key = None

def tearDown(self):
"""Close the database file and unlink it."""
cred.database.db.session.remove()
cred.database.db.drop_all()

@testutil.authenticate('write')
def test_posting_a_complete_event(self):
"""Create a valid new event."""
# Post the request to the test server
response = self.client.post(
'/events',
data=json.dumps(test_event),
content_type='application/json'
)
resp = json.loads(response.data.decode('utf-8'))
# Run self.assertEqual on all items in a dict
testutil.assertEqual(self, {
response.status_code: 201,
resp['status']: 201,
resp['message']: 'Created Event',
'id' in resp['event']: True,
'uri' in resp['event']: True
})

在上面,test_posting_a_complete_event会因为uri项而随机失败。

nosetests 的完整错误输出

======================================================================
ERROR: Create a valid new event.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/tehnix/Dropbox/github/Tehnix/cred/cred/test/util.py", line 34, in wrapped
fun(self, *args, **kwargs)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/cred/test/test_events.py", line 37, in test_posting_a_complete_event
resp = json.loads(response.data.decode('utf-8'))
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
nose.proxy.ValueError: Expecting value: line 1 column 1 (char 0)
-------------------- >> begin captured logging << --------------------
cred-server: ERROR: Exception on /events [POST]
Traceback (most recent call last):
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask_restful/__init__.py", line 265, in error_router
return original_handler(e)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask_restful/__init__.py", line 446, in wrapper
resp = resource(*args, **kwargs)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask_restful/__init__.py", line 550, in dispatch_request
resp = meth(*args, **kwargs)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/cred/resources/events.py", line 88, in post
'event': marshal(event, simple_event_fields)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask_restful/__init__.py", line 603, in marshal
return OrderedDict([(envelope, OrderedDict(items))]) if envelope else OrderedDict(items)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/collections/__init__.py", line 56, in __init__
self.__update(*args, **kwds)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/bin/../lib/python3.4/_collections_abc.py", line 602, in update
for key, value in other:
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask_restful/__init__.py", line 602, in <genexpr>
for k, v in fields.items())
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask_restful/fields.py", line 294, in output
o = urlparse(url_for(endpoint, _external=self.absolute, **data))
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/flask/helpers.py", line 305, in url_for
force_external=external)
File "/Users/tehnix/Dropbox/github/Tehnix/cred/env/lib/python3.4/site-packages/werkzeug/routing.py", line 1678, in build
raise BuildError(endpoint, values, method)
werkzeug.routing.BuildError: ('events_item', {'_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x102df6d68>}, None)

最佳答案

在返回之前,您需要在“fields.Url”内分配events_items

此链接How to add fields url for nested output fields in flask restful解释更多

但是如果您使用的是蓝图,请将“.”添加到 fields.Url 例如'uri': fields.Url('.events_items')

引用https://github.com/flask-restful/flask-restful/issues/266#issuecomment-46678118

关于python - 测试 Flask-Restful 应用程序时随机出现 'werkzeug.routing.BuildError',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com