import json

import pytest

from rohrpost.mixins import PushNotificationOnChangeModelMixin


class ReplyChannel:
    def __init__(self):
        self.data = []
        self.closed = False

    def send(self, message_dict):
        if not self.closed:
            self.data.append(json.loads(message_dict.get('text')))
            self.closed = message_dict.get('close', False)


class Message:
    def __init__(self):
        self.reply_channel = ReplyChannel()


@pytest.fixture
def message():
    return Message()


class MockModel:
    def save(self, *args, **kwargs):
        if not self.pk:
            self.pk = 1
        return self

    def delete(self, *args, **kwargs):
        pass


class PlainExampleModel(PushNotificationOnChangeModelMixin, MockModel):
    def __init__(self):
        self.pk = None
        self.name = 'test object name'

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        super().delete(*args, **kwargs)


class ModelWithAttrMixin:
    group_name = 'attribute-example-{pk}'


class ModelWithMethodMixin:
    def get_group_name(self):
        return 'method-example-{self.pk}'.format(self=self)


class ModelWithDataMixin:
    def get_push_notification_data(self):
        return {
            'extra_name': self.name,
            'extra_name_backwards': self.name[::-1],
        }


@pytest.fixture
def plain_obj():
    return PlainExampleModel()


@pytest.fixture
def obj_with_attr():
    class ModelWithAttr(ModelWithAttrMixin, PlainExampleModel):
        pass
    return ModelWithAttr()


@pytest.fixture
def obj_with_method():
    class ModelWithMethod(ModelWithMethodMixin, PlainExampleModel):
        pass
    return ModelWithMethod()


@pytest.fixture
def obj_with_method_and_attr():
    class ModelWithMethodAndAttr(ModelWithMethodMixin, ModelWithAttrMixin, PlainExampleModel):
        pass
    return ModelWithMethodAndAttr()


@pytest.fixture
def obj_with_data():
    class ModelWithData(ModelWithDataMixin, PlainExampleModel):
        pass
    return ModelWithData()

@@ -1000,24 +1000,253 @@
 **kwargs)%0A%0A%0A
+class ExampleSerializer:%0A    def __init__(self, obj):%0A        self.obj = obj%0A%0A    @property%0A    def data(self):%0A        return %7B%0A            'serialized_id': self.obj.pk,%0A            'serialized_name': self.obj.name,%0A        %7D%0A%0A%0A
 class ModelW
@@ -1610,16 +1610,91 @@
     %7D%0A%0A%0A
+class ModelWithSerializerMixin:%0A    serializer_class = ExampleSerializer%0A%0A%0A
 @pytest.
@@ -2272,113 +2272,490 @@
 ith_
-data():%0A    class ModelWithData(ModelWithDataMixin, PlainExampleModel):%0A        pass%0A    return ModelWith
+serializer():%0A    class ModelWithSerializer(ModelWithSerializerMixin, PlainExampleModel):%0A        pass%0A    return ModelWithSerializer()%0A%0A%0A@pytest.fixture%0Adef obj_with_data():%0A    class ModelWithData(ModelWithDataMixin, PlainExampleModel):%0A        pass%0A    return ModelWithData()%0A%0A%0A@pytest.fixture%0Adef obj_with_serializer_and_data():%0A    class ModelWithSerializerAndData(ModelWithSerializerMixin, ModelWithDataMixin, PlainExampleModel):%0A        pass%0A    return ModelWithSerializerAnd
 Data

Add a test serializer