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

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

from ..actions import NarcistAction


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

        self.assertEqual(
            task.name,
            "Narcist action It's all about me solipsism",
            "task name should be based on action name class attribute",
        )

    def test_get_name_with_argument_name(self):
        task = Task.create(NarcistAction(name="I am henk"))
        self.assertEqual(
            task.name,
            "Narcist action I am henk solipsism",
            "task name should be based on argument passed to action constructor",
        )

    def test_there_can_only_be_one(self):
        Task.create(NarcistAction())
        with self.assertRaises(ValidationError, msg="It is exclusive you dig?"):
            Task.create(NarcistAction())

    def there_can_be_two_but_not_two_active(self):
        task = Task.create(NarcistAction())
        task.run()
        self.assertEqual(task.status, TaskStatus.SUCCESSFUL)
        task = Task.create(NarcistAction())

    def test_async(self):
        task = Task.create(NarcistAction())
        self.assertEqual(task.status, TaskStatus.PENDING)
        task.run_async()
        task.refresh_from_db()
        self.assertEqual(task.status, TaskStatus.SUCCESSFUL)
