Having done some preliminary coding on this, I get the feeling that
the positive assertion is quite useful, but assertContextNotContains
is superfluous. Theoretically, you could use it to test that
unexpected keys don't end up in the context dictionary through the use
of locals(), but you shouldn't be passing locals() to the template
context anyway. I don't see a proper real-world use case for it here.

The code is below. I'm unsure about the method signature and could use
some input on it. The current form allows for checking a single
variable name through 'varname', one or more variable names through
'variables', and one or more variable names and values through
'dictionary'.

I've tested this code for a while now, and IMHO it feels pretty
comfortable and flexible to use as-is, allowing for mixing many
different types of template context checks without too much
complexity. Your mileage or tastes may vary, of course.

Note: for this to function, the "Ready for checkin" patch in #13092
must be applied first to allow "varname in response.context" for
ContextList objects.

If the proposal is shot down, I won't go any further. If the general
idea is approved, I'll post a proper diff into a new ticket, with any
modifications suggested here.

- JK Laiho

- - - - -


def assertContextContains(self, response, varname=None, variables=[],
                          dictionary={}, msg_prefix=''):
    """
    Asserts that response.context contains the variable name passed in
as
    ``varname``, and/or each variable name specified in ``variables``,
    and/or each variable name and their associated values specified in
    ``dictionary``.
    """
    if msg_prefix:
        msg_prefix += ": "

    if varname is not None:
        self.failUnless(varname in response.context,
            msg_prefix + "The variable '%s' was not found in "
            "response.context" % varname)

    for key in variables:
        self.failUnless(key in response.context,
            msg_prefix + "The variable '%s' was not found in "
            "response.context" % key)

    for key, value in dictionary.items():
        self.failUnless(key in response.context,
            msg_prefix + "The variable '%s' was not found in "
            "response.context" % key)
        # String cast for e.g. a Form() in a view's template context
to
        # equal a Form() instantiated in a test method
        self.failUnless(smart_str(response.context[key],
response._charset) == smart_str(value, response._charset),
            msg_prefix + "The value of the context variable '%s' was "
                "incorrect." % (key))

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to