def test_insert(self): self.assertEqual("", ExampleConfig.current().string_field) request = self.factory.post('/config/ExampleConfig', {"string_field": "string_value"}) request.user = self.user __ = self.current_view(request) self.assertEqual("string_value", ExampleConfig.current().string_field) self.assertEqual(self.user, ExampleConfig.current().changed_by)
def test_no_config_empty_cache(self): # First time reads from the database with self.assertNumQueries(1): current = ExampleConfig.current() self.assertEqual(current.int_field, 10) self.assertEqual(current.string_field, '') # Follow-on request reads from cache with no database request. with self.assertNumQueries(0): cached_current = ExampleConfig.current() self.assertEqual(cached_current.int_field, 10) self.assertEqual(cached_current.string_field, '')
def test_cache_set(self): # Nothing is cached, so we take a database hit for this. with self.assertNumQueries(1): default = ExampleConfig.current() self.assertEqual(default.string_field, '') first = ExampleConfig(changed_by=self.user) first.string_field = 'first' first.save() # The save() call above should have invalidated the cache, so we expect # to see a query to get the current config value. with self.assertNumQueries(1): current = ExampleConfig.current() self.assertEqual(current.string_field, 'first')
def test_config_ordering(self): with freeze_time('2012-01-01'): first = ExampleConfig(changed_by=self.user) first.string_field = 'first' first.save() second = ExampleConfig(changed_by=self.user) second.string_field = 'second' second.save() self.assertEqual(ExampleConfig.current().string_field, 'second')
def test_multiple_inserts(self): for i in six.moves.range(3): # pylint: disable=no-member self.assertEqual(i, ExampleConfig.objects.all().count()) request = self.factory.post('/config/ExampleConfig', {"string_field": str(i)}) request.user = self.user response = self.current_view(request) self.assertEqual(201, response.status_code) self.assertEqual(i + 1, ExampleConfig.objects.all().count()) self.assertEqual(str(i), ExampleConfig.current().string_field)
def test_cache_set(self): with self.assertNumQueries(1): default = ExampleConfig.current('left', 'right', self.user) self.assertEqual(default.string_field, '') first = ExampleKeyedConfig( changed_by=self.user, left='left', right='right', user=self.user, string_field='first', ) first.save() # Cache is invalidated by the save above, so another query is made. with self.assertNumQueries(1): ExampleKeyedConfig.current('left', 'right', self.user)