def test_can_change_site_preference_value(self):
        site_pref1 = site_preferences_registry.get("TestSitePref1", "test")
        site_pref1.value = "new value"

        self.assertEqual(site_preferences_registry.get("TestSitePref1", "test").value, "new value")

        user_pref1 = user_preferences_registry.get("TestUserPref1", "test")
        user_pref1.value = "new value"

        self.assertEqual(user_preferences_registry.get("TestUserPref1", "test").value, "new value")
    def test_site_preference_stay_unique_in_db(self):
        site_pref1 = site_preferences_registry.get("TestSitePref1", "test")
        site_pref1.to_model(site=self.test_site, value="new value")

        duplicate = SitePreferenceModel(section="test", name="TestSitePref1", site=self.test_site)

        with self.assertRaises(IntegrityError):
            duplicate.save()
    def test_site_preference_is_saved_to_database(self):
        site_pref1 = site_preferences_registry.get("TestSitePref1", "test")
        site_pref1.to_model(site=self.test_site, value="new site value")

        test_site_pref1 = SitePreferenceModel.objects.get(section="test", name="TestSitePref1", site=self.test_site)
        self.assertEqual(site_pref1, test_site_pref1.preference)
        self.assertEqual(test_site_pref1.section, "test")
        self.assertEqual(test_site_pref1.name, "TestSitePref1")
        self.assertEqual(test_site_pref1.value, "new site value")
    def test_preference_value_set_to_default(self):
        pref = user_preferences_registry.get("TestUserPref1", "test")
        pref.to_model(user=self.test_user)

        instance = UserPreferenceModel.objects.get(section="test", name="TestUserPref1", user=self.test_user)
        self.assertEqual(pref.default, instance.value)

        pref = site_preferences_registry.get("TestSitePref1", "test")
        pref.to_model(site=self.test_site)

        instance = SitePreferenceModel.objects.get(section="test", name="TestSitePref1", site=self.test_site)
        self.assertEqual(pref.default, instance.value)
    def test_can_get_preference_value_by_key(self):
        site_pref1 = site_preferences_registry.get("TestSitePref1", "test")
        self.assertEqual(site_pref1.default, TestSitePref1.default)

        user_pref1 = user_preferences_registry.get("TestUserPref1", "test")
        self.assertEqual(user_pref1.default, TestUserPref1.default)