Exemplo n.º 1
0
    def test_should_return_empty_dict_when_exception(self):
        # Given
        client = MagicMock()
        client.hget = MagicMock()
        client.hget.side_effect = redis.exceptions.RedisError

        # When
        result = get_offer_details(client=client, offer_id=1)

        # Then
        assert result == {}
Exemplo n.º 2
0
    def test_should_return_empty_dict_when_offer_does_exists(self):
        # Given
        client = MagicMock()
        client.hget = MagicMock()
        client.hget.return_value = None

        # When
        result = get_offer_details(client=client, offer_id=1)

        # Then
        client.hget.assert_called_once_with("indexed_offers", 1)
        assert result == {}
Exemplo n.º 3
0
    def test_should_return_offer_details_when_offer_exists(self):
        # Given
        client = MagicMock()
        client.hget = MagicMock()
        client.hget.return_value = (
            '{"dateRange": ["2020-01-01 10:00:00", "2020-01-06 12:00:00"], "name": "super offre"}'
        )

        # When
        result = get_offer_details(client=client, offer_id=1)

        # Then
        client.hget.assert_called_once_with("indexed_offers", 1)
        assert result == {"dateRange": ["2020-01-01 10:00:00", "2020-01-06 12:00:00"], "name": "super offre"}
Exemplo n.º 4
0
def process_eligible_offers(client: Redis,
                            offer_ids: List[int],
                            from_provider_update: bool = False) -> None:
    offers_to_add = []
    offers_to_delete = []
    pipeline = client.pipeline()

    offers = offer_queries.get_offers_by_ids(offer_ids)
    for offer in offers:
        offer_exists = check_offer_exists(client=client, offer_id=offer.id)

        if offer and offer.isBookable:
            if from_provider_update and offer_exists:
                offer_details = get_offer_details(client=client,
                                                  offer_id=offer.id)
                if offer_details and is_eligible_for_reindexing(
                        offer, offer_details):
                    offers_to_add.append(build_object(offer=offer))
                    add_to_indexed_offers(
                        pipeline=pipeline,
                        offer_id=offer.id,
                        offer_details=_build_offer_details_to_be_indexed(
                            offer))
            else:
                offers_to_add.append(build_object(offer=offer))
                add_to_indexed_offers(
                    pipeline=pipeline,
                    offer_id=offer.id,
                    offer_details=_build_offer_details_to_be_indexed(offer))
        else:
            if offer_exists:
                offers_to_delete.append(offer.id)

    if len(offers_to_add) > 0:
        _process_adding(pipeline=pipeline,
                        client=client,
                        offer_ids=offer_ids,
                        adding_objects=offers_to_add)

    if len(offers_to_delete) > 0:
        _process_deleting(client=client, offer_ids_to_delete=offers_to_delete)

    if not (offers_to_add or offers_to_delete):
        logger.info("[ALGOLIA] no objects were added nor deleted!")