# (c) cavaliba.com - test_data_revision.py


from django.test import TestCase

import app_home.cache as cache

from app_data.models import DataRevision
from app_data.revision import revision_add_raw
from app_data.revision import revision_get
from app_data.revision import revision_purge
from app_data.data import Instance
from tests import helper


class TestDataRevision(TestCase):

    def setUp(self):
        cache.clear()

    def test_revision_add_raw(self):
        revision_add_raw(
            aaa=None,
            classname="classtest",
            keyname="keytest",
            action="test")
        count = DataRevision.objects.count()
        self.assertEqual(count, 1)
        rev = DataRevision.objects.first()
        self.assertEqual(rev.classname, "classtest")
        self.assertEqual(rev.keyname, "keytest")
        self.assertEqual(rev.displayname, "")
        self.assertEqual(rev.action, "test")

    def test_revision_add_raw_with_displayname(self):
        revision_add_raw(
            aaa=None,
            classname="classtest",
            keyname="keytest",
            action="test")
        rev = DataRevision.objects.first()
        rev.displayname = "Test Display Name"
        rev.save()
        rev_updated = DataRevision.objects.first()
        self.assertEqual(rev_updated.displayname, "Test Display Name")

    def test_revision_get(self):
        revision_add_raw(
            aaa=None,
            classname="classtest",
            keyname="keytest",
            action="test")
        revqs = revision_get(classname="classtest", keyname="keytest")
        self.assertEqual(len(revqs), 1)

    def test_revision_purge(self):
        count = revision_purge()
        self.assertEqual(count, 0)

    # TODO
    # revision_add
    # client view edit instance/user/group/role => see Revision entry
    # client view detail => Revision dropdwon content


class TestGetDictForExportRevision(TestCase):

    fixtures = ["test"]

    def setUp(self):
        cache.clear()
        helper.add_schema(classname="revtest")
        helper.add_instance(classname="revtest", keyname="obj01")
        for i in range(15):
            revision_add_raw(classname="revtest", keyname="obj01", action="edit")

    def test_rev_none_no_revision_key(self):
        instance = Instance.from_keyname("revtest", "obj01")
        data = instance.get_dict_for_export(rev=None)
        self.assertNotIn("_revision", data)

    def test_rev_zero_no_revision_key(self):
        instance = Instance.from_keyname("revtest", "obj01")
        data = instance.get_dict_for_export(rev=0)
        self.assertNotIn("_revision", data)

    def test_rev_positive_returns_revision_key(self):
        instance = Instance.from_keyname("revtest", "obj01")
        data = instance.get_dict_for_export(rev=5)
        self.assertIn("_revision", data)

    def test_rev_limits_count(self):
        instance = Instance.from_keyname("revtest", "obj01")
        data = instance.get_dict_for_export(rev=5)
        self.assertEqual(len(data["_revision"]), 5)

    def test_rev_capped_at_available(self):
        instance = Instance.from_keyname("revtest", "obj01")
        data = instance.get_dict_for_export(rev=100)
        self.assertEqual(len(data["_revision"]), 15)

    def test_rev_entry_fields(self):
        instance = Instance.from_keyname("revtest", "obj01")
        data = instance.get_dict_for_export(rev=1)
        entry = data["_revision"][0]
        self.assertIn("login", entry)
        self.assertIn("date", entry)
        self.assertIn("action", entry)

    def test_rev_no_revisions_in_db(self):
        helper.add_instance(classname="revtest", keyname="obj02")
        instance = Instance.from_keyname("revtest", "obj02")
        data = instance.get_dict_for_export(rev=10)
        self.assertNotIn("_revision", data)
