From 2c59d78cde32cfd6f6fcda1f9aa78f94b38e5930 Mon Sep 17 00:00:00 2001 From: hukl Date: Tue, 6 Oct 2009 20:54:47 +0200 Subject: updated globalize2 --- .../test/model/active_record/migration_test.rb | 82 ++++++++-- .../model/active_record/sti_translated_test.rb | 26 +-- .../test/model/active_record/translated_test.rb | 174 ++++++++++++--------- 3 files changed, 177 insertions(+), 105 deletions(-) (limited to 'vendor/plugins/globalize2/test/model') diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb index c539fb6..eb6533d 100644 --- a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb @@ -6,17 +6,19 @@ require 'globalize/model/active_record' ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) # Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) +require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) class MigrationTest < ActiveSupport::TestCase def setup reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb')) end - + test 'globalize table added' do assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) Post.create_translation_table! :subject => :string, :content => :text - assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.index_exists?( :post_translations, :post_id ) columns = Post.connection.columns( :post_translations ) assert locale = columns.detect {|c| c.name == 'locale' } assert_equal :string, locale.type @@ -31,43 +33,93 @@ class MigrationTest < ActiveSupport::TestCase assert updated_at = columns.detect {|c| c.name == 'updated_at' } assert_equal :datetime, updated_at.type end - + test 'globalize table dropped' do assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) Post.create_translation_table! :subject => :string, :content => :text - assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.table_exists?( :post_translations ) + assert Post.connection.index_exists?( :post_translations, :post_id ) Post.drop_translation_table! assert !Post.connection.table_exists?( :post_translations ) + assert !Post.connection.index_exists?( :post_translations, :post_id ) end - test 'exception on untranslated field inputs' do - assert_raise Globalize::Model::UntranslatedMigrationField do - Post.create_translation_table! :subject => :string, :content => :text, :bogus => :string - end - end - test 'exception on missing field inputs' do assert_raise Globalize::Model::MigrationMissingTranslatedField do Post.create_translation_table! :content => :text end end - + test 'exception on bad input type' do assert_raise Globalize::Model::BadMigrationFieldType do Post.create_translation_table! :subject => :string, :content => :integer end end - + + test "exception on bad input type isn't raised for untranslated fields" do + assert_nothing_raised do + Post.create_translation_table! :subject => :string, :content => :string, :views_count => :integer + end + end + test 'create_translation_table! should not be called on non-translated models' do assert_raise NoMethodError do - Blog.create_translation_table! :name => :string + Blog.create_translation_table! :name => :string end end test 'drop_translation_table! should not be called on non-translated models' do assert_raise NoMethodError do - Blog.drop_translation_table! + Blog.drop_translation_table! + end + end + + test "translation_index_name returns a readable index name when it's not longer than 50 characters" do + assert_equal 'index_post_translations_on_post_id', Post.send(:translation_index_name) + end + + test "translation_index_name returns a hashed index name when it's longer than 50 characters" do + class UltraLongModelNameWithoutProper < ActiveRecord::Base + translates :foo end + expected = 'index_44eba0f057e01a590ffccd0b8a3b5c78979539cd' + actual = UltraLongModelNameWithoutProper.send(:translation_index_name) + + assert_equal expected, actual + end + + test 'globalize table added when table has long name' do + UltraLongModelNameWithoutProper.create_translation_table!( + :subject => :string, :content => :text + ) + + assert UltraLongModelNameWithoutProper.connection.table_exists?( + :ultra_long_model_name_without_proper_translations + ) + assert UltraLongModelNameWithoutProper.connection.index_exists?( + :ultra_long_model_name_without_proper_translations, + :name => UltraLongModelNameWithoutProper.send( + :translation_index_name + ) + ) + end + + test 'globalize table dropped when table has long name' do + UltraLongModelNameWithoutProper.create_translation_table!( + :subject => :string, :content => :text + ) + + UltraLongModelNameWithoutProper.drop_translation_table! + + assert !UltraLongModelNameWithoutProper.connection.table_exists?( + :ultra_long_model_name_without_proper_translations + ) + + assert !UltraLongModelNameWithoutProper.connection.index_exists?( + :ultra_long_model_name_without_proper_translations, + :ultra_long_model_name_without_proper_id + ) end end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb index 14d7d0f..6980ba0 100644 --- a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb @@ -6,17 +6,17 @@ require 'globalize/model/active_record' ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) # Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) +require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) class StiTranslatedTest < ActiveSupport::TestCase def setup I18n.locale = :'en-US' - I18n.fallbacks.clear + I18n.fallbacks.clear reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) end - + def teardown - I18n.fallbacks.clear + I18n.fallbacks.clear end test "works with simple dynamic finders" do @@ -34,7 +34,7 @@ class StiTranslatedTest < ActiveSupport::TestCase child.content = 'baz' assert_member 'content', child.changed end - + test 'change attribute on globalized model after locale switching' do child = Child.create :content => 'foo' assert_equal [], child.changed @@ -46,17 +46,17 @@ class StiTranslatedTest < ActiveSupport::TestCase test 'fallbacks with lots of locale switching' do I18n.fallbacks.map :'de-DE' => [ :'en-US' ] child = Child.create :content => 'foo' - + I18n.locale = :'de-DE' assert_equal 'foo', child.content - + I18n.locale = :'en-US' child.update_attribute :content, 'bar' - + I18n.locale = :'de-DE' assert_equal 'bar', child.content end - + test "saves all locales, even after locale switching" do child = Child.new :content => 'foo' I18n.locale = 'de-DE' @@ -66,10 +66,10 @@ class StiTranslatedTest < ActiveSupport::TestCase child.save I18n.locale = 'en-US' child = Child.first - assert_equal 'foo', child.content + assert_equal 'foo', child.content I18n.locale = 'de-DE' - assert_equal 'bar', child.content + assert_equal 'bar', child.content I18n.locale = 'he-IL' - assert_equal 'baz', child.content - end + assert_equal 'baz', child.content + end end \ No newline at end of file diff --git a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb index 3e9ea00..f54894e 100644 --- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb +++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb @@ -6,25 +6,25 @@ require 'globalize/model/active_record' ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated) # Load Post model -require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' ) +require File.join( File.dirname(__FILE__), '..', '..', 'data', 'models' ) class TranslatedTest < ActiveSupport::TestCase def setup I18n.locale = :'en-US' - I18n.fallbacks.clear + I18n.fallbacks.clear reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb')) ActiveRecord::Base.locale = nil end - + def teardown - I18n.fallbacks.clear + I18n.fallbacks.clear end test "modifiying translated fields" do post = Post.create :subject => 'foo' assert_equal 'foo', post.subject post.subject = 'bar' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject end test "modifiying translated fields while switching locales" do @@ -38,7 +38,7 @@ class TranslatedTest < ActiveSupport::TestCase I18n.locale = :'de-DE' post.subject = 'bar' end - + test "has post_translations" do post = Post.create assert_nothing_raised { post.globalize_translations } @@ -49,9 +49,9 @@ class TranslatedTest < ActiveSupport::TestCase post = Post.create :subject => 'foo' assert_equal 1, post.globalize_translations.size I18n.locale = :en - assert_equal 1, post.globalize_translations.size + assert_equal 1, post.globalize_translations.size end - + test "returns the value passed to :subject" do post = Post.new assert_equal 'foo', (post.subject = 'foo') @@ -59,12 +59,12 @@ class TranslatedTest < ActiveSupport::TestCase test "translates subject and content into en-US" do post = Post.create :subject => 'foo', :content => 'bar' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content assert post.save post.reload - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "finds a German post" do @@ -73,9 +73,9 @@ class TranslatedTest < ActiveSupport::TestCase post = Post.first post.subject = 'baz (de)' post.save - assert_equal 'baz (de)', Post.first.subject + assert_equal 'baz (de)', Post.first.subject I18n.locale = :'en-US' - assert_equal 'foo (en)', Post.first.subject + assert_equal 'foo (en)', Post.first.subject end test "saves an English post and loads test correctly" do @@ -83,23 +83,23 @@ class TranslatedTest < ActiveSupport::TestCase post = Post.create :subject => 'foo', :content => 'bar' assert post.save post = Post.first - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "updates an attribute" do post = Post.create :subject => 'foo', :content => 'bar' post.update_attribute :subject, 'baz' - assert_equal 'baz', Post.first.subject + assert_equal 'baz', Post.first.subject end test "update_attributes failure" do post = Post.create :subject => 'foo', :content => 'bar' assert !post.update_attributes( { :subject => '' } ) assert_nil post.reload.attributes['subject'] - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject end - + test "validates presence of :subject" do post = Post.new assert !post.save @@ -115,35 +115,35 @@ class TranslatedTest < ActiveSupport::TestCase post.save I18n.locale = 'en-US' post = Post.first - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject I18n.locale = 'de-DE' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject end test "keeping one field in new locale when other field is changed" do - I18n.fallbacks.map 'de-DE' => [ 'en-US' ] + I18n.fallbacks.map 'de-DE' => [ 'en-US' ] post = Post.create :subject => 'foo' I18n.locale = 'de-DE' post.content = 'bar' - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject end - + test "modifying non-required field in a new locale" do - I18n.fallbacks.map 'de-DE' => [ 'en-US' ] + I18n.fallbacks.map 'de-DE' => [ 'en-US' ] post = Post.create :subject => 'foo' I18n.locale = 'de-DE' post.content = 'bar' - assert post.save + assert post.save end - + test "returns the value for the correct locale, after locale switching, without saving" do post = Post.create :subject => 'foo' I18n.locale = 'de-DE' post.subject = 'bar' I18n.locale = 'en-US' - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject I18n.locale = 'de-DE' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject end test "saves all locales, even after locale switching" do @@ -155,11 +155,11 @@ class TranslatedTest < ActiveSupport::TestCase post.save I18n.locale = 'en-US' post = Post.first - assert_equal 'foo', post.subject + assert_equal 'foo', post.subject I18n.locale = 'de-DE' - assert_equal 'bar', post.subject + assert_equal 'bar', post.subject I18n.locale = 'he-IL' - assert_equal 'baz', post.subject + assert_equal 'baz', post.subject end test "resolves a simple fallback" do @@ -170,8 +170,8 @@ class TranslatedTest < ActiveSupport::TestCase post.content = 'bar' post.save I18n.locale = 'de-DE' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "resolves a simple fallback without reloading" do @@ -181,8 +181,8 @@ class TranslatedTest < ActiveSupport::TestCase post.subject = 'baz' post.content = 'bar' I18n.locale = 'de-DE' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "resolves a complex fallback without reloading" do @@ -195,8 +195,8 @@ class TranslatedTest < ActiveSupport::TestCase post.subject = 'baz' post.content = 'bar' I18n.locale = 'de' - assert_equal 'foo', post.subject - assert_equal 'bar', post.content + assert_equal 'foo', post.subject + assert_equal 'bar', post.content end test "returns nil if no translations are found" do @@ -224,14 +224,14 @@ class TranslatedTest < ActiveSupport::TestCase I18n.locale = 'de-DE' assert_equal 'bar', blog.posts.last.subject end - + test "works with simple dynamic finders" do foo = Post.create :subject => 'foo' Post.create :subject => 'bar' post = Post.find_by_subject('foo') assert_equal foo, post end - + test 'change attribute on globalized model' do post = Post.create :subject => 'foo', :content => 'bar' assert_equal [], post.changed @@ -253,30 +253,30 @@ class TranslatedTest < ActiveSupport::TestCase test 'fallbacks with lots of locale switching' do I18n.fallbacks.map :'de-DE' => [ :'en-US' ] post = Post.create :subject => 'foo' - + I18n.locale = :'de-DE' assert_equal 'foo', post.subject - + I18n.locale = :'en-US' post.update_attribute :subject, 'bar' - + I18n.locale = :'de-DE' assert_equal 'bar', post.subject end - + test 'reload' do post = Post.create :subject => 'foo', :content => 'bar' post.subject = 'baz' - assert_equal 'foo', post.reload.subject + assert_equal 'foo', post.reload.subject end - + test 'complex writing and stashing' do post = Post.create :subject => 'foo', :content => 'bar' post.subject = nil assert_nil post.subject - assert !post.valid? + assert !post.valid? end - + test 'translated class locale setting' do assert ActiveRecord::Base.respond_to?(:locale) assert_equal :'en-US', I18n.locale @@ -291,11 +291,11 @@ class TranslatedTest < ActiveSupport::TestCase assert_equal :fr, I18n.locale assert_equal :es, ActiveRecord::Base.locale end - + test "untranslated class responds to locale" do assert Blog.respond_to?(:locale) end - + test "to ensure locales in different classes are the same" do ActiveRecord::Base.locale = :de assert_equal :de, ActiveRecord::Base.locale @@ -304,30 +304,30 @@ class TranslatedTest < ActiveSupport::TestCase assert_equal :es, ActiveRecord::Base.locale assert_equal :es, Parent.locale end - + test "attribute saving goes by content locale and not global locale" do ActiveRecord::Base.locale = :de assert_equal :'en-US', I18n.locale Post.create :subject => 'foo' assert_equal :de, Post.first.globalize_translations.first.locale end - + test "attribute loading goes by content locale and not global locale" do post = Post.create :subject => 'foo' assert_equal :'en-US', ActiveRecord::Base.locale ActiveRecord::Base.locale = :de assert_equal :'en-US', I18n.locale post.update_attribute :subject, 'foo [de]' - assert_equal 'foo [de]', Post.first.subject + assert_equal 'foo [de]', Post.first.subject ActiveRecord::Base.locale = :'en-US' - assert_equal 'foo', Post.first.subject + assert_equal 'foo', Post.first.subject end test "access content locale before setting" do Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)" assert_nothing_raised { ActiveRecord::Base.locale } end - + test "translated_locales" do Post.locale = :de post = Post.create :subject => 'foo' @@ -338,121 +338,141 @@ class TranslatedTest < ActiveSupport::TestCase assert_equal [ :de, :es, :fr ], post.translated_locales assert_equal [ :de, :es, :fr ], Post.first.translated_locales end - + test "including globalize_translations" do I18n.locale = :de Post.create :subject => "Foo1", :content => "Bar1" Post.create :subject => "Foo2", :content => "Bar2" - + class << Post def tranlsations_included self.all(:include => :globalize_translations) end end - + default = Post.all.map {|x| [x.subject, x.content]} with_include = Post.tranlsations_included.map {|x| [x.subject, x.content]} assert_equal default, with_include end - + test "setting multiple translations at once with options hash" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :de => {:subject => "foo2", :content => "foo2"}, :en => {:subject => "bar2", :content => "bar2"} } post.set_translations options post.reload - + assert ["bar2", "bar2"], [post.subject, post.content] Post.locale = :de assert ["foo2", "foo2"], [post.subject, post.content] end - + test "setting only one translation with set_translations" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :en => {:subject => "bar2", :content => "bar2"} } post.set_translations options post.reload - + assert ["bar2", "bar2"], [post.subject, post.content] Post.locale = :de assert ["foo1", "foo1"], [post.subject, post.content] end - + test "setting only selected attributes with set_translations" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :de => {:content => "foo2"}, :en => {:subject => "bar2"} } post.set_translations options post.reload - + assert ["bar2", "bar1"], [post.subject, post.content] Post.locale = :de assert ["foo1", "foo2"], [post.subject, post.content] end - + test "setting invalid attributes raises ArgumentError" do Post.locale = :de post = Post.create :subject => "foo1", :content => "foo1" Post.locale = :en post.update_attributes( :subject => "bar1", :content => "bar1" ) - + options = { :de => {:fake => "foo2"} } exception = assert_raise(ActiveRecord::UnknownAttributeError) do post.set_translations options end assert_equal "unknown attribute: fake", exception.message end - + test "reload accepting find options" do p = Post.create :subject => "Foo", :content => "Bar" assert p.reload(:readonly => true, :lock => true) assert_raise(ArgumentError) { p.reload(:foo => :bar) } end - + test "dependent destroy of translation" do p = Post.create :subject => "Foo", :content => "Bar" assert_equal 1, PostTranslation.count p.destroy assert_equal 0, PostTranslation.count end - + test "translating subclass of untranslated comment model" do translated_comment = TranslatedComment.create(:post => @post) assert_nothing_raised { translated_comment.globalize_translations } end - + test "modifiying translated comments works as expected" do I18n.locale = :en translated_comment = TranslatedComment.create(:post => @post, :content => 'foo') assert_equal 'foo', translated_comment.content - + I18n.locale = :de translated_comment.content = 'bar' assert translated_comment.save assert_equal 'bar', translated_comment.content - + I18n.locale = :en assert_equal 'foo', translated_comment.content - + assert_equal 2, translated_comment.globalize_translations.size end + + test "can create a proxy class for a namespaced model" do + module Foo + module Bar + class Baz < ActiveRecord::Base + translates :bumm + end + end + end + end + + test "attribute translated before type cast" do + Post.locale = :en + post = Post.create :subject => 'foo', :content => 'bar' + Post.locale = :de + post.update_attribute :subject, "German foo" + assert_equal 'German foo', post.subject_before_type_cast + Post.locale = :en + assert_equal 'foo', post.subject_before_type_cast + end end # TODO should validate_presence_of take fallbacks into account? maybe we need # an extra validation call, or more options for validate_presence_of. # TODO error checking for fields that exist in main table, don't exist in # proxy table, aren't strings or text -# +# # TODO allow finding by translated attributes in conditions? # TODO generate advanced dynamic finders? -- cgit v1.3