MatchesStructure doesn't work with dicts

Bug #972135 reported by Julian Edwards
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
testtools
Triaged
Wishlist
Unassigned

Bug Description

MatchesStructure is one of my favourite matchers! However, it doesn't work if the matchee is a dict, like this:

        params = {"this": 1, "that": 2, "tother":3}
        user.this = 1
        user.that = 2
        user.tother = 3
        self.assertThat(
            params,
            MatchesStructure.fromExample(
                user, "this", "that", "tother"))

If it detected that "params" was a dict and DTRT, that would be awesome.

Tags: matchers
Revision history for this message
Jonathan Lange (jml) wrote :

Good thought!

Changed in testtools:
status: New → Triaged
importance: Undecided → Wishlist
tags: added: matchers
Revision history for this message
Jonathan Lange (jml) wrote :

Workarounds:

1. Helper function for dict -> object

class _Object(object):
    def __init__(self, data):
        for name, value in data:
            setattr(self, name, value)

def dict_to_object(data):
    return _Object(data)

self.assertThat(
    dict_to_object(params),
    MatchesStructure.fromExample(user, 'this', 'that', 'tother'))

2. Helper function for object -> dict

def getattrs(obj, *keys):
    d = {}
    for key in keys:
        d[key] = getattr(obj, key)
    return d

self.assertThat(params, Equals(dict_to_object(user, 'this', 'that', 'tother')))

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.