test_service_cache_ttl.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Tests for the Airbnb reservation-detail TTL cache (TTLCache in _month_cache.py).
  2. Month-cache mechanics (TTL, in-flight dedup, suspect retry, on_evict wiring)
  3. are covered generically across both clients in
  4. test_service_client_conformance.py; TTLCache mechanics in general are covered
  5. in test_month_cache.py. This file is what's left that's Airbnb-specific: the
  6. per-reservation detail cache (self._details) has no analog on Booking.com.
  7. """
  8. import time
  9. from room_cal.service_api._utils import CACHE_TTL
  10. _EXPIRED = -(CACHE_TTL + 1)
  11. def _airbnb_client():
  12. from room_cal.service_api._airbnb import _AirbnbClient
  13. return _AirbnbClient(cookies='test', listing_id='12345678')
  14. class TestAirbnbDetailTTL:
  15. def _stub_gql(self, client, det):
  16. async def _gql(op, h, variables, referer):
  17. return {'data': {'presentation': {'hostingDetails': {'stayHostingDetails': det}}}}
  18. client._gql = _gql
  19. async def test_fresh_detail_served_from_cache(self):
  20. client = _airbnb_client()
  21. client._details.set('C1', {'cached': True})
  22. self._stub_gql(client, {'fresh': True})
  23. assert await client._fetch_one_detail('C1') == {'cached': True}
  24. async def test_expired_detail_refetches(self):
  25. client = _airbnb_client()
  26. client._details._data['C1'] = {'cached': True}
  27. client._details._fetched_at['C1'] = time.monotonic() + _EXPIRED
  28. self._stub_gql(client, {'fresh': True})
  29. assert await client._fetch_one_detail('C1') == {'fresh': True}
  30. assert client._details.get('C1') == {'fresh': True}