building tests that deal with redis and cache_method decorator
So I'm trying to build some tests for a project using redis but I have two methods giving me trouble.
both methods use the @cache_method()
decorator and will spit out a failure report akin to AssertionError: [] != []
or AsserionError: [<ObjectName: instance_name>] != [<ObjectName: instance_name>]
. The tests are both something along t开发者_如何学Che line of:
self.assertEquals(self.ObjectName.Method(), ObjectName.objects.none())
or
self.assertEquals(self.ObjectName.Method(), ObjectName.objects.filter(...))
If I use something like self.assertEquals(type(), type())
the test passes so I'm at a complete loss.
my test class looks like this:
class SimpleTest(TestCase):
def setUp(self):
self.reset_pool()
self.cache = self.get_cache()
self.cache.clear()
... setup a bunch of self.Object instances for the actual tests ...
def reset_pool(self):
if hasattr(self, 'cache'):
self.cache._client.connection_pool.disconnect()
def get_cache(self, backend=None):
if VERSION[0] == 1 and VERSION[1] < 3:
cache = get_cache(backend or 'redis_cache.cache://127.0.0.1:6379?db=15')
elif VERSION[0] == 1 and VERSION[1] >= 3:
cache = get_cache(backend or 'default')
return cache
How can I get these tests to pass?
Does self.ObjectName.Method()
return a QuerySet? If not I'd suspect that is your problem. ObjectName.objects.filter(...)
will return a QuerySet. If your method returns anything else, then naturally the assert will fail.
More specifically ObjectName.objects.none()
will return an django.db.models.query.EmtpyQuerySet and filter()
will return either a django.db.models.query.EmptyQuerySet
or a django.db.models.query.QuerySet
depending on what, if anything, matches the filter.
So it seems somewhere you are trying to compare an ObjectName.method()
that is either:
a) not returning a QuerySet but something different
-or-
b) is returning QuerySet when an EmptyQuerySet is expected (such as when none() is called), or vice-versa.
Since we don't know what ObjectName.method()
is or what it returns, you'll need to look into that/those method(s) to see what it is returning and possibly why.
I hope that points you in the right direction.
精彩评论