# (c) cavaliba.com - test_instance_classmethods.py

from django.test import TestCase

import app_home.cache as cache
from app_data.data import Instance
from app_data.group import Group
from app_data.user import User
from tests import helper


class InstanceGetDnTest(TestCase):
    def setUp(self):
        cache.clear()
        helper.add_schema(classname="testdn")
        helper.add_instance(
            classname="testdn", keyname="obj01", fields={"displayname": "Object One"}
        )
        helper.add_instance(classname="testdn", keyname="obj02", fields={"displayname": ""})
        helper.add_instance(classname="testdn", keyname="obj03")

    def test_returns_displayname(self):
        result = Instance.get_dn(classname="testdn", keyname="obj01")
        self.assertEqual(result, "Object One")

    def test_returns_none_for_missing_keyname(self):
        result = Instance.get_dn(classname="testdn", keyname="does_not_exist")
        self.assertIsNone(result)

    def test_returns_none_when_keyname_is_none(self):
        result = Instance.get_dn(classname="testdn", keyname=None)
        self.assertIsNone(result)

    def test_returns_none_when_classname_is_none(self):
        result = Instance.get_dn(classname=None, keyname="obj01")
        self.assertIsNone(result)

    def test_subclass_uses_own_classname(self):
        helper.add_user(login="userdn01", displayname="User DN One")
        result = User.get_dn(keyname="userdn01")
        self.assertEqual(result, "User DN One")

    def test_subclass_returns_none_for_missing(self):
        result = User.get_dn(keyname="no_such_user")
        self.assertIsNone(result)


class InstanceGetAllKeynameTest(TestCase):
    def setUp(self):
        cache.clear()
        helper.add_schema(classname="testkn")
        helper.add_instance(classname="testkn", keyname="kn01")
        helper.add_instance(classname="testkn", keyname="kn02")
        helper.add_instance(classname="testkn", keyname="kn03")

    def test_returns_list(self):
        result = Instance.get_all_keyname(classname="testkn")
        self.assertIsInstance(result, list)

    def test_returns_all_keynames(self):
        result = Instance.get_all_keyname(classname="testkn")
        self.assertIn("kn01", result)
        self.assertIn("kn02", result)
        self.assertIn("kn03", result)

    def test_returns_empty_for_unknown_classname(self):
        result = Instance.get_all_keyname(classname="no_such_class")
        self.assertEqual(result, [])

    def test_returns_empty_when_classname_is_none(self):
        result = Instance.get_all_keyname(classname=None)
        self.assertEqual(result, [])

    def test_excludes_other_classname(self):
        helper.add_schema(classname="testkn_other")
        helper.add_instance(classname="testkn_other", keyname="other01")
        result = Instance.get_all_keyname(classname="testkn")
        self.assertNotIn("other01", result)

    def test_subclass_uses_own_classname(self):
        helper.add_group(name="grp_kn01")
        helper.add_group(name="grp_kn02")
        result = Group.get_all_keyname()
        self.assertIn("grp_kn01", result)
        self.assertIn("grp_kn02", result)
