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_equality_custom_fields_to_ignore(self):
        config = ExampleConfig(changed_by=self.user, string_field='first')
        config.save()

        # id, change_date, and changed_by will all be different for a newly created entry
        self.assertTrue(
            ExampleConfig.equal_to_current({"string_field": "first"}))
        self.assertFalse(
            ExampleConfig.equal_to_current({"string_field": "first"},
                                           fields_to_ignore=("change_date",
                                                             "changed_by")))
        self.assertFalse(
            ExampleConfig.equal_to_current({"string_field": "first"},
                                           fields_to_ignore=("id",
                                                             "changed_by")))
        self.assertFalse(
            ExampleConfig.equal_to_current({"string_field": "first"},
                                           fields_to_ignore=("change_date",
                                                             "id")))

        # Test the ability to ignore a different field ("int_field").
        self.assertFalse(
            ExampleConfig.equal_to_current({
                "string_field": "first",
                "int_field": 20
            }))
        self.assertTrue(
            ExampleConfig.equal_to_current(
                {
                    "string_field": "first",
                    "int_field": 20
                },
                fields_to_ignore=("id", "change_date", "changed_by",
                                  "int_field")))
    def test_equality(self):
        config = ExampleConfig(changed_by=self.user, string_field='first')
        config.save()

        self.assertTrue(
            ExampleConfig.equal_to_current({"string_field": "first"}))
        self.assertTrue(
            ExampleConfig.equal_to_current({
                "string_field": "first",
                "enabled": False
            }))
        self.assertTrue(
            ExampleConfig.equal_to_current({
                "string_field": "first",
                "int_field": 10
            }))

        self.assertFalse(
            ExampleConfig.equal_to_current({
                "string_field": "first",
                "enabled": True
            }))
        self.assertFalse(
            ExampleConfig.equal_to_current({
                "string_field": "first",
                "int_field": 20
            }))
        self.assertFalse(
            ExampleConfig.equal_to_current({"string_field": "second"}))

        self.assertFalse(ExampleConfig.equal_to_current({}))
    def test_always_insert(self):
        config = ExampleConfig(changed_by=self.user, string_field='first')
        config.save()
        config.string_field = 'second'
        config.save()

        self.assertEqual(2, ExampleConfig.objects.all().count())
    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_get_current(self):
        request = self.factory.get('/config/ExampleConfig')
        request.user = self.user
        response = self.current_view(request)
        self.assertEqual('', response.data['string_field'])
        self.assertEqual(10, response.data['int_field'])
        self.assertEqual(None, response.data['changed_by'])
        self.assertEqual(False, response.data['enabled'])
        self.assertEqual(None, response.data['change_date'])

        ExampleConfig(string_field='string_value', int_field=20).save()

        response = self.current_view(request)
        self.assertEqual('string_value', response.data['string_field'])
        self.assertEqual(20, response.data['int_field'])