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

[REF] tests : Refactoring of the tests #296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jaeschwa
Copy link
Contributor

Enhance Testing Framework, Refactor XML Parsing with etree, and Enforce Field Constraints. To streamline the testing process and reduce the burden of code reviews, this commit introduces a multitude of enhancements and refactors within the codebase. Key improvements include:

  • New tests for manifests and xml

  • A shift from regex-based to etree-based XML parsing

  • Implementation of strict field constraints

  • Added Extensive Tests:

    • Implemented numerous new tests to simplify testing and reduce review complexity.
    • Expanded coverage across multiple modules to ensure robustness.
  • Refactored XML Parsing:

    • Replaced regex-based parsing with etree from the lxml library for improved accuracy and maintainability.
    • Enhanced error handling and logging for XML processing.
  • Implemented check_fields and FIELDS_NOT_TO_SET_DIRECTLY:

    • Introduced the check_fields function to validate that certain fields are not set directly in XML data because those are readonly fields computed fields.
    • Defined FIELDS_NOT_TO_SET_DIRECTLY to specify fields across various model fields that should be defined in their related model.
  • Updated Manifest Validation:

    • Strengthened manifest checks to include mandatory keys and correct dependency ordering.
    • Ensured theme dependencies are listed last and dependencies are alphabetically sorted.

These changes significantly improve the maintainability and reliability of the codebase by enhancing test coverage, ensuring proper XML handling, and enforcing strict field constraints. The refactored XML parsing with etree provides a more robust solution, while the check_fields mechanism safeguards against improper data manipulation. Enhanced manifest validations and logging facilitate smoother development and deployment processes.

@robodoo
Copy link
Collaborator

robodoo commented Nov 21, 2024

Pull request status dashboard

Enhance Testing Framework, Refactor XML Parsing with etree,
and Enforce Field Constraints. To streamline the testing process
and reduce the burden of code reviews, this commit introduces a
multitude of enhancements and refactors within the codebase.
Key improvements include:
 - New tests for manifests and xml
 - A shift from regex-based to etree-based XML parsing
 - Implementation of strict field constraints

- Added Extensive Tests:
  - Implemented numerous new tests to simplify testing and reduce review complexity.
  - Expanded coverage across multiple modules to ensure robustness.

- Refactored XML Parsing:
  - Replaced regex-based parsing with `etree` from the `lxml`
     library for improved accuracy and maintainability.
  - Enhanced error handling and logging for XML processing.

- Implemented `check_fields` and `FIELDS_NOT_TO_SET_DIRECTLY`:
  - Introduced the `check_fields` function to validate that certain
    fields are not set directly in XML data because those are readonly
    fields computed fields.
  - Defined `FIELDS_NOT_TO_SET_DIRECTLY` to specify fields across various
    model fields that should be defined in their related model.

- Updated Manifest Validation:
  - Strengthened manifest checks to include mandatory keys and correct
     dependency ordering.
  - Ensured theme dependencies are listed last and dependencies are
    alphabetically sorted.

These changes significantly improve the maintainability and reliability
of the codebase by enhancing test coverage, ensuring proper XML handling,
and enforcing strict field constraints. The refactored XML parsing with
`etree` provides a more robust solution, while the `check_fields` mechanism
safeguards against improper data manipulation. Enhanced manifest validations
and logging facilitate smoother development and deployment processes.
Copy link
Collaborator

@vava-odoo vava-odoo left a comment

Choose a reason for hiding this comment

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

First pass 😴

@@ -78,7 +78,7 @@ def test_cloc_exclude_view(self):
for module in self.installed_modules:
for cloc_entry in c.modules.get(module, {}):
message = "The view '%s' is counted in the maintenance lines. " % cloc_entry
message += "Please add a '__cloc_exclude__' entry in 'ir_model_data'."
message += "Please add a '__cloc_exclude__' entry in 'manifest'."
Copy link
Collaborator

Choose a reason for hiding this comment

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

useless diff (still wrong and easier for possible conflicts aftewards)

"Wrong %s '%r' in manifest, it should be %s" % (key, value, expected_value),
)
self.assertIsInstance(value, expected_type, f"Wrong type for '{key}' in {module}, expected {expected_type}")
if expected_value and key not in ['description']:
Copy link
Collaborator

Choose a reason for hiding this comment

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

expected_value of description falsy (empty string). Why do you need it?

Suggested change
if expected_value and key not in ['description']:
if expected_value:

)
self.assertIsInstance(value, expected_type, f"Wrong type for '{key}' in {module}, expected {expected_type}")
if expected_value and key not in ['description']:
self.assertEqual(value, expected_value, f"Incorrect value for '{key}' in {module}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.assertEqual(value, expected_value, f"Incorrect value for '{key}' in {module}")
self.assertEqual(value, expected_value, f"Incorrect value for '{key}' in manifest of {module}")

Comment on lines -48 to -51
def _test_manifest_keys(self, module, manifest_data):
super()._test_manifest_keys(module, manifest_data)
for key in MANDATORY_KEYS:
self.assertIn(key, manifest_data, "Missing key %s in manifest" % key)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test was checking that the key was defined + the super was checking that no other unknown key is defined

def _validate_manifest(self, module, manifest_data):
# Check manifest keys and values
for key, expected_value in MANDATORY_KEYS.items():
value = manifest_data.get(key)
Copy link
Collaborator

Choose a reason for hiding this comment

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

if not value, missing key

Comment on lines -194 to -195
# if s.count("x_studio"):
# _logger.warning("Please remove 'studio' from 'x_studio' in %s.", file_name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would be nice to restore, now that the fix of company_id is merged

Comment on lines +567 to +569
field_models = root.xpath(f"//field[@model='{model}']")
function_models = root.xpath(f"//function[@model='{model}']")
if records and not field_models and not function_models and noupdate:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
field_models = root.xpath(f"//field[@model='{model}']")
function_models = root.xpath(f"//function[@model='{model}']")
if records and not field_models and not function_models and noupdate:
if records and noupdate:

Comment on lines +575 to +579
for model in MODELS_NOT_TO_UPDATE:
records = root.xpath(f"//record[@model='{model}']")
field_models = root.xpath(f"//field[@model='{model}']")
function_models = root.xpath(f"//function[@model='{model}']")
if records and not field_models and not function_models and not noupdate:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we shouldn't remove the MODELS_NOT_TO_UPDATE list and assume everything else should be noupdate (no huge list to maintain).
We just need to find records that are not from a model in the other list and not noupdate.

Comment on lines +587 to +591
try:
root = etree.fromstring(xml_content.encode('utf-8'))
except etree.XMLSyntaxError as e:
_logger.error("XML syntax error in file %s: %s", filename, e)
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we're using etree in almost all methods, this could be done once in the main one.

_logger.warning(
"Model %s should not be updated, please add 'noupdate=\"1\"' in the header of %s.",
model,
filename,
)

def _check_useless_models(self, s, filename):
def _check_useless_models(self, xml_content, filename):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method can be removed in my opinion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants