我有一个简单的FastAPI应用程序,我正在尝试为其创建测试pytest。
我的目标是测试应用程序在出现不同错误时的行为方式。
\n\n我的应用程序中有一个简单的健康检查路线:
\n\nfrom fastapi import APIRouter\n\nrouter = APIRouter()\n\n\n@router.get("/health")\nasync def health():\n return "It\'s working \xe2\x9c\xa8"\nRun Code Online (Sandbox Code Playgroud)\n\n现在在我的 pytest 模块中,我正在尝试修补上述函数,以便它引发不同的错误。\n我正在使用,unittest.mock但我得到了非常奇怪的行为。
import pytest\nfrom unittest import mock\n\nfrom fastapi import HTTPException\nfrom starlette.testclient import TestClient\n\nimport app.api.health\nfrom app.main import app # this is my application (FastAPI instance) with the `router` attached\n\n\n@pytest.fixture()\ndef client():\n with TestClient(app) as test_client:\n yield test_client\n\n\ndef test_simple(client):\n def mock_health_function():\n raise HTTPException(status_code=400, detail=\'gibberish\')\n\n with mock.patch(\'app.api.health.health\', mock_health_function):\n response = client.get(HEALTHCHECK_PATH)\n\n with pytest.raises(HTTPException): # this check passes …Run Code Online (Sandbox Code Playgroud) 我有以下型号:
from django.db import models
class City(models.Model):
name = models.CharField(max_length=30)
last_update = models.DateTimeField(null=True)
class BusStop(models.Model):
city = models.ForeignKey(City, on_delete=models.CASCADE)
name = models.CharField(max_length=200, blank=True, default='')
Run Code Online (Sandbox Code Playgroud)
现在使用 Django Rest Framework,我想创建序列化器,它将返回城市详细信息以及城市中所有公交车站的列表 - 但我希望该列表仅是带有公交车站名称的字符串,如下所示:
{
"id": 1
"name": "City"
"last_update": "2019-09-19T22:13:54.851363Z"
"bus_stops": [
"stop1",
"stop2",
"stop3"
]
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我所尝试的是以下序列化器:
{
"id": 1
"name": "City"
"last_update": "2019-09-19T22:13:54.851363Z"
"bus_stops": [
"stop1",
"stop2",
"stop3"
]
}
Run Code Online (Sandbox Code Playgroud)
但这会创建其中包含“名称”的对象列表。那么,如何创建一个仅包含BusStop名称(作为字符串)的列表?
我有两个模型:User和Track。
User可以完成并获得积分Track。
用户架构:
let userSchema = new mongoose.Schema({
name: {type: String, required: true},
completedTracks: [{
type: mongoose.Schema.ObjectId,
ref: 'Track',
}],
});
Run Code Online (Sandbox Code Playgroud)
轨道架构:
let userSchema = new mongoose.Schema({
name: {type: String, required: true},
completedTracks: [{
type: mongoose.Schema.ObjectId,
ref: 'Track',
}],
});
Run Code Online (Sandbox Code Playgroud)
我正在模型中存储对已完成轨道的引用User。
我想要做的是查询多个用户,仅显示每个用户完成的曲目的_id,并添加有关新属性中得分总分的信息score。
我想实现的结果示例:
let trackSchema = new mongoose.Schema({
points: {
type: Number,
default: 0,
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试创建虚拟来返回分数,但它user.completedTracks不起作用ObjectIds
[
{
"completedTracks": [
"5a9f14f18e04b3225953954c",
"5aa5a8fa7367661c03f57a99",
"5aa1aa5a7da9fd2946b01c04" …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Django 管理站点中为我的模型创建自定义视图。我ModelAdmin为我的模型创建了Document这样的名字:
from django.http import HttpResponse
from django.urls import path
from django.contrib import admin
from my_app.models import Document
@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path('my-view/', self.admin_site.admin_view(self.my_view))
]
return urls + custom_urls
def my_view(self, request):
return HttpResponse('test')
Run Code Online (Sandbox Code Playgroud)
根据文档(v2.1),我应该能够访问my_view,/admin/my_app/document/my-view但是当我尝试访问此 URL 时,django 将我重定向到管理员主页,并显示以下警告:
Document with ID "my-view" doesn't exist. Perhaps it was deleted?
Run Code Online (Sandbox Code Playgroud)
所以看起来 django 看不到我的自定义网址。我错过了什么?我怎样才能让它发挥作用?
django ×2
django-admin ×1
fastapi ×1
javascript ×1
mongodb ×1
mongoose ×1
node.js ×1
pytest ×1
python ×1
starlette ×1