From 316a089b75072a492dd4432b81b4213813296a6c Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Tue, 13 Feb 2024 10:56:04 -0500 Subject: [PATCH] Fix bug with claiming offer If an offer is claimed for its entire duration, and someone else tries to claim the offer without specifying a start/end time, then the db function will think that the next available end time is before the current time. This fixes that issue, as well as renaming the db function to be clearer regarding its function. --- esi_leap/api/controllers/v1/offer.py | 3 ++- esi_leap/db/api.py | 6 +++--- esi_leap/db/sqlalchemy/api.py | 5 +++-- esi_leap/objects/offer.py | 5 +++-- esi_leap/tests/db/sqlalchemy/test_api.py | 27 +++++++++++++++++++----- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/esi_leap/api/controllers/v1/offer.py b/esi_leap/api/controllers/v1/offer.py index 604ac59b..fccd8a02 100644 --- a/esi_leap/api/controllers/v1/offer.py +++ b/esi_leap/api/controllers/v1/offer.py @@ -272,7 +272,8 @@ def claim(self, offer_uuid, new_lease): lease_dict['start_time'] = datetime.datetime.now() if 'end_time' not in lease_dict: - q = offer.get_first_availability(lease_dict['start_time']) + q = offer.get_next_lease_start_time( + lease_dict['start_time']) if q is None: lease_dict['end_time'] = offer.end_time else: diff --git a/esi_leap/db/api.py b/esi_leap/db/api.py index f7da30c3..d0734476 100644 --- a/esi_leap/db/api.py +++ b/esi_leap/db/api.py @@ -92,9 +92,9 @@ def offer_get_conflict_times(offer_ref): return IMPL.offer_get_conflict_times(offer_ref) -def offer_get_first_availability(offer_uuid, start, end): - return IMPL.offer_get_first_availability( - offer_uuid, start, end) +def offer_get_next_lease_start_time(offer_uuid, start): + return IMPL.offer_get_next_lease_start_time( + offer_uuid, start) def offer_verify_availability(offer_ref, start, end): diff --git a/esi_leap/db/sqlalchemy/api.py b/esi_leap/db/sqlalchemy/api.py index 18a171d0..dca88c90 100644 --- a/esi_leap/db/sqlalchemy/api.py +++ b/esi_leap/db/sqlalchemy/api.py @@ -169,7 +169,7 @@ def offer_get_conflict_times(offer_ref): ).all() -def offer_get_first_availability(offer_uuid, start): +def offer_get_next_lease_start_time(offer_uuid, start): l_query = model_query(models.Lease) return l_query.with_entities( @@ -179,7 +179,8 @@ def offer_get_first_availability(offer_uuid, start): (models.Lease.status == statuses.ACTIVE) ).\ order_by(models.Lease.start_time).\ - filter(models.Lease.end_time >= start).first() + filter((models.Lease.end_time >= start) & + (models.Lease.start_time >= start)).first() def offer_verify_availability(offer_ref, start, end): diff --git a/esi_leap/objects/offer.py b/esi_leap/objects/offer.py index 598e73ac..ce4c4880 100644 --- a/esi_leap/objects/offer.py +++ b/esi_leap/objects/offer.py @@ -113,8 +113,9 @@ def get_availabilities(self): return avails - def get_first_availability(self, start): - return self.dbapi.offer_get_first_availability(self.uuid, start) + def get_next_lease_start_time(self, start): + return self.dbapi.offer_get_next_lease_start_time( + self.uuid, start) def create(self, context=None): updates = self.obj_get_changes() diff --git a/esi_leap/tests/db/sqlalchemy/test_api.py b/esi_leap/tests/db/sqlalchemy/test_api.py index 8dd2c610..f80b3063 100644 --- a/esi_leap/tests/db/sqlalchemy/test_api.py +++ b/esi_leap/tests/db/sqlalchemy/test_api.py @@ -169,6 +169,20 @@ status=statuses.EXPIRED, ) +test_lease_7 = dict( + uuid='77777', + project_id='1e5533_2', + owner_id='0wn3r_2', + name='l1', + resource_uuid='1111', + resource_type='dummy_node', + purpose='test_purpose', + start_time=now - datetime.timedelta(days=10), + end_time=now + datetime.timedelta(days=10), + properties={}, + status=statuses.ACTIVE, +) + test_event_1 = dict( id=1, event_type='fake:event:start', @@ -340,15 +354,18 @@ def test_offer_get_conflict_times(self): [(now + datetime.timedelta(days=50), now + datetime.timedelta(days=60))]) - def test_offer_get_first_availability(self): + def test_offer_get_next_lease_start_time(self): o1 = api.offer_create(test_offer_1) - self.assertEqual(api.offer_get_first_availability + self.assertEqual(api.offer_get_next_lease_start_time (o1.uuid, o1.start_time,), None) test_lease_3['offer_uuid'] = o1.uuid api.lease_create(test_lease_3) - self.assertEqual(api.offer_get_first_availability(o1.uuid, - o1.start_time), - (now + datetime.timedelta(days=50),)) + test_lease_7['offer_uuid'] = o1.uuid + api.lease_create(test_lease_7) + self.assertEqual( + api.offer_get_next_lease_start_time( + o1.uuid, o1.start_time), + (now + datetime.timedelta(days=50),)) def test_offer_get_by_uuid(self): o1 = api.offer_create(test_offer_1)