-
Notifications
You must be signed in to change notification settings - Fork 421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unit tests for mongoose_lazy_routing module #3127
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3127 +/- ##
==========================================
- Coverage 79.31% 79.25% -0.06%
==========================================
Files 394 394
Lines 32150 32155 +5
==========================================
- Hits 25500 25485 -15
- Misses 6650 6670 +20
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks fine, I almost understand the tests.
Added comments about whitespaces though.
test/mongoose_lazy_routing_SUITE.erl
Outdated
?assertEqual([], get_all_unregistered_iqs()), | ||
meck:reset(gen_iq_component), | ||
%% register 2 IQ handlers for ?HOST_TYPE_1 subdomains, then add one subdomain and | ||
%% one domain (just to ensure that domain doesn't affect anything) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer tests to follow this convention:
can_register_iq_handler_for_subdomain() ->
%% GIVEN idk, for me nothing is given in this case
%% WHEN Register IQ handler for subdomain
?assertEqual(true, maybe_add_domain_or_subdomain(?SUBDOMAIN_2)), %% ONE subdomain
%% THEN registered
assert_subdomain_registered(?SUBDOMAIN_2).
If we want to test registering two subdomains, we do another case:
can_register_iq_handler_for_two_subdomains() ->
%% GIVEN idk, for me nothing is given in this case
%% WHEN Register IQ handler for subdomains
?assertEqual(true, maybe_add_domain_or_subdomain(?SUBDOMAIN_1)),
?assertEqual(true, maybe_add_domain_or_subdomain(?SUBDOMAIN_2)),
%% THEN registered
assert_subdomain_registered(?SUBDOMAIN_1),
assert_subdomain_registered(?SUBDOMAIN_2).
If we want to test unregister:
can_unregister_iq_handler_for_two_subdomains() ->
%% GIVEN two registered domains
?assertEqual(true, maybe_add_domain_or_subdomain(?SUBDOMAIN_1)),
?assertEqual(true, maybe_add_domain_or_subdomain(?SUBDOMAIN_2)),
%% WHEN Register IQ handler for subdomains
unregister_subdomain(?SUBDOMAIN_1),
unregister_subdomain(?SUBDOMAIN_2),
%% THEN registered
assert_subdomain_not_registered(?SUBDOMAIN_1),
assert_subdomain_not_registered(?SUBDOMAIN_2).
I am not a big fun of asserts in the given part, so:
can_unregister_iq_handler_for_two_subdomains() ->
%% GIVEN two registered domains
given_subdomains([?SUBDOMAIN_1, ?SUBDOMAIN_2]),
%% WHEN Unegister IQ handler for subdomains
unregister_subdomain(?SUBDOMAIN_1),
unregister_subdomain(?SUBDOMAIN_2),
%% THEN not registered
assert_subdomain_not_registered(?SUBDOMAIN_1),
assert_subdomain_not_registered(?SUBDOMAIN_2).
If you check more than two things in a case, it is still ok, unless test fails. After that it's hard to understand what is actually failing.
If you do something that is not described in test name - it's very hard to understand if the test is valid or not.
Overall, it's a good idea to follow GIVEN... WHEN... THAN...
structure.
And make helper functions for GIVEN and THAN conditions.
And generally, it's good idea to think about post conditions. They are our side effects. What does it mean subdomain not registered? It could be encapsulated inside that assert_subdomain_not_registered
function.
Side effects dictate the name of the test case. Action (WHEN block) in the test is the secondary, but also should be present in the name.
%% Was can_unregister_iq_handler_for_two_subdomains
unregistered_subdomain_is_not_listed() ->
%% GIVEN
given_subdomains([?SUBDOMAIN_1]),
%% WHEN
unregister_subdomain(?SUBDOMAIN_1),
%% THEN not registered
assert_subdomain_not_registered(?SUBDOMAIN_1).
unregistered_subdomain_looses_iq_handlers() ->
%% GIVEN
given_subdomains([?SUBDOMAIN_1]),
given_iq_handler(...),
%% WHEN
unregister_subdomain(?SUBDOMAIN_1),
%% THEN not registered
assert_iq_handler_not_registered(...).
Also, testing with more complex preconditions is cool, but harder to read. Still can be done, but in a separate test case.
%% Was can_unregister_iq_handler_for_two_subdomains
unregistered_subdomain_is_not_listed_when_there_is_another_domain() ->
%% GIVEN
given_subdomains([?SUBDOMAIN_2]),
unregistered_subdomain_looses_iq_handlers().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that splitting this huge test case could help with readability.
Personally I think that naming functions given_*
is not a good practice - I'd rather keep the namesregister_subdomains
, register_iq_handler
etc. to be explicit about what they are exactly doing.
IMO there is no need for a significant rework here, splitting could help (test sequence in a group could suffice), but it's not a must.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given, when, then
template doesn't fit well for the cases that test correct setup/correct rollback
behaviour.
I will try to split can_register_and_unregister_iq_handler_for_domain
and can_register_and_unregister_iq_handler_for_subdomain
test case into 4 smaller
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I have a few minor comments.
test/mongoose_lazy_routing_SUITE.erl
Outdated
?assertEqual([], get_all_unregistered_iqs()), | ||
meck:reset(gen_iq_component), | ||
%% register 2 IQ handlers for ?HOST_TYPE_1 subdomains, then add one subdomain and | ||
%% one domain (just to ensure that domain doesn't affect anything) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that splitting this huge test case could help with readability.
Personally I think that naming functions given_*
is not a good practice - I'd rather keep the namesregister_subdomains
, register_iq_handler
etc. to be explicit about what they are exactly doing.
IMO there is no need for a significant rework here, splitting could help (test sequence in a group could suffice), but it's not a must.
a28d8eb
to
744e0d4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
?assertEqual([], get_all_registered_iqs()), | ||
?assertEqual([], get_all_unregistered_iqs()). | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: double empty line.
This PR introduces unit tests for mongoose_lazy_routing module