from django.test import TestCase
from django.core.exceptions import ValidationError

from ocyan.task.models import Task
from ocyan.task.choices import TaskStatus, LogType

from ..actions import WeirdoAction, IgnorableErrorAction


class ActionTest(TestCase):
    def test_get_name(self):
        task = Task.create(WeirdoAction())

        self.assertEqual(
            task.name,
            "Weirdo action I LIKE BAAAALLLRTRTHTHGHGHHG *(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^",
            "task name should be based on action name class attribute",
        )

    def test_get_name_with_argument_name(self):
        task = Task.create(WeirdoAction(name="I am henk"))
        self.assertEqual(
            task.name,
            "Weirdo action I am henk *(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^*(&&^(#)))))))#*&&&&T^",
            "task name should be based on argument passed to action constructor",
        )

    def test_there_can_only_be_one(self):
        Task.create(WeirdoAction(code="klazien"))
        with self.assertRaises(ValidationError, msg="It is unique you dig?"):
            Task.create(WeirdoAction(code="klazien"))

    def test_there_can_be_not_two_no_never(self):
        task = Task.create(WeirdoAction())
        task.run()
        self.assertEqual(task.status, TaskStatus.SUCCESSFUL)
        with self.assertRaises(ValidationError, msg="It is unique you dig?"):
            task = Task.create(WeirdoAction())

    def test_task_with_comments(self):
        task = Task.create(IgnorableErrorAction())
        task.run()
        self.assertEqual(task.status, TaskStatus.SUCCESSFUL)
        last_log = task.logs.order_by("-timestamp").first()
        self.assertEqual(last_log.log_type, LogType.ERROR)
