# (c) cavaliba.com - test_instance_before_revision.py

from django.test import TestCase

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

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


class TestBeforeRevision(TestCase):
    def setUp(self):
        cache.clear()
        helper.add_schema(
            classname="brtest",
            field_definition={"title": {"dataformat": "string", "displayname": "Title"}},
        )

    def test_before_revision_empty_on_init(self):
        instance = Instance(classname="brtest")
        self.assertEqual(instance.before_revision, {})

    def test_before_revision_set_on_merge_import(self):
        instance = Instance(classname="brtest")
        instance.merge_import({"classname": "brtest", "keyname": "obj01"})
        self.assertNotEqual(instance.before_revision, {})
        self.assertIn("keyname", instance.before_revision)

    def test_before_revision_not_overwritten_on_second_merge(self):
        helper.add_instance(classname="brtest", keyname="obj02")
        instance = Instance.from_keyname("brtest", "obj02")
        instance.merge_import({"classname": "brtest", "keyname": "obj02", "displayname": "First"})
        first_snapshot = instance.before_revision.copy()
        instance.merge_import({"classname": "brtest", "keyname": "obj02", "displayname": "Second"})
        self.assertEqual(instance.before_revision, first_snapshot)

    def test_before_revision_set_by_merge_request(self):
        instance = Instance(classname="brtest")
        from django.test import RequestFactory

        rf = RequestFactory()
        request = rf.post("/", {"keyname": "obj03", "displayname": "Test"})
        instance.merge_request(request)
        self.assertNotEqual(instance.before_revision, {})

    def test_diff_revision_empty_when_no_before(self):
        instance = Instance(classname="brtest")
        self.assertEqual(instance.diff_revision(), {})

    def test_diff_revision_empty_when_no_change(self):
        helper.add_instance(classname="brtest", keyname="obj04")
        instance = Instance.from_keyname("brtest", "obj04")
        instance.merge_import({"classname": "brtest", "keyname": "obj04"})
        self.assertEqual(instance.diff_revision(), {})

    def test_diff_revision_detects_displayname_change(self):
        helper.add_instance(classname="brtest", keyname="obj05", fields={"displayname": "Old"})
        instance = Instance.from_keyname("brtest", "obj05")
        instance.merge_import({"classname": "brtest", "keyname": "obj05", "displayname": "New"})
        diff = instance.diff_revision()
        self.assertIn("displayname", diff)
        self.assertEqual(diff["displayname"]["before"], "Old")
        self.assertEqual(diff["displayname"]["after"], "New")

    def test_diff_revision_detects_is_enabled_change(self):
        helper.add_instance(classname="brtest", keyname="obj06")
        instance = Instance.from_keyname("brtest", "obj06")
        instance.merge_import({"classname": "brtest", "keyname": "obj06", "is_enabled": "false"})
        diff = instance.diff_revision()
        self.assertIn("is_enabled", diff)

    def test_before_revision_cleared_after_revision_add(self):
        instance = Instance(classname="brtest")
        instance.merge_import({"classname": "brtest", "keyname": "obj07"}, aaa=AAA)
        instance._revision_add(action="create")
        self.assertEqual(instance.before_revision, {})
