Skip to content

Commit e3aa336

Browse files
committed
Sonarcloud smell resolves continued for Init
1 parent dd91749 commit e3aa336

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

tests/test_init.py

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626

2727
pp = PrettyPrinter(indent=8)
2828

29+
CORE_LOCATIONS = "/core/locations"
30+
CORE_LOCATIONS_TAIL = "/core/locations{tail:.*}"
31+
CORE_APPLIANCES_TAIL = "/core/appliances{tail:.*}"
32+
CORE_NOTIFICATIONS_TAIL = "/core/notifications{tail:.*}"
33+
CORE_RULES_TAIL = "/core/rules{tail:.*}"
34+
EMPTY_XML = "<xml />"
35+
2936
_LOGGER = logging.getLogger(__name__)
3037
_LOGGER.setLevel(logging.DEBUG)
3138

@@ -93,37 +100,32 @@ async def setup_app(
93100
app.router.add_get("/system", self.smile_status)
94101

95102
if broken:
96-
app.router.add_get("/core/locations", self.smile_broken)
103+
app.router.add_get(CORE_LOCATIONS, self.smile_broken)
97104
elif timeout:
98-
app.router.add_get("/core/locations", self.smile_timeout)
105+
app.router.add_get(CORE_LOCATIONS, self.smile_timeout)
99106
else:
100-
app.router.add_get("/core/locations", self.smile_locations)
107+
app.router.add_get(CORE_LOCATIONS, self.smile_locations)
101108

102109
# Introducte timeout with 2 seconds, test by setting response to 10ms
103110
# Don't actually wait 2 seconds as this will prolongue testing
104111
if not raise_timeout:
105112
app.router.add_route(
106-
"PUT", "/core/locations{tail:.*}", self.smile_set_temp_or_preset
107-
)
108-
app.router.add_route(
109-
"DELETE", "/core/notifications{tail:.*}", self.smile_del_notification
113+
"PUT", CORE_LOCATIONS_TAIL, self.smile_set_temp_or_preset
110114
)
111-
app.router.add_route("PUT", "/core/rules{tail:.*}", self.smile_set_schedule)
112115
app.router.add_route(
113-
"DELETE", "/core/notifications{tail:.*}", self.smile_del_notification
116+
"DELETE", CORE_NOTIFICATIONS_TAIL, self.smile_del_notification
114117
)
118+
app.router.add_route("PUT", CORE_RULES_TAIL, self.smile_set_schedule)
115119
if not stretch:
116-
app.router.add_route(
117-
"PUT", "/core/appliances{tail:.*}", self.smile_set_relay
118-
)
120+
app.router.add_route("PUT", CORE_APPLIANCES_TAIL, self.smile_set_relay)
119121
else:
120122
app.router.add_route(
121-
"PUT", "/core/appliances{tail:.*}", self.smile_set_relay_stretch
123+
"PUT", CORE_APPLIANCES_TAIL, self.smile_set_relay_stretch
122124
)
123125
else:
124-
app.router.add_route("PUT", "/core/locations{tail:.*}", self.smile_timeout)
125-
app.router.add_route("PUT", "/core/rules{tail:.*}", self.smile_timeout)
126-
app.router.add_route("PUT", "/core/appliances{tail:.*}", self.smile_timeout)
126+
app.router.add_route("PUT", CORE_LOCATIONS_TAIL, self.smile_timeout)
127+
app.router.add_route("PUT", CORE_RULES_TAIL, self.smile_timeout)
128+
app.router.add_route("PUT", CORE_APPLIANCES_TAIL, self.smile_timeout)
127129
app.router.add_route(
128130
"DELETE", "/core/notifications{tail:.*}", self.smile_timeout
129131
)
@@ -187,31 +189,31 @@ async def smile_status(self, request):
187189
@classmethod
188190
async def smile_set_temp_or_preset(cls, request):
189191
"""Render generic API calling endpoint."""
190-
text = "<xml />"
192+
text = EMPTY_XML
191193
raise aiohttp.web.HTTPAccepted(text=text)
192194

193195
@classmethod
194196
async def smile_set_schedule(cls, request):
195197
"""Render generic API calling endpoint."""
196-
text = "<xml />"
198+
text = EMPTY_XML
197199
raise aiohttp.web.HTTPAccepted(text=text)
198200

199201
@classmethod
200202
async def smile_set_relay(cls, request):
201203
"""Render generic API calling endpoint."""
202-
text = "<xml />"
204+
text = EMPTY_XML
203205
raise aiohttp.web.HTTPAccepted(text=text)
204206

205207
@classmethod
206208
async def smile_set_relay_stretch(cls, request):
207209
"""Render generic API calling endpoint."""
208-
text = "<xml />"
210+
text = EMPTY_XML
209211
raise aiohttp.web.HTTPOk(text=text)
210212

211213
@classmethod
212214
async def smile_del_notification(cls, request):
213215
"""Render generic API calling endpoint."""
214-
text = "<xml />"
216+
text = EMPTY_XML
215217
raise aiohttp.web.HTTPAccepted(text=text)
216218

217219
@classmethod
@@ -252,7 +254,7 @@ async def connect(
252254
"""Connect to a smile environment and perform basic asserts."""
253255
port = aiohttp.test_utils.unused_port()
254256
test_password = "".join(
255-
secrets.choice(string.ascii_lowercase) for i in range(8)
257+
secrets.choice(string.ascii_lowercase) for _ in range(8)
256258
)
257259

258260
# Happy flow
@@ -266,15 +268,18 @@ async def connect(
266268
client = aiohttp.test_utils.TestClient(server)
267269
websession = client.session
268270

269-
url = f"{server.scheme}://{server.host}:{server.port}/core/locations"
271+
url = f"{server.scheme}://{server.host}:{server.port}{CORE_LOCATIONS}"
270272

271273
# Try/exceptpass to accommodate for Timeout of aoihttp
272274
try:
273275
resp = await websession.get(url)
274276
assumed_status = self.connect_status(broken, timeout, fail_auth)
275277
assert resp.status == assumed_status
278+
timeoutpass_result = False
279+
assert timeoutpass_result
276280
except Exception: # pylint: disable=broad-except
277-
assert True
281+
timeoutpass_result = True
282+
assert timeoutpass_result
278283

279284
if not broken and not timeout and not fail_auth:
280285
text = await resp.text()
@@ -289,9 +294,11 @@ async def connect(
289294
port=server.port,
290295
websession=None,
291296
)
292-
assert False
297+
lack_of_websession = False
298+
assert lack_of_websession
293299
except Exception: # pylint: disable=broad-except
294-
assert True
300+
lack_of_websession = True
301+
assert lack_of_websession
295302

296303
smile = pw_smile.Smile(
297304
host=server.host,
@@ -385,6 +392,7 @@ def show_setup(location_list, device_list):
385392
_LOGGER.info(" ! no devices found in this location")
386393
assert False
387394

395+
# pragma warning disable S3776
388396
@pytest.mark.asyncio
389397
async def device_test(
390398
self,
@@ -472,6 +480,8 @@ async def device_test(
472480
assert tests == asserts
473481
_LOGGER.debug("Number of test-assert: %s", asserts)
474482

483+
# pragma warning restore S3776
484+
475485
@pytest.mark.asyncio
476486
async def tinker_switch(
477487
self, smile, dev_id=None, members=None, model="relay", unhappy=False
@@ -485,7 +495,7 @@ async def tinker_switch(
485495
try:
486496
await smile.set_switch_state(dev_id, members, model, new_state)
487497
tinker_switch_passed = True
488-
_LOGGER.info(" + worked as intended")
498+
_LOGGER.info(" + tinker_switch worked as intended")
489499
except pw_exceptions.PlugwiseError:
490500
_LOGGER.info(" + locked, not switched as expected")
491501
return False
@@ -514,17 +524,17 @@ async def tinker_thermostat_temp(
514524
_LOGGER.info("- Adjusting temperature to %s", test_temp)
515525
try:
516526
await smile.set_temperature(loc_id, test_temp)
517-
_LOGGER.info(" + worked as intended")
527+
_LOGGER.info(" + tinker_thermostat_temp worked as intended")
518528
return True
519529
except (
520530
pw_exceptions.ErrorSendingCommandError,
521531
pw_exceptions.ResponseError,
522532
):
523533
if unhappy:
524-
_LOGGER.info(" + failed as expected")
534+
_LOGGER.info(" + tinker_thermostat_temp failed as expected")
525535
return True
526536
else: # pragma: no cover
527-
_LOGGER.info(" - failed unexpectedly")
537+
_LOGGER.info(" - tinker_thermostat_temp failed unexpectedly")
528538
return True
529539

530540
@pytest.mark.asyncio
@@ -540,7 +550,7 @@ async def tinker_thermostat_preset(self, smile, loc_id, unhappy=False):
540550
try:
541551
await smile.set_preset(loc_id, new_preset)
542552
tinker_preset_passed = True
543-
_LOGGER.info(" + worked as intended")
553+
_LOGGER.info(" + tinker_thermostat_preset worked as intended")
544554
except pw_exceptions.PlugwiseError:
545555
_LOGGER.info(" + found invalid preset, as expected")
546556
tinker_preset_passed = True
@@ -550,9 +560,9 @@ async def tinker_thermostat_preset(self, smile, loc_id, unhappy=False):
550560
):
551561
if unhappy:
552562
tinker_preset_passed = True
553-
_LOGGER.info(" + failed as expected")
563+
_LOGGER.info(" + tinker_thermostat_preset failed as expected")
554564
else: # pragma: no cover
555-
_LOGGER.info(" - failed unexpectedly")
565+
_LOGGER.info(" - tinker_thermostat_preset failed unexpectedly")
556566
return False
557567

558568
return tinker_preset_passed
@@ -641,9 +651,9 @@ async def tinker_dhw_mode(smile):
641651
_LOGGER.info("%s", f"- Adjusting dhw mode to {mode}{warning}")
642652
try:
643653
await smile.set_dhw_mode(mode)
644-
_LOGGER.info(" + worked as intended")
654+
_LOGGER.info(" + tinker_dhw_mode worked as intended")
645655
except pw_exceptions.PlugwiseError:
646-
_LOGGER.info(" + found invalid mode, as expected")
656+
_LOGGER.info(" + tinker_dhw_mode found invalid mode, as expected")
647657

648658
@staticmethod
649659
async def tinker_regulation_mode(smile):
@@ -656,9 +666,11 @@ async def tinker_regulation_mode(smile):
656666
_LOGGER.info("%s", f"- Adjusting regulation mode to {mode}{warning}")
657667
try:
658668
await smile.set_regulation_mode(mode)
659-
_LOGGER.info(" + worked as intended")
669+
_LOGGER.info(" + tinker_regulation_mode worked as intended")
660670
except pw_exceptions.PlugwiseError:
661-
_LOGGER.info(" + found invalid mode, as expected")
671+
_LOGGER.info(
672+
" + tinker_regulation_mode found invalid mode, as expected"
673+
)
662674

663675
@staticmethod
664676
async def tinker_max_boiler_temp(smile):
@@ -669,9 +681,9 @@ async def tinker_max_boiler_temp(smile):
669681
for test in ["maximum_boiler_temperature", "bogus_temperature"]:
670682
try:
671683
await smile.set_number_setpoint(test, dev_id, new_temp)
672-
_LOGGER.info(" + worked as intended")
684+
_LOGGER.info(" + tinker_max_boiler_temp worked as intended")
673685
except pw_exceptions.PlugwiseError:
674-
_LOGGER.info(" + failed as intended")
686+
_LOGGER.info(" + tinker_max_boiler_temp failed as intended")
675687

676688
@staticmethod
677689
async def tinker_temp_offset(smile, dev_id):
@@ -680,10 +692,10 @@ async def tinker_temp_offset(smile, dev_id):
680692
_LOGGER.info("- Adjusting temperature offset to %s", new_offset)
681693
try:
682694
await smile.set_temperature_offset("dummy", dev_id, new_offset)
683-
_LOGGER.info(" + worked as intended")
695+
_LOGGER.info(" + tinker_temp_offset worked as intended")
684696
return True
685697
except pw_exceptions.PlugwiseError:
686-
_LOGGER.info(" + failed as intended")
698+
_LOGGER.info(" + tinker_temp_offset failed as intended")
687699
return False
688700

689701
@staticmethod

0 commit comments

Comments
 (0)