我想在我的Django测试期间同步调用芹菜任务而不需要运行芹菜工作者.为了达到这个目的,我CELERY_ALWAYS_EAGER=True在settings.py中指定但它似乎不起作用.所以我决定将override_settings装饰器应用于看起来像这样的特定测试
@override_settings(CELERY_ALWAYS_EAGER=True, BROKER_BACKEND='memory',
CELERY_EAGER_PROPAGATES_EXCEPTIONS=True)
def test_foo(self):
...
Run Code Online (Sandbox Code Playgroud)
不幸的是,这个测试仍然在我的芹菜工作者中调用任务.我可以缺少什么?具体来说,我正在使用Django 1.10和Celery 4.0.0.
我正在关注这个答案Python: Graph using NetworkX and mplleaflet,但不能修改它以显示节点标签。这是我正在使用的代码。
import matplotlib.pyplot as plt
import mplleaflet
import networkx as nx
small_graph = nx.Graph()
small_graph.add_node('1')
small_graph.add_node('2')
small_graph.add_node('3')
small_graph.add_node('4')
small_graph.add_edge('1', '2')
small_graph.add_edge('3', '2')
small_graph.add_edge('3', '4')
pos = {'1': [55, 10], '2': [56, 11], '3': [57, 12], '4': [58, 12]}
labels = {'1': 'Node 1', '2': 'Node 2', '3': 'Node 3', '4': 'Node 4'}
fig, ax = plt.subplots()
nx.draw_networkx_nodes(small_graph,pos=pos,node_size=40,node_color='red',edge_color='k',alpha=.5)
nx.draw_networkx_edges(small_graph,pos=pos,edge_color='gray', alpha=.4)
nx.draw_networkx_labels(small_graph,pos=pos, labels=labels, font_size=10)
plt.show()
# mplleaflet.display(fig=fig)
Run Code Online (Sandbox Code Playgroud)
如果我使用标准,plt.show()我可以看到正确显示的标签
. 但是,如果我取消注释最后一行以在地图上显示此图表,我只会得到
这绝对不是字体大小的问题,因为调整这个值没有任何区别。这完全有可能实现这一目标吗?如果没有,您推荐哪些替代方案?
我对python-requests模块有疑问。根据文档
多亏了urllib3,保持活动状态在会话中是100%自动的!您在会话中发出的任何请求都将自动重用适当的连接!
我的示例代码如下所示:
def make_double_get_request():
response = requests.get(url=API_URL, headers=headers, timeout=10)
print response.text
response = requests.get(url=API_URL, headers=headers, timeout=10)
print response.text
Run Code Online (Sandbox Code Playgroud)
但是我收到的日志告诉我们,每个请求都会启动新的HTTP连接:
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): url
DEBUG:requests.packages.urllib3.connectionpool:"GET url HTTP/1.1" 200 None
response text goes here
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): url
DEBUG:requests.packages.urllib3.connectionpool:"GET url HTTP/1.1" 200 None
response text goes here
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?通过查看带有Wireshark的数据包,似乎实际上它们已经设置了保持活动状态。
我正在使用Django Rest Framework(版本3.6.2)创建REST API。我定义了继承自GenericViewSet并具有重写retrieve方法的视图集,以实现自定义行为。
class FooViewSet(viewsets.GenericViewSet):
serializer_class = FooSerializer
def retrieve(self, request, *args, **kwargs):
...
serializer = self.get_serializer(data)
return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)
我想在从浏览器访问此端点时拥有BrowsableAPI,并json在从代码访问该端点时接收响应。我已使用以下设置配置了DRF:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'TEST_REQUEST_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'TEST_REQUEST_DEFAULT_FORMAT':'json'
}
Run Code Online (Sandbox Code Playgroud)
一切正常,我可以从浏览器访问可浏览的API,使用Postman工具发出请求时,我会得到json响应。不幸的是,在测试期间我无法获得相同的结果。
class GetFooDetailViewTest(APITestCase):
def test_get_should_return_json(self):
response = self.client.get(self.VIEW_URL)
self.assertEqual(response.content_type, "application/json")
Run Code Online (Sandbox Code Playgroud)
我希望响应将content_type设置为application/json(这是我可以在浏览器和Postman的响应中看到的标头)。但是此测试失败- response.content_type设置为None。在调试该测试时,我发现response._headers字典看起来像这样
{
'vary': ('Vary', 'Cookie'),
'x-frame-options': ('X-Frame-Options', 'SAMEORIGIN'),
'content-type': ('Content-Type', 'application/json'),
'allow': ('Allow', 'GET, PUT, DELETE, OPTIONS')
}
Run Code Online (Sandbox Code Playgroud)
因此,似乎设置了正确的标头,但没有将其填充到content_type属性中。我想念什么吗?
python django json django-rest-framework django-rest-viewsets
python ×4
django ×2
celery ×1
django-tests ×1
graph ×1
http ×1
json ×1
keep-alive ×1
leaflet ×1
matplotlib ×1
networkx ×1