Ticket #12057: django1.1.1_cache_read_defaultcallable.patch

File django1.1.1_cache_read_defaultcallable.patch, 1.6 KB (added by Michael Thornhill, 15 years ago)

Test case for django 1.1.1 showing that callables for default are called on cache read

  • tests/regressiontests/cache/tests.py

     
    210210        }
    211211        self.cache.set("stuff", stuff)
    212212        self.assertEqual(self.cache.get("stuff"), stuff)
     213       
     214    def test_model_instance(self):
     215        from models import Poll, expensive_calculation
     216        expensive_calculation.num_runs = 0
     217        my_poll = Poll(question="Well?")
     218        pub_date = my_poll.pub_date
     219        self.cache.set('question', my_poll, 100)
     220        cached_poll = self.cache.get('question')
     221        self.assertEqual(cached_poll.pub_date, pub_date) 
     222        # We only want the default expensive calculation run once   
     223        self.assertEqual(expensive_calculation.num_runs, 1) 
    213224
    214225    def test_expiration(self):
    215226        # Cache values can be set to expire
  • tests/regressiontests/cache/models.py

     
     1from django.db import models
     2from datetime import datetime
     3import traceback
     4
     5def expensive_calculation():
     6    expensive_calculation.num_runs += 1
     7   
     8    if expensive_calculation.num_runs > 1:
     9        traceback.print_stack()
     10     
     11    return datetime.now()   
     12
     13class Poll(models.Model):
     14    question = models.CharField(max_length=200)
     15    pub_date = models.DateTimeField('date published', default=expensive_calculation)
Back to Top