Skip to content

Commit

Permalink
Merge 6c96984 into 45f94a8
Browse files Browse the repository at this point in the history
  • Loading branch information
shosca committed Jun 26, 2019
2 parents 45f94a8 + 6c96984 commit 44ae44e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 14 deletions.
20 changes: 9 additions & 11 deletions .importanizerc
@@ -1,13 +1,11 @@
{
"add_imports": [
"from __future__ import absolute_import, print_function, unicode_literals"
],
"length": 120,
"groups": [
{"type": "stdlib"},
{"type": "remainder"},
{"type": "packages", "packages": ["django"]},
{"type": "packages", "packages": ["rest_framework"]},
{"type": "local"}
]
"length": 120,
"groups": [
{ "type": "stdlib" },
{ "type": "remainder" },
{ "type": "packages", "packages": ["django"] },
{ "type": "packages", "packages": ["rest_framework"] },
{ "type": "packages", "packages": ["rest_dataclasses"] },
{ "type": "local" }
]
}
20 changes: 20 additions & 0 deletions .travis.yml
@@ -0,0 +1,20 @@
---
language: python
dist: xenial
cache:
directories:
- $HOME/.cache/pip
- $HOME/.cache/pre-commit

python:
- "3.7"

install:
- pip install -U -r requirements.txt

script:
- make lint
- make coverage

after_success:
coveralls
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -36,7 +36,7 @@ def read(fname):
author=about["__author__"],
author_email=about["__author_email__"],
description=about["__description__"],
install_requires=["djangorestframework"],
install_requires=["django", "djangorestframework"],
license="MIT",
long_description=read("README.rst"),
name="django-rest-dataclasses",
Expand Down
58 changes: 56 additions & 2 deletions tests/test_serializers.py
Expand Up @@ -2,13 +2,14 @@
from __future__ import absolute_import, print_function, unicode_literals
import dataclasses as da

from rest_dataclasses.serializers import DataclassSerializer

from django.core.exceptions import ValidationError as DjangoValidationError
from django.test import SimpleTestCase

from rest_framework import fields
from rest_framework.exceptions import ValidationError

from rest_dataclasses.serializers import DataclassSerializer


@da.dataclass
class User:
Expand Down Expand Up @@ -232,3 +233,56 @@ class Meta:

with self.assertRaises(ValidationError):
serializer.save()

def test_create_star_source(self):
class Serializer(DataclassSerializer):
class Meta:
model = User
fields = ["name"]

class StarSerializer(DataclassSerializer):
user = Serializer(source="*")

class Meta:
model = User
fields = ["id", "user"]

serializer = StarSerializer(data={"user": {"name": "shosca"}, "id": 1})
serializer.is_valid(raise_exception=True)
user = serializer.save()

self.assertDictEqual(da.asdict(user), {"id": 1, "name": "shosca", "email": None})

def test_create_source_not_in_validated_data(self):
class Serializer(DataclassSerializer):
class Meta:
model = Line
fields = "__all__"

serializer = Serializer(data={})
serializer.is_valid(raise_exception=True)
line = serializer.save()

self.assertDictEqual(da.asdict(line), {"a": None, "b": None})

def test_validation_error_on_save(self):
class Serializer(DataclassSerializer):
class Meta:
model = User
fields = ["name"]

def perform_update(self, instance, validated_data, errors):
raise DjangoValidationError("test")

class StarSerializer(DataclassSerializer):
user = Serializer(source="*")

class Meta:
model = User
fields = ["id", "user"]

serializer = StarSerializer(data={"user": {"name": "shosca"}, "id": 1})
serializer.is_valid(raise_exception=True)

with self.assertRaises(ValidationError):
serializer.save()
16 changes: 16 additions & 0 deletions tests/test_utils.py
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import unittest

from django.core.exceptions import ValidationError

from rest_dataclasses.utils import _django_to_drf


class TestUtils(unittest.TestCase):
def test_django_to_drf(self):
self.assertEqual(_django_to_drf("hello"), "hello")
self.assertEqual(_django_to_drf(["hello"]), ["hello"])
self.assertEqual(_django_to_drf({"hello": "world"}), {"hello": "world"})
self.assertEqual(_django_to_drf(ValidationError("hello")), ["hello"])
self.assertEqual(_django_to_drf(ValidationError({"hello": "world"})), {"hello": ["world"]})

0 comments on commit 44ae44e

Please sign in to comment.