# pylint: disable-msg=C0103,W0603
# Note about pylint disables - pylint thinks my globals are constants,
# and they're not, so disabling C0103. It also doesn't like globals,
# which I don't care much about because I want to use them here in my
# tests.
"""test-{{ snakeName }}-api.py - Tests of {{ name }} APIs"""
import logging
from .TestUtil import get_active_session, get_response_with_jwt, log_response_error

LOGGER = logging.getLogger()
TEST_SESSION = get_active_session()
added_id = -1
{% if foreignKeys|length > 0 -%}
{% for fkey in foreignKeys -%}
added_{{ fkey.name }} = -1
{% endfor %}
def setUp():
    {% for fkey in foreignKeys %}
    global added_{{ fkey.name }}
    create_{{ fkey.name }}_obj = {{ fkey.createObj }}
    resp_{{ fkey.name }} = get_response_with_jwt(TEST_SESSION, 'POST', '/{{ fkey.snakeName }}s', create_{{ fkey.name }}_obj)
    assert resp_{{ fkey.name }}.status_code == 201
    added_{{ fkey.name }} = resp_{{ fkey.name }}.json()['{{ fkey.idField }}']{% endfor %}

def tearDown():
    {% for fkey in foreignKeys %}
    global added_{{ fkey.name }}
    resp_{{ fkey.name }} = get_response_with_jwt(TEST_SESSION, 'DELETE', '/{{ fkey.snakeName }}s/{}'.format(added_{{ fkey.name }}))
    assert resp_{{ fkey.name }}.status_code == 204{% endfor %}
{% endif %}
def test_{{ snakeName }}_create():
    """{{ name }}APIs: Test create API"""
    #pylint: disable=W0603
    global added_id
    create_obj = {{ createObj }}
    {% if foreignKeys|length > 0 %}{% for fkey in foreignKeys %}create_obj['{{ fkey.name}}'] = added_{{ fkey.name }}{% endfor %}{% endif %}
    resp = get_response_with_jwt(TEST_SESSION, 'POST', '/{{ snakeName }}s', create_obj)
    log_response_error(resp)
    assert resp.status_code == 201
    {% if idCount == 1 %}json = resp.json()
    assert json['{{ name_id }}']
    added_id = json['{{ name_id }}']{% endif %}

def test_{{ snakeName }}_get():
    """--> get returns details """
    global added_id
    {% if foreignKeys|length > 0 %}{% for fkey in foreignKeys %}global added_{{ fkey.name }}{% endfor %}{% endif %}
    {% if idCount == 1 %}resp = get_response_with_jwt(TEST_SESSION, 'GET', '/{{ snakeName}}s/{}'.format(added_id)){% else %}
    resp = get_response_with_jwt(TEST_SESSION, 'GET', '/{{ snakeName }}s/{}/{}'.format({{ idsString }})){% endif %}
    log_response_error(resp)
    assert resp.status_code == 200
    json = resp.json()
    LOGGER.debug('json: {}'.format(json))
    {% for getAssert in getAsserts %}{{ getAssert }}
    {% endfor %}
{% if hasSearch -%}
def test_{{ snakeName }}_search():
    """--> tests search """
    {% if foreignKeys|length > 0 %}{% for fkey in foreignKeys %}global added_{{ fkey.name }}{% endfor %}{% endif %}
    resp = get_response_with_jwt(TEST_SESSION, 'GET', '/{{ snakeName}}s?search_text=')
    log_response_error(resp)
    assert resp.status_code == 200
    json_list = resp.json()
    LOGGER.debug('json_list: {}'.format(json_list))
    assert len(json_list) == 1
    json = json_list[0]
    {% for getAssert in getAsserts %}{{ getAssert }}
    {% endfor %}{% endif %}

def test_{{ snakeName }}_put():
    """--> put updates object """
    global added_id
    {% if foreignKeys|length > 0 %}{% for fkey in foreignKeys %}global added_{{ fkey.name }}{% endfor %}{% endif %}
    update_data = {{ unitTestEditObject }}
    {% if foreignKeys|length > 0 %}{% for fkey in foreignKeys %}update_data['{{ fkey.name}}'] = added_{{ fkey.name }}{% endfor %}{% endif %}
    {% if idCount == 1 %}resp = get_response_with_jwt(TEST_SESSION, 'PUT', '/{{ snakeName}}s/{}'.format(added_id), update_data){% else %}
    resp = get_response_with_jwt(TEST_SESSION, 'PUT', '/{{ snakeName }}s/{}/{}'.format({{ idsString }}), update_data){% endif %}
    log_response_error(resp)
    assert resp.status_code == 200
    {% if idCount == 1 %}resp2 = get_response_with_jwt(TEST_SESSION, 'GET', '/{{ snakeName}}s/{}'.format(added_id)){% else %}
    resp2 = get_response_with_jwt(TEST_SESSION, 'GET', '/{{ snakeName }}s/{}/{}'.format({{ idsString }})){% endif %}
    log_response_error(resp2)
    assert resp2.status_code == 200
    json = resp2.json()
    LOGGER.debug('json: {}'.format(json))
    {{ unitTestAssert }}

def test_{{ snakeName }}_delete():
    """--> delete returns 204"""
    {% if idCount == 1 %}resp = get_response_with_jwt(TEST_SESSION, 'DELETE', '/{{ snakeName}}s/{}'.format(added_id)){% else %}
    resp = get_response_with_jwt(TEST_SESSION, 'DELETE', '/{{ snakeName }}s/{}/{}'.format({{ idsString }})){% endif %}
    log_response_error(resp)
    assert resp.status_code == 204
