Skip to content

Commit f8ed5cb

Browse files
authored
Merge pull request #2 from eadwinCode/codev_test_fix
Codev Test Fix
2 parents 8fd31ee + f7336c8 commit f8ed5cb

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tests/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ def __str__(self):
2121

2222
class Client(models.Model):
2323
key = models.CharField(max_length=20, unique=True)
24+
25+
26+
class Day(models.Model):
27+
name = models.CharField(max_length=20, unique=True)
28+
29+
30+
class Week(models.Model):
31+
name = models.CharField(max_length=20, unique=True)
32+
days = models.ManyToManyField(Day)

tests/test_converters.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.db.models import Manager
88

99
from ninja_schema import ModelSchema
10+
from tests.models import Week
1011

1112

1213
def test_inheritance():
@@ -347,3 +348,24 @@ class Config:
347348
data = BarSchema.from_orm(bar).dict()
348349

349350
assert data == {"id": 1, "m2m": [1]}
351+
352+
353+
def test_manytomany_validation():
354+
bar = Mock()
355+
bar.pk = "555555s"
356+
357+
foo = Mock()
358+
foo.pk = 1
359+
360+
class WeekSchema(ModelSchema):
361+
class Config:
362+
model = Week
363+
364+
with pytest.raises(Exception, match="Invalid type"):
365+
WeekSchema(name="FirstWeek", days=["1", "2"])
366+
367+
with pytest.raises(Exception, match="Invalid type"):
368+
WeekSchema(name="FirstWeek", days=[bar, bar])
369+
370+
schema = WeekSchema(name="FirstWeek", days=[foo, foo])
371+
assert schema.dict() == {"id": None, "name": "FirstWeek", "days": [1, 1]}

0 commit comments

Comments
 (0)