{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}

{{ macros.header(["BankAccount"]) }}
{% macro test_case(case) -%}
    def test_{{ case["description"] | to_snake }}(self):
        account = BankAccount()
        {%- set inputs = case["input"]["operations"] -%}
        {%- if case["expected"]["error"] -%}
            {%- set error_case = true -%}
            {%- set error_msg =  case["expected"]["error"] -%}
            {%- set error_operation = inputs[-1]["operation"] -%}
            {%- set final_value = inputs[-1]["amount"] -%}
            {%- set inputs = inputs[:-1] -%}
        {% endif %}
        {%- for input in inputs -%}
            {%- set operation = input["operation"] -%}
            {%- set value = input["amount"] -%}
            {%- set expected = case["expected"] %}
            {%- if operation and value %}
        account.{{ operation }}({{ value }})
            {%- elif not value and operation %}
                {%- if not error_case and operation == "balance" %}
        self.assertEqual(account.get_balance(), {{ expected }})
                {%- else  %}
        account.{{ operation }}()
                {%- endif  %}
            {%- endif  %}
        {%- endfor %}
        {%- if error_case %}
        with self.assertRaises(ValueError) as err:
            {%- if error_operation == "balance" %}
            account.get_balance({{ final_value if final_value else "" }})
            {%- else  %}
            account.{{ error_operation }}({{ final_value if final_value else "" }})
            {%- endif  %}
        self.assertEqual(type(err.exception), ValueError)
        self.assertEqual(err.exception.args[0], "{{ error_msg }}")
            {%- endif  %}
{% endmacro %}


class {{ exercise | camel_case }}Test(unittest.TestCase):
    {% for case in cases -%}
        {{ test_case(case) }}
    {% endfor %}
    {% if additional_cases | length -%}

        # Additional tests for this track
        {% for case in additional_cases -%}
            {{ test_case(case) }}
        {% endfor %}
    {%- endif %}
