def test_sync_anytrait_items_not_event(self): """ Test sychronizing trait with name *_items which is a normal trait rather than an event trait for listening to list/dict/set mutation. """ class MyPreferencesHelper(PreferencesHelper): preferences_path = Str('my_section') names_items = Str() helper = MyPreferencesHelper(preferences=self.preferences) helper.names_items = "Hello" self.preferences.save(self.tmpfile) new_preferences = Preferences() new_preferences.load(self.tmpfile) self.assertEqual( sorted(new_preferences.keys("my_section")), ["names_items"] ) self.assertEqual( new_preferences.get("my_section.names_items"), str(helper.names_items), )
def test_preferences_page_apply_skip_items_traits(self): """ Test _items traits from List mutation are skipped. """ # Regression test for enthought/apptools#129 class MyPreferencesPage(PreferencesPage): preferences_path = "my_ref.pref" names = List(Str()) preferences = Preferences() pref_page = MyPreferencesPage( preferences=preferences, names=["1"], ) pref_page.names.append("2") pref_page.apply() self.assertEqual(preferences.get("my_ref.pref.names"), str(["1", "2"])) self.assertEqual(preferences.keys("my_ref.pref"), ["names"])
def test_preferences_page_apply(self): """ Test applying the preferences """ # this sets up imitate Mayavi usage. class MyPreferencesPage(PreferencesPage): # the following set default values for class traits category = "Application" help_id = "" name = "Note" preferences_path = "my_ref.pref" # custom preferences backend = Enum("auto", "simple", "test") traits_view = View(Group(Item("backend"))) preferences = Preferences() pref_page = MyPreferencesPage( preferences=preferences, category="Another Application", help_id="this_wont_be_saved", name="Different Note", # custom preferences backend="simple", ) pref_page.apply() self.assertEqual(preferences.get("my_ref.pref.backend"), "simple") self.assertEqual(preferences.keys("my_ref.pref"), ["backend"]) # this is not saved by virtue of it being static and never assigned to self.assertIsNone(preferences.get("my_ref.pref.traits_view")) # These are skipped because this trait is defined on the # PreferencesPage. self.assertIsNone(preferences.get("my_ref.pref.help_id")) self.assertIsNone(preferences.get("my_ref.pref.category")) self.assertIsNone(preferences.get("my_ref.pref.name"))
def test_sync_anytrait_items_overload(self): """ Test sychronizing trait with name *_items not to be mistaken as the event trait for mutating list/dict/set """ class MyPreferencesPage(PreferencesPage): preferences_path = Str('my_section') names_items = Str() preferences = Preferences() pref_page = MyPreferencesPage(preferences=preferences) pref_page.names_items = "Hello" pref_page.apply() self.assertEqual(sorted(preferences.keys("my_section")), ["names_items"]) self.assertEqual( preferences.get("my_section.names_items"), "Hello", )
def test_mutate_list_of_values(self): """ Mutated list should be saved and _items events not to be saved in the preferences. """ # Regression test for enthought/apptools#129 class MyPreferencesHelper(PreferencesHelper): preferences_path = Str('my_section') list_of_str = List(Str) helper = MyPreferencesHelper(list_of_str=["1"]) # Now modify the list to fire _items event helper.list_of_str.append("2") self.preferences.save(self.tmpfile) new_preferences = Preferences() new_preferences.load(self.tmpfile) self.assertEqual( new_preferences.get("my_section.list_of_str"), str(["1", "2"]) ) self.assertEqual(new_preferences.keys("my_section"), ["list_of_str"])