aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test
diff options
context:
space:
mode:
authortaryn <teast@globalpersonals.co.uk>2009-08-19 11:57:50 +0100
committerJoshua Peek <josh@joshpeek.com>2009-08-19 09:03:17 -0500
commitc2f90d6530dfd0ed68df9f4c429d0f498235e1d4 (patch)
tree97778398363924edd2fc580c642ccefdbf16babf /activeresource/test
parentef935240582ef6a7d47a9716e8269db817c91503 (diff)
downloadrails-c2f90d6530dfd0ed68df9f4c429d0f498235e1d4.tar.gz
rails-c2f90d6530dfd0ed68df9f4c429d0f498235e1d4.tar.bz2
rails-c2f90d6530dfd0ed68df9f4c429d0f498235e1d4.zip
Added validations to ActiveResource. Added a smoke test to see if we can add a validation and use it, and add a validates callback and use it.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activeresource/test')
-rw-r--r--activeresource/test/fixtures/project.rb25
-rw-r--r--activeresource/test/validations_test.rb49
2 files changed, 74 insertions, 0 deletions
diff --git a/activeresource/test/fixtures/project.rb b/activeresource/test/fixtures/project.rb
new file mode 100644
index 0000000000..e15fa6f620
--- /dev/null
+++ b/activeresource/test/fixtures/project.rb
@@ -0,0 +1,25 @@
+# used to test validations
+class Project < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000"
+
+ validates_presence_of :name
+ validate :description_greater_than_three_letters
+
+ # to test the validate *callback* works
+ def description_greater_than_three_letters
+ errors.add :description, 'must be greater than three letters long' if description.length < 3 unless description.blank?
+ end
+
+
+ # stop-gap accessor to default this attribute to nil
+ # Otherwise the validations fail saying that the method does not exist.
+ # In future, method_missing will be updated to not explode on a known
+ # attribute.
+ def name
+ attributes['name'] || nil
+ end
+ def description
+ attributes['description'] || nil
+ end
+end
+
diff --git a/activeresource/test/validations_test.rb b/activeresource/test/validations_test.rb
new file mode 100644
index 0000000000..f5a43b1ac1
--- /dev/null
+++ b/activeresource/test/validations_test.rb
@@ -0,0 +1,49 @@
+require 'abstract_unit'
+require "fixtures/project"
+
+# The validations are tested thoroughly under ActiveModel::Validations
+# This test case simply makes sur that they are all accessible by
+# Active Resource objects.
+class ValidationsTest < ActiveModel::TestCase
+ VALID_PROJECT_HASH = { :name => "My Project", :description => "A project" }
+ def setup
+ @my_proj = VALID_PROJECT_HASH.to_xml(:root => "person")
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.post "/projects.xml", {}, @my_proj, 201, 'Location' => '/projects/5.xml'
+ end
+ end
+
+ def test_validates_presence_of
+ p = new_project(:name => nil)
+ assert !p.valid?, "should not be a valid record without name"
+ assert !p.save, "should not have saved an invalid record"
+ assert_equal ["can't be blank"], p.errors[:name], "should have an error on name"
+
+ p.name = "something"
+
+ assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
+ end
+
+ def test_validate_callback
+ # we have a callback ensuring the description is longer thn three letters
+ p = new_project(:description => 'a')
+ assert !p.valid?, "should not be a valid record when it fails a validation callback"
+ assert !p.save, "should not have saved an invalid record"
+ assert_equal ["must be greater than three letters long"], p.errors[:description], "should be an error on description"
+
+ # should now allow this description
+ p.description = 'abcd'
+ assert p.save, "should have saved after fixing the validation, but had: #{p.errors.inspect}"
+ end
+
+ protected
+
+ # quickie helper to create a new project with all the required
+ # attributes.
+ # Pass in any params you specifically want to override
+ def new_project(opts = {})
+ Project.new(VALID_PROJECT_HASH.merge(opts))
+ end
+
+end
+