# (c) cavaliba.com - tests /data / cache

from django.test import TestCase

import app_home.cache as cache
from app_data.data import Instance
from app_data.models import DataEAV
from tests import helper


class TestDataEAV(TestCase):
    def setUp(self):
        cache.clear()
        helper.add_schema(
            classname="test1",
            field_definition={"myipv4": {"dataformat": "ipv4", "displayname": "myipv4"}},
        )
        helper.add_instance(classname="test1", keyname="test1-01", fields={"myipv4": "10.1.2.3"})

    def test_eav_fixture(self):

        eavobj = DataEAV.objects.filter(
            classname="test1",
            keyname="test1-01",
            fieldname="myipv4",
        ).first()

        self.assertEqual(eavobj.format, "ipv4")
        self.assertEqual(eavobj.value, "10.1.2.3")

    def test_eav_insert(self):

        instance = Instance(classname="test1", keyname="test_eav")
        instance.create()
        self.assertEqual(instance.is_bound, True)

        eavobj = DataEAV.objects.filter(classname="test1", keyname="test_eav").all()
        self.assertTrue(len(eavobj) > 0)

    def test_eav_delete(self):

        instance = Instance.from_keyname(classname="test1", keyname="test1-01")
        instance.delete()
        self.assertEqual(instance.is_bound, False)

        eavobj = DataEAV.objects.filter(classname="test1", keyname="test1-01").all()
        self.assertTrue(len(eavobj) == 0)
