def test_payload_tracker_set_account_and_request_id(payload_tracker,
                                                    tracker_datetime_mock,
                                                    subtests):
    expected_account = "789"
    expected_request_id = "1234567890"
    producer = Mock()

    tracker = payload_tracker(account=expected_account,
                              request_id=expected_request_id,
                              producer=producer)

    methods_to_test = get_payload_tracker_methods(tracker)

    for method_to_test, expected_status in methods_to_test:
        with subtests.test(method_to_test=method_to_test):
            method_to_test()

            expected_msg = build_expected_tracker_message(
                account=expected_account,
                status=expected_status,
                request_id=expected_request_id,
                datetime_mock=tracker_datetime_mock,
            )

            assert_mock_send_call(producer, DEFAULT_TOPIC, expected_msg)

            producer.reset_mock()
def test_payload_tracker_processing_context_error(payload_tracker,
                                                  tracker_datetime_mock,
                                                  subtests):
    expected_request_id = "REQUEST_ID"
    expected_processing_status = "processing"
    expected_processing_status_msg = "ima processing msg"
    expected_inventory_id = uuid.uuid4()

    producer = Mock()

    tracker = payload_tracker(request_id=expected_request_id,
                              producer=producer)

    operations = [None, "test operation"]

    for current_operation in operations:
        with subtests.test(current_operation=current_operation):
            with pytest.raises(ValueError):
                with PayloadTrackerProcessingContext(
                        payload_tracker=tracker,
                        processing_status_message=
                        expected_processing_status_msg,
                        current_operation=current_operation,
                ) as processing_context:
                    expected_msg = build_expected_tracker_message(
                        status=expected_processing_status,
                        status_msg=expected_processing_status_msg,
                        request_id=expected_request_id,
                        datetime_mock=tracker_datetime_mock,
                    )

                    assert_mock_send_call(producer, DEFAULT_TOPIC,
                                          expected_msg)
                    producer.reset_mock()
                    processing_context.inventory_id = expected_inventory_id
                    method_to_raise_exception()

            expected_msg = build_expected_tracker_message(
                status="processing_error",
                status_msg=build_payload_tracker_context_error_message(
                    "ValueError", current_operation,
                    "something bad happened!"),
                request_id=expected_request_id,
                datetime_mock=tracker_datetime_mock,
            )

            expected_msg["inventory_id"] = str(expected_inventory_id)

            assert tracker.inventory_id is None

            assert_mock_send_call(producer, DEFAULT_TOPIC, expected_msg)

            producer.reset_mock()
def test_payload_tracker_processing_context_success(payload_tracker,
                                                    tracker_datetime_mock,
                                                    subtests):
    expected_request_id = "REQUEST_ID"
    expected_processing_status = "processing"
    expected_processing_status_msg = "ima processing msg"
    expected_inventory_id = uuid.uuid4()

    producer = Mock()

    tracker = payload_tracker(request_id=expected_request_id,
                              producer=producer)

    success_status_msgs = [None, "ima success status msg"]

    for success_status_msg in success_status_msgs:
        with subtests.test(success_status_msg=success_status_msg):
            with PayloadTrackerProcessingContext(
                    payload_tracker=tracker,
                    processing_status_message=expected_processing_status_msg,
                    success_status_message=success_status_msg,
            ) as processing_context:
                expected_msg = build_expected_tracker_message(
                    status=expected_processing_status,
                    status_msg=expected_processing_status_msg,
                    request_id=expected_request_id,
                    datetime_mock=tracker_datetime_mock,
                )

                assert_mock_send_call(producer, DEFAULT_TOPIC, expected_msg)

                processing_context.inventory_id = expected_inventory_id

                producer.reset_mock()

            expected_msg = build_expected_tracker_message(
                status="processing_success",
                status_msg=success_status_msg,
                request_id=expected_request_id,
                datetime_mock=tracker_datetime_mock,
            )

            expected_msg["inventory_id"] = str(expected_inventory_id)

            assert tracker.inventory_id is None

            assert_mock_send_call(producer, DEFAULT_TOPIC, expected_msg)

            producer.reset_mock()
def test_payload_tracker_context_error(payload_tracker, tracker_datetime_mock,
                                       subtests):
    expected_request_id = "REQUEST_ID"
    expected_received_status_msg = "ima received msg"

    producer = Mock()

    tracker = payload_tracker(request_id=expected_request_id,
                              producer=producer)

    operations = [None, "test operation"]

    for current_operation in operations:
        with subtests.test(current_operation=current_operation):
            with pytest.raises(ValueError):
                with PayloadTrackerContext(
                        payload_tracker=tracker,
                        received_status_message=expected_received_status_msg,
                        current_operation=current_operation,
                ):
                    expected_msg = build_expected_tracker_message(
                        status="received",
                        status_msg=expected_received_status_msg,
                        request_id=expected_request_id,
                        datetime_mock=tracker_datetime_mock,
                    )

                    assert_mock_send_call(producer, DEFAULT_TOPIC,
                                          expected_msg)
                    producer.reset_mock()
                    method_to_raise_exception()

            expected_msg = build_expected_tracker_message(
                status="error",
                status_msg=build_payload_tracker_context_error_message(
                    "ValueError", current_operation,
                    "something bad happened!"),
                request_id=expected_request_id,
                datetime_mock=tracker_datetime_mock,
            )

            assert_mock_send_call(producer, DEFAULT_TOPIC, expected_msg)

            producer.reset_mock()
def test_payload_tracker_configure_topic(payload_tracker,
                                         tracker_datetime_mock):
    expected_topic = "ima.kafka.topic"
    expected_request_id = "13579"

    expected_msg = build_expected_tracker_message(
        status="received",
        request_id=expected_request_id,
        datetime_mock=tracker_datetime_mock)

    with patch.dict("os.environ",
                    {"PAYLOAD_TRACKER_KAFKA_TOPIC": expected_topic}):
        producer = Mock()
        tracker = payload_tracker(request_id=expected_request_id,
                                  producer=producer)

        # FIXME: test other methods
        tracker.payload_received()

        assert_mock_send_call(producer, expected_topic, expected_msg)
def test_payload_tracker_context_success(payload_tracker,
                                         tracker_datetime_mock, subtests):
    expected_request_id = "REQUEST_ID"
    expected_received_status_msg = "ima received msg"

    producer = Mock()

    tracker = payload_tracker(request_id=expected_request_id,
                              producer=producer)

    success_status_msgs = [None, "ima success status msg"]

    for success_status_msg in success_status_msgs:
        with subtests.test(success_status_msg=success_status_msg):
            with PayloadTrackerContext(
                    payload_tracker=tracker,
                    received_status_message=expected_received_status_msg,
                    success_status_message=success_status_msg,
            ):
                expected_msg = build_expected_tracker_message(
                    status="received",
                    status_msg=expected_received_status_msg,
                    request_id=expected_request_id,
                    datetime_mock=tracker_datetime_mock,
                )

                assert_mock_send_call(producer, DEFAULT_TOPIC, expected_msg)

                producer.reset_mock()

            expected_msg = build_expected_tracker_message(
                status="success",
                status_msg=success_status_msg,
                request_id=expected_request_id,
                datetime_mock=tracker_datetime_mock,
            )

            assert_mock_send_call(producer, DEFAULT_TOPIC, expected_msg)

            producer.reset_mock()