{{ header }}
# These tests are also run as part of the following suites:
#
{% for suite in tests.suites %}
#    {{ suite.suite }}
{% endfor %}
#
# The following processes are executed for these tests:
#
{% for proc in tests.processes %}
#    {{ proc.tool }}:
  {% for p in proc.processes %}
#      {{ p.name }}
  {% endfor %}
{% endfor %}

import unittest
import datetime
from os import path

from etltest.data_connector import DataConnector
from etltest.process_executor import ProcessExecutor
from etltest.utilities.settings_manager import SettingsManager


class {{ testGroup }}Test(unittest.TestCase):

    def setUp(self):
          # Queries for loading test data.
      {% for set in tests.dataset %}
            DataConnector("{{ set.source }}").insert_data("{{ set.table }}", {{ set.records }})
      {% endfor %}

      {% for tool in tests.processes %}
            {{ tool.tool }}_settings = SettingsManager().get_tool("{{ tool.tool }}")
            {{ tool.tool }}_code_path = SettingsManager().system_variable_replace({{ tool.tool }}_settings["code_path"])
      {% for job in tool.processes %}
            ProcessExecutor("{{ tool.tool }}").execute_process("{{ job.type }}",
            path.join({{ tool.tool }}_code_path, "{{ job.name }}"))
      {% endfor %}
      {% endfor %}

    def tearDown(self):
       # Clean up testing environment.

      {% for set in tests.dataset %}
        DataConnector("{{ set.source }}").truncate_data("{{ set.table }}")
      {% endfor %}

{% for test in tests.tests %}
    def {{ test.name }}(self):
        # {{ test.desc }}

        given_result = DataConnector("{{ test.query.source }}").select_data("{{ test.query.select }}",
                        "{{ test.query.from }}", "{{ test.query.where }}")
      {% if test.query.result is defined and test.query.result not in ('BooleanTrue', 'BooleanFalse', 'IsNone', 'IsNotNone')%}

        expected_result = [{{ test.query.result }}]
      {% endif %}

      {% if test.type == 'Equal' or test.type is not defined %}
        self.assertEqual(given_result, expected_result)
      {% elif test.type == 'NotEqual' %}
        self.assertNotEqual(given_result, expected_result)
      {% elif test.type == 'BooleanTrue' %}
        self.assertTrue(given_result)
      {% elif test.type == 'BooleanFalse' %}
        self.assertFalse(given_result)
      {% elif test.type == 'Is' %}
        self.assertIs(given_result, expected_result)
      {% elif test.type == 'IsNot' %}
        self.assertIsNot(given_result, expected_result)
      {% elif test.type == 'IsNone' %}
        self.assertIsNone(given_result)
      {% elif test.type == 'IsNotNone' %}
        self.assertIsNotNone(given_result)
      {% elif test.type == 'In' %}
        self.assertIn(given_result, expected_result)
      {% elif test.type == 'NotIn' %}
        self.assertNotIn(given_result, expected_result)
      {% elif test.type == 'IsInstance' %}
        self.assertIsInstance(given_result, expected_result)
      {% elif test.type == 'IsNotInstance' %}
        self.assertIsNotInstance(given_result, expected_result)
      {% else %}
        self.assertEqual(given_result, expected_result)
      {% endif %}

{% endfor %}

if __name__ == "__main__":
    unittest.main()