from django.test import TestCase
from django.urls import reverse

from rest_framework import status

from Dormroom.models import Dormroom
from SIFUser.mixins import AuthTestMixin
from StudentVillage.models import StudentVillage
from Washlist.jobs import reset_washlists
from Washlist.models.Templates import TemplateListItem, TemplateWashList
from Washlist.models.WashLists import ListItem
from Washlist.serializer import TemplateWashListSerializer


class WashListTemplateTest(TestCase):
    room = None

    def setUp(self):
        village = StudentVillage.objects.create(name="Moholt")
        self.room = Dormroom.objects.create(number=1, village=village)

        temp_list = TemplateWashList.objects.create(title="Moholt")
        village.templateWashList = temp_list
        village.save()

    def test_add_to_template_adds_to_each_list(self):
        desc = "Vask badet"
        temp_list = TemplateWashList.objects.get(title="Moholt")

        TemplateListItem.objects.create(description=desc, washlist=temp_list).save()
        self.assertEqual(desc, ListItem.objects.get(dormroom=self.room).description)


class WeeklyResetOfWashlistsTest(TestCase):
    def setUp(self):
        """
        Create a Washlist item that is completed
        the method also sets up a village and a room to relate the Washlist item to
        satisfy the db constraints
        """

        village = StudentVillage.objects.create(name="Moholt")
        self.room = Dormroom.objects.create(number=1, village=village)

        temp_list = TemplateWashList.objects.create(title="Moholt")
        village.templateWashList = temp_list
        village.save()

        self.item = ListItem.objects.create(
            pk=1, dormroom=self.room, desc="Vask badet", completed=True
        )
        self.item.save()

    def test_job_resets_items(self):
        """
        Test that job to reset Washlist items when run manually actually rests the databases
        Washlist items
        """
        reset_washlists()
        self.assertEqual(False, ListItem.objects.get(pk=1).completed)


class WashlistTemplateAPITest(AuthTestMixin):
    def setUp(self):
        super().setUp()
        self.temp_list = TemplateWashList.objects.create(title="Moholt")
        village = StudentVillage.objects.create(
            name="Moholt", templateWashList=self.temp_list
        )
        self.room = Dormroom.objects.create(number=1, village=village)
        self.item = ListItem.objects.create(
            pk=1, dormroom=self.room, desc="Vask badet", completed=True
        )

    def test_get_template_list(self):
        url = reverse("templatewashlist-list")
        response = self.client.get(url, HTTP_AUTHORIZATION=self.auth)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data[0],
            TemplateWashListSerializer(
                TemplateWashList.objects.get(title="Moholt")
            ).data,
        )

    def test_get_detail_template_list(self):
        url = reverse("templatewashlist-detail", args=[1])
        response = self.client.get(url, HTTP_AUTHORIZATION=self.auth)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data,
            TemplateWashListSerializer(
                TemplateWashList.objects.get(title="Moholt")
            ).data,
        )

    def test_add_template_washlist(self):
        url = reverse("templatewashlist-list")
        response = self.client.post(
            url, {"title": "Tyholt", "village": 1}, HTTP_AUTHORIZATION=self.auth
        )

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(
            response.data,
            TemplateWashListSerializer(
                TemplateWashList.objects.get(title="Tyholt")
            ).data,
        )

    def test_partial_update(self):
        url = reverse("templatewashlist-detail", args=[1])
        response = self.client.patch(
            url, {"title": "Berg"}, HTTP_AUTHORIZATION=self.auth
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data,
            TemplateWashListSerializer(TemplateWashList.objects.get(title="Berg")).data,
        )

    def test_destroy(self):
        url = reverse("templatewashlist-detail", args=[1])
        response = self.client.delete(url, HTTP_AUTHORIZATION=self.auth)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(TemplateWashList.objects.count(), 0)

[((19, 18, 19, 62), 'StudentVillage.models.StudentVillage.objects.create', 'StudentVillage.objects.create', (), '', False, 'from StudentVillage.models import StudentVillage\n'), ((20, 20, 20, 70), 'Dormroom.models.Dormroom.objects.create', 'Dormroom.objects.create', (), '', False, 'from Dormroom.models import Dormroom\n'), ((22, 20, 22, 67), 'Washlist.models.Templates.TemplateWashList.objects.create', 'TemplateWashList.objects.create', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((28, 20, 28, 64), 'Washlist.models.Templates.TemplateWashList.objects.get', 'TemplateWashList.objects.get', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((42, 18, 42, 62), 'StudentVillage.models.StudentVillage.objects.create', 'StudentVillage.objects.create', (), '', False, 'from StudentVillage.models import StudentVillage\n'), ((43, 20, 43, 70), 'Dormroom.models.Dormroom.objects.create', 'Dormroom.objects.create', (), '', False, 'from Dormroom.models import Dormroom\n'), ((45, 20, 45, 67), 'Washlist.models.Templates.TemplateWashList.objects.create', 'TemplateWashList.objects.create', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((49, 20, 51, 9), 'Washlist.models.WashLists.ListItem.objects.create', 'ListItem.objects.create', (), '', False, 'from Washlist.models.WashLists import ListItem\n'), ((59, 8, 59, 25), 'Washlist.jobs.reset_washlists', 'reset_washlists', ({}, {}), '()', False, 'from Washlist.jobs import reset_washlists\n'), ((66, 25, 66, 72), 'Washlist.models.Templates.TemplateWashList.objects.create', 'TemplateWashList.objects.create', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((67, 18, 69, 9), 'StudentVillage.models.StudentVillage.objects.create', 'StudentVillage.objects.create', (), '', False, 'from StudentVillage.models import StudentVillage\n'), ((70, 20, 70, 70), 'Dormroom.models.Dormroom.objects.create', 'Dormroom.objects.create', (), '', False, 'from Dormroom.models import Dormroom\n'), ((71, 20, 73, 9), 'Washlist.models.WashLists.ListItem.objects.create', 'ListItem.objects.create', (), '', False, 'from Washlist.models.WashLists import ListItem\n'), ((76, 14, 76, 46), 'django.urls.reverse', 'reverse', ({(76, 22, 76, 45): '"""templatewashlist-list"""'}, {}), "('templatewashlist-list')", False, 'from django.urls import reverse\n'), ((88, 14, 88, 58), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n'), ((100, 14, 100, 46), 'django.urls.reverse', 'reverse', ({(100, 22, 100, 45): '"""templatewashlist-list"""'}, {}), "('templatewashlist-list')", False, 'from django.urls import reverse\n'), ((114, 14, 114, 58), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n'), ((126, 14, 126, 58), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n'), ((130, 25, 130, 57), 'Washlist.models.Templates.TemplateWashList.objects.count', 'TemplateWashList.objects.count', ({}, {}), '()', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((30, 8, 30, 77), 'Washlist.models.Templates.TemplateListItem.objects.create', 'TemplateListItem.objects.create', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((31, 31, 31, 71), 'Washlist.models.WashLists.ListItem.objects.get', 'ListItem.objects.get', (), '', False, 'from Washlist.models.WashLists import ListItem\n'), ((60, 32, 60, 58), 'Washlist.models.WashLists.ListItem.objects.get', 'ListItem.objects.get', (), '', False, 'from Washlist.models.WashLists import ListItem\n'), ((83, 16, 83, 60), 'Washlist.models.Templates.TemplateWashList.objects.get', 'TemplateWashList.objects.get', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((95, 16, 95, 60), 'Washlist.models.Templates.TemplateWashList.objects.get', 'TemplateWashList.objects.get', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((109, 16, 109, 60), 'Washlist.models.Templates.TemplateWashList.objects.get', 'TemplateWashList.objects.get', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n'), ((122, 39, 122, 81), 'Washlist.models.Templates.TemplateWashList.objects.get', 'TemplateWashList.objects.get', (), '', False, 'from Washlist.models.Templates import TemplateListItem, TemplateWashList\n')]