Ticket #12057: django1.0_cache_read_defaultcallable.patch

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

Test case for django 1.0.x showing that callables for default are NOT called on cache read

  • tests/regressiontests/cache/tests.py

     
    180180        }
    181181        self.cache.set("stuff", stuff)
    182182        self.assertEqual(self.cache.get("stuff"), stuff)
     183       
     184    def test_model_instance(self):
     185        from models import Poll, expensive_calculation
     186        expensive_calculation.num_runs = 0
     187        my_poll = Poll(question="Well?")
     188        pub_date = my_poll.pub_date
     189        self.cache.set('question', my_poll, 100)
     190        cached_poll = self.cache.get('question')
     191        self.assertEqual(cached_poll.pub_date, pub_date) 
     192        # We only want the default expensive calculation run once   
     193        self.assertEqual(expensive_calculation.num_runs, 1)
    183194
    184195    def test_expiration(self):
    185196        # Cache values can be set to expire
  • tests/regressiontests/cache/models.py

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