# (c) cavaliba.com - tests / pipeline field_lookup

from django.test import TestCase

import app_home.cache as cache

from app_data.pipeline import task_field_lookup
from app_user.models import SireneUser, SireneGroup, SirenePermission
from tests import helper


class PipelineLookupTest(TestCase):

    def setUp(self):
        cache.clear()
        SireneUser.objects.create(login="alice")
        SireneGroup.objects.create(keyname="grp_test", is_role=False)
        SireneGroup.objects.create(keyname="role_test", is_role=True)
        SirenePermission.objects.create(keyname="p_test_access")
        helper.add_schema(classname="lookupschema")
        helper.add_instance(classname="lookupschema", keyname="item01")


    def test_lookup_user_found(self):
        datadict = {"src": "alice", "dst": ""}
        task_field_lookup(datadict, ["dst", "_user", "src"])
        self.assertEqual(datadict["dst"], "alice")


    def test_lookup_user_not_found(self):
        datadict = {"src": "nobody", "dst": "init"}
        task_field_lookup(datadict, ["dst", "_user", "src"])
        self.assertEqual(datadict["dst"], "")


    def test_lookup_group_found(self):
        datadict = {"src": "grp_test", "dst": ""}
        task_field_lookup(datadict, ["dst", "_group", "src"])
        self.assertEqual(datadict["dst"], "grp_test")


    def test_lookup_group_not_found(self):
        datadict = {"src": "no_such_group", "dst": "init"}
        task_field_lookup(datadict, ["dst", "_group", "src"])
        self.assertEqual(datadict["dst"], "")


    def test_lookup_role_found(self):
        datadict = {"src": "role_test", "dst": ""}
        task_field_lookup(datadict, ["dst", "_role", "src"])
        self.assertEqual(datadict["dst"], "role_test")


    def test_lookup_role_not_found(self):
        datadict = {"src": "no_such_role", "dst": "init"}
        task_field_lookup(datadict, ["dst", "_role", "src"])
        self.assertEqual(datadict["dst"], "")


    def test_lookup_permission_found(self):
        datadict = {"src": "p_test_access", "dst": ""}
        task_field_lookup(datadict, ["dst", "_permission", "src"])
        self.assertEqual(datadict["dst"], "p_test_access")


    def test_lookup_permission_not_found(self):
        datadict = {"src": "p_no_such", "dst": "init"}
        task_field_lookup(datadict, ["dst", "_permission", "src"])
        self.assertEqual(datadict["dst"], "")


    def test_lookup_schema_found(self):
        datadict = {"src": "item01", "dst": ""}
        task_field_lookup(datadict, ["dst", "lookupschema", "src"])
        self.assertEqual(datadict["dst"], "item01")


    def test_lookup_schema_not_found(self):
        datadict = {"src": "no_such_item", "dst": "init"}
        task_field_lookup(datadict, ["dst", "lookupschema", "src"])
        self.assertEqual(datadict["dst"], "")


    def test_lookup_missing_srcfield(self):
        datadict = {"dst": "init"}
        task_field_lookup(datadict, ["dst", "_user", "src"])
        self.assertEqual(datadict.get("dst"), "init")


    def test_lookup_missing_taskopt(self):
        datadict = {"src": "alice", "dst": "init"}
        task_field_lookup(datadict, ["dst", "_user"])
        self.assertEqual(datadict.get("dst"), "init")
