Skip to content

Commit 25f1d66

Browse files
committed
refactor(tests): use explicit assertions following Python idioms
- Use `is None` / `is not None` for explicit None checks (PEP 8) - Use `pytest.raises(TypeError, lambda: expr)` for exception tests - Keep `assert collection` for empty/non-empty checks (Pythonic)
1 parent 0aaf7c5 commit 25f1d66

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

tests/test_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ async def test_get_setup(
194194
assert len(setup.gateways) == gateway_count
195195

196196
for device in setup.devices:
197-
assert device.gateway_id
198-
assert device.device_address
199-
assert device.protocol
197+
assert device.gateway_id is not None
198+
assert device.device_address is not None
199+
assert device.protocol is not None
200200

201201
@pytest.mark.parametrize(
202202
"fixture_name",
@@ -238,7 +238,7 @@ async def test_get_diagnostic_data(self, client: OverkizClient, fixture_name: st
238238

239239
with patch.object(aiohttp.ClientSession, "get", return_value=resp):
240240
diagnostics = await client.get_diagnostic_data()
241-
assert diagnostics
241+
assert diagnostics is not None
242242

243243
@pytest.mark.parametrize(
244244
"fixture_name, exception, status_code",
@@ -446,16 +446,16 @@ async def test_get_scenarios(
446446
assert len(scenarios) == scenario_count
447447

448448
for scenario in scenarios:
449-
assert scenario.oid
449+
assert scenario.oid is not None
450450
assert scenario.label is not None
451451
assert scenario.actions
452452

453453
for action in scenario.actions:
454-
assert action.device_url
454+
assert action.device_url is not None
455455
assert action.commands
456456

457457
for command in action.commands:
458-
assert command.name
458+
assert command.name is not None
459459

460460

461461
class MockResponse:

tests/test_models.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_none_states(self):
201201
hump_device = humps.decamelize(RAW_DEVICES)
202202
del hump_device["states"]
203203
device = Device(**hump_device)
204-
assert not device.states.get(STATE)
204+
assert device.states.get(STATE) is None
205205

206206

207207
class TestStates:
@@ -211,19 +211,19 @@ def test_empty_states(self):
211211
"""An empty list yields an empty States object with no state found."""
212212
states = States([])
213213
assert not states
214-
assert not states.get(STATE)
214+
assert states.get(STATE) is None
215215

216216
def test_none_states(self):
217217
"""A None value for states should behave as empty."""
218218
states = States(None)
219219
assert not states
220-
assert not states.get(STATE)
220+
assert states.get(STATE) is None
221221

222222
def test_getter(self):
223223
"""Retrieve a known state and validate its properties."""
224224
states = States(RAW_STATES)
225225
state = states.get(STATE)
226-
assert state
226+
assert state is not None
227227
assert state.name == STATE
228228
assert state.type == DataType.STRING
229229
assert state.value == "alarm name"
@@ -232,7 +232,7 @@ def test_getter_missing(self):
232232
"""Requesting a missing state returns falsy (None)."""
233233
states = States(RAW_STATES)
234234
state = states.get("FooState")
235-
assert not state
235+
assert state is None
236236

237237

238238
class TestState:
@@ -246,8 +246,7 @@ def test_int_value(self):
246246
def test_bad_int_value(self):
247247
"""Accessor raises TypeError if the state type mismatches expected int."""
248248
state = State(name="state", type=DataType.BOOLEAN, value=False)
249-
with pytest.raises(TypeError):
250-
assert state.value_as_int
249+
pytest.raises(TypeError, lambda: state.value_as_int)
251250

252251
def test_float_value(self):
253252
"""Float typed state returns proper float accessor."""
@@ -257,8 +256,7 @@ def test_float_value(self):
257256
def test_bad_float_value(self):
258257
"""Accessor raises TypeError if the state type mismatches expected float."""
259258
state = State(name="state", type=DataType.BOOLEAN, value=False)
260-
with pytest.raises(TypeError):
261-
assert state.value_as_float
259+
pytest.raises(TypeError, lambda: state.value_as_float)
262260

263261
def test_bool_value(self):
264262
"""Boolean typed state returns proper boolean accessor."""
@@ -268,8 +266,7 @@ def test_bool_value(self):
268266
def test_bad_bool_value(self):
269267
"""Accessor raises TypeError if the state type mismatches expected bool."""
270268
state = State(name="state", type=DataType.INTEGER, value=1)
271-
with pytest.raises(TypeError):
272-
assert state.value_as_bool
269+
pytest.raises(TypeError, lambda: state.value_as_bool)
273270

274271
def test_str_value(self):
275272
"""String typed state returns proper string accessor."""
@@ -279,8 +276,7 @@ def test_str_value(self):
279276
def test_bad_str_value(self):
280277
"""Accessor raises TypeError if the state type mismatches expected string."""
281278
state = State(name="state", type=DataType.BOOLEAN, value=False)
282-
with pytest.raises(TypeError):
283-
assert state.value_as_str
279+
pytest.raises(TypeError, lambda: state.value_as_str)
284280

285281
def test_dict_value(self):
286282
"""JSON object typed state returns proper dict accessor."""
@@ -290,8 +286,7 @@ def test_dict_value(self):
290286
def test_bad_dict_value(self):
291287
"""Accessor raises TypeError if the state type mismatches expected dict."""
292288
state = State(name="state", type=DataType.BOOLEAN, value=False)
293-
with pytest.raises(TypeError):
294-
assert state.value_as_dict
289+
pytest.raises(TypeError, lambda: state.value_as_dict)
295290

296291
def test_list_value(self):
297292
"""JSON array typed state returns proper list accessor."""
@@ -301,5 +296,4 @@ def test_list_value(self):
301296
def test_bad_list_value(self):
302297
"""Accessor raises TypeError if the state type mismatches expected list."""
303298
state = State(name="state", type=DataType.BOOLEAN, value=False)
304-
with pytest.raises(TypeError):
305-
assert state.value_as_list
299+
pytest.raises(TypeError, lambda: state.value_as_list)

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_generate_local_server(self):
1818
"""Create a local server descriptor using the host and default values."""
1919
local_server = generate_local_server(host=LOCAL_HOST)
2020

21-
assert local_server
21+
assert local_server is not None
2222
assert (
2323
local_server.endpoint
2424
== "https://gateway-1234-5678-1243.local:8443/enduser-mobile-web/1/enduserAPI/"
@@ -36,7 +36,7 @@ def test_generate_local_server_by_ip(self):
3636
configuration_url="https://somfy.com",
3737
)
3838

39-
assert local_server
39+
assert local_server is not None
4040
assert (
4141
local_server.endpoint
4242
== "https://192.168.1.105:8443/enduser-mobile-web/1/enduserAPI/"

0 commit comments

Comments
 (0)