# coding: utf-8

"""
    Kestra EE

    All API operations, except for Superadmin-only endpoints, require a tenant identifier in the HTTP path.<br/> Endpoints designated as Superadmin-only are not tenant-scoped.

    The version of the OpenAPI document: v1
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


import unittest, pytest
from kestrapy import Configuration, KestraClient, KVControllerApiDeleteBulkRequest


class TestKVApi(unittest.TestCase):
    """KVApi unit test stubs"""

    def setUp(self) -> None:
        configuration = Configuration()
        configuration.host = "http://localhost:9902"
        configuration.username = "root@root.com"
        configuration.password = "Root!1234"

        self.kestra_client = KestraClient(configuration)
        self.tenant = "main"
        self.namespace = "test.namespace"

    def tearDown(self) -> None:
        pass

    def test_set_key_value(self) -> None:
        """Test case for set_key_value

        Puts a key-value pair in store
        """
        key = "test_set_key_value"
        value = "hello-kestra"

        self.kestra_client.kv.set_key_value(namespace=self.namespace, key=key, tenant=self.tenant, body=value)

        fetched = self.kestra_client.kv.key_value(namespace=self.namespace, key=key, tenant=self.tenant)
        assert getattr(fetched, 'value', None) is not None or fetched is not None

        try:
            self.kestra_client.kv.delete_key_value(namespace=self.namespace, key=key, tenant=self.tenant)
        except Exception:
            pass

    def test_get_key_value(self) -> None:
        """Test case for get_key_value

        Get value for a key
        """
        key = "test_get_key_value"
        value = "value-get"

        self.kestra_client.kv.set_key_value(namespace=self.namespace, key=key, tenant=self.tenant, body=value)
        fetched = self.kestra_client.kv.key_value(namespace=self.namespace, key=key, tenant=self.tenant)
        assert fetched is not None

    def test_list_keys(self) -> None:
        """Test case for list_keys

        List all keys for a namespace
        """
        key = "test_list_keys"
        value = "value-list"

        self.kestra_client.kv.set_key_value(namespace=self.namespace, key=key, tenant=self.tenant, body=value)

        entries = self.kestra_client.kv.list_keys(namespace=self.namespace, tenant=self.tenant)
        assert any(getattr(e, 'key', None) == key for e in entries)

    @pytest.mark.skip(reason="flaky with the run-test, works fine on 8080")
    def test_list_keys_with_inheritence(self) -> None:
        """Test case for list_keys_with_inheritence

        List all keys for inherited namespaces
        """
        key = "test_list_keys_with_inheritence"
        value = "value-inherited"
        parent_namespace = "test"
        child_namespace = f"{parent_namespace}.namespace"
        self.kestra_client.kv.set_key_value(namespace=parent_namespace, key=key, tenant=self.tenant, body=value)

        # Check with parent namespace before
        entries = self.kestra_client.kv.list_keys(namespace=parent_namespace, tenant=self.tenant)
        assert any(getattr(e, 'key', None) == key for e in entries)

        found = False
        entries = self.kestra_client.kv.list_keys_with_inheritence(namespace=child_namespace, tenant=self.tenant)
        if any(getattr(e, 'key', None) == key for e in entries):
            found = True

        assert found, f"key {key} not found after 3 attempts"

    def test_delete_key_value(self) -> None:
        """Test case for delete_key_value

        Delete a key-value pair
        """
        key = "test_delete_key_value"
        value = "to-delete"

        self.kestra_client.kv.set_key_value(namespace=self.namespace, key=key, tenant=self.tenant, body=value)

        deleted = self.kestra_client.kv.delete_key_value(namespace=self.namespace, key=key, tenant=self.tenant)
        assert deleted is True or deleted is None

        with self.assertRaises(Exception):
            self.kestra_client.kv.key_value(namespace=self.namespace, key=key, tenant=self.tenant)

    def test_delete_key_values(self) -> None:
        """Test case for delete_key_values

        Bulk-delete multiple key/value pairs from the given namespace.
        """
        key1 = "test_delete_key_values_1"
        key2 = "test_delete_key_values_2"
        self.kestra_client.kv.set_key_value(namespace=self.namespace, key=key1, tenant=self.tenant, body="v1")
        self.kestra_client.kv.set_key_value(namespace=self.namespace, key=key2, tenant=self.tenant, body="v2")

        req = KVControllerApiDeleteBulkRequest(keys=[key1, key2])
        resp = self.kestra_client.kv.delete_key_values(namespace=self.namespace, tenant=self.tenant, kv_controller_api_delete_bulk_request=req)

        assert resp is not None

        for k in (key1, key2):
            with self.assertRaises(Exception):
                self.kestra_client.kv.key_value(namespace=self.namespace, key=k, tenant=self.tenant)


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