# (c) cavaliba.com - test_instance_enable_disable.py

from django.test import TestCase

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

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


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

    def test_enable_disabled_instance_adds_revision(self):
        helper.add_instance(classname="edtest", keyname="obj01", fields={"is_enabled": "false"})
        instance = Instance.from_keyname("edtest", "obj01")
        instance.iobj.is_enabled = False
        instance.iobj.save()
        instance.is_enabled = False
        r = instance.enable(aaa=AAA)
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(
                classname="edtest", keyname="obj01", action="enable"
            ).count(),
            1,
        )

    def test_enable_already_enabled_no_revision(self):
        helper.add_instance(classname="edtest", keyname="obj02")
        instance = Instance.from_keyname("edtest", "obj02")
        self.assertTrue(instance.is_enabled)
        r = instance.enable(aaa=AAA)
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(
                classname="edtest", keyname="obj02", action="enable"
            ).count(),
            0,
        )

    def test_disable_enabled_instance_adds_revision(self):
        helper.add_instance(classname="edtest", keyname="obj03")
        instance = Instance.from_keyname("edtest", "obj03")
        self.assertTrue(instance.is_enabled)
        r = instance.disable(aaa=AAA)
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(
                classname="edtest", keyname="obj03", action="disable"
            ).count(),
            1,
        )

    def test_disable_already_disabled_no_revision(self):
        helper.add_instance(classname="edtest", keyname="obj04", fields={"is_enabled": "false"})
        instance = Instance.from_keyname("edtest", "obj04")
        instance.iobj.is_enabled = False
        instance.iobj.save()
        instance.is_enabled = False
        r = instance.disable(aaa=AAA)
        self.assertTrue(r)
        self.assertEqual(
            DataRevision.objects.filter(
                classname="edtest", keyname="obj04", action="disable"
            ).count(),
            0,
        )

    def test_enable_with_aaa_sets_username_in_revision(self):
        helper.add_instance(classname="edtest", keyname="obj05", fields={"is_enabled": "false"})
        instance = Instance.from_keyname("edtest", "obj05")
        instance.iobj.is_enabled = False
        instance.iobj.save()
        instance.is_enabled = False
        instance.enable(aaa={"username": "syncuser", "perms": []})
        rev = DataRevision.objects.filter(
            classname="edtest", keyname="obj05", action="enable"
        ).first()
        self.assertIsNotNone(rev)
        self.assertEqual(rev.username, "syncuser")
