{# Macro to get Sample input value #}
{%- macro get_input_sample(i) -%}{% if i.input_type=="select" %}"{{i["values"][0]["label"]}}"{% elif i.input_type=="multiselect" %}"{{i["values"][0]["label"]}}"{% elif i.input_type=="number" %}123{% elif i.input_type=="boolean" %}True{% elif i.input_type=="datepicker" %}1518480000000{% elif i.input_type=="datetimepicker" %}1518367008000{% elif i.input_type=="textarea" %}{"type": "text", "content": "sample line one\nsample line two"}{%else%}"sample text"{%endif%}{%- endmacro -%}

# -*- coding: utf-8 -*-
"""Tests using pytest_resilient_circuits"""

import pytest
from resilient_circuits.util import get_config_data, get_function_definition
from resilient_circuits import SubmitTestFunction, FunctionResult

PACKAGE_NAME = "{{package_name}}"
FUNCTION_NAME = "{{name}}"

# Read the default configuration-data section from the package
config_data = get_config_data(PACKAGE_NAME)

# Provide a simulation of the Resilient REST API (uncomment to connect to a real appliance)
resilient_mock = "pytest_resilient_circuits.BasicResilientMock"


def call_{{name}}_function(circuits, function_params, timeout=5):
    # Create the submitTestFunction event
    evt = SubmitTestFunction("{{name}}", function_params)

    # Fire a message to the function
    circuits.manager.fire(evt)

    # circuits will fire an "exception" event if an exception is raised in the FunctionComponent
    # return this exception if it is raised
    exception_event = circuits.watcher.wait("exception", parent=None, timeout=timeout)

    if exception_event is not False:
        exception = exception_event.args[1].args[1]
        raise exception

    # else return the FunctionComponent's results
    else:
        event = circuits.watcher.wait("{{name}}_result", parent=evt, timeout=timeout)
        assert event
        assert isinstance(event.kwargs["result"], FunctionResult)
        pytest.wait_for(event, "complete", True)
        return event.kwargs["result"].value


class Test{{name|camel}}:
    """ Tests for the {{name}} function"""

    def test_function_definition(self):
        """ Test that the package provides customization_data that defines the function """
        func = get_function_definition(PACKAGE_NAME, FUNCTION_NAME)
        assert func is not None

    mock_inputs_1 = {
        {% for i in inputs %}
        "{{i.name}}": {{get_input_sample(i)}}{{ "," if not loop.last }}
        {% endfor %}
    }

    expected_results_1 = {"value": "xyz"}

    mock_inputs_2 = {
        {% for i in inputs %}
        "{{i.name}}": {{get_input_sample(i)}}{{ "," if not loop.last }}
        {% endfor %}
    }

    expected_results_2 = {"value": "xyz"}

    @pytest.mark.parametrize("mock_inputs, expected_results", [
        (mock_inputs_1, expected_results_1),
        (mock_inputs_2, expected_results_2)
    ])
    def test_success(self, circuits_app, mock_inputs, expected_results):
        """ Test calling with sample values for the parameters """

        results = call_{{name}}_function(circuits_app, mock_inputs)
        assert(expected_results == results)
