dabla commented on code in PR #62790: URL: https://github.com/apache/airflow/pull/62790#discussion_r2986260098
########## providers/ibm/mq/tests/unit/ibm/mq/hooks/test_mq.py: ########## @@ -0,0 +1,248 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import asyncio +from unittest import mock +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest + +from airflow.models import Connection +from airflow.providers.ibm.mq.hooks.mq import ( + _BACKOFF_BASE, + _BACKOFF_FACTOR, + _BACKOFF_MAX, + IBMMQHook, +) + +MQ_PAYLOAD = """RFH x"MQSTR <mcd><Msd>jms_map</Msd></mcd> <jms><Dst>topic://localhost/topic</Dst><Tms>1772121947476</Tms><Dlv>2</Dlv><Uci dt='bin.hex'>414D5143514D4941303054202020202069774D7092F81057</Uci></jms>L<usr><XMSC_CLIENT_ID>local</XMSC_CLIENT_ID><release>26.01.00</release></usr> 4<mqps><Top>topic</Top></mqps> {}""" + + +def mq_connection(): + """Create a test MQ connection object.""" + return Connection( + conn_id="mq_conn", + conn_type="mq", + host="mq.example.com", + login="user", + password="pass", + port=1414, + extra='{"queue_manager": "QM1", "channel": "DEV.APP.SVRCONN"}', + ) + + [email protected] +def mock_get_connection(): + """Fixture that mocks BaseHook.get_connection to return a test connection.""" + with patch("airflow.providers.ibm.mq.hooks.mq.BaseHook.get_connection") as mock_conn: + mock_conn.return_value = mq_connection() + yield mock_conn + + [email protected] +def patch_sync_to_async(): + """Patch sync_to_async to call the wrapped function directly for testing.""" + + def sync_to_async(func): + """Wrap a sync function so it can be awaited directly.""" + + async def wrapper(*args, **kwargs): + return func(*args, **kwargs) + + return wrapper + + with patch("airflow.providers.ibm.mq.hooks.mq.sync_to_async", side_effect=sync_to_async): + yield + + +def fake_get(*args, **kwargs): + import ibmmq + + raise ibmmq.MQMIError("connection broken", reason=ibmmq.CMQC.MQRC_CONNECTION_BROKEN) + + [email protected] +class TestIBMMQHook: + """Tests for the IBM MQ hook.""" + + @pytest.fixture(autouse=True) + def setup_connections(self, create_connection_without_db): + create_connection_without_db( + Connection( + conn_id="mq_conn", + conn_type="mq", + host="mq.example.com", + login="user", + password="pass", + port=1414, + extra='{"queue_manager": "QM1", "channel": "DEV.APP.SVRCONN"}', + ) + ) + self.hook = IBMMQHook("mq_conn") + + @patch("ibmmq.connect") + @patch("ibmmq.Queue") + async def test_aconsume_message( + self, mock_queue_class, mock_connect, mock_get_connection, patch_sync_to_async + ): + """Test consuming a single message.""" + + mock_qmgr = MagicMock() + mock_connect.return_value = mock_qmgr + + mock_queue = MagicMock() + mock_queue_class.return_value = mock_queue + mock_queue.get.return_value = MQ_PAYLOAD.format("test message").encode() Review Comment: We cannot use spec as that would mean we need the real ibmmq classes which won't be available at runtime during CI/CD -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
