|
22 | 22 | from collections import MutableMapping
|
23 | 23 | import datetime
|
24 | 24 | import json
|
25 |
| -import pickle |
26 | 25 | import re
|
27 | 26 |
|
28 | 27 |
|
@@ -618,84 +617,24 @@ class CacheRenderTest(TestCase):
|
618 | 617 |
|
619 | 618 | urls = 'tests.test_renderers'
|
620 | 619 |
|
621 |
| - cache_key = 'just_a_cache_key' |
622 |
| - |
623 |
| - @classmethod |
624 |
| - def _get_pickling_errors(cls, obj, seen=None): |
625 |
| - """ Return any errors that would be raised if `obj' is pickled |
626 |
| - Courtesy of koffie @ http://stackoverflow.com/a/7218986/109897 |
627 |
| - """ |
628 |
| - if seen is None: |
629 |
| - seen = [] |
630 |
| - try: |
631 |
| - state = obj.__getstate__() |
632 |
| - except AttributeError: |
633 |
| - return |
634 |
| - if state is None: |
635 |
| - return |
636 |
| - if isinstance(state, tuple): |
637 |
| - if not isinstance(state[0], dict): |
638 |
| - state = state[1] |
639 |
| - else: |
640 |
| - state = state[0].update(state[1]) |
641 |
| - result = {} |
642 |
| - for i in state: |
643 |
| - try: |
644 |
| - pickle.dumps(state[i], protocol=2) |
645 |
| - except pickle.PicklingError: |
646 |
| - if not state[i] in seen: |
647 |
| - seen.append(state[i]) |
648 |
| - result[i] = cls._get_pickling_errors(state[i], seen) |
649 |
| - return result |
650 |
| - |
651 |
| - def http_resp(self, http_method, url): |
652 |
| - """ |
653 |
| - Simple wrapper for Client http requests |
654 |
| - Removes the `client' and `request' attributes from as they are |
655 |
| - added by django.test.client.Client and not part of caching |
656 |
| - responses outside of tests. |
657 |
| - """ |
658 |
| - method = getattr(self.client, http_method) |
659 |
| - resp = method(url) |
660 |
| - resp._closable_objects = [] |
661 |
| - del resp.client, resp.request |
662 |
| - try: |
663 |
| - del resp.wsgi_request |
664 |
| - except AttributeError: |
665 |
| - pass |
666 |
| - return resp |
667 |
| - |
668 |
| - def test_obj_pickling(self): |
669 |
| - """ |
670 |
| - Test that responses are properly pickled |
671 |
| - """ |
672 |
| - resp = self.http_resp('get', '/cache') |
673 |
| - |
674 |
| - # Make sure that no pickling errors occurred |
675 |
| - self.assertEqual(self._get_pickling_errors(resp), {}) |
676 |
| - |
677 |
| - # Unfortunately LocMem backend doesn't raise PickleErrors but returns |
678 |
| - # None instead. |
679 |
| - cache.set(self.cache_key, resp) |
680 |
| - self.assertTrue(cache.get(self.cache_key) is not None) |
681 |
| - |
682 | 620 | def test_head_caching(self):
|
683 | 621 | """
|
684 | 622 | Test caching of HEAD requests
|
685 | 623 | """
|
686 |
| - resp = self.http_resp('head', '/cache') |
687 |
| - cache.set(self.cache_key, resp) |
688 |
| - |
689 |
| - cached_resp = cache.get(self.cache_key) |
690 |
| - self.assertIsInstance(cached_resp, Response) |
| 624 | + response = self.client.head('/cache') |
| 625 | + cache.set('key', response) |
| 626 | + cached_response = cache.get('key') |
| 627 | + assert isinstance(cached_response, Response) |
| 628 | + assert cached_response.content == response.content |
| 629 | + assert cached_response.status_code == response.status_code |
691 | 630 |
|
692 | 631 | def test_get_caching(self):
|
693 | 632 | """
|
694 | 633 | Test caching of GET requests
|
695 | 634 | """
|
696 |
| - resp = self.http_resp('get', '/cache') |
697 |
| - cache.set(self.cache_key, resp) |
698 |
| - |
699 |
| - cached_resp = cache.get(self.cache_key) |
700 |
| - self.assertIsInstance(cached_resp, Response) |
701 |
| - self.assertEqual(cached_resp.content, resp.content) |
| 635 | + response = self.client.get('/cache') |
| 636 | + cache.set('key', response) |
| 637 | + cached_response = cache.get('key') |
| 638 | + assert isinstance(cached_response, Response) |
| 639 | + assert cached_response.content == response.content |
| 640 | + assert cached_response.status_code == response.status_code |
0 commit comments