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

feat(): add assert_match_with_ignore #161

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

Conversation

LuisFros
Copy link

@LuisFros LuisFros commented May 7, 2021

Description

This is a complementary method to allow asserting snapshots while ignoring certain fields.

Why?

  • The most classic use case for this method is autogenerated IDs that will change in between test batteries. (For example if databases are reused or random test order implies IDs are not unique)
  • Also when using random generated data without seeds

Solution

It covers recursive:

  • Lists
  • Dictionaries
  • Tuples

Additionally, it allows to exclude multiple values in the same "level".

 def assert_match_with_ignore(self, data, ignore_keys):
        """Extension of assert_match to ignore data.
        Args:
            data (dict,list,tuple): Data to be asserted
            ignored_keys (list): List of strings containing path to be ignored,
            special character "[.]" can be used to ignore multiple elements in list.
            (See Example 2)
        Returns:
            None: Asserts if the values are the same
        Raises:
            AssertionError: If the snapshot is different than the incoming data
        Examples:
            Test examples at: apps/tests/utils/test_asserts.py
            Example 1:
            >>> data={"dict1": {"dict2": {"dict3": {"id": "importantId", "other": "value"}}}}
            >>> ignore_keys=["dict1.dict2.dict3.id"]
            >>> assert_match_with_ignore(data,ignore_keys)
                # Will create the following snapshot
                snapshots['example_snapshot'] = {
                    'dict1': {
                        'dict2': {
                            'dict3': {
                                'id': None,
                                'other': 'value'
                            }
                        }
                    }
                }
            ---
            Example 2:
            >>> data=[
                    {
                    "name": "objectList",
                    "children": [
                        {"id": "random_string", "name": "child_1",},
                        {"id": "random_string2", "name": "child_2",},
                    ],
                    }
                ]
            >>> ignore_keys=["[0].children[.].id"]
            >>> assert_match_with_ignore(data,ignore_keys)
                # Will create the following snapshot
                snapshots['example2_snapshot'] = [
                    {
                    "name": "objectList",
                    "children": [
                        {"id": None, "name": "child_1",},
                        {"id": None, "name": "child_2",},
                    ],
                    }
                ]
        """

        self.assert_match(clear_ignore_keys(data, ignore_keys))

@LuisFros LuisFros changed the title feat(): add assert_with_ignore feat(): add assert_match_with_ignore May 7, 2021
@LuisFros LuisFros force-pushed the feat/assert_match_with_ignore branch 2 times, most recently from 363c491 to e9d6b51 Compare May 7, 2021 13:55
fix(): add compatibility with PY 3.5
@LuisFros LuisFros force-pushed the feat/assert_match_with_ignore branch from e9d6b51 to 2d36ace Compare May 7, 2021 13:57
@syrusakbary
Copy link
Owner

This looks great, however I think we may not need a new assert_match_with_ignore.

We can probably just add the clear_ignore_keys method so people can use assert_match normally.

@LuisFros
Copy link
Author

@syrusakbary thank you for your answer, sounds like a good proposal.
I am using pytest and one reason to add a new method was that it's more practical to use the fixture and then snapshot.assert_match_with_ignore instead of having to import a method from the library like from snapshot.utils import clear_ignore_keys every time we need to use it. What are your thoughts on this?

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.

2 participants