Skip to content

Commit 04f9a1d

Browse files
fix: manually iterate over cases instead of using pytest.mark.parametrize
1 parent 6f4e8b7 commit 04f9a1d

1 file changed

Lines changed: 59 additions & 65 deletions

File tree

tests/unit/test_bundle.py

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
33

4-
import pytest
54
import unittest
65
from pathlib import Path
76
from unittest import mock
87
from mock import patch, Mock, ANY
9-
from typing import Dict, Union
8+
from typing import Dict, List, Tuple
109

1110
import yaml
1211

@@ -170,9 +169,13 @@ async def test_run_with_charmhub_charm(self):
170169
charm_origin=ANY,
171170
num_units="num_units")
172171

173-
@pytest.mark.parametrize(
174-
'storage_arg_for_change,storage_arg_for_deploy',
175-
[
172+
async def test_run_with_storage_variations(self):
173+
"""Test that various valid storage constraints are parsed as expected before model._deploy is called.
174+
175+
Uses the mock call logic from test_run_with_charmhub_charm, which will run before this test.
176+
"""
177+
storage_arg_pairs: List[Tuple[Dict[str, str], Dict[str, constraints.StorageConstraintDict]]] = [
178+
# (storage_arg_for_change, storage_arg_for_deploy)
176179
({'some-label': 'ebs,100G,1'}, {'some-label': {'count': 1, 'pool': 'ebs', 'size': 102400}}),
177180
({'some-label': 'ebs,2.1G,3'}, {'some-label': {'count': 3, 'pool': 'ebs', 'size': 2150}}),
178181
({'some-label': 'ebs,100G'}, {'some-label': {'count': 1, 'pool': 'ebs', 'size': 102400}}),
@@ -196,66 +199,57 @@ async def test_run_with_charmhub_charm(self):
196199
},
197200
),
198201
]
199-
)
200-
async def test_run_with_storage_variations(
201-
self,
202-
storage_arg_for_change: Dict[str, str],
203-
storage_arg_for_deploy: Dict[str, Dict[str, Union[int, str]]],
204-
):
205-
"""Test that various valid storage constraints are parsed as expected before model._deploy is called.
206-
207-
Uses the mock call logic from test_run_with_charmhub_charm, which will run before this test.
208-
"""
209-
change = AddApplicationChange(
210-
1,
211-
[],
212-
params={
213-
"charm": "charm",
214-
"series": "series",
215-
"application": "application",
216-
"options": "options",
217-
"constraints": "constraints",
218-
"storage": storage_arg_for_change,
219-
"endpoint-bindings": "endpoint_bindings",
220-
"resources": "resources",
221-
"devices": "devices",
222-
"num-units": "num_units",
223-
"channel": "channel",
224-
},
225-
)
226-
# mock model
227-
model = Mock()
228-
model._deploy = mock.AsyncMock(return_value=None)
229-
model._add_charmhub_resources = mock.AsyncMock(return_value=["resource1"])
230-
model.applications = {}
231-
# mock context
232-
context = Mock()
233-
context.resolve.return_value = "ch:charm1"
234-
context.origins = {"ch:charm1": Mock()}
235-
context.trusted = False
236-
context.model = model
237-
# mock info_func
238-
info_func = mock.AsyncMock(return_value=["12345", "name"])
239-
# patch and call
240-
with patch.object(charmhub.CharmHub, 'get_charm_id', info_func):
241-
result = await change.run(context)
242-
assert result == "application"
243-
# asserts
244-
model._deploy.assert_called_once()
245-
model._deploy.assert_called_with(
246-
charm_url="ch:charm1",
247-
application="application",
248-
series="series",
249-
config="options",
250-
constraints="constraints",
251-
endpoint_bindings="endpoint_bindings",
252-
resources=["resource1"],
253-
storage=storage_arg_for_deploy, # we're testing this
254-
devices="devices",
255-
channel="channel",
256-
charm_origin=ANY,
257-
num_units="num_units",
258-
)
202+
for storage_arg_for_change, storage_arg_for_deploy in storage_arg_pairs:
203+
change = AddApplicationChange(
204+
1,
205+
[],
206+
params={
207+
"charm": "charm",
208+
"series": "series",
209+
"application": "application",
210+
"options": "options",
211+
"constraints": "constraints",
212+
"storage": storage_arg_for_change,
213+
"endpoint-bindings": "endpoint_bindings",
214+
"resources": "resources",
215+
"devices": "devices",
216+
"num-units": "num_units",
217+
"channel": "channel",
218+
},
219+
)
220+
# mock model
221+
model = Mock()
222+
model._deploy = mock.AsyncMock(return_value=None)
223+
model._add_charmhub_resources = mock.AsyncMock(return_value=["resource1"])
224+
model.applications = {}
225+
# mock context
226+
context = Mock()
227+
context.resolve.return_value = "ch:charm1"
228+
context.origins = {"ch:charm1": Mock()}
229+
context.trusted = False
230+
context.model = model
231+
# mock info_func
232+
info_func = mock.AsyncMock(return_value=["12345", "name"])
233+
# patch and call
234+
with patch.object(charmhub.CharmHub, 'get_charm_id', info_func):
235+
result = await change.run(context)
236+
assert result == "application"
237+
# asserts
238+
model._deploy.assert_called_once()
239+
model._deploy.assert_called_with(
240+
charm_url="ch:charm1",
241+
application="application",
242+
series="series",
243+
config="options",
244+
constraints="constraints",
245+
endpoint_bindings="endpoint_bindings",
246+
resources=["resource1"],
247+
storage=storage_arg_for_deploy, # we're testing this
248+
devices="devices",
249+
channel="channel",
250+
charm_origin=ANY,
251+
num_units="num_units",
252+
)
259253

260254
async def test_run_with_charmhub_charm_no_channel(self):
261255
"""Test to verify if when the given channel is None, the channel defaults to "local/stable", which

0 commit comments

Comments
 (0)