test/test_settings.rb in settings-goo-0.2.0 vs test/test_settings.rb in settings-goo-0.3.0

- old
+ new

@@ -6,10 +6,18 @@ @key_name = 'some_key' Rails.cache.clear end ############################## + # Validations # + ############################## + test 'should require key' do + setting = Settings.create(:key => nil) + assert_not_nil setting.errors.on(:key) + end + + ############################## # Proper Serialization # ############################## test 'should serialize string correctly' do val = 'some string' setting = Settings.create(:key => @key_name, :value => val) @@ -62,10 +70,17 @@ #key given as symbol setting = Settings[@key_name.to_sym] assert_equal setting, val end + test 'should retrieve key immediatley after setting (when created with symbol)' do + key = :with_key + val = 'some val' + Settings[key] = val + assert_equal val, Settings[key] + end + test 'should set key' do val = true Settings[@key_name] = val setting = Settings.find_by_key(@key_name) assert_equal setting.value, val @@ -82,11 +97,17 @@ end test 'should return nil when trying to remove non-existent key' do assert_nil Settings.remove :non_existent_key end - + + test 'should not create setting with nil key' do + assert_no_difference('Settings.count') do + Settings[nil] = 'something' + end + end + ############################## # Duplicate Handling # ############################## test 'should not allow creation of duplicate keys' do val = true @@ -144,7 +165,53 @@ Settings[@key_name] = 'some val' #Now fetch it so it will be cached Settings[@key_name] Settings.remove @key_name assert_nil Rails.cache.fetch(cache_key) + end + + test 'should create cache for Settings.keys' do + cache_key = "settings/settings.keys" + #Create a key + Settings[@key_name] = 'some val' + #Call to set cache + Settings.keys + assert_not_nil Rails.cache.fetch(cache_key) + end + + test 'should remove cache for Settings.keys whenever a new key is added' do + cache_key = "settings/settings.keys" + #Create a key + Settings[@key_name] = 'some val' + #Call to set cache + Settings.keys + assert_not_nil Rails.cache.fetch(cache_key) + #Add a new key + Settings[:another_key] = 'some other val' + assert_nil Rails.cache.fetch(cache_key) + end + + test 'should remove cache for Settings.keys whenever a key is updated' do + cache_key = "settings/settings.keys" + #Create a key + Settings[@key_name] = 'some val' + #Call to set cache + Settings.keys + assert_not_nil Rails.cache.fetch(cache_key) + #Update a key + setting = Settings.find_by_key(@key_name.to_s) + setting.update_attribute(:key, 'another_key') + assert_nil Rails.cache.fetch(cache_key) + end + + test 'should remove cache for Settings.keys whenever a key is removed' do + cache_key = "settings/settings.keys" + #Create a key + Settings[@key_name] = 'some val' + #Call to set cache + Settings.keys + assert_not_nil Rails.cache.fetch(cache_key) + #Update a key + Settings.remove(@key_name) + assert_nil Rails.cache.fetch(cache_key) end end