<commit_before>import factory

from unittest.mock import Mock

from django.test import TestCase

from rest_framework import serializers

from factory.django import DjangoModelFactory

from apps.accounts.tests import ResidentFactory
from apps.buildings.tests import BuildingFactory

from .models import Amenity, Booking
from .serializers import BookingSerializer


class AmenityFactory(DjangoModelFactory):

    class Meta:
        model = Amenity

    name = "Sauna"
    building = factory.SubFactory(BuildingFactory)
    is_available = True


class BookingFactory(DjangoModelFactory):

    class Meta:
        model = Booking

    resident = factory.SubFactory(ResidentFactory)
    amenity = factory.SubFactory(AmenityFactory)


class BookingSerializerTests(TestCase):

    def test_validate_amenity_if_available(self):

        amenity = AmenityFactory(is_available=True)
        request = Mock()
        request.building = amenity.building

        attrs = {'amenity': amenity}
        source = 'amenity'
        serializer = BookingSerializer(context={'request': request})
        serializer.validate_amenity(attrs, source)

    def test_validate_amenity_if_not_available(self):

        amenity = AmenityFactory(is_available=False)
        request = Mock()
        request.building = amenity.building

        attrs = {'amenity': amenity}
        source = 'amenity'
        serializer = BookingSerializer(context={'request': request})
        self.assertRaises(
            serializers.ValidationError,
            serializer.validate_amenity, attrs, source)

    def test_validate_amenity_if_does_not_exist(self):

        amenity = AmenityFactory.create()

        request = Mock()
        # different building
        request.building = BuildingFactory.create(address_1='Sesame St')

        attrs = {'amenity': amenity}
        source = 'amenity'
        serializer = BookingSerializer(context={'request': request})
        self.assertRaises(
            serializers.ValidationError,
            serializer.validate_amenity, attrs, source)

    def test_validate_amenity_if_conflicting_dates(self):

        booking = BookingFactory.create(reserved_from=date_from,
                                        reserved_to=date_to)<commit_msg>More booking fixes<commit_after>import datetime
import factory

from unittest.mock import Mock

from django.test import TestCase

from rest_framework import serializers

from factory.django import DjangoModelFactory

from apps.accounts.tests import ResidentFactory
from apps.buildings.tests import BuildingFactory

from .models import Amenity, Booking
from .serializers import BookingSerializer


class AmenityFactory(DjangoModelFactory):

    class Meta:
        model = Amenity

    name = "Sauna"
    building = factory.SubFactory(BuildingFactory)
    is_available = True


class BookingFactory(DjangoModelFactory):

    class Meta:
        model = Booking

    resident = factory.SubFactory(ResidentFactory)
    amenity = factory.SubFactory(AmenityFactory)


class BookingSerializerTests(TestCase):

    def test_validate_amenity_if_available(self):

        amenity = AmenityFactory(is_available=True)
        request = Mock()
        request.building = amenity.building

        attrs = {'amenity': amenity}
        source = 'amenity'
        serializer = BookingSerializer(context={'request': request})
        serializer.validate_amenity(attrs, source)

    def test_validate_amenity_if_not_available(self):

        amenity = AmenityFactory(is_available=False)
        request = Mock()
        request.building = amenity.building

        attrs = {'amenity': amenity}
        source = 'amenity'
        serializer = BookingSerializer(context={'request': request})
        self.assertRaises(
            serializers.ValidationError,
            serializer.validate_amenity, attrs, source)

    def test_validate_amenity_if_does_not_exist(self):

        amenity = AmenityFactory.create()

        request = Mock()
        # different building
        request.building = BuildingFactory.create(address_1='Sesame St')

        attrs = {'amenity': amenity}
        source = 'amenity'
        serializer = BookingSerializer(context={'request': request})
        self.assertRaises(
            serializers.ValidationError,
            serializer.validate_amenity, attrs, source)

    def test_validate_amenity_if_conflicting_dates(self):

        date_from = datetime.datetime.now() + datetime.timedelta(hours=4)
        date_to = datetime.datetime.now() + datetime.timedelta(hours=5)

        resident = ResidentFactory.create()
        amenity = AmenityFactory.create(building=resident.apartment.building)

        booking = BookingFactory.create(reserved_from=date_from,
                                        reserved_to=date_to,
                                        resident=resident,
                                        amenity=amenity)

        attrs = {
            'reserved_from': date_from,
            'reserved_to': date_to,
            'amenity': booking.amenity
        }
        serializer = BookingSerializer()
        serializer.init_data = {}
        self.assertRaises(serializers.ValidationError,
                          serializer.validate, attrs)
{'fraction_non_alphanumeric': 0.07767361775396978, 'fraction_numerical': 0.0007652573177731012, 'mean_word_length': 2.861152141802068, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6070263', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1369, 'n_words': 282}