Debian Codegraph @

tests/test_orm_relations.py

raw symbols SCIP
1 from django.db import models
2
3 from ninja import NinjaAPI
4 from ninja.orm import create_schema
5 from ninja.testing import TestClient
6
7
8 def test_manytomany():
9 class SomeRelated(models.Model):
10 f = models.CharField()
11
12 class Meta:
13 app_label = "tests"
14
15 class ModelWithM2M(models.Model):
16 m2m = models.ManyToManyField(SomeRelated, blank=True)
17
18 class Meta:
19 app_label = "tests"
20
21 WithM2MSchema = create_schema(ModelWithM2M, exclude=["id"])
22
23 api = NinjaAPI()
24
25 @api.post("/bar")
26 def post_with_m2m(request, payload: WithM2MSchema):
27 return payload.dict()
28
29 client = TestClient(api)
30
31 response = client.post("/bar", json={"m2m": [1, 2]})
32 assert response.status_code == 200, str(response.json())
33 assert response.json() == {"m2m": [1, 2]}
34
35 response = client.post("/bar", json={"m2m": []})
36 assert response.status_code == 200, str(response.json())
37 assert response.json() == {"m2m": []}