# (c) cavaliba.com - test / views / data related objects

from django.core.cache import cache as cache_django
from django.test import TestCase, override_settings
from django.urls import reverse

import app_home.cache as cache
from tests import helper


class TestViewsDataRelated(TestCase):
    def setUp(self):
        cache.clear()
        cache_django.clear()

    def _setup_related_schemas(self):
        helper.add_schema("tschema_b", displayname="Schema B")
        helper.add_schema(
            "tschema_a",
            displayname="Schema A",
            field_definition={
                "ref_b": {
                    "dataformat": "schema",
                    "dataformat_ext": "tschema_b",
                }
            },
        )
        helper.add_instance("tschema_b", "inst_b1")
        helper.add_instance("tschema_b", "inst_b2")
        helper.add_instance("tschema_a", "inst_a1", fields={"ref_b": "inst_b1"})
        helper.add_instance("tschema_a", "inst_a2", fields={"ref_b": "inst_b1"})
        helper.add_instance("tschema_a", "inst_a3", fields={"ref_b": "inst_b2"})

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_instance_detail_related_table_present(self):
        helper.add_admin_user(login="unittest")
        self._setup_related_schemas()

        from app_data.models import DataInstance

        b1 = DataInstance.objects.get(classname="tschema_b", keyname="inst_b1")

        response = self.client.get(
            reverse("app_data:instance_detail", kwargs={"id": b1.id}), follow=True
        )
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "related-objects-2")

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_instance_detail_related_link_format(self):
        helper.add_admin_user(login="unittest")
        self._setup_related_schemas()

        from app_data.models import DataInstance

        b1 = DataInstance.objects.get(classname="tschema_b", keyname="inst_b1")

        response = self.client.get(
            reverse("app_data:instance_detail", kwargs={"id": b1.id}), follow=True
        )
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "query=RELATED:tschema_b:inst_b1")

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_instance_list_related_filter(self):
        helper.add_admin_user(login="unittest")
        self._setup_related_schemas()

        response = self.client.get(
            reverse("app_data:instance_list", kwargs={"classname": "tschema_a"})
            + "?query=RELATED:tschema_b:inst_b1",
            follow=True,
        )
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "inst_a1")
        self.assertContains(response, "inst_a2")
        self.assertNotContains(response, "inst_a3")

    @override_settings(CAVALIBA_AUTH_MODE="unittest")
    def test_instance_list_related_filter_other_instance(self):
        helper.add_admin_user(login="unittest")
        self._setup_related_schemas()

        response = self.client.get(
            reverse("app_data:instance_list", kwargs={"classname": "tschema_a"})
            + "?query=RELATED:tschema_b:inst_b2",
            follow=True,
        )
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "inst_a3")
        self.assertNotContains(response, "inst_a1")
        self.assertNotContains(response, "inst_a2")
