from collections import OrderedDict

import pytest

from adhocracy4.exports.mixins import ExportModelFieldsMixin
from tests.apps.ideas.models import Idea


@pytest.mark.django_db
def test_model_fields_mixin(idea):
    class Mixin(ExportModelFieldsMixin):
        model = Idea
        fields = ['description', 'name']
        html_fields = ['description']

    mixin = Mixin()

    virtual = mixin.get_virtual_fields(OrderedDict())
    assert list(virtual.items()) == [
        ('description', 'Description'),
        ('name', 'name')
    ]

    idea.description = '&nbsp; &amp;&euro;&lt;&quot;&auml;&ouml;&uuml;' \
                       '&#x1F4A9;&nbsp; '
    assert mixin.get_description_data(idea) == '&€<"äöü💩'


@pytest.mark.django_db
def test_model_fields_mixin_exclude(idea):
    class Mixin(ExportModelFieldsMixin):
        model = Idea
        exclude = ['moderator_feedback', 'moderator_statement',
                   'point', 'point_label']

    mixin = Mixin()

    virtual = mixin.get_virtual_fields({})

    assert sorted(virtual.keys()) == ['category', 'created', 'creator',
                                      'description', 'id', 'labels',
                                      'modified', 'module', 'name']

@@ -1212,16 +1212,437 @@
 odule', 'name'%5D%0A
+%0A%0A@pytest.mark.django_db%0Adef test_model_fields_mixin_related_fields(idea):%0A    class Mixin(ExportModelFieldsMixin):%0A        model = Idea%0A        fields = %5B'name', 'module'%5D%0A        related_fields = %7B'module': %5B'name', 'description'%5D%7D%0A%0A    mixin = Mixin()%0A%0A    virtual = mixin.get_virtual_fields(%7B%7D)%0A%0A    assert sorted(virtual.keys()) == %5B'module_description', 'module_name',%0A                                      'name'%5D%0A

test related_fields option in base mixin