api: subscription overlapping, date_end is excluded (#61573)

This commit is contained in:
Lauréline Guérin 2022-02-14 13:46:23 +01:00
parent df4e9f8104
commit 75bc19b333
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 52 additions and 59 deletions

View File

@ -1997,8 +1997,8 @@ class SubscriptionsAPI(ListAPIView):
agenda=self.agenda,
user_external_id=serializer.validated_data['user_external_id'],
).extra(
where=["(date_start, date_end) OVERLAPS (%s, %s) OR date_start = %s OR date_end = %s"],
params=[date_start, date_end, date_end, date_start],
where=["(date_start, date_end) OVERLAPS (%s, %s)"],
params=[date_start, date_end],
)
if overlapping_subscription_qs.exists():
raise APIErrorBadRequest(N_('another subscription overlapping this period already exists'))
@ -2062,8 +2062,8 @@ class SubscriptionAPI(APIView):
)
.exclude(pk=self.subscription.pk)
.extra(
where=["(date_start, date_end) OVERLAPS (%s, %s) OR date_start = %s OR date_end = %s"],
params=[date_start, date_end, date_end, date_start],
where=["(date_start, date_end) OVERLAPS (%s, %s)"],
params=[date_start, date_end],
)
)
if (

View File

@ -219,20 +219,20 @@ def test_api_create_subscription_check_dates(app, user):
agenda=agenda,
user_external_id='xxx',
date_start=datetime.date(year=2021, month=9, day=1),
date_end=datetime.date(year=2021, month=9, day=30),
date_end=datetime.date(year=2021, month=10, day=1),
)
Subscription.objects.create(
agenda=agenda,
user_external_id='yyy', # another user
date_start=datetime.date(year=2021, month=8, day=1),
date_end=datetime.date(year=2021, month=8, day=31),
date_end=datetime.date(year=2021, month=9, day=1),
)
other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
Subscription.objects.create(
agenda=other_agenda, # another agenda
user_external_id='xxx',
date_start=datetime.date(year=2021, month=8, day=1),
date_end=datetime.date(year=2021, month=8, day=31),
date_end=datetime.date(year=2021, month=9, day=1),
)
app.authorization = ('Basic', ('john.doe', 'password'))
@ -250,7 +250,25 @@ def test_api_create_subscription_check_dates(app, user):
params.update(
{
'date_start': '2021-09-01',
'date_end': '2021-09-30',
'date_end': '2021-10-01',
}
)
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params.update(
{
'date_start': '2021-08-01',
'date_end': '2021-09-02',
}
)
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params.update(
{
'date_start': '2021-09-30',
'date_end': '2021-11-01',
}
)
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
@ -262,31 +280,13 @@ def test_api_create_subscription_check_dates(app, user):
'date_end': '2021-09-01',
}
)
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params.update(
{
'date_start': '2021-09-30',
'date_end': '2021-10-31',
}
)
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params, status=400)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params.update(
{
'date_start': '2021-08-01',
'date_end': '2021-08-31',
}
)
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params)
assert resp.json['err'] == 0
params.update(
{
'date_start': '2021-10-01',
'date_end': '2021-10-31',
'date_end': '2021-11-01',
}
)
resp = app.post('/api/agenda/%s/subscription/' % agenda.slug, params=params)
@ -631,20 +631,20 @@ def test_api_patch_subscription_check_dates(app, user):
agenda=agenda,
user_external_id='xxx',
date_start=datetime.date(year=2021, month=9, day=1),
date_end=datetime.date(year=2021, month=9, day=30),
date_end=datetime.date(year=2021, month=10, day=1),
)
Subscription.objects.create(
agenda=agenda,
user_external_id='yyy', # another user
date_start=datetime.date(year=2021, month=8, day=1),
date_end=datetime.date(year=2021, month=8, day=31),
date_end=datetime.date(year=2021, month=9, day=1),
)
other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
Subscription.objects.create(
agenda=other_agenda, # another agenda
user_external_id='xxx',
date_start=datetime.date(year=2021, month=8, day=1),
date_end=datetime.date(year=2021, month=8, day=31),
date_end=datetime.date(year=2021, month=9, day=1),
)
# subscription to update
@ -652,13 +652,13 @@ def test_api_patch_subscription_check_dates(app, user):
agenda=agenda,
user_external_id='xxx',
date_start=datetime.date(year=2021, month=8, day=1),
date_end=datetime.date(year=2021, month=8, day=31),
date_end=datetime.date(year=2021, month=9, day=1),
)
app.authorization = ('Basic', ('john.doe', 'password'))
params = {
'date_end': '2021-09-01',
'date_end': '2021-09-02',
}
resp = app.patch(
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
@ -676,7 +676,25 @@ def test_api_patch_subscription_check_dates(app, user):
params = {
'date_start': '2021-09-01',
'date_end': '2021-09-30',
'date_end': '2021-10-01',
}
resp = app.patch(
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params = {
'date_start': '2021-08-01',
'date_end': '2021-09-02',
}
resp = app.patch(
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params = {
'date_start': '2021-09-30',
'date_end': '2021-11-01',
}
resp = app.patch(
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
@ -687,37 +705,12 @@ def test_api_patch_subscription_check_dates(app, user):
'date_start': '2021-08-01',
'date_end': '2021-09-01',
}
resp = app.patch(
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params = {
'date_start': '2021-09-30',
'date_end': '2021-10-31',
}
resp = app.patch(
'/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params, status=400
)
assert resp.json['err_desc'] == 'another subscription overlapping this period already exists'
params = {
'date_start': '2021-08-01',
'date_end': '2021-08-31',
}
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
assert resp.json['err'] == 0
params = {
'date_start': '2021-10-01',
'date_end': '2021-10-31',
}
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
assert resp.json['err'] == 0
params = {
'date_start': '2021-10-02',
'date_end': '2021-10-31',
'date_end': '2021-11-01',
}
resp = app.patch('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk), params=params)
assert resp.json['err'] == 0