/vendor/plugins/i18n_backend_database/spec/database_spec.rb

https://github.com/smtrox214/echo · Ruby · 199 lines · 138 code · 25 blank · 36 comment · 18 complexity · 27e416868215f34559024b57c4e1eca7 MD5 · raw file

  1. require File.dirname(__FILE__) + '/spec_helper'
  2. describe I18n::Backend::Database do
  3. describe "an instance" do
  4. before {
  5. I18n.locale = "es"
  6. @locales = [:en, :es, :it]
  7. @locale = mock_model(Locale, { :code => "es" })
  8. Locale.stub!(:available_locales).and_return(@locales)
  9. Locale.stub!(:find_by_code).and_return(@locale)
  10. @database = I18n::Backend::Database.new
  11. }
  12. it "should use the current Rails cache store if none are provided" do
  13. @database.cache_store.should == Rails.cache
  14. end
  15. it "should use a custom cache store if provided" do
  16. @database = I18n::Backend::Database.new({:cache_store => :mem_cache_store})
  17. @database.cache_store.class.should == ActiveSupport::Cache::MemCacheStore
  18. end
  19. it "should have default localize text tag if none provided" do
  20. @database.localize_text_tag.should == '^^'
  21. end
  22. it "should use custom localize text tag if provided" do
  23. @database = I18n::Backend::Database.new({:localize_text_tag => '##'})
  24. @database.localize_text_tag.should == '##'
  25. end
  26. it "should delegate the call to available_locales to the Locale class" do
  27. Locale.should_receive(:available_locales)
  28. @database.available_locales
  29. end
  30. it "should return all the available locales on a call to available_locales" do
  31. @database.available_locales.should == @locales
  32. end
  33. it "should return a cache key of locale:key on call to build_cache_key" do
  34. hash_key = Translation.hk("hola me amigo!")
  35. Translation.ck(@locale, "hola me amigo!", 1).should == "es:#{hash_key}"
  36. end
  37. it "should generate a Base64 encoded (minus newline), MD5 encrypted hash, based on the key" do
  38. encrypted_key = Digest::MD5.hexdigest("aoeuaoeu")
  39. completed_key = Translation.hk("aoeuaoeu")
  40. encrypted_key.should == Base64.decode64(completed_key).gsub(/\n/, '')
  41. end
  42. it "should have a nil locale cache by default" do
  43. @database.locale.should == nil
  44. end
  45. it "should be able to set the locale cache by passing a locale code into locale=" do
  46. @database.locale = "es"
  47. @database.locale.should == @locale
  48. end
  49. end
  50. # describe "omg an aminal" do
  51. #
  52. # before(:each) do
  53. # Locale.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
  54. # Locale.create!(:code => "en")
  55. # end
  56. #
  57. # it "should contain one 'blank' key in the database" do
  58. # Locale.validates_presence_of :code
  59. # l = Locale.new
  60. # l.valid?
  61. # Translation.find_by_value("can't be blank").should_not be_nil
  62. # end
  63. #
  64. # it "should contain one 'blank' key and one custom 'blank' key in the database" do
  65. # Locale.validates_presence_of :code, :message => "ain't blank sucka"
  66. # l = Locale.new
  67. # l.valid?
  68. # Translation.find_by_value("ain't blank sucka").should_not be_nil
  69. # Translation.find_by_value("can't be blank").should be_nil
  70. # end
  71. #
  72. # it "should use the blank code if a custom code is present, but not enabled" do
  73. # Locale.validates_presence_of :code, :message => "ain't blank sucka"
  74. #
  75. # l = Locale.new
  76. # l.valid?
  77. # l.errors_on(:code).should include("ain't blank sucka")
  78. #
  79. # Locale.validates_presence_of :code
  80. #
  81. # l = Locale.new
  82. # l.valid?
  83. # l.errors_on(:code).should include("can't be blank")
  84. # end
  85. # end
  86. describe "translating a key" do
  87. describe "for the first time in the default locale" do
  88. before {
  89. I18n.locale = "en"
  90. I18n.default_locale = "en"
  91. Locale.create({:code => "en", :name => "English"})
  92. @database = I18n::Backend::Database.new
  93. @database.translate(:en, "dog")
  94. }
  95. it "should set the value of the translation" do
  96. Translation.first.value.should == "dog"
  97. end
  98. end
  99. describe "for the first time in an alternate locale" do
  100. before {
  101. I18n.locale = "es"
  102. I18n.default_locale = "en"
  103. Locale.create({:code => "en", :name => "English"})
  104. Locale.create({:code => "es", :name => "Spanish"})
  105. @database = I18n::Backend::Database.new
  106. @database.translate(:es, "dog")
  107. }
  108. it "should set the value of the translation to nil" do
  109. Translation.first.value.should == nil
  110. end
  111. end
  112. end
  113. describe "setting a locale in context" do
  114. before {
  115. I18n.locale = "es"
  116. @locale = mock_model(Locale, { :code => "es" })
  117. @database = I18n::Backend::Database.new
  118. }
  119. describe "on a new instance when the cache locale is nil" do
  120. before {
  121. Locale.stub!(:find_by_code).and_return(@locale)
  122. }
  123. it "should return a locale record for the current locale in context" do
  124. Locale.should_receive(:find_by_code).with(I18n.locale)
  125. @database.send(:locale_in_context, I18n.locale)
  126. end
  127. end
  128. describe "when passing in a temporary locale that's different from the local cache" do
  129. before {
  130. Locale.stub!(:find_by_code).with("it").and_return(@locale)
  131. @database.locale = "it"
  132. }
  133. it "should return a locale record for the temporary locale" do
  134. Locale.should_receive(:find_by_code).with("it")
  135. @database.send(:locale_in_context, "it")
  136. end
  137. it "should set the locale to the temporary value" do
  138. @database.send(:locale_in_context, "it").should == @locale
  139. end
  140. end
  141. describe "when passing in a temporary locale that's the same as the local cache" do
  142. before {
  143. Locale.stub!(:find_by_code).with("es").and_return(@locale)
  144. @database.locale = "es"
  145. }
  146. it "should set the locale to the temporary value" do
  147. @database.send(:locale_in_context, "es").should == @locale
  148. end
  149. end
  150. describe "when the locale is the same as the cache" do
  151. before {
  152. Locale.stub!(:find_by_code).with("es").and_return(@locale)
  153. }
  154. it "should update the locale cache with the new locale" do
  155. @database.locale = "es"
  156. @database.send(:locale_in_context, "es").should == @database.locale
  157. end
  158. end
  159. describe "when the locale is different than the cache" do
  160. before {
  161. Locale.stub!(:find_by_code).with("es").and_return(@locale)
  162. I18n.locale = "it"
  163. }
  164. it "should update the locale cache with the new locale" do
  165. @database.locale = "es"
  166. Locale.should_receive(:find_by_code).with("it")
  167. @database.send(:locale_in_context, "it")
  168. end
  169. end
  170. end
  171. end