#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""
test_in_serializers
-------------------

Tests of the fields cooperation in the serializer interfaces for serialization, de-serialization,
and validation.

"""


# Django settings:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = __name__

from django.conf.global_settings import CACHES  # NOQA
SECRET_KEY = 's3cr3t'


from rest_framework import serializers
from rest_framework.compat import six

from drf_compound_fields.fields import DictField
from drf_compound_fields.fields import ListField


class ListSerializer(serializers.Serializer):
    emails = ListField(serializers.EmailField(), required=False)


def test_non_list():
    serializer = ListSerializer(data={'emails': 'notAList'})
    assert not serializer.is_valid(), 'Non-list value should not be allowed'
    assert 'emails' in serializer.errors, 'Non-list value should produce a field error'
    assert serializer.errors['emails'], 'Non-list value error should be non-empty'


def test_invalid_list_item():
    serializer = ListSerializer(data={'emails': ['<EMAIL>', 'notAnEmail']})
    assert not serializer.is_valid(), 'Invalid list-item should not be allowed'
    assert 'emails' in serializer.errors, 'Invalid list-item should produce a field error'
    assert serializer.errors['emails'], 'Invalid list-item errors should be non-empty {0}'.format(
        serializer.errors['emails'])
    assert [1] == list(six.iterkeys(serializer.errors['emails'][0]))


def test_empty_list():
    serializer = ListSerializer(data={'emails': []})
    assert serializer.is_valid(), 'Empty list should be allowed'


def test_valid_list():
    serializer = ListSerializer(data={'emails': ['<EMAIL>']})
    assert serializer.is_valid(), 'Valid list should be allowed'


class DictSerializer(serializers.Serializer):
    emails = DictField(serializers.EmailField(), required=False)


def test_non_dict():
    serializer = DictSerializer(data={'emails': 'notADict'})
    assert not serializer.is_valid(), 'Non-dict value should not be allowed'
    assert 'emails' in serializer.errors, 'Non-dict value should produce a field error'
    assert serializer.errors['emails'], 'Non-dict value error should be non-empty'


def test_invalid_dict_value():
    serializer = DictSerializer(data={'emails': {'a': '<EMAIL>',
                                      'b': 'notAnEmail'}})
    assert not serializer.is_valid(), 'Invalid dict-value should not be allowed'
    assert 'emails' in serializer.errors, 'Invalid dict-value should produce a field error'
    assert serializer.errors['emails'], 'Invalid dict-value errors should be non-empty {0}'.format(
        serializer.errors['emails'])
    assert ['b'] == list(six.iterkeys(serializer.errors['emails'][0]))


def test_empty_dict():
    serializer = DictSerializer(data={'emails': {}})
    assert serializer.is_valid(), 'Empty dict should be allowed'


def test_valid_dict():
    serializer = DictSerializer(data={'emails': {'a': '<EMAIL>'}})
    assert serializer.is_valid(), 'Valid dict shouild be allowed'

#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""
test_in_serializers
-------------------

Tests of the fields cooperation in the serializer interfaces for serialization, de-serialization,
and validation.

"""


# Django settings:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = __name__

from django.conf.global_settings import CACHES  # NOQA
SECRET_KEY = 's3cr3t'


from rest_framework import serializers
from rest_framework.compat import six

from drf_compound_fields.fields import DictField
from drf_compound_fields.fields import ListField


class ListSerializer(serializers.Serializer):
    emails = ListField(serializers.EmailField(), required=False)


def test_non_list():
    serializer = ListSerializer(data={'emails': 'notAList'})
    assert not serializer.is_valid(), 'Non-list value should not be allowed'
    assert 'emails' in serializer.errors, 'Non-list value should produce a field error'
    assert serializer.errors['emails'], 'Non-list value error should be non-empty'


def test_invalid_list_item():
    serializer = ListSerializer(data={'emails': ['<EMAIL>', 'notAnEmail']})
    assert not serializer.is_valid(), 'Invalid list-item should not be allowed'
    assert 'emails' in serializer.errors, 'Invalid list-item should produce a field error'
    assert serializer.errors['emails'], 'Invalid list-item errors should be non-empty {0}'.format(
        serializer.errors['emails'])
    assert [1] == list(six.iterkeys(serializer.errors['emails'][0]))


def test_empty_list():
    serializer = ListSerializer(data={'emails': []})
    assert serializer.is_valid(), 'Empty list should be allowed'


def test_valid_list():
    serializer = ListSerializer(data={'emails': ['<EMAIL>']})
    assert serializer.is_valid(), 'Valid list should be allowed'


class DictSerializer(serializers.Serializer):
    emails = DictField(serializers.EmailField(), required=False)


def test_non_dict():
    serializer = DictSerializer(data={'emails': 'notADict'})
    assert not serializer.is_valid(), 'Non-dict value should not be allowed'
    assert 'emails' in serializer.errors, 'Non-dict value should produce a field error'
    assert serializer.errors['emails'], 'Non-dict value error should be non-empty'


def test_invalid_dict_value():
    serializer = DictSerializer(data={'emails': {'a': '<EMAIL>',
                                      'b': 'notAnEmail'}})
    assert not serializer.is_valid(), 'Invalid dict-value should not be allowed'
    assert 'emails' in serializer.errors, 'Invalid dict-value should produce a field error'
    assert serializer.errors['emails'], 'Invalid dict-value errors should be non-empty {0}'.format(
        serializer.errors['emails'])
    assert ['b'] == list(six.iterkeys(serializer.errors['emails'][0]))


def test_empty_dict():
    serializer = DictSerializer(data={'emails': {}})
    assert serializer.is_valid(), 'Empty dict should be allowed'


def test_valid_dict():
    serializer = DictSerializer(data={'emails': {'a': '<EMAIL>'}})
    assert serializer.is_valid(), 'Valid dict shouild be allowed'

#!/usr/bin/env python # -*- coding: utf-8 -*- test_in_serializers -------------------  Tests of the fields cooperation in the serializer interfaces for serialization, de-serialization, and validation. # Django settings: # NOQA