diff options
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/attributes_test.rb | 30 | ||||
-rw-r--r-- | activemodel/test/cases/json_serialization_test.rb | 64 | ||||
-rw-r--r-- | activemodel/test/cases/naming_test.rb | 27 | ||||
-rw-r--r-- | activemodel/test/cases/validations/presence_validation_test.rb | 18 |
4 files changed, 130 insertions, 9 deletions
diff --git a/activemodel/test/cases/attributes_test.rb b/activemodel/test/cases/attributes_test.rb new file mode 100644 index 0000000000..5f3ea839a4 --- /dev/null +++ b/activemodel/test/cases/attributes_test.rb @@ -0,0 +1,30 @@ +require 'cases/helper' + +class AttributesTest < ActiveModel::TestCase + class Person + include ActiveModel::Attributes + attr_accessor :name + end + + test "reads attribute" do + p = Person.new + assert_equal nil, p.read_attribute(:name) + + p.name = "Josh" + assert_equal "Josh", p.read_attribute(:name) + end + + test "writes attribute" do + p = Person.new + assert_equal nil, p.name + + p.write_attribute(:name, "Josh") + assert_equal "Josh", p.name + end + + test "returns all attributes" do + p = Person.new + p.name = "Josh" + assert_equal({"name" => "Josh"}, p.attributes) + end +end diff --git a/activemodel/test/cases/json_serialization_test.rb b/activemodel/test/cases/json_serialization_test.rb new file mode 100644 index 0000000000..abcec67a85 --- /dev/null +++ b/activemodel/test/cases/json_serialization_test.rb @@ -0,0 +1,64 @@ +require 'cases/helper' + +class JsonSerializationTest < ActiveModel::TestCase + class Contact + extend ActiveModel::Naming + include ActiveModel::Serializers::JSON + attr_accessor :name, :age, :created_at, :awesome, :preferences + end + + def setup + @contact = Contact.new + @contact.name = 'Konata Izumi' + @contact.age = 16 + @contact.created_at = Time.utc(2006, 8, 1) + @contact.awesome = true + @contact.preferences = { 'shows' => 'anime' } + end + + test "should include root in json" do + begin + Contact.include_root_in_json = true + json = @contact.to_json + + assert_match %r{^\{"contact":\{}, json + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + ensure + Contact.include_root_in_json = false + end + end + + test "should encode all encodable attributes" do + json = @contact.to_json + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should allow attribute filtering with only" do + json = @contact.to_json(:only => [:name, :age]) + + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert_no_match %r{"awesome":true}, json + assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_no_match %r{"preferences":\{"shows":"anime"\}}, json + end + + test "should allow attribute filtering with except" do + json = @contact.to_json(:except => [:name, :age]) + + assert_no_match %r{"name":"Konata Izumi"}, json + assert_no_match %r{"age":16}, json + assert_match %r{"awesome":true}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"preferences":\{"shows":"anime"\}}, json + end +end diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb new file mode 100644 index 0000000000..e75d4541a3 --- /dev/null +++ b/activemodel/test/cases/naming_test.rb @@ -0,0 +1,27 @@ +require 'cases/helper' + +class NamingTest < Test::Unit::TestCase + def setup + @model_name = ActiveModel::Name.new('Post::TrackBack') + end + + def test_singular + assert_equal 'post_track_back', @model_name.singular + end + + def test_plural + assert_equal 'post_track_backs', @model_name.plural + end + + def test_element + assert_equal 'track_back', @model_name.element + end + + def test_collection + assert_equal 'post/track_backs', @model_name.collection + end + + def test_partial_path + assert_equal 'post/track_backs/track_back', @model_name.partial_path + end +end diff --git a/activemodel/test/cases/validations/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb index f6bed4903a..aa5bdf1e62 100644 --- a/activemodel/test/cases/validations/presence_validation_test.rb +++ b/activemodel/test/cases/validations/presence_validation_test.rb @@ -31,15 +31,15 @@ class PresenceValidationTest < ActiveModel::TestCase assert t.save end - def test_validates_presence_of_with_custom_message_using_quotes - repair_validations(Developer) do - Developer.validates_presence_of :non_existent, :message=> "This string contains 'single' and \"double\" quotes" - d = Developer.new - d.name = "Joe" - assert !d.valid? - assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors[:non_existent] - end - end + # def test_validates_presence_of_with_custom_message_using_quotes + # repair_validations(Developer) do + # Developer.validates_presence_of :non_existent, :message=> "This string contains 'single' and \"double\" quotes" + # d = Developer.new + # d.name = "Joe" + # assert !d.valid? + # assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors[:non_existent] + # end + # end def test_validates_presence_of_for_ruby_class repair_validations(Person) do |