Djangoids:

This is from Cuker's fork of django-test-extensions:

    def assert_model_changes(self, mod, item, frum, too, lamb):
        source = open(lamb.func_code.co_filename, 'r').readlines()
[lamb.func_code.co_firstlineno - 1]
        source = source.replace('lambda:', '').strip()
        model  = str(mod.__class__).replace("'>", '').split('.')[-1]

        should = '%s.%s should equal `%s` before your activation line,
`%s`' % \
                  (model, item, frum, source)

        self.assertEqual(frum, mod.__dict__[item], should)
        lamb()
        mod = mod.__class__.objects.get(pk=mod.pk)

        should = '%s.%s should equal `%s` after your activation line, `
%s`' % \
                  (model, item, too, source)

        self.assertEqual(too, mod.__dict__[item], should)
        return mod

You call it like this:

        self.assert_model_changes( blog, 'post_count', 41, 42, lambda:
            blog.write_one_post('yack yack yack') )

The assertion covers the common situation where we assert a member
value before and after a method call, to check that it changed (or
that it did _not_ change!).

If the assertion fails, it prints out a complete diagnostic, including
the money line - "blog.write_one_post('yack yack yack')", the type of
the blog model ("Blog"), the attribute checked ("post_count"), the
expected value, AND whether the assertion failed before or after the
money line.

(The "money line" is jargon for the "Activate" line in the Assemble
Activate Assert pattern. It's the production-code target of the test
case.)

One little question - how can we cleanly upgrade this to eval() a
string instead of serve an attribute?

        self.assert_model_changes( blog, 'post.count()', 41, 42,
lambda:
            blog.write_one_post('yack yack yack') )

The code shows how to .reload() in Django, because (unless I'm wrong),
Django models don't have a method to reload themselves from their data
stores.

Another little question: If anyone can suggest a code cleanup, I'm
there!

--
  Phlip

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

Reply via email to