class APITestCase(APITestCase):
    client_class = APIClient class CustomPatternModel(models.Model):
    name = models.CharField(max_length=10) class CustomTypedPatternModel(models.Model):
    name = models.CharField(max_length=10) class CustomType(models.Model):
    name = models.CharField(max_length=10)

    def __str__(self):
        return self.name class Campaign(models.Model):
    pass class Attribute(models.Model):
    name = models.CharField(max_length=10)
    campaign = models.ForeignKey(Campaign, models.CASCADE)
    is_active = models.BooleanField()
    category = models.CharField(max_length=10, blank=True) class Entity(models.Model):
    campaign = models.ForeignKey(Campaign, models.CASCADE)

    class Meta:
        verbose_name_plural = 'entities' class EntitySerializerBase(patterns.AttachedModelSerializer):
    values = ValueSerializerBase(many=True)

    class Meta:
        model = Entity
        fields = '__all__' class EntitySerializerCampaignID(EntitySerializerBase):
    values = ValueSerializerCampaignID(many=True)

    class Meta(EntitySerializerBase.Meta):
        pass class EntitySerializerIsActiveT(EntitySerializerBase):
    values = ValueSerializerIsActiveT(many=True)

    class Meta(EntitySerializerBase.Meta):
        pass class EntitySerializerIsActiveF(EntitySerializerBase):
    values = ValueSerializerIsActiveF(many=True)

    class Meta(EntitySerializerBase.Meta):
        pass class EntitySerializerActiveTCampaignID(EntitySerializerBase):
    values = ValueSerializerIsActiveTCampaignID(many=True)

    class Meta(EntitySerializerBase.Meta):
        pass class EntitySerializerCategoryDim(EntitySerializerBase):
    values = ValueSerializerCategoryDim(many=True)

    class Meta(EntitySerializerBase.Meta):
        pass class EntitySerializerCategoryEmpty(EntitySerializerBase):
    values = ValueSerializerCategoryEmpty(many=True)

    class Meta(EntitySerializerBase.Meta):
        pass class EntitySerializerCategoryCtxt(EntitySerializerBase):
    values = ValueSerializerCategoryCtxt(many=True)

    class Meta(EntitySerializerBase.Meta):
        pass
from wq.db import rest
from .base import APITestCase
from rest_framework import status
from django.contrib.auth.models import User
from tests.patterns_app.models import (
    CustomPatternModel, CustomTypedPatternModel, CustomType,
    Campaign, Attribute, Entity
    )
from tests.patterns_app.serializers import (
    EntitySerializerBase, EntitySerializerCampaignID,
    EntitySerializerIsActiveT, EntitySerializerIsActiveF,
    EntitySerializerActiveTCampaignID, EntitySerializerCategoryDim,
    EntitySerializerCategoryEmpty, EntitySerializerCategoryCtxt,
    )
                    'label': 'Name',
                    'type': 'string',
                    'wq:length': 10,
                }, {
                    'name': 'value',
                    'label': 'Value',
                    'type': 'decimal',
                }, {
                    'name': 'type',
                    'label': 'Type',
                    'type': 'select one',
                    'wq:ForeignKey': 'customtype',
                    'bind': {'required': True},
                }],
                'initial': {
                    'type_field': 'type',
                    'filter': {}
                }
            },
        ], response.data['pages']['customtypedpatternmodel']['form'])

    def test_custompattern_post(self):
        form = {
            'name': 'Test 3',

            'attachments[0][name]': 'Attachment',
            'attachments[1][name]': 'Attachment',
        }

        response = self.client.post('/custompatternmodels.json', form)
        self.assertEqual(
            response.status_code, status.HTTP_201_CREATED, response.data
        )
        self.assertEqual(response.data['name'], "Test 3")
        self.assertIn("attachments", response.data)
        atts = response.data['attachments']
        self.assertEqual(len(atts), 2)
        self.assertEqual(atts[0]['name'], 'Attachment')

    def test_custompattern_put(self):
        form = {
            'name': "Test 1'",
            'attachments[0][name]': "Attachment"
        }

        response = self.client.put(
            '/custompatternmodels/%s.json' % self.instance.pk,
            form
        )
        self.assertEqual(
            response.status_code, status.HTTP_200_OK, response.data
        )
        self.instance = CustomPatternModel.objects.get(pk=self.instance.pk)
        self.assertEqual(self.instance.name, "Test 1'")
        self.assertEqual(self.instance.attachments.count(), 1)
        self.assertEqual(self.instance.attachments.first().name, "Attachment")

    def test_customtypedpattern_post(self):
        form = {
            'name': 'Test 4',
            'attachments[0][name]': 'Attachment',
            'attachments[0][type_id]': self.type.pk,
        }

        response = self.client.post('/customtypedpatternmodels.json', form)
        self.assertEqual(
            response.status_code, status.HTTP_201_CREATED, response.data
        )
        self.assertEqual(response.data['name'], "Test 4")
        self.assertIn("attachments", response.data)
        atts = response.data['attachments']
        self.assertEqual(len(atts), 1)
        self.assertEqual(atts[0]['type_label'], 'Custom')

    def test_customtypedpattern_post_empty(self):
        form = {
            'name': 'Test 5',
            'attachments': [
                 {'value': '', 'type_id': self.type.pk, 'name': ''},
                 {'value': 0,  'type_id': self.type.pk, 'name': ''},
            ]
        }

        response = self.client.post(
            '/customtypedpatternmodels.json', form, format='json'
        )
        self.assertEqual(
            response.status_code, status.HTTP_201_CREATED, response.data
        )
        self.assertIn("attachments", response.data)
        atts = response.data['attachments']
        self.assertEqual(len(atts), 1)
        self.assertEqual(atts[0]['type_label'], 'Custom')
        self.assertEqual(atts[0]['value'], 0)

    def test_customtypedpattern_put(self):
        form = {
            'name': "Test 2'",
            'attachments[0][name]': "Attachment",
            'attachments[0][type_id]': self.type.pk,
        }

        response = self.client.put(
            '/customtypedpatternmodels/%s.json' % self.typeinstance.pk,
            form
        )
        self.assertEqual(
            response.status_code, status.HTTP_200_OK, response.data
        )
        self.typeinstance = CustomTypedPatternModel.objects.get(
            pk=self.typeinstance.pk
        )
        self.assertEqual(self.typeinstance.name, "Test 2'")
        self.assertEqual(self.typeinstance.attachments.count(), 1)
        self.assertEqual(
            self.typeinstance.attachments.first().type, self.type
        )

    def test_eavfilter_empty(self):
        rest.router.register_serializer(

            Entity,