Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Dictionaries Should Be Equal should support ignoring keys #2717

@bbpatel2001

Description

@bbpatel2001

Problem:
Current implementation of "Dictionaries Should Be Equal" returns true if two dictionaries are identical. Sometimes we need to compare two dictionaries by ignoring some keys. i.e. If we have large dictionaries and one dictionary does not have some keys at nth level but still keyword should return True for all other keys if matches . Take example of following two dictionaries. expected_dict is my expected dictionary and Actual dictionary is created from UI data. Actual dictionary does not contain "Forged Source" key. If key is 1st or 2nd level we can pop key and compare it but if we want to ignore any key which is at the nth level it is very difficult.

expected_dict = {
  "alert_main_list": {
  "test":"test1",
  "IP": {
      "1.1.1.16": {
        "#_of_CLUSTERS": "1",
        "#_of_FINGERPRINTS": "1",
        "IP_ADDRESS": "1.1.1.1",
        "HIGHEST_CONFIDENCE": "49",
        "EVENT_COUNT": "1",
        "abcd" : ['I','Love','Robot', 'Framework'],
        "numbers" : [ 1, 2, 3, 4, 5,"aa"]
      }
    },
  },
  "User": {
      "[email protected]": {
        "ALERT_TRIGGERS": {
          "Suspicious Behavior": {
            "Username is an email address": "1"
          },
          "Suspicious Source": {
            "Forum Spam Blacklist": "1"
          },
          "Forged Source": {
            "Web Browser : Watson Lookup": "1"
          }
        },        
      }
    },
}
Actual_dict = {
  "alert_main_list": {
  "test":"test1",
  "IP": {
      "1.1.1.16": {
        "#_of_CLUSTERS": "1",
        "#_of_FINGERPRINTS": "1",
        "IP_ADDRESS": "1.1.1.1",
        "HIGHEST_CONFIDENCE": "49",
        "EVENT_COUNT": "1",
        "abcd" : ['I','Love','Robot', 'Framework'],
        "numbers" : [ 1, 2, 3, 4, 5,"aa"]
      }
    },
  },
  "User": {
      "[email protected]": {
        "ALERT_TRIGGERS": {
          "Suspicious Behavior": {
            "Username is an email address": "1"
          },
          "Suspicious Source": {
            "Forum Spam Blacklist": "1"
          },
        },        
      }
    },
}

I am proposing
new keyword "Dictionaries Should Ignore And Equal"
that will take three parameters: (I am OK to change name of keyword if accepted this proposal. )

1st parameter : Actual dictionary [Type: Dict]
2nd parameter : Expected dictionary [Type: Dict]
3rd Parameter : Ignore list [Type: List] : List of keys to be ignored

This keyword will not match key which is listed in third parameter
Dictionaries Should Ignore And Equal | d1 | d2 | ['Forged Source'] This will return True

I will add few more example if required.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions