aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource/test')
-rw-r--r--activeresource/test/abstract_unit.rb8
-rw-r--r--activeresource/test/base_test.rb60
-rw-r--r--activeresource/test/connection_test.rb11
-rw-r--r--activeresource/test/fixtures/person.rb3
-rw-r--r--activeresource/test/http_mock.rb84
5 files changed, 166 insertions, 0 deletions
diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb
new file mode 100644
index 0000000000..ddc00e307c
--- /dev/null
+++ b/activeresource/test/abstract_unit.rb
@@ -0,0 +1,8 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+$:.unshift(File.dirname(__FILE__) + '/.')
+
+require 'active_resource'
+require 'test/unit'
+require 'active_support/breakpoint'
+
+require "#{File.dirname(__FILE__)}/http_mock" \ No newline at end of file
diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb
new file mode 100644
index 0000000000..3c87ca532c
--- /dev/null
+++ b/activeresource/test/base_test.rb
@@ -0,0 +1,60 @@
+require "#{File.dirname(__FILE__)}/abstract_unit"
+require "fixtures/person"
+
+class BaseTest < Test::Unit::TestCase
+ def setup
+ ActiveResource::HttpMock.respond_to(
+ ActiveResource::Request.new(:get, "/people/1.xml") => ActiveResource::Response.new("<person><name>Matz</name><id type='integer'>1</id></person>"),
+ ActiveResource::Request.new(:get, "/people/2.xml") => ActiveResource::Response.new("<person><name>David</name><id type='integer'>2</id></person>"),
+ ActiveResource::Request.new(:put, "/people/1.xml") => ActiveResource::Response.new({}, 200),
+ ActiveResource::Request.new(:delete, "/people/1.xml") => ActiveResource::Response.new({}, 200),
+ ActiveResource::Request.new(:delete, "/people/2.xml") => ActiveResource::Response.new({}, 400),
+ ActiveResource::Request.new(:post, "/people.xml") => ActiveResource::Response.new({}, 200),
+ ActiveResource::Request.new(:get, "/people/99.xml") => ActiveResource::Response.new({}, 404),
+ ActiveResource::Request.new(:get, "/people.xml") => ActiveResource::Response.new(
+ "<people><person><name>Matz</name><id type='integer'>1</id></person><person><name>David</name><id type='integer'>2</id></person></people>"
+ )
+ )
+ end
+
+ def test_collection_name
+ assert_equal "people", Person.collection_name
+ end
+
+ def test_find_by_id
+ matz = Person.find(1)
+ assert_kind_of Person, matz
+ assert_equal "Matz", matz.name
+ end
+
+ def test_find_all
+ all = Person.find(:all)
+ assert_equal 2, all.size
+ assert_kind_of Person, all.first
+ assert_equal "Matz", all.first.name
+ assert_equal "David", all.last.name
+ end
+
+ def test_find_first
+ matz = Person.find(:first)
+ assert_kind_of Person, matz
+ assert_equal "Matz", matz.name
+ end
+
+ def test_find_by_id_not_found
+ assert_raises(ActiveResource::ResourceNotFound) { Person.find(99) }
+ end
+
+ def test_update
+ matz = Person.find(:first)
+ matz.name = "David"
+ assert_kind_of Person, matz
+ assert_equal "David", matz.name
+ matz.save
+ end
+
+ def test_destroy
+ assert Person.find(1).destroy
+ assert_raises(ActiveResource::ClientError) { Person.find(2).destroy }
+ end
+end \ No newline at end of file
diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb
new file mode 100644
index 0000000000..1c5951ca91
--- /dev/null
+++ b/activeresource/test/connection_test.rb
@@ -0,0 +1,11 @@
+require "#{File.dirname(__FILE__)}/abstract_unit"
+require "fixtures/person"
+
+class ConnectionTest < Test::Unit::TestCase
+ def setup
+ end
+
+ def test_something
+ true
+ end
+end \ No newline at end of file
diff --git a/activeresource/test/fixtures/person.rb b/activeresource/test/fixtures/person.rb
new file mode 100644
index 0000000000..4914863230
--- /dev/null
+++ b/activeresource/test/fixtures/person.rb
@@ -0,0 +1,3 @@
+class Person < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000/"
+end \ No newline at end of file
diff --git a/activeresource/test/http_mock.rb b/activeresource/test/http_mock.rb
new file mode 100644
index 0000000000..a4bc7e7cb2
--- /dev/null
+++ b/activeresource/test/http_mock.rb
@@ -0,0 +1,84 @@
+require 'active_resource/connection'
+
+module ActiveResource
+ class HttpMock
+ class << self
+ def requests
+ @@requests ||= []
+ end
+
+ def responses
+ @@responses ||= {}
+ end
+
+ def respond_to(pairs)
+ reset!
+ pairs.each do |(path, response)|
+ responses[path] = response
+ end
+ end
+
+ def reset!
+ requests.clear
+ responses.clear
+ end
+ end
+
+ for method in [ :post, :put, :get, :delete ]
+ module_eval <<-EOE
+ def #{method}(*arguments)
+ request = ActiveResource::Request.new(:#{method}, *arguments)
+ self.class.requests << request
+ self.class.responses[request] || raise("No response recorded for: \#{request}")
+ end
+ EOE
+ end
+
+ def initialize(site)
+ @site = site
+ end
+ end
+
+ class Request
+ attr_accessor :path, :method, :body
+
+ def initialize(method, path, body = nil)
+ @method, @path, @body = method, path, body
+ end
+
+ def ==(other_request)
+ other_request.hash == hash
+ end
+
+ def eql?(other_request)
+ self == other_request
+ end
+
+ def to_s
+ "<#{method.to_s.upcase}: #{path} (#{body})>"
+ end
+
+ def hash
+ "#{path}#{method}".hash
+ end
+ end
+
+ class Response
+ attr_accessor :body, :code
+
+ def initialize(body, code = 200)
+ @body, @code = body, code
+ end
+
+ def success?
+ (200..299).include?(code)
+ end
+ end
+
+ class Connection
+ private
+ def http
+ @http ||= HttpMock.new(@site)
+ end
+ end
+end \ No newline at end of file