import pytest

from rotation.serializers import SizeRotationSettingsSerializer
from incident.models import Incident
from incident.serializers.incident import DumpIncidentSerializer



@pytest.mark.django_db
class TestDumpIncidentSerializer:

    @pytest.fixture(autouse=True)
    def setup_test(self, add_user_with_permissions):
        self.user = add_user_with_permissions(username='test_user1', password='test_pass1', is_superuser=True)
        Incident.objects.create(
            title='test resoled incident with user',
            importance=10,
            event_count=10,
            events='',
            status=Incident.Status.RESOLVED,
            assigned_to=self.user,
        )
        Incident.objects.create(
            title='test resoled incident without user',
            importance=10,
            event_count=10,
            events='',
            status=Incident.Status.RESOLVED,
        )

    @pytest.mark.unit
    def test_assigned_to_data_with_user(self):
        incident = Incident.objects.filter(title='test resoled incident with user').first()
        assert incident is not None
        data = DumpIncidentSerializer(incident).data
        assert 'assigned_to' in data
        assert 'id' in data['assigned_to']
        assert self.user.id == data['assigned_to']['id']
        assert 'username' in data['assigned_to']
        assert self.user.username == data['assigned_to']['username']
        assert 'first_name' in data['assigned_to']
        assert self.user.first_name == data['assigned_to']['first_name']

    @pytest.mark.unit
    def test_assigned_data_without_user(self):
        incident = Incident.objects.filter(title='test resoled incident without user').first()
        assert incident is not None
        data = DumpIncidentSerializer(incident).data
        assert 'assigned_to' in data
        assert data['assigned_to'] is None

    @pytest.mark.unit
    def test_validate_size_settings(self):
        data = {
            'rotation_type': '1',
            'size_rotation': '5242880',
        }
        serializer = SizeRotationSettingsSerializer(data=data)
        assert serializer.is_valid()

        data = {
            'rotation_type': '1',
            'size_rotation': 'bad',
        }
        serializer = SizeRotationSettingsSerializer(data=data)
        assert not serializer.is_valid()
