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_user_preference_stay_unique_in_db(self):
        user_pref1 = user_preferences_registry.get("TestUserPref1", "test")
        user_pref1.to_model(user=self.test_user, value="new value")

        duplicate = UserPreferenceModel(section="test", name="TestUserPref1", user=self.test_user)

        with self.assertRaises(IntegrityError):
            duplicate.save()
    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_user_preference_is_saved_to_database(self):
        with self.settings(CREATE_DEFAULT_PREFERENCES_FOR_NEW_USERS=False):
            user = User(username="******")
            user.save()

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

        test_user_pref1 = UserPreferenceModel.objects.get(section="test", name="TestUserPref1", user=user)
        self.assertEqual(user_pref1, test_user_pref1.preference)
        self.assertEqual(test_user_pref1.section, "test")
        self.assertEqual(test_user_pref1.name, "TestUserPref1")
        self.assertEqual(test_user_pref1.value, "new user 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)