Skip to content
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

test: increase coverage of unit tests generator classes #1659

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

hetangmodi-crest
Copy link
Contributor

Issue number:
ADDON-79015

PR Type

What kind of change does this PR introduce?

  • Feature
  • Bug Fix
  • Refactoring (no functional or API changes)
  • Documentation Update
  • Maintenance (dependency updates, CI, etc.)

Summary

Changes

Added unit test cases for the generators module to achieve 100% test coverage.

User experience

No change in user experience.

Checklist

If an item doesn't apply to your changes, leave it unchecked.

Review

  • self-review - I have performed a self-review of this change according to the development guidelines
  • Changes are documented. The documentation is understandable, examples work (more info)
  • PR title and description follows the contributing principles
  • meeting - I have scheduled a meeting or recorded a demo to explain these changes (if there is a video, put a link below and in the ticket)

Tests

See the testing doc.

  • Unit - tests have been added/modified to cover the changes
  • Smoke - tests have been added/modified to cover the changes
  • UI - tests have been added/modified to cover the changes
  • coverage - I have checked the code coverage of my changes (see more)

Demo/meeting:

Reviewers are encouraged to request meetings or demos if any part of the change is unclear

@hetangmodi-crest hetangmodi-crest added the tests Updates in test cases (unit, smoke, ui) label Mar 25, 2025
@hetangmodi-crest hetangmodi-crest self-assigned this Mar 25, 2025
@hetangmodi-crest hetangmodi-crest requested a review from a team as a code owner March 25, 2025 11:19
@hetangmodi-crest hetangmodi-crest changed the title tests: unit test cases for generators to increase coverage test: increase coverage of unit tests generator classes Mar 25, 2025
@hetangmodi-crest hetangmodi-crest requested a review from a team as a code owner March 25, 2025 11:32
@@ -58,6 +82,32 @@ def test_set_attribute_with_no_pages(
assert not hasattr(default_xml, "default_xml_content")


@patch(
"splunk_add_on_ucc_framework.generators.xml_files.DefaultXml._set_attributes",
Copy link
Contributor

Choose a reason for hiding this comment

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

I commented out this patch and the test passes. Is it needed and expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it would work, but I am using the Mockist style unit testing - that the function calls do not go outside that function and I have the complete control of all returned objects from function calls made.
If we comment it, the parent's methods would be called, which is the way of Classical style unit testing, both would work.

new_callable=list,
)
@patch("splunk_add_on_ucc_framework.generators.file_generator.logger")
def test_begin_if_empty_dict(
Copy link
Contributor

Choose a reason for hiding this comment

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

What should this test check? Which dict is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test case checks the mentioned condition. It increases the coverage to 100%.

ucc_dir=ucc_dir,
addon_name=ta_name,
)
alert_html._set_attributes()
Copy link
Contributor

Choose a reason for hiding this comment

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

This call is redundant as it is already called in the init

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the code.
Removed explicit calls to _set_attributes() wherever not required.


server_conf._gc_schema = None

server_conf._set_attributes()
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we will be requiring to explicitly call _set_attributes() as after creating an instance of ServerConf we are setting _gc_schema to None.

):
global_config_for_alerts = MagicMock()
global_config_for_alerts.alerts = []
mock_normalize.normalize.return_value = {"schema.content": {"modular_alerts": []}}
Copy link
Contributor

Choose a reason for hiding this comment

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

The test passes without this mock. Is it needed?

Copy link
Contributor Author

@hetangmodi-crest hetangmodi-crest Mar 27, 2025

Choose a reason for hiding this comment

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

I added it as I was following Mockist style unit test, however the test works without it too. Shall I remove it if we are following Classical style unit test?

Copy link
Member

@artemrys artemrys left a comment

Choose a reason for hiding this comment

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

I think the logic here can be simplified a lot - FileGenerator, XMLGenerator and HTMLGenerator can be deleted and the files generation logic can be delegated to the implementation classes themselves.

XMLGenerator and HTMLGenerator are converting None to {"": ""} or taking what's there if not None. DefaultXml should be able to do it on it's own.

FileGenerator is a bit different story but yet similar. Implementation classes generate 2 files - .conf and .conf.spec. But actually maybe only half of the classes generate both those files. Can we make those implementation classes implement generate method themselves?

Something like (I did not test if that works but the idea should be clear)

    def generate(self) -> Dict[str, str]:
        conf_file = self.generate_conf()
        conf_spec_file = self.generate_conf_spec()
        return {
            **conf_file,
            **conf_spec_file,
        }

This can be used in AlertActionsConf for example.

@kkedziak-splunk I would appreciate your opinion on this matter.

@patch(
"splunk_add_on_ucc_framework.generators.conf_files.ConfGenerator.generate_conf_spec"
)
def test_generate_conf_and_conf_spec_is_absent(
Copy link
Member

Choose a reason for hiding this comment

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

This test case is essentially the same as the one below (test_generate_conf).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I have removed the test case.

Copy link

github-actions bot commented Apr 4, 2025

Code Coverage 🎉

Type PR Develop Change Status
Line Coverage 88.23% 87.33% 0.90% 🟢 Increased
Branch Coverage 82.13% 80.53% 1.60% 🟢 Increased

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XXL tests Updates in test cases (unit, smoke, ui)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants