How can I test if two users can reserve the same car simultaneously?
def test_if_two_users_can_reserve_the_same_car_simultaneously(self):
with patch.object(
timezone,
"now",
return_value=make_aware(
datetime.datetime.combine(
datetime.date.today() + datetime.timedelta(days=1), datetime.time(10, 30, 0)
),
timezone=pytz.timezone("UTC"),
),
):
self.client.login(username=self.user.username, password=self.PASSWORD)
url = reverse("booking-list")
data = {
"book_for": datetime.datetime.combine(
datetime.date.today() + datetime.timedelta(days=1), datetime.time(11, 30, 0)
),
}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
with patch.object(
timezone,
"now",
return_value=make_aware(
datetime.datetime.combine(
datetime.date.today() + datetime.timedelta(days=1), datetime.time(10, 30, 0)
),
timezone=pytz.timezone("UTC"),
),
):
self.client.login(
username=self.another_user.username, password=self.PASSWORD
)
url = reverse("booking-list")
data = {
"book_for": datetime.datetime.combine(
datetime.date.today() + datetime.timedelta(days=1), datetime.time(11, 30, 0)
),
}
response = self.client.post(url, data=data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
This is how I wrote it, but in the unit test, it runs line by line. So first one will create then move to second one but I want them both run at same time. (Please answer with an example)
This is its model:
class Booking(models.Model):
id = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
user_location = models.PointField(help_text="user current location")
user_address = models.TextField(help_text="user current address")
start_location = models.PointField(
blank=True,
help_text="location where the user pick up the car from",
)
start_address = models.TextField(
blank=True,
default="",
help_text="address where the user pick up the car from",
)
end_location = models.PointField(
null=True,
blank=True,
help_text="desitination location that a user wants to drive to or driven by the driver",
)
end_address = models.TextField(
blank=True,
default="",
help_text="desitination address that a user wants to drive to or driven by the driver",
)
vehicle = models.ForeignKey(
Vehicle,
on_delete=models.PROTECT,
)
book_for = models.DateTimeField()
drop_off_datetime = models.DateTimeField(
null=True,
blank=True,
help_text="the drop off time for booking",
)
status = models.CharField(
max_length=25,
choices=BookingStatus.choices,
default=BookingStatus.OPEN,
)
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True)
As you can see I used UUIDField.
def perform_create(self, serializer):
serializer.save(user=self.request.user)
constraints:
class Meta:
constraints = [
UniqueConstraint(
fields=["user"],
condition=Q(status=BookingStatus.ACTIVE),
name="unique_active_booking",
)
]
ordering = (
"-book_for",
"start_address",
)
enter code here