Skip to content

Add / Update Unit Tests#58

Open
JoshuaAppleby wants to merge 28 commits into
mainfrom
add-unit-tests
Open

Add / Update Unit Tests#58
JoshuaAppleby wants to merge 28 commits into
mainfrom
add-unit-tests

Conversation

@JoshuaAppleby

@JoshuaAppleby JoshuaAppleby commented Aug 2, 2023

Copy link
Copy Markdown
Contributor

Add comprehensive unit tests and add proper typing for modules such as GUI.

@JoshuaAppleby JoshuaAppleby added the enhancement New feature or request label Aug 2, 2023
@JoshuaAppleby
JoshuaAppleby force-pushed the add-unit-tests branch 4 times, most recently from 82a6a92 to 14b20f5 Compare August 21, 2023 13:58
@JoshuaAppleby
JoshuaAppleby force-pushed the add-unit-tests branch 3 times, most recently from d2140c3 to a55c136 Compare August 24, 2023 08:18
@JoshuaAppleby JoshuaAppleby changed the title Draft: Add / Update Unit Tests Add / Update Unit Tests Aug 25, 2023

@MJGaughran MJGaughran left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some initial feedback. I think a number apply to the majority of test cases.

I understand that this PR is still in progress, so just keep it as a Draft for now.

Comment thread src/dls_bba/components.py
Comment thread tests/test_algorithm.py
machine = machine_setup
algorithm = TEST_ALG(machine)
single_offsets = algorithm.create_offsets_dict(TEST_RESULTS_SINGLE, TEST_METADATA)
algorithm.apply_bba_offsets(single_offsets)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to check whether the offsets are actually getting applied? e.g.

assert bpm.offset == new_value

but using the actual attributes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not with the current structure of the unit tests.

Comment thread tests/test_algorithm.py Outdated
@mock.patch("dls_bba.machine.Machine.apply_feedbacks", return_value=None)
@mock.patch("dls_bba.worker.ask_question", side_effect=[True])
@mock.patch("dls_bba.algorithm.bba_offsets_plot", return_value=None)
def test_use_bba_offsets_yes_pass(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, there is no difference in the test for either True or False responses so you are not testing the actual outcome. A unit test can also query a mock object to ensure it has been interacted with correctly. Happy to talk about this as it isn't always easy.

Comment thread tests/test_beam_current.py Outdated

@mock.patch("dls_bba.machine.Machine.get_beam_current", side_effect=[0])
@mock.patch("dls_bba.worker.ask_question", side_effect=[None])
def test_beamcurrentcheck_init(mock_ask_question, mock_beam_current, machine_setup):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure testing basic init methods is very helpful. I think this is made more obvious by the fact that you have to query a protected (sort of private) attribute.

Comment thread tests/test_beam_current.py Outdated
@mock.patch("dls_bba.machine.Machine.get_beam_current", side_effect=[0, 16, 17])
@mock.patch("dls_bba.worker.ask_question", side_effect=[True])
@mock.patch("dls_bba.machine.Machine.check_feedbacks", return_value=None)
def test_beamcurrentcheck_topup_user_returns_yes(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is relevant to many tests. There are several function naming issues:

  1. Repeating the name of the class. This is already specified in the test file name.
  2. Not including conjunctions like if, when etc
  3. Shortening / reducing method names for little gain

test_topup_beam_runs_topup_when_user_selects_yes

Comment thread tests/test_beam_current.py Outdated
@mock.patch("dls_bba.machine.Machine.get_beam_current", side_effect=[0, 14, 16])
@mock.patch("dls_bba.worker.ask_question", side_effect=[True, True])
@mock.patch("dls_bba.machine.Machine.check_feedbacks", return_value=None)
def test_beamcurrentcheck_topup_user_returns_yes_then_yes_then_pass(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous test suggests everything works fine if the user selects Yes. But then we have the additional Yes and Pass.

Something is missing from this test name. Same for the next test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

Comment thread tests/test_common.py Outdated
"dls_bba.machine.Machine._get_effective_corrector",
side_effect=_get_effective_corrector,
)
def test_apply_golden_without_machine(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mentioned similar issues elsewhere; we need to ensure that the golden offsets are actually getting applied somewhere. This could be as simple as asserting that the correct (public) function is getting called, or checking e.g. one of several PVs and values set by caput has the correct form.

Comment thread tests/test_components.py Outdated
def machine_setup():
machine = Machine()
return machine
def test_from_name_h(machine_setup):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use more complete sentences and clear abbreviations.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. it doesn't have to be a complete sentence! Just more than this

Comment thread tests/test_machine.py
machine.quad2bpm("INVALID_QUADRUPOLE")


# TODO: Test get_enabled_bpms / measure_bpms / get_bba_offsets / retry_command.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend to add these for this PR?

@JoshuaAppleby JoshuaAppleby added python Pull requests that update Python code bug Something isn't working labels Aug 31, 2023
@T-Nicholls T-Nicholls mentioned this pull request Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants