test/armot_test.rb in armot-0.3.1 vs test/armot_test.rb in armot-0.3.2

- old
+ new

@@ -8,10 +8,12 @@ name.to_s end end class ArmotTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Pending + def setup setup_db I18n.locale = I18n.default_locale = :en I18n.fallbacks.clear Post.create(:title => 'English title') @@ -323,7 +325,166 @@ test "should return db-column values even if not persisted" do post = Post.new post[:title] = "Hello world" assert_equal "Hello world", post.title + end + + test "should fetch all translations with only one query with multiple armotized parameters" do + pending "should be implemented in the active_record specific gem" do + post = Post.first + post.text = "English text" + post.save! + + res = count_query_reads_for("I18n::Backend::ActiveRecord::Translation") do + a = Post.first + a.text + a.title + end + + assert_equal 1, res + end + end + + test "should not save the record if it has not changed" do + pending "should be implemented in the active_record specific gem" do + post = Post.last + post.title = "ENG title" + post.text = "English text" + post.save! + + res = count_query_updates_for("I18n::Backend::ActiveRecord::Translation") do + a = Post.first + a.title = "ENG Second version" + a.text = "English text" + a.save! + end + + assert_equal 1, res + end + end + + test ".armotized_attributes" do + assert_equal [:title, :text], Post.armotized_attributes + end + + test "multiple armotize calls raise an error" do + assert_raise Armot::DoubleDeclarationError do + class FooBar < ActiveRecord::Base + armotize :foo + armotize :bar + end + end + end + + test "the setter method shold return the assigned value" do + post = Post.last + res = (post.title = "Foo bar title") + assert_equal "Foo bar title", res + end + + test "an armotized class should not have armotized accessors by default" do + post = Post.last + assert_equal false, post.respond_to?(:title_en) + end + + test ".define_localized_accessors_for should define localized accessors for the current locales" do + post = Post.last + I18n.locale = :es + post.title = "SP titulo" + post.save! # Just save here to make I18n.available_locales aware of both :es and :en + + assert_equal [:es, :en].sort, I18n.available_locales.sort + + class FuzzBar < Post + define_localized_accessors_for :title + end + + foo = FuzzBar.new + foo.title = "Cucamonga" + foo.save! + assert_equal true, foo.respond_to?(:title_en) + assert_equal true, foo.respond_to?(:"title_en=") + assert_equal true, foo.respond_to?(:title_es) + assert_equal true, foo.respond_to?(:"title_es=") + end + + test "localized getters behaviour" do + class FuzzBar < Post + define_localized_accessors_for :title + end + + foo = FuzzBar.new + foo.title = "EN - title" + I18n.locale = :es + foo.title = "ES - titulo" + foo.save! + + I18n.locale = :en + assert_equal "EN - title", foo.title_en + assert_equal "ES - titulo", foo.title_es + end + + test "localized setters behaviour" do + class FuzzBar < Post + define_localized_accessors_for :title + end + + foo = FuzzBar.new + foo.title = "EN - title" + I18n.locale = :es + foo.title = "ES - titulo" + foo.save! + + I18n.locale = :en + res = (foo.title_es = "Segundo titulo") + assert_equal "Segundo titulo", foo.title_es + assert_equal "Segundo titulo", res + end + + test "after using localized accessors the I18n.locale should remain the same" do + class FuzzBar < Post + define_localized_accessors_for :title + end + + foo = FuzzBar.new + foo.title = "EN - title" + I18n.locale = :es + foo.title = "ES - titulo" + foo.save! + + I18n.locale = :klingon + foo.title_es = "Segundo titulo" + foo.title_en + assert_equal :klingon, I18n.locale + end + + test "localized accessors should work for more than one attribute" do + class FuzzBar < Post + define_localized_accessors_for :title, :text + end + + foo = FuzzBar.new + foo.title = "EN - title" + foo.text = "EN - body text" + foo.save! + assert_equal true, foo.respond_to?(:title_en) + assert_equal true, foo.respond_to?(:"title_en=") + assert_equal true, foo.respond_to?(:text_en) + assert_equal true, foo.respond_to?(:"text_en=") + end + + test ".define_localized_accessors_for :all" do + class FuzzBar < Post + define_localized_accessors_for :all + end + + foo = FuzzBar.new + foo.title = "EN - title" + foo.text = "EN - body text" + foo.save! + assert_equal true, foo.respond_to?(:title_en) + assert_equal true, foo.respond_to?(:"title_en=") + assert_equal true, foo.respond_to?(:text_en) + assert_equal true, foo.respond_to?(:"text_en=") end end