Permalink
Browse files

clean up tests a bit, add validation tests

  • Loading branch information...
1 parent f7b6e74 commit 63f203731bc725361eb7cfd31464f10d895b7789 @svenfuchs svenfuchs committed Dec 20, 2009
View
10 notes.textile
@@ -1,3 +1,13 @@
+virtual locale attribute for translated records:
+
+Post.create(:title => 'Titel', :locale => :de)
+
+maybe allow to set the locale attribute name for separating display and content locales.
+
+
+
+------------------------------------------------------------------------------
+
Stopped DB Backend in the middle, here's where we left off:
h1. Some Notes
View
4 test/active_record/fallbacks_test.rb
@@ -1,5 +1,5 @@
-require File.dirname(__FILE__) + '/../test_helper'
-require File.join( File.dirname(__FILE__) + '/../data/models' )
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../data/models')
if I18n.respond_to?(:fallbacks)
class TranslatedTest < ActiveSupport::TestCase
View
4 test/active_record/migration_test.rb
@@ -1,5 +1,5 @@
-require File.join( File.dirname(__FILE__) + '/../test_helper' )
-require File.join( File.dirname(__FILE__) + '/../data/models' )
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../data/models')
class MigrationTest < ActiveSupport::TestCase
def setup
View
4 test/active_record/sti_translated_test.rb
@@ -1,5 +1,5 @@
-require File.join( File.dirname(__FILE__) + '/../test_helper' )
-require File.join( File.dirname(__FILE__) + '/../data/models' )
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../data/models')
class StiTranslatedTest < ActiveSupport::TestCase
def setup
View
4 test/active_record/translates_test.rb
@@ -1,5 +1,5 @@
-require File.join( File.dirname(__FILE__) + '/../test_helper' )
-require File.join( File.dirname(__FILE__) + '/../data/models' )
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../data/models')
class TranslatesTest < ActiveSupport::TestCase
def setup
View
4 test/active_record/translation_class_test.rb
@@ -1,5 +1,5 @@
-require File.join( File.dirname(__FILE__) + '/../test_helper' )
-require File.join( File.dirname(__FILE__) + '/../data/models' )
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../data/models')
class TranlationClassTest < ActiveSupport::TestCase
def setup
View
75 test/active_record/validation_tests.rb
@@ -0,0 +1,75 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../data/models')
+
+class ValidationTest < ActiveSupport::TestCase
+ def setup
+ reset_db!
+ end
+
+ def teardown
+ Validatee.instance_variable_set(:@validate_callbacks, CallbackChain.new)
+ end
+
+ test "validates_presence_of" do
+ Validatee.class_eval { validates_presence_of :string }
+ assert !Validatee.new.valid?
+ assert Validatee.new(:string => 'foo').valid?
+ end
+
+ test "validates_confirmation_of" do
+ Validatee.class_eval { validates_confirmation_of :string }
+ assert !Validatee.new(:string => 'foo', :string_confirmation => 'bar').valid?
+ assert Validatee.new(:string => 'foo', :string_confirmation => 'foo').valid?
+ end
+
+ test "validates_acceptance_of" do
+ Validatee.class_eval { validates_acceptance_of :string, :accept => '1' }
+ assert !Validatee.new(:string => '0').valid?
+ assert Validatee.new(:string => '1').valid?
+ end
+
+ test "validates_length_of (:is)" do
+ Validatee.class_eval { validates_length_of :string, :is => 1 }
+ assert !Validatee.new(:string => 'aa').valid?
+ assert Validatee.new(:string => 'a').valid?
+ end
+
+ test "validates_format_of" do
+ Validatee.class_eval { validates_format_of :string, :with => /^\d+$/ }
+ assert !Validatee.new(:string => 'a').valid?
+ assert Validatee.new(:string => '1').valid?
+ end
+
+ test "validates_inclusion_of" do
+ Validatee.class_eval { validates_inclusion_of :string, :in => %(a) }
+ assert !Validatee.new(:string => 'b').valid?
+ assert Validatee.new(:string => 'a').valid?
+ end
+
+ test "validates_exclusion_of" do
+ Validatee.class_eval { validates_exclusion_of :string, :in => %(b) }
+ assert !Validatee.new(:string => 'b').valid?
+ assert Validatee.new(:string => 'a').valid?
+ end
+
+ test "validates_numericality_of" do
+ Validatee.class_eval { validates_numericality_of :string }
+ assert !Validatee.new(:string => 'a').valid?
+ assert Validatee.new(:string => '1').valid?
+ end
+
+ # This doesn't pass and Rails' validates_uniqueness_of implementation doesn't
+ # seem to be extensible easily. One can work around that by either defining
+ # a custom validation on the Validatee model itself, or by using validates_uniqueness_of
+ # on Validatee::Translation.
+ #
+ # test "validates_uniqueness_of" do
+ # Validatee.class_eval { validates_uniqueness_of :string }
+ # Validatee.create!(:string => 'a')
+ # assert !Validatee.new(:string => 'a').valid?
+ # assert Validatee.new(:string => 'b').valid?
+ # end
+
+ # test "validates_associated" do
+ # end
+end
View
191 test/active_record_test.rb
@@ -1,162 +1,167 @@
-require File.dirname(__FILE__) + '/test_helper'
-require File.join( File.dirname(__FILE__) + '/data/models' )
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+require File.expand_path(File.dirname(__FILE__) + '/data/models')
+
+# Higher level tests.
class ActiveRecordTest < ActiveSupport::TestCase
def setup
- I18n.locale = :'en-US'
+ I18n.locale = :en
reset_db!
ActiveRecord::Base.locale = nil
end
- test "modifiying translated fields" do
- post = Post.create(:subject => 'foo')
- assert_equal 'foo', post.subject
- post.subject = 'bar'
- assert_equal 'bar', post.subject
+ def assert_translated(locale, record, names, expected)
+ I18n.locale = locale
+ assert_equal Array(expected), Array(names).map { |name| record.send(name) }
end
- test "modifiying translated fields while switching locales" do
- post = Post.create(:subject => 'foo')
- assert_equal 'foo', post.subject
- I18n.locale = :'de-DE'
- post.subject = 'bar'
- assert_equal 'bar', post.subject
- I18n.locale = :'en-US'
- assert_equal 'foo', post.subject
- I18n.locale = :'de-DE'
- post.subject = 'bar'
+ test "a translated record has translations" do
+ assert_equal [], Post.new.translations
end
- test "has post_translations" do
- post = Post.create
- assert_nothing_raised { post.translations }
+ test "saves a translated version of the record for each locale" do
+ post = Post.create(:subject => 'title')
+ I18n.locale = :de
+ post.update_attributes(:subject => 'Titel')
+
+ assert_equal 2, post.translations.size
+ assert_equal %w(de en), post.translations.map(&:locale).map(&:to_s).sort
+ assert_equal %w(Titel title), post.translations.map(&:subject).sort
end
- test "has German post_translations" do
+ test "a translated record has German translations" do
I18n.locale = :de
post = Post.create(:subject => 'foo')
assert_equal 1, post.translations.size
- I18n.locale = :en
- assert_equal 1, post.translations.size
+ assert_equal [:de], post.translations.map { |t| t.locale }
+ end
+
+ test "modifiying translated fields while switching locales" do
+ post = Post.create(:subject => 'title', :content => 'content')
+ assert_equal %w(title content), [post.subject, post.content]
+
+ I18n.locale = :de
+ post.subject, post.content = 'Titel', 'Inhalt'
+
+ assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt))
+ assert_translated(:en, post, [:subject, :content], %w(title content))
+ assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt))
+
+ post.save
+ post.reload
+
+ assert_translated(:en, post, [:subject, :content], %w(title content))
+ assert_translated(:de, post, [:subject, :content], %w(Titel Inhalt))
end
- test "returns the value passed to :subject" do
- post = Post.new
- assert_equal 'foo', (post.subject = 'foo')
+ test "attribute writers do return their argument" do
+ value = (Post.new.subject = 'foo')
+ assert_equal 'foo', value
end
- test "translates subject and content into en-US" do
+ test "update_attribute succeeds with valid values" do
post = Post.create(:subject => 'foo', :content => 'bar')
+ post.update_attribute(:subject, 'baz')
+ assert_equal 'baz', Post.first.subject
+ end
+
+ test "update_attributes fails with invalid values" 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 'bar', post.content
- assert post.save
+ end
+
+ test 'reload works' do
+ post = Post.create(:subject => 'foo', :content => 'bar')
+ post.subject = 'baz'
post.reload
assert_equal 'foo', post.subject
- assert_equal 'bar', post.content
+ end
+
+ test "returns nil if no translations are found (unsaved record)" do
+ post = Post.new(:subject => 'foo')
+ assert_equal 'foo', post.subject
+ assert_nil post.content
+ end
+
+ test "returns nil if no translations are found (saved record)" do
+ post = Post.create(:subject => 'foo')
+ post.reload
+ assert_equal 'foo', post.subject
+ assert_nil post.content
end
test "finds a German post" do
post = Post.create(:subject => 'foo (en)', :content => 'bar')
- I18n.locale = 'de-DE'
+ I18n.locale = :de
post = Post.first
post.subject = 'baz (de)'
post.save
assert_equal 'baz (de)', Post.first.subject
- I18n.locale = :'en-US'
+ I18n.locale = :en
assert_equal 'foo (en)', Post.first.subject
end
test "saves an English post and loads correctly" do
- assert_nil Post.first
- post = Post.create :subject => 'foo', :content => 'bar'
+ post = Post.create(:subject => 'foo', :content => 'bar')
assert post.save
post = Post.first
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
- 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
- end
-
- test "validates presence of :subject" do
- post = Post.new
- assert !post.save
-
- post = Post.new :subject => 'foo'
- assert post.save
- end
-
test "returns the value for the correct locale, after locale switching" do
- post = Post.create :subject => 'foo'
- I18n.locale = 'de-DE'
+ post = Post.create(:subject => 'foo')
+ I18n.locale = :de
post.subject = 'bar'
post.save
- I18n.locale = 'en-US'
+ I18n.locale = :en
post = Post.first
assert_equal 'foo', post.subject
- I18n.locale = 'de-DE'
+ I18n.locale = :de
assert_equal 'bar', post.subject
end
test "returns the value for the correct locale, after locale switching, without saving" do
post = Post.create :subject => 'foo'
- I18n.locale = 'de-DE'
+ I18n.locale = :de
post.subject = 'bar'
- I18n.locale = 'en-US'
+ I18n.locale = :en
assert_equal 'foo', post.subject
- I18n.locale = 'de-DE'
+ I18n.locale = :de
assert_equal 'bar', post.subject
end
test "saves all locales, even after locale switching" do
post = Post.new :subject => 'foo'
- I18n.locale = 'de-DE'
+ I18n.locale = :de
post.subject = 'bar'
- I18n.locale = 'he-IL'
+ I18n.locale = :he
post.subject = 'baz'
post.save
- I18n.locale = 'en-US'
+ I18n.locale = :en
post = Post.first
assert_equal 'foo', post.subject
- I18n.locale = 'de-DE'
+ I18n.locale = :de
assert_equal 'bar', post.subject
- I18n.locale = 'he-IL'
+ I18n.locale = :he
assert_equal 'baz', post.subject
end
- test "returns nil if no translations are found" do
- post = Post.new(:subject => 'foo')
- assert_equal 'foo', post.subject
- assert_nil post.content
- end
-
- test "returns nil if no translations are found; reloaded" do
- post = Post.create(:subject => 'foo')
- post = Post.first
- assert_equal 'foo', post.subject
- assert_nil post.content
- end
-
test "works with associations" do
blog = Blog.create
post1 = blog.posts.create(:subject => 'foo')
- I18n.locale = 'de-DE'
+
+ I18n.locale = :de
post2 = blog.posts.create(:subject => 'bar')
assert_equal 2, blog.posts.size
- I18n.locale = 'en-US'
+
+ I18n.locale = :en
assert_equal 'foo', blog.posts.first.subject
assert_nil blog.posts.last.subject
- I18n.locale = 'de-DE'
+
+ I18n.locale = :de
assert_equal 'bar', blog.posts.last.subject
end
@@ -185,12 +190,6 @@ def setup
assert_equal [ 'subject' ], post.changed
end
- test 'reload' do
- post = Post.create(:subject => 'foo', :content => 'bar')
- post.subject = 'baz'
- assert_equal 'foo', post.reload.subject
- end
-
test 'complex writing and stashing' do
post = Post.create(:subject => 'foo', :content => 'bar')
post.subject = nil
@@ -202,8 +201,8 @@ def setup
test 'translated class locale setting' do
assert ActiveRecord::Base.respond_to?(:locale)
- assert_equal :'en-US', I18n.locale
- assert_equal :'en-US', ActiveRecord::Base.locale
+ assert_equal :en, I18n.locale
+ assert_equal :en, ActiveRecord::Base.locale
I18n.locale = :de
assert_equal :de, I18n.locale
assert_equal :de, ActiveRecord::Base.locale
@@ -230,19 +229,19 @@ def setup
test "attribute saving goes by content locale and not global locale" do
ActiveRecord::Base.locale = :de
- assert_equal :'en-US', I18n.locale
+ assert_equal :en, I18n.locale
Post.create :subject => 'foo'
assert_equal :de, Post.first.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
+ assert_equal :en, ActiveRecord::Base.locale
ActiveRecord::Base.locale = :de
- assert_equal :'en-US', I18n.locale
+ assert_equal :en, I18n.locale
post.update_attribute(:subject, 'foo [de]')
assert_equal 'foo [de]', Post.first.subject
- ActiveRecord::Base.locale = :'en-US'
+ ActiveRecord::Base.locale = :en
assert_equal 'foo', Post.first.subject
end
View
9 test/data/models.rb
@@ -1,3 +1,8 @@
+require 'ruby2ruby'
+require 'parse_tree'
+require 'parse_tree_extensions'
+require 'pp'
+
class PostTranslation < ActiveRecord::Base
def existing_method ; end
end
@@ -39,3 +44,7 @@ def do_reload
reload
end
end
+
+class Validatee < ActiveRecord::Base
+ translates :string
+end
View
9 test/data/schema.rb
@@ -1,5 +1,4 @@
ActiveRecord::Schema.define do
-
create_table :blogs, :force => true do |t|
t.string :description
end
@@ -36,4 +35,12 @@
t.text :content
end
+ create_table :validatees, :force => true do |t|
+ end
+
+ create_table :validatee_translations, :force => true do |t|
+ t.string :locale
+ t.references :validatee
+ t.string :string
+ end
end
View
27 test/test_helper.rb
@@ -46,3 +46,30 @@ def index_exists?(table_name, column_name)
end
end
end
+
+# module ActiveRecord
+# class BaseWithoutTable < Base
+# self.abstract_class = true
+#
+# def create_or_update
+# errors.empty?
+# end
+#
+# class << self
+# def columns()
+# @columns ||= []
+# end
+#
+# def column(name, sql_type = nil, default = nil, null = true)
+# columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+# reset_column_information
+# end
+#
+# # Do not reset @columns
+# def reset_column_information
+# read_methods.each { |name| undef_method(name) }
+# @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil
+# end
+# end
+# end
+# end

0 comments on commit 63f2037

Please sign in to comment.