
from django.test import TestCase
from django.core.cache import cache
from django.core.cache.backends.base import BaseCache
from django_redis.cache import RedisCache
from django.core.exceptions import ObjectDoesNotExist
from unittest.mock import patch
from ..PortalREMax.custom_cache import CustomCacheBackend, get_cache_tags
from .models import *
from .serializers import *
from rest_framework.test import APITestCase
from rest_framework import status

class OfficesModelTestCase(TestCase):
    def setUp(self):
        Offices.objects.create(office_id=1, name="Office 1", website="http://www.office1.com")

    def test_office_str_representation(self):
        office = Offices.objects.get(office_id=1)
        self.assertEqual(str(office), "Office 1")

    def test_office_website(self):
        office = Offices.objects.get(office_id=1)
        self.assertEqual(office.website, "http://www.office1.com")




class OfficeSerializerTestCase(APITestCase):
    def setUp(self):
        self.office_data = {
            "office_id": 1,
            "name": "Office 1",
            "website": "http://www.office1.com",
            "telephone": "123-456-7890",
            # Add other fields here...
        }
        self.office = Offices.objects.create(**self.office_data)

    def test_office_serializer(self):
        serializer = OfficeSerializer(instance=self.office)
        expected_data = {
            "office_id": 1,
            "name": "Office 1",
            "website": "http://www.office1.com",
            "telephone": "123-456-7890",
            # Add other fields here...
        }
        self.assertEqual(serializer.data, expected_data)


class OfficeViewTestCase(APITestCase):
    def setUp(self):
        self.office_data = {
            "office_id": 1,
            "name": "Office 1",
            "website": "http://www.office1.com",
            "telephone": "123-456-7890",
            # Add other fields here...
        }
        self.office = Offices.objects.create(**self.office_data)
        self.url = "/offices/"  # Replace this with your actual API endpoint URL

    def test_list_offices(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        offices = Offices.objects.all()
        serializer = OfficeSerializer(offices, many=True)
        self.assertEqual(response.data, serializer.data)

    def test_retrieve_office(self):
        response = self.client.get(f"{self.url}{self.office.office_id}/")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        serializer = OfficeSerializer(self.office)
        self.assertEqual(response.data, serializer.data)