Closed janeliu-slac closed 1 month ago
As an aside, I think it's worthwhile developing the tests as you write the code. I'm often guilty of writing code then making tests after, but practicing proper test-driven development can often help speed up your development.
In this case, if you set up tests that ran the happi load
CLI with various argument combinations, you'd have uncovered some of the bugs I mentioned above
I updated the test_load_glob_args
function so for res in results
will print out the name of each of the mock devices, but got stuck trying to get those names into runner.invoke(happi_cli, ['--path', happi_cfg, 'load', name1, name2])
. Is there a way to add these names without hardcoding it into runner.invoke
? Currently, client.search_regex(name='tst_base_.*')
generates 2 results, but there may be more if someone adds more mock devices in the future.
def test_load_glob_args(
caplog: pytest.LogCaptureFixture,
client: happi.client.Client,
happi_cfg: str,
runner: CliRunner
):
# get search results
results = client.search_regex(name='tst_base_.*')
for res in results:
name = res.item['name'] # the results are 'tst_base_pim' and 'tst_base_pim2'
print(name)
# try to load the item
with mock.patch.object(IPython, 'start_ipython') as m:
_ = runner.invoke(
happi_cli, ['--path', happi_cfg, 'load', 'tst_base_pim', 'tst_base_pim2']
)
m.assert_called_once_with(argv=['--quick'], user_ns=devices)
with caplog.at_level(logging.INFO):
assert "Creating shell with devices" in caplog.text
Hi Jane, if you want to discuss your code you should push it so we can make in-line comments. The test you show here calls runner.invoke without using the glob so I'm confused
It seems like client.load_device()
isn't able to accept globs so I used client.search_regex(name='tst_base_.*')
, which returns multiple SearchResult objects. I was able to get name
from SearchResult objects but wasn't sure how to pass them into runner.invoke()
. I rewrote the code and just commited it (it's definitely not passing pytest
).
I swear these are my last nitpicks, then I think we're good to go
Lol it's fine. I'm learning a lot and taking notes for future projects.
Description
load
function to support searching and loading in one user-friendly expression. Theload
function gathers the results of each search term individually and loads the union of the results.print
statements.load
function.load
function of cli.py removedterm=[]
and replaced with[item]
for readability.Motivation and Context
It allows users to search and load device objects using one CLI expression.
https://github.com/pcdshub/happi/issues/231
How Has This Been Tested?
Manual testing and unit tests.
Where Has This Been Documented?
Documentation is in the code and release notes.
Pre-merge checklist
docs/pre-release-notes.sh
and created a pre-release documentation page