Skip to content

Commit a3452ae

Browse files
committed
add additional tests
1 parent 9594f62 commit a3452ae

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

tests/test_requirements.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ async def test_install_unpinned_requirements(hass, caplog):
164164
entry = MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})
165165
entry.add_to_hass(hass)
166166

167+
# Check that unpinned version gets skipped because a version is already
168+
# installed
167169
process_requirements.return_value = {
168170
"my-package-name": {
169171
ATTR_SOURCES: [
@@ -178,6 +180,8 @@ async def test_install_unpinned_requirements(hass, caplog):
178180
await hass.async_block_till_done()
179181
assert not ha_install_requirements.called
180182

183+
# Check that unpinned version gets installed because it isn't already
184+
# installed
181185
process_requirements.return_value = {
182186
"my-package-name": {
183187
ATTR_SOURCES: [
@@ -200,8 +204,97 @@ async def test_install_unpinned_requirements(hass, caplog):
200204
await hass.async_block_till_done()
201205
assert ha_install_requirements.called
202206
assert ha_install_requirements.call_args[0][2] == ["my-package-name", "my-package-name-1==2.0.1"]
207+
# my-package-name will show as not installed and therefore won't be included
203208
assert entry.data[CONF_INSTALLED_PACKAGES] == {"my-package-name-1": "2.0.1"}
204209

210+
# Check that entry.data[CONF_INSTALLED_PACKAGES] gets updated with a version number
211+
# when unpinned version was requested
212+
with patch("custom_components.pyscript.requirements.installed_version", return_value="1.1.1"):
213+
process_requirements.return_value = {
214+
"my-package-name": {
215+
ATTR_SOURCES: [
216+
f"{PYSCRIPT_FOLDER}/requirements.txt",
217+
f"{PYSCRIPT_FOLDER}/apps/app1/requirements.txt",
218+
],
219+
ATTR_VERSION: UNPINNED_VERSION,
220+
ATTR_INSTALLED_VERSION: None,
221+
},
222+
"my-package-name-1": {
223+
ATTR_SOURCES: [
224+
f"{PYSCRIPT_FOLDER}/requirements.txt",
225+
f"{PYSCRIPT_FOLDER}/apps/app1/requirements.txt",
226+
],
227+
ATTR_VERSION: "2.0.1",
228+
ATTR_INSTALLED_VERSION: None,
229+
},
230+
}
231+
await install_requirements(hass, entry, PYSCRIPT_FOLDER)
232+
await hass.async_block_till_done()
233+
assert ha_install_requirements.called
234+
assert ha_install_requirements.call_args[0][2] == ["my-package-name", "my-package-name-1==2.0.1"]
235+
assert entry.data[CONF_INSTALLED_PACKAGES] == {
236+
"my-package-name": "1.1.1",
237+
"my-package-name-1": "2.0.1",
238+
}
239+
240+
# Check that package gets removed from entry.data[CONF_INSTALLED_PACKAGES] when it was
241+
# previously installed by pyscript but version was changed presumably by another system
242+
process_requirements.return_value = {
243+
"my-package-name": {
244+
ATTR_SOURCES: [
245+
f"{PYSCRIPT_FOLDER}/requirements.txt",
246+
f"{PYSCRIPT_FOLDER}/apps/app1/requirements.txt",
247+
],
248+
ATTR_VERSION: UNPINNED_VERSION,
249+
ATTR_INSTALLED_VERSION: "2.0.0",
250+
},
251+
"my-package-name-1": {
252+
ATTR_SOURCES: [
253+
f"{PYSCRIPT_FOLDER}/requirements.txt",
254+
f"{PYSCRIPT_FOLDER}/apps/app1/requirements.txt",
255+
],
256+
ATTR_VERSION: "2.0.1",
257+
ATTR_INSTALLED_VERSION: None,
258+
},
259+
}
260+
await install_requirements(hass, entry, PYSCRIPT_FOLDER)
261+
await hass.async_block_till_done()
262+
assert ha_install_requirements.called
263+
assert ha_install_requirements.call_args[0][2] == ["my-package-name-1==2.0.1"]
264+
assert entry.data[CONF_INSTALLED_PACKAGES] == {"my-package-name-1": "2.0.1"}
265+
266+
267+
async def test_install_requirements_not_allowed(hass):
268+
"""Test that install requirements will not work because 'allow_all_imports' is False."""
269+
with patch(
270+
"custom_components.pyscript.requirements.process_all_requirements"
271+
) as process_requirements, patch(
272+
"custom_components.pyscript.requirements.async_process_requirements"
273+
) as ha_install_requirements:
274+
entry = MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: False})
275+
entry.add_to_hass(hass)
276+
277+
# Check that packages get installed correctly
278+
process_requirements.return_value = {
279+
"my-package-name": {
280+
ATTR_SOURCES: [
281+
f"{PYSCRIPT_FOLDER}/requirements.txt",
282+
f"{PYSCRIPT_FOLDER}/apps/app1/requirements.txt",
283+
],
284+
ATTR_VERSION: "2.0.1",
285+
ATTR_INSTALLED_VERSION: None,
286+
},
287+
"my-package-name-alternate": {
288+
ATTR_SOURCES: [f"{PYSCRIPT_FOLDER}/requirements.txt"],
289+
ATTR_VERSION: "2.0.1",
290+
ATTR_INSTALLED_VERSION: None,
291+
},
292+
}
293+
assert await install_requirements(hass, entry, PYSCRIPT_FOLDER) is None
294+
await hass.async_block_till_done()
295+
296+
assert not ha_install_requirements.called
297+
205298

206299
def test_process_requirements():
207300
"""Test process requirements function."""

0 commit comments

Comments
 (0)