# (c) cavaliba.com - test_instance_write_to_db.py

from django.test import TestCase

import app_home.cache as cache
from app_data.data import Instance
from app_data.models import DataInstance, DataRevision
from tests import helper

AAA = {"username": "testuser", "perms": ["p_data_admin"]}


class TestWriteToDb(TestCase):
    def setUp(self):
        cache.clear()
        helper.add_schema(classname="wtest")

    def test_write_to_db_saves_instance(self):
        instance = Instance(classname="wtest")
        instance.iobj = DataInstance()
        instance.iobj.classname = "wtest"
        instance.keyname = "obj01"
        instance.displayname = "Obj 01"
        r = instance._write_to_iobj()
        self.assertTrue(r)
        self.assertTrue(DataInstance.objects.filter(classname="wtest", keyname="obj01").exists())

    def test_write_to_db_not_called_by_update_on_no_change(self):
        helper.add_instance(classname="wtest", keyname="obj01")
        instance = Instance.from_keyname("wtest", "obj01")
        iobj_before = instance.iobj.last_update
        # merge_import with identical data — sets before_revision
        instance.merge_import({"classname": "wtest", "keyname": "obj01"}, aaa=AAA)
        r = instance.update()
        self.assertTrue(r)
        # last_update should not have changed since no diff
        instance2 = Instance.from_keyname("wtest", "obj01")
        self.assertEqual(instance2.iobj.last_update, iobj_before)

    def test_create_adds_revision(self):
        instance = Instance(classname="wtest")
        instance.merge_import({"classname": "wtest", "keyname": "obj02"}, aaa=AAA)
        r = instance.create()
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(
                classname="wtest", keyname="obj02", action="create"
            ).count(),
            1,
        )

    def test_update_adds_revision_on_change(self):
        helper.add_instance(classname="wtest", keyname="obj03")
        instance = Instance.from_keyname("wtest", "obj03")
        instance.merge_import(
            {"classname": "wtest", "keyname": "obj03", "displayname": "Changed"}, aaa=AAA
        )
        r = instance.update()
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(classname="wtest", keyname="obj03", action="edit").count(),
            1,
        )

    def test_update_skips_revision_on_no_change(self):
        helper.add_instance(classname="wtest", keyname="obj04", fields={"displayname": "Same"})
        instance = Instance.from_keyname("wtest", "obj04")
        instance.merge_import(
            {"classname": "wtest", "keyname": "obj04", "displayname": "Same"}, aaa=AAA
        )
        r = instance.update()
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(classname="wtest", keyname="obj04", action="edit").count(),
            0,
        )

    def test_init_adds_revision(self):
        instance = Instance(classname="wtest")
        instance.merge_import({"classname": "wtest", "keyname": "obj05"}, aaa=AAA)
        r = instance.init()
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(classname="wtest", keyname="obj05", action="init").count(),
            1,
        )

    def test_delete_adds_revision(self):
        helper.add_instance(classname="wtest", keyname="obj06")
        instance = Instance.from_keyname("wtest", "obj06")
        instance.aaa_revision = AAA
        r = instance.delete()
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(
                classname="wtest", keyname="obj06", action="delete"
            ).count(),
            1,
        )
