Opened 14 years ago

Closed 12 years ago

#15753 closed Bug (fixed)

test_simple_sitemap fails after executing test_requestsite_sitemap when Sites app and FetchFromCacheMiddleware are installed.

Reported by: lucho Owned by: nobody
Component: contrib.sitemaps Version: dev
Severity: Normal Keywords: test
Cc: krzysiumed@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

test_simple_sitemap fails after executing test_requestsite_sitemap when Sites app and FetchFromCacheMiddleware are installed.

The error:

======================================================================
 FAIL: test_simple_sitemap (django.contrib.sitemaps.tests.basic.SitemapTests)
 A simple sitemap can be rendered
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/usr/local/lib/python2.6/dist-packages/Django-1.3-py2.6.egg/django/contrib/sitemaps/tests/basic.py", line 70, in test_simple_sitemap
     """ % (self.base_url, date.today().strftime('%Y-%m-%d')))
 AssertionError: '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n<url><loc>http://testserver/location/</loc><lastmod>2011-04-04</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>\n</urlset>\n' != '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n<url><loc>http://example.com/location/</loc><lastmod>2011-04-04</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>\n</urlset>\n'
 

The way to reproduce it:

  1. Use Sites app in your project
  2. Use FetchFromCacheMiddleware middleware in your project
  3. Run the sitemaps.SitemapTests suite

The code that triggers the problem is this http://code.djangoproject.com/browser/django/trunk/django/contrib/sitemaps/tests/basic.py#L144

# Which does that - setting the Site app as not installed and requesting the url
Site._meta.installed = False
response = self.client.get('/simple/sitemap.xml')

If I execute the test case separate (./manage.py test sitemaps.SitemapTests.test_simple_sitemap) no error is observed, but executing the whole test suite leads to error in test_simple_sitemap which is executed right after the test_requestsite_sitemap.

If I comment out the test_requestsite_sitemap, no problem is observed.
If I don't use (comment out) FetchFromCacheMiddleware, no problem is observed.

This makes me think that it is some problem related to caching.

Attachments (1)

fix_15753.diff (801 bytes ) - added by lucho 14 years ago.
Clears the cache in setUp

Download all attachments as: .zip

Change History (10)

comment:1 by lucho, 14 years ago

Sites = django.contrib.sites

by lucho, 14 years ago

Attachment: fix_15753.diff added

Clears the cache in setUp

comment:2 by lucho, 14 years ago

I added a patch that worked for me fix_15753.diff.

It simply clears the cache in setUp.

comment:3 by Jacob, 14 years ago

Easy pickings: unset
Has patch: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

Looks like the fix works, but I need to know *why* it works before I can feel comfortable closing this one out.

comment:4 by Christopher Medrela, 13 years ago

UI/UX: unset

I cannot reproduce this bug. The problem doesn't appear with the latest version. I checked revision 15906 tagged as 1.3 version, but the problem didn't appear too. I suggest to close this ticket.

comment:5 by Christopher Medrela, 13 years ago

Cc: krzysiumed@… added

comment:6 by Claude Paroz, 13 years ago

It is still reproducible. You must ensure to have all the necessary cache machinery enabled in your project. https://docs.djangoproject.com/en/1.3/topics/cache/

comment:7 by Claude Paroz, 13 years ago

Keywords: test added
Version: 1.3SVN

The problem is that in those tests, there are several requests for the same URL ('/simple/sitemap.xml', '/simple/sitemap-simple.xml'), so that's totally previsible that when caching is enabled, some already rendered contents are reused. The patch from lucho is one way to solve this. Another one would be to empty the cache just for the failing tests. A third approach would be to isolate the tests from unwanted middlewares (override_settings(MIDDLEWARE_CLASSES = ...).

comment:8 by Christopher Medrela, 13 years ago

Patch needs improvement: unset

The response is cached indeed. I think cleaning cache in setUp is the easiest solution.

How cache is used during running tests? During executing django.contrib.sitemaps.tests.http.HTTPSitemapTest.test_requestsite_sitemap the LocMemCache instance is used:

  • its method get is called with argument key='views.decorators.cache.cache_header..8e8e090224d76db794280be1b767ecc0.en-us'; it returns None;
  • next its method set is called with arguments key='views.decorators.cache.cache_header..8e8e090224d76db794280be1b767ecc0.en-us' and value=[];
  • next its method set is called one more time with arguments key='views.decorators.cache.cache_page..GET.8e8e090224d76db794280be1b767ecc0.d41d8cd98f00b204e9800998ecf8427e.en-us' and value=<django.template.response.TemplateResponse object at 0x2894a10>`; after rendering the TemplateResponse instance its content is
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url><loc>http://testserver/location/</loc><lastmod>2012-02-05</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
    </urlset>
    

Then the django.contrib.sitemaps.tests.http.HTTPSitemapTest.test_simple_sitemap is run:

  • method get is called with argument key='views.decorators.cache.cache_header..8e8e090224d76db794280be1b767ecc0.en-us'; it returns [];
  • method get is called one more time with argument key='views.decorators.cache.cache_page..GET.8e8e090224d76db794280be1b767ecc0.d41d8cd98f00b204e9800998ecf8427e.en-us'; it returns <django.template.response.TemplateResponse object at 0x28d9a10> - the cached TemplateResponse instance.

comment:9 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In 2ee6a46696d4936fbfcb7adc74eb33e50cb9aefe:

Fixed #15753 -- Cleared cache between sitemaps tests

When caching was activated, test_simple_sitemap would fail
because the test result was fetched from cache.
Thanks lucho for the initial patch and krzysiumed@… for
the review.

Note: See TracTickets for help on using tickets.
Back to Top