Coverage for tests/brokers/mqtt/test_testclient.py: 99%

106 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1import asyncio 

2from unittest.mock import MagicMock 

3 

4import pytest 

5 

6from faststream.mqtt.broker.broker import MQTTBroker 

7from faststream.mqtt.testing import FakeProducer, TestMQTTBroker, mqtt_topic_matches 

8from tests.brokers.base.testclient import BrokerTestclientTestcase 

9 

10from .basic import MQTTMemoryTestcaseConfig 

11 

12_SKIP_V311 = "not supported in MQTT 3.1.1" 

13 

14 

15@pytest.mark.mqtt() 

16@pytest.mark.asyncio() 

17class TestTestclient(MQTTMemoryTestcaseConfig, BrokerTestclientTestcase): 

18 def get_fake_producer_class(self) -> type: 

19 return FakeProducer 

20 

21 async def test_consume_with_filter( 

22 self, queue: str, mock: MagicMock, event: asyncio.Event, event2: asyncio.Event 

23 ) -> None: 

24 if self.version == "3.1.1": 

25 pytest.skip(_SKIP_V311) 

26 await super().test_consume_with_filter(queue, mock, event, event2) 

27 

28 async def test_response( 

29 self, queue: str, mock: MagicMock, event: asyncio.Event 

30 ) -> None: 

31 if self.version == "3.1.1": 

32 pytest.skip(_SKIP_V311) 

33 await super().test_response(queue, mock, event) 

34 

35 async def test_custom_id_generator(self, queue: str, mock: MagicMock) -> None: 

36 if self.version == "3.1.1": 

37 pytest.skip(_SKIP_V311) 

38 await super().test_custom_id_generator(queue, mock) 

39 

40 async def test_reply_to( 

41 self, queue: str, mock: MagicMock, event: asyncio.Event 

42 ) -> None: 

43 if self.version == "3.1.1": 

44 pytest.skip(_SKIP_V311) 

45 await super().test_reply_to(queue, mock, event) 

46 

47 async def test_subscriber_assertion_checks_body_fields_and_context( 

48 self, queue: str 

49 ) -> None: 

50 if self.version == "3.1.1": 

51 pytest.skip(_SKIP_V311) 

52 await super().test_subscriber_assertion_checks_body_fields_and_context(queue) 

53 

54 async def test_publisher_assert_called_once_with(self, queue: str) -> None: 

55 if self.version == "3.1.1": 

56 pytest.skip(_SKIP_V311) 

57 await super().test_publisher_assert_called_once_with(queue) 

58 

59 async def test_subscriber_assert_called_with_reads_the_last_call( 

60 self, queue: str 

61 ) -> None: 

62 if self.version == "3.1.1": 

63 pytest.skip(_SKIP_V311) 

64 await super().test_subscriber_assert_called_with_reads_the_last_call(queue) 

65 

66 async def test_subscriber_assert_any_call_searches_every_call( 

67 self, queue: str 

68 ) -> None: 

69 if self.version == "3.1.1": 

70 pytest.skip(_SKIP_V311) 

71 await super().test_subscriber_assert_any_call_searches_every_call(queue) 

72 

73 async def test_publisher_assertions_share_the_recorded_calls( 

74 self, queue: str 

75 ) -> None: 

76 if self.version == "3.1.1": 

77 pytest.skip(_SKIP_V311) 

78 await super().test_publisher_assertions_share_the_recorded_calls(queue) 

79 

80 @pytest.mark.connected() 

81 async def test_broker_gets_patched_attrs_within_cm(self) -> None: 

82 await super().test_broker_gets_patched_attrs_within_cm(FakeProducer) 

83 

84 @pytest.mark.connected() 

85 async def test_broker_with_real_doesnt_get_patched(self) -> None: 

86 await super().test_broker_with_real_doesnt_get_patched() 

87 

88 @pytest.mark.connected() 

89 async def test_broker_with_real_patches_publishers_and_subscribers( 

90 self, 

91 queue: str, 

92 ) -> None: 

93 await super().test_broker_with_real_patches_publishers_and_subscribers(queue) 

94 

95 

96@pytest.mark.mqtt() 

97class TestTopicMatching: 

98 """Unit tests for the MQTT wildcard matching helper.""" 

99 

100 def test_exact_match(self) -> None: 

101 assert mqtt_topic_matches("sensors/temp", "sensors/temp") 

102 

103 def test_exact_no_match(self) -> None: 

104 assert not mqtt_topic_matches("sensors/temp", "sensors/humidity") 

105 

106 def test_single_level_wildcard(self) -> None: 

107 assert mqtt_topic_matches("sensors/+/temp", "sensors/room1/temp") 

108 assert not mqtt_topic_matches("sensors/+/temp", "sensors/room1/floor2/temp") 

109 

110 def test_multi_level_wildcard(self) -> None: 

111 assert mqtt_topic_matches("sensors/#", "sensors/room1/temp") 

112 assert mqtt_topic_matches("sensors/#", "sensors/room1/floor/temp") 

113 assert mqtt_topic_matches("sensors/#", "sensors") 

114 

115 def test_root_hash(self) -> None: 

116 assert mqtt_topic_matches("#", "anything/at/all") 

117 

118 def test_shared_subscription(self) -> None: 

119 assert mqtt_topic_matches("$share/workers/sensors/#", "sensors/temp") 

120 assert not mqtt_topic_matches("$share/workers/sensors/+", "sensors/a/b") 

121 

122 

123@pytest.mark.mqtt() 

124@pytest.mark.asyncio() 

125class TestWildcardRouting: 

126 """Verify that TestMQTTBroker routes wildcard topics correctly.""" 

127 

128 async def test_plus_wildcard_routes(self, queue: str) -> None: 

129 broker = MQTTBroker() 

130 

131 @broker.subscriber(f"{queue}/+/temp") 

132 async def handler(temperature: float) -> None: 

133 pass 

134 

135 async with TestMQTTBroker(broker) as br: 

136 await br.start() 

137 await br.publish("22.5", f"{queue}/room1/temp") 

138 handler.mock.assert_called_once_with("22.5") 

139 

140 async def test_hash_wildcard_routes(self, queue: str) -> None: 

141 broker = MQTTBroker() 

142 

143 @broker.subscriber(f"{queue}/#") 

144 async def handler(msg: str) -> None: 

145 pass 

146 

147 async with TestMQTTBroker(broker) as br: 

148 await br.start() 

149 await br.publish("data", f"{queue}/a/b/c") 

150 handler.mock.assert_called_once_with("data") 

151 

152 async def test_no_match_skips_handler(self, queue: str) -> None: 

153 broker = MQTTBroker() 

154 

155 @broker.subscriber(f"{queue}/exact") 

156 async def handler(msg: str) -> None: 

157 pass 

158 

159 async with TestMQTTBroker(broker) as br: 

160 await br.start() 

161 await br.publish("data", f"{queue}/other") 

162 handler.mock.assert_not_called() 

163 

164 async def test_shared_subscription_routing(self, queue: str) -> None: 

165 """Messages to a shared group topic should reach exactly one subscriber.""" 

166 broker = MQTTBroker() 

167 

168 call_count = 0 

169 

170 @broker.subscriber(f"{queue}/data", shared="workers") 

171 async def handler(msg: str) -> None: 

172 nonlocal call_count 

173 call_count += 1 

174 

175 async with TestMQTTBroker(broker) as br: 

176 await br.start() 

177 await br.publish("hello", f"{queue}/data") 

178 assert call_count == 1