import time

from APITaxi2 import tasks


class TestCleanGeoindexTimestamps:
    def test_expired(self, app):
        now = time.time()
        expired = now - 300

        # Store fresh locations
        app.redis.zadd('timestamps_id', {'FRESH_TAXI_ID': now})
        app.redis.geoadd('geoindex', 2.22, 48.88, 'FRESH_TAXI_ID')
        app.redis.zadd('timestamps', {'FRESH_TAXI_ID:OPERATOR': now})
        app.redis.geoadd('geoindex_2', 2.22, 48.88, 'FRESH_TAXI_ID:OPERATOR')

        # Store expired locations
        app.redis.zadd('timestamps_id', {'EXPIRED_TAXI_ID': expired})
        app.redis.geoadd('geoindex', 2.22, 48.88, 'EXPIRED_TAXI_ID')
        app.redis.zadd('timestamps', {'EXPIRED_TAXI_ID:OPERATOR': expired})
        app.redis.geoadd('geoindex_2', 2.22, 48.88, 'EXPIRED_TAXI_ID:OPERATOR')

        tasks.clean_geoindex_timestamps()

        # Expired locations have been removed, fresh locations are still there
        assert len(app.redis.zrange('timestamps_id', 0, -1)) == 1
        assert len(app.redis.zrange('geoindex', 0, -1)) == 1
        assert len(app.redis.zrange('timestamps', 0, -1)) == 1
        assert len(app.redis.zrange('geoindex_2', 0, -1)) == 1

@@ -9,22 +9,182 @@
 ime%0A
-%0Afrom APITaxi2
+from unittest import mock%0A%0Afrom APITaxi_models2 import Hail, VehicleDescription%0Afrom APITaxi_models2.unittest.factories import HailFactory, VehicleDescriptionFactory%0A%0Afrom ..
  imp
@@ -1319,24 +1319,1706 @@
 oindex_2', 0, -1)) == 1%0A
+%0A%0Aclass TestHandleHailTimeout:%0A    def test_timeout(self, app):%0A        %22%22%22Hail reaches timeout. Status is initially sent_to_operator, and it%0A        still has the same status when timeout function is called. New taxi%0A        status is 'off'.%22%22%22%0A        hail = HailFactory(status='sent_to_operator')%0A        hail_id = hail.id%0A%0A        # Hail status timeout: it is still sent_to_operator%0A        with mock.patch.object(tasks.operators.current_app.logger, 'warning') as mocked_logger:%0A            tasks.handle_hail_timeout(hail.id, hail.operateur.id, 'sent_to_operator', 'failure', 'off')%0A            assert mocked_logger.call_count == 1%0A%0A        # Since handle_hail_timeout commits the session, we have to fetch Hail%0A        # back to read its properties.%0A        hail = Hail.query.get(hail_id)%0A        assert hail.status == 'failure'%0A%0A        vehicle_description = VehicleDescription.query.one()%0A        assert vehicle_description.status == 'off'%0A%0A    def test_no_timeout(self, app):%0A        %22%22%22Hail doesn't reach timeout. Status is initially%0A        received_by_operator, and it is sent_to_operator when timeout function%0A        is called.%22%22%22%0A        hail = HailFactory(status='sent_to_operator')%0A        VehicleDescriptionFactory(vehicle=hail.taxi.vehicle)%0A%0A        hail.status = 'received_by_operator'%0A        with mock.patch.object(tasks.operators.current_app.logger, 'warning') as mocked_logger:%0A            tasks.handle_hail_timeout(hail.id, hail.operateur.id, 'sent_to_operator', 'failure', None)%0A            assert mocked_logger.call_count == 0%0A%0A        # No need to fetch back hail since session has not been committed.%0A        assert hail.status == 'received_by_operator'%0A

Add unittests for tasks.handle_hail_timeout