{%- macro paramvalue(param) -%}{%if param.input_type in ["select", "multiselect"]%}self.get_select_param(kwargs.get("{{param.name}}")){%
elif param.input_type in ["textarea"]%}self.get_textarea_param(kwargs.get("{{param.name}}")){%
else %}kwargs.get("{{param.name}}"){%endif%}{%- endmacro -%}
{%- macro paramsample(param) -%}
{%if param.input_type=="select"%}{{param["values"]|list|map(attribute="label")|sample()|pretty}}{%
elif param.input_type=="multiselect"%}{{param["values"]|list|map(attribute="label")|sample(2)|pretty}}{%
elif param.input_type=="number"%}123{%
elif param.input_type=="boolean"%}True{%
elif param.input_type=="datepicker"%}1518480000000{%
elif param.input_type=="datetimepicker"%}1518367008000{%
elif param.input_type=="textarea"%}{"type": "text", "content": "line1\nline2"}{%
else%}"text"{%endif%}
{%- endmacro -%}
# -*- coding: utf-8 -*-
"""Tests using pytest_resilient_circuits"""

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

PACKAGE_NAME = "{{package}}"
FUNCTION_NAME = "{{loopvar}}"

# 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_{{loopvar}}_function(circuits, function_params, timeout=10):
    # Fire a message to the function
    evt = SubmitTestFunction("{{loopvar}}", function_params)
    circuits.manager.fire(evt)
    event = circuits.watcher.wait("{{loopvar}}_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{{loopvar|camel}}:
    """ Tests for the {{loopvar}} 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

    @pytest.mark.parametrize("{%set comma=joiner(", ")%}{%for p in functions[loopvar].view_items%}{%set pdef=function_params[p.content]%}{{comma()}}{{pdef.name}}{%endfor%}{{comma()}}expected_results", [
        ({%set comma=joiner(", ")%}{%for p in functions[loopvar].view_items%}{%set pdef=function_params[p.content]%}{{comma()}}{{paramsample(pdef)}}{%endfor%}{{comma()}}{"value": "xyz"}),
        ({%set comma=joiner(", ")%}{%for p in functions[loopvar].view_items%}{%set pdef=function_params[p.content]%}{{comma()}}{{paramsample(pdef)}}{%endfor%}{{comma()}}{"value": "xyz"})
    ])
    def test_success(self, circuits_app, {%set comma=joiner(", ")%}{%for p in functions[loopvar].view_items%}{%set pdef=function_params[p.content]%}{{comma()}}{{pdef.name}}{%endfor%}{{comma()}}expected_results):
        """ Test calling with sample values for the parameters """
        function_params = { {%set comma=joiner(",")%}{%for p in functions[loopvar].view_items%}{%set pdef=function_params[p.content]%}{{comma()}}
            "{{pdef.name}}": {{pdef.name}}{%endfor%}
        }
        results = call_{{loopvar}}_function(circuits_app, function_params)
        assert(expected_results == results)
