from django.forms import model_to_dict

class Test{{ model_name }}Serializer(TestCase):

    def test_expected_fields(self):
        """
        This test verifies what fields are expected when serializing a {{ model_name }}
        using a {{ model_name }}Serializer.
        """
        {{ model_name | to_snake_case}} = {{ model_name }}Factory.create()
        serializer = {{ model_name }}Serializer(instance={{ model_name | to_snake_case}})
        {{ model_name | to_snake_case}}_data = serializer.data
        {{ model_name | to_snake_case}}_data_keys = [{% for field in fields %}'{{ field }}', {% endfor %}]
        self.assertEqual(set({{ model_name | to_snake_case}}_data.keys()), set({{ model_name | to_snake_case}}_data_keys))

    def test_creation(self):
        """
        This test verifies that the serializer can create a {{ model_name }} in the database.
        """
        # Create a {{ model_name }} object to serialize
        {{ model_name | to_snake_case}} = {{ model_name }}Factory.create()

        # Convert the model to dictionary
        {{ model_name | to_snake_case}}_dict = model_to_dict({{ model_name | to_snake_case}})
        # Delete the object from the database
        {{ model_name | to_snake_case}}.delete()
        # Eliminate the primary key (id) from the dictionary
        {{ model_name | to_snake_case}}_dict.pop('id')

        serializer = {{ model_name }}Serializer(data={{ model_name | to_snake_case}}_dict)
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertEqual({{ model_name }}.objects.count(), 1)
    {%  for var_name in string_vars%}
    def test_update_{{ var_name }}(self):
        {{ model_name | to_snake_case}} = {{ model_name }}Factory.create({{ var_name }}='OLD {{ var_name | upper }}')

        {{ model_name | to_snake_case}}_dict = model_to_dict({{ model_name | to_snake_case}})

        {{ model_name | to_snake_case}}_dict['{{ var_name }}'] = 'NEW {{ var_name | upper }}'

        serializer = {{ model_name }}Serializer(data={{ model_name | to_snake_case}}_dict, instance={{ model_name | to_snake_case}})
        self.assertTrue(serializer.is_valid())
        serializer.save()
        self.assertEqual({{ model_name }}.objects.filter({{ var_name }}='NEW {{ var_name | upper}}').count(), 1)
    {% endfor %}

    def test_serialize_many(self):
        {{ model_name }}Factory.create_batch(10)
        {{ model_name | to_snake_case}}s = {{ model_name }}.objects.all()
        serializer = {{ model_name }}Serializer({{ model_name | to_snake_case}}s, many=True)

        {{ model_name | to_snake_case}}_data_many = serializer.data

        #write_assertions({{ model_name | to_snake_case}}_data_many, '{{ model_name | to_snake_case}}_data_many', type_only=True)
        self.fail('Not implemented')
