"""
Test case for {{ class_name }}
"""

import os
import sys
import unittest

sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), '../src'.replace('/', os.sep))))

from {{ package_name | lower }} import {{ class_name }}

{%- for import_type in import_types if import_type not in ['decimal.Decimal', 'datetime.datetime', 'datetime.date', 'datetime.time', 'datetime.timedelta'] %}
{%- set import_type_name = 'Test_'+import_type.split('.')[-1] %}
{%- set import_package_name = 'test_'+'_'.join(import_type.split('.')[:-1]) | lower %}

{%- if import_type.startswith('.') %}
from .{{ import_package_name }} import {{ import_type_name }}
{%- else %}
from {{ import_package_name }} import {{ import_type_name }}
{%- endif -%}
{%- endfor %}
{%- for import_type in import_types if import_type in ['datetime.datetime', 'datetime.date', 'datetime.time', 'datetime.timedelta'] %}
{%- if loop.first %}
import datetime
{%- endif %}
{%- endfor %}


class {{ test_class_name }}(unittest.TestCase):
    """
    Test case for {{ class_name }}
    """

    def setUp(self):
        """
        Set up test case
        """
        self.instance = {{ test_class_name }}.create_instance()

    @staticmethod
    def create_instance():
        """
        Create instance of {{ class_name }} for testing
        """
        instance = {{ class_name }}(
            {%- for field in fields %}
            {{ field.name }}={{ field.test_value }}{%- if not loop.last %},{%- endif %}
            {%- endfor %}
        )
        return instance

    {% for field in fields %}
    def test_{{ field.name }}_property(self):
        """
        Test {{ field.name }} property
        """
        test_value = {{ field.test_value }}
        self.instance.{{ field.name }} = test_value
        self.assertEqual(self.instance.{{ field.name }}, test_value)
    {% endfor %}

    {%- if avro_annotation %}
    def test_to_byte_array_avro(self):
        """
        Test to_byte_array method with avro media type
        """
        media_type = "application/vnd.apache.avro+avro"
        bytes_data = self.instance.to_byte_array(media_type)
        new_instance = {{ class_name }}.from_data(bytes_data, media_type)
        bytes_data2 = new_instance.to_byte_array(media_type)
        self.assertEqual(bytes_data, bytes_data2)
    {%- endif %}

    {%- if dataclasses_json_annotation %}
    def test_to_byte_array_json(self):
        """
        Test to_byte_array method with json media type
        """
        media_type = "application/json"
        bytes_data = self.instance.to_byte_array(media_type)
        new_instance = {{ class_name }}.from_data(bytes_data, media_type)
        bytes_data2 = new_instance.to_byte_array(media_type)
        self.assertEqual(bytes_data, bytes_data2)

    def test_to_json():
        """
        Test to_json method
        """
        json_data = self.instance.to_json()
        media_type = "application/json"
        new_instance = {{ class_name }}.from_data(json_data)
        json_data2 = new_instance.to_json()
        self.assertEqual(json_data, json_data2)
    {%- endif %}

