dcramer/mock-django

Add QuerySet methods to actually do the filtering

bbirand opened this issue · 3 comments

I realize this will sound far-fetched, but is there a plan to actually implement the QuerySet methods, or maybe delegate them to Django somehow? I'm trying to test a function that returns a filtered set of objects. I modified it to take a QuerySet as a parameter, and I figured I should mock it.

I figured one way to do this is to pass a list of objects that behaves like an actual QuerySet, and then do some asserts on the return values. Of course, for this to work, the .filter() etc.. methods on the mock QuerySet should actually remove the items from the list.

Does this make sense? Would this even be useful, or am I missing a key testing strategy that would invalidate this approach?

Ideally we would spec it from Django so it doesnt require so much upkeep

In all honesty, I haven't used this library that much. It's pretty prototype-y so "what's best" is kind of still up in the air :)

+1 for spec'ing from Django, but I'm not sure we'd want to reimplement Django-like functionality.

@bbirand Ordinarily I'd suggest doing things like asserting that the Mocks were called with the right args instead of looking for the right results, but that actually doesn't work (see below). This may be a regression due to #20. I'll file an issue to get that fixed.

def test_can_treat_as_a_mock(self):
    """
    We should be able to treat the result of any manager calls as Mocks.
    """
    qs = QuerySetMock(None, 1, 2, 3)
    qs.filter(filter_arg='dummy').order_by('-dummy_ordering')

    # These currently fail
    qs.filter.assert_called_with(filter_arg='dummy')
    qs.filter().order_by.assert_called_with('-dummy_ordering')

You can use https://github.com/kadirpekel/liquer for filtering.