syrusakbary / snapshottest

Snapshot Testing utils for Python 📸
MIT License
525 stars 102 forks source link

feat(): add assert_match_with_ignore #161

Open LuisFros opened 3 years ago

LuisFros commented 3 years ago

Description

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

Solution

It covers recursive:

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))
syrusakbary commented 2 years ago

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 commented 2 years ago

@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?