active_resource_test_helper 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +19 -0
- data/README.rdoc +148 -0
- data/Rakefile +65 -0
- data/VERSION +1 -0
- data/lib/active_resource_test_helper.rb +29 -0
- data/lib/active_resource_test_helper/active_resource/http_mock.rb +77 -0
- data/lib/active_resource_test_helper/factory.rb +37 -0
- data/lib/active_resource_test_helper/ohm/model.rb +27 -0
- data/test/active_resource_factories_test.rb +71 -0
- data/test/active_resource_http_mock_test.rb +67 -0
- data/test/active_resource_test_helper_test.rb +29 -0
- data/test/factories/user_factory.rb +10 -0
- data/test/ohm_model_changes_test.rb +33 -0
- data/test/test_helper.rb +5 -0
- metadata +102 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Flavio Castelli <flavio@castelli.name>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
= active_resource_test_helper
|
2
|
+
|
3
|
+
active_resource_test_helper makes it easier to use ActiveResouce::HttpMock[http://api.rubyonrails.org/classes/ActiveResource/HttpMock.html].
|
4
|
+
|
5
|
+
Instead of declaring manually all the request-response pairs, it's possible to
|
6
|
+
use dynamically generated contents. These contents are defined using
|
7
|
+
factory_girl[http://github.com/thoughtbot/factory_girl] and are stored into a
|
8
|
+
Redis[http://code.google.com/p/redis/] database using ohm[http://github.com/soveran/ohm].
|
9
|
+
|
10
|
+
Usually ActiveResource tests look like that:
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
|
14
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
15
|
+
mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml"
|
16
|
+
mock.get "/people/1.xml", {}, @matz
|
17
|
+
mock.put "/people/1.xml", {}, nil, 204
|
18
|
+
mock.delete "/people/1.xml", {}, nil, 200
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_get_matz
|
23
|
+
person = Person.find(1)
|
24
|
+
assert_equal "Matz", person.name
|
25
|
+
end
|
26
|
+
|
27
|
+
While using active_resource_test_helper an common ActiveResource test would be something like that:
|
28
|
+
|
29
|
+
class RemoteUserTest < Test::Unit::TestCase
|
30
|
+
include ActiveResourceTestHelper
|
31
|
+
active_resource_factories :user
|
32
|
+
|
33
|
+
def setup
|
34
|
+
assert_equal 0, User.count
|
35
|
+
@users = []
|
36
|
+
20.times do |i|
|
37
|
+
@users << Factory.create(:basic_user)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_find_by_id
|
42
|
+
@users.each do |expected_user|
|
43
|
+
user = UserResource.find(expected_user.id)
|
44
|
+
assert_not_nil user
|
45
|
+
assert_equal expected_user.first_name, user.first_name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
== HTTP methods supported
|
51
|
+
|
52
|
+
Currently active_resource_test_helper dynamic contents are served only by _get_ requests.
|
53
|
+
In the future they will be used also by _post_, _put_, _delete_ and _head_ operations.
|
54
|
+
|
55
|
+
=== Get requests
|
56
|
+
|
57
|
+
The following operations are currently fully supported:
|
58
|
+
|
59
|
+
Person.find(1)
|
60
|
+
Person.find(:all)
|
61
|
+
Person.find(:first)
|
62
|
+
Person.find(:last)
|
63
|
+
Person.find(:all, :params => { :title => "CEO" })
|
64
|
+
Person.find(:first, :params => { :first_name => "flavio", :last_name => "castelli" })
|
65
|
+
|
66
|
+
== Requirements
|
67
|
+
|
68
|
+
Install Redis[http://code.google.com/p/redis/]. On most platforms it's as easy as grabbing the sources, running make and then putting the redis-server binary in the PATH.
|
69
|
+
|
70
|
+
Once you have it installed, you can execute redis-server and it will run on localhost:6379 by default. Check the redis.conf file that comes with the sources if you want to change some settings.
|
71
|
+
|
72
|
+
Then install the active_resource_test_helper gem:
|
73
|
+
|
74
|
+
sudo gem install active_resource_test_helper
|
75
|
+
|
76
|
+
== Usage
|
77
|
+
|
78
|
+
In order to use active_resource_test_helper inside of your tests you have to:
|
79
|
+
|
80
|
+
* require 'active_resource_test_helper'
|
81
|
+
* include the ActiveResourceTestHelper module inside of your test
|
82
|
+
|
83
|
+
This is a small example:
|
84
|
+
|
85
|
+
require 'active_resource_test_helper'
|
86
|
+
|
87
|
+
class MyTest < Test::Unit::TestCase
|
88
|
+
include ActiveResourceTestHelper
|
89
|
+
active_resource_factories :user, :post, :comment
|
90
|
+
end
|
91
|
+
|
92
|
+
Obviously you have also to define the factories used by your test (see below).
|
93
|
+
|
94
|
+
You can find more examples under the _test_ directory.
|
95
|
+
|
96
|
+
== Defining factories
|
97
|
+
|
98
|
+
Factories used by ActiveResource::HttpMock are defined with the _Factory.define_active_resource_factory_ method:
|
99
|
+
|
100
|
+
Factory.define_active_resource_factory(:basic_user, :class => "User") do |u|
|
101
|
+
u.sequence(:first_name) {|n| "first_name#{n}"}
|
102
|
+
u.sequence(:last_name) {|n| "last_name#{n}"}
|
103
|
+
u.admin false
|
104
|
+
u.email {|u| "#{u.first_name}.#{u.last_name}@example.com" }
|
105
|
+
u.age {rand(30) + 18}
|
106
|
+
end
|
107
|
+
|
108
|
+
If you are already familiar with factory_girl[http://github.com/thoughtbot/factory_girl] you will probably have already noticed we are using the same syntax.
|
109
|
+
|
110
|
+
=== What happens behind the scenes
|
111
|
+
|
112
|
+
_active_resource_test_helper_ will automatically generate an ohm[http://github.com/soveran/ohm]
|
113
|
+
model for each build class used by the active resource factories.
|
114
|
+
|
115
|
+
These models will have one attribute and one index per each attribute declared inside of the factory.
|
116
|
+
|
117
|
+
The factory defined into the previous example will generate this model:
|
118
|
+
|
119
|
+
class User < Ohm::Model
|
120
|
+
index :id
|
121
|
+
|
122
|
+
attribute :first_name
|
123
|
+
index :first_name
|
124
|
+
|
125
|
+
attribute :last_name
|
126
|
+
index :last_name
|
127
|
+
|
128
|
+
attribute :admin
|
129
|
+
index :admin
|
130
|
+
|
131
|
+
attribute :email
|
132
|
+
index :email
|
133
|
+
|
134
|
+
attribute :age
|
135
|
+
index :age
|
136
|
+
|
137
|
+
alias :save! :save
|
138
|
+
end
|
139
|
+
|
140
|
+
== Transactional factories
|
141
|
+
|
142
|
+
Currently redis doesn't have a complete transaction support. active_resource_test_helper removes from the redis database all the instances of the models defined by the ActiveResource factories that are used by the test.
|
143
|
+
This operation is executed before and after each test execution.
|
144
|
+
|
145
|
+
== Statically defined responses
|
146
|
+
|
147
|
+
It's possible to use dynamic contents and static pairs of request-response at the same time.
|
148
|
+
However the responses defined in the static way have precedence over those generated in the dynamic way.
|
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
task :default => "test"
|
6
|
+
|
7
|
+
namespace :test do
|
8
|
+
desc "Test all classes"
|
9
|
+
Rake::TestTask.new(:all) do |t|
|
10
|
+
t.libs << "test"
|
11
|
+
t.pattern = 'test/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Run all the unit tests"
|
17
|
+
task :test do
|
18
|
+
Rake::Task["test:all"].invoke
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Generate documentation.'
|
22
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
23
|
+
rdoc.rdoc_dir = 'rdoc'
|
24
|
+
rdoc.title = 'active_resource_test_hekper'
|
25
|
+
rdoc.options << '--line-numbers' << "--main" << "README.rdoc"
|
26
|
+
rdoc.rdoc_files.include('README.rdoc')
|
27
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require 'jeweler'
|
32
|
+
Jeweler::Tasks.new do |s|
|
33
|
+
s.name = %q{active_resource_test_helper}
|
34
|
+
s.summary = %q{ActiveResource test helper.}
|
35
|
+
s.description = %q{active_resource_test_helper makes it easier to use ActiveResouce::HttpMock.
|
36
|
+
Instead of declaring manually all the request-response pairs,
|
37
|
+
it’s possible to use dynamically generated contents.
|
38
|
+
These contents are defined using factory_girl and are
|
39
|
+
stored into a Redis database using ohm.}
|
40
|
+
|
41
|
+
s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'test/**/*.rb']
|
42
|
+
s.require_path = 'lib'
|
43
|
+
s.test_files = Dir[*['test/**/*_test.rb']]
|
44
|
+
|
45
|
+
s.has_rdoc = true
|
46
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
47
|
+
s.rdoc_options = ['--line-numbers', "--main", "README.rdoc"]
|
48
|
+
|
49
|
+
s.authors = ["Flavio Castelli"]
|
50
|
+
s.email = %q{flavio@castelli.name}
|
51
|
+
s.homepage = "http://github.com/flavio/active_resource_test_helper"
|
52
|
+
|
53
|
+
s.add_dependency "factory_girl", ">= 1.2.3"
|
54
|
+
s.add_dependency "redis", ">= 1.0.4"
|
55
|
+
s.add_dependency "ohm", ">= 0.0.35"
|
56
|
+
|
57
|
+
s.platform = Gem::Platform::RUBY
|
58
|
+
end
|
59
|
+
Jeweler::GemcutterTasks.new
|
60
|
+
rescue LoadError
|
61
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
62
|
+
end
|
63
|
+
|
64
|
+
desc "Clean files generated by rake tasks"
|
65
|
+
task :clobber => [:clobber_rdoc]
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_resource_test_helper/ohm/model'
|
2
|
+
require 'active_resource_test_helper/active_resource/http_mock'
|
3
|
+
require 'active_resource_test_helper/factory'
|
4
|
+
|
5
|
+
module ActiveResourceTestHelper #:nodoc: all
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
def run(*args, &block)
|
11
|
+
ActiveResource::HttpMock.dynamic_models.each do |model|
|
12
|
+
model.destroy_all
|
13
|
+
end
|
14
|
+
|
15
|
+
super(*args, &block)
|
16
|
+
|
17
|
+
ActiveResource::HttpMock.dynamic_models.each do |model|
|
18
|
+
model.destroy_all
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def active_resource_factories *args
|
24
|
+
args.each do |name|
|
25
|
+
ActiveResource::HttpMock.register_factory name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "active_resource"
|
2
|
+
require "active_resource/http_mock"
|
3
|
+
require "active_support"
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module ActiveResource # :nodoc:
|
7
|
+
|
8
|
+
# Adds new convenience methods to ActiveResource::HttpMock[http://api.rubyonrails.org/classes/ActiveResource/HttpMock.html].
|
9
|
+
class HttpMock
|
10
|
+
# Return a list of all the dynamic models used by HttpMock
|
11
|
+
def self.dynamic_models
|
12
|
+
@@dynamic_models ||= []
|
13
|
+
end
|
14
|
+
|
15
|
+
# Add a new dynamic model by registering a new factory.
|
16
|
+
# Do not use this method directly, Use the active_resource_factories method
|
17
|
+
# provided by the ActiveResourceTestHelper module.
|
18
|
+
def self.register_factory name
|
19
|
+
build_class = Factory.factory_by_name(name).build_class
|
20
|
+
dynamic_models << build_class unless dynamic_models.include? build_class
|
21
|
+
end
|
22
|
+
|
23
|
+
# Unregister all the dynamic models.
|
24
|
+
def reset_dynamic_models!
|
25
|
+
@@dynamic_models.clear
|
26
|
+
end
|
27
|
+
|
28
|
+
alias old_get get
|
29
|
+
|
30
|
+
def get(path, headers) #:nodoc:
|
31
|
+
begin
|
32
|
+
# lookup into the static mocks
|
33
|
+
old_get path, headers
|
34
|
+
rescue InvalidRequestError
|
35
|
+
# lookup into the dynamic mocks
|
36
|
+
model_class = HttpMock.dynamic_models.find{|m| !(path =~ dynamic_model_regexp(m)).nil?}
|
37
|
+
if model_class.nil?
|
38
|
+
raise
|
39
|
+
else
|
40
|
+
if $1.nil? and $2.nil?
|
41
|
+
# all the items have been requested
|
42
|
+
items = entries_to_valid_array(model_class.all)
|
43
|
+
response = Response.new(items.to_xml(:root => model_class.to_s))
|
44
|
+
elsif !$1.nil?
|
45
|
+
# a specific item has been requested
|
46
|
+
match = model_class.find(:id => $1.delete('/').to_i).entries.first
|
47
|
+
|
48
|
+
if match.nil?
|
49
|
+
response = Response.new(nil, 404)
|
50
|
+
else
|
51
|
+
response = Response.new(match.to_hash.to_xml(:root => model_class.to_s))
|
52
|
+
end
|
53
|
+
else
|
54
|
+
# query contains params
|
55
|
+
params = {}
|
56
|
+
CGI::parse($2[1,$2.size]).each do |key, value|
|
57
|
+
params[key.to_sym] = value.first
|
58
|
+
end
|
59
|
+
items = entries_to_valid_array(model_class.find(params).entries)
|
60
|
+
response = Response.new(items.to_xml(:root => model_class.to_s))
|
61
|
+
end
|
62
|
+
self.class.responses << response
|
63
|
+
response
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def dynamic_model_regexp model # :nodoc:
|
70
|
+
/\/#{model.to_s.underscore.pluralize}(\/\d+)?\.xml(\?.*)?/
|
71
|
+
end
|
72
|
+
|
73
|
+
def entries_to_valid_array entries# :nodoc:
|
74
|
+
entries.sort{|a,b| a.id.to_i <=> b.id.to_i}.map{|i| i.to_hash}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'ohm'
|
2
|
+
|
3
|
+
# Adds a new type of factory to factory_girl[http://github.com/thoughtbot/factory_girl].
|
4
|
+
class Factory
|
5
|
+
# Define a new factory to use inside of ActiveRecord tests.
|
6
|
+
# Uses the same syntax of factory_girl[http://github.com/thoughtbot/factory_girl].
|
7
|
+
def self.define_active_resource_factory name, options = {}, &definition
|
8
|
+
raise "no block given" unless block_given?
|
9
|
+
|
10
|
+
Factory.define name, options.dup, &definition
|
11
|
+
|
12
|
+
return if options.include? :parent
|
13
|
+
return if options.include?(:class) and const_defined?(options[:class].to_s.camelcase)
|
14
|
+
|
15
|
+
if options.include?(:class) and !const_defined?(options[:class].to_s.camelcase)
|
16
|
+
model_class_name = options[:class].to_s
|
17
|
+
else
|
18
|
+
model_class_name = name.to_s.capitalize
|
19
|
+
end
|
20
|
+
|
21
|
+
Object.module_eval <<-EOE, __FILE__, __LINE__
|
22
|
+
class #{model_class_name} < Ohm::Model
|
23
|
+
index :id
|
24
|
+
alias :save! :save
|
25
|
+
end
|
26
|
+
EOE
|
27
|
+
|
28
|
+
const_get(model_class_name).class_eval do
|
29
|
+
Factory.factories[name].attributes.each do |attr|
|
30
|
+
attribute attr.name.to_sym
|
31
|
+
index attr.name.to_sym
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'factory_girl'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'ohm'
|
2
|
+
|
3
|
+
module Ohm # :nodoc:
|
4
|
+
# Adds some convenience methods to Ohm::Model[http://github.com/soveran/ohm].
|
5
|
+
class Model
|
6
|
+
# Convert the model to hash.
|
7
|
+
def to_hash
|
8
|
+
hash = {:id => self.id.to_i}
|
9
|
+
self.attributes.each do |attr|
|
10
|
+
hash[attr] = self.send attr
|
11
|
+
end
|
12
|
+
hash
|
13
|
+
end
|
14
|
+
|
15
|
+
# Destroy all instances of this model
|
16
|
+
def self.destroy_all
|
17
|
+
self.all.each do |i|
|
18
|
+
i.delete
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return the number of instances of this model saved into redis
|
23
|
+
def self.count
|
24
|
+
self.all.size
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
Factory.define_active_resource_factory(:book) do |b|
|
4
|
+
b.sequence(:author) {|n| "author foo#{n}"}
|
5
|
+
b.sequence(:title) {|n| "title#{n}"}
|
6
|
+
b.sequence(:genre) {|n| "genre #{n}"}
|
7
|
+
b.pages {rand(500) + 30}
|
8
|
+
end
|
9
|
+
|
10
|
+
Factory.define_active_resource_factory(:fantasy_book, :parent => :book) do |b|
|
11
|
+
b.genre "fantasy"
|
12
|
+
b.pages {rand(500) + 130}
|
13
|
+
end
|
14
|
+
|
15
|
+
class ActiveResourceFactoriesTest < Test::Unit::TestCase
|
16
|
+
def setup
|
17
|
+
Book.destroy_all
|
18
|
+
end
|
19
|
+
|
20
|
+
def teardown
|
21
|
+
Book.destroy_all
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_factory_should_be_defined
|
25
|
+
assert_not_nil Factory.factory_by_name(:book)
|
26
|
+
assert_not_nil Factory.factory_by_name(:fantasy_book)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_model_class_should_be_defined
|
30
|
+
assert_nothing_raised do
|
31
|
+
assert_not_nil Book.new
|
32
|
+
end
|
33
|
+
assert_raise NameError do
|
34
|
+
FantasyBook.new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_factory_create_works
|
39
|
+
assert Book.all.empty?
|
40
|
+
|
41
|
+
Factory.create(:book)
|
42
|
+
assert_equal 1, Book.all.size
|
43
|
+
|
44
|
+
fbook = Factory.create(:fantasy_book)
|
45
|
+
assert_equal 2, Book.all.size
|
46
|
+
|
47
|
+
assert_equal "fantasy", fbook.genre
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_model_attributes
|
51
|
+
book = Book.new
|
52
|
+
assert(book.attributes.include?(:author))
|
53
|
+
assert(book.attributes.include?(:title))
|
54
|
+
assert(book.attributes.include?(:pages))
|
55
|
+
assert(book.attributes.include?(:genre))
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_model_indices
|
59
|
+
book = Book.new
|
60
|
+
assert(book.indices.include?(:author))
|
61
|
+
assert( book.indices.include?(:title))
|
62
|
+
assert( book.indices.include?(:pages))
|
63
|
+
assert( book.indices.include?(:genre))
|
64
|
+
assert( book.indices.include?(:id))
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_model_has_save!
|
68
|
+
book = Book.new
|
69
|
+
assert( book.methods.include?("save!"))
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_resource'
|
3
|
+
|
4
|
+
class UserResource < ActiveResource::Base
|
5
|
+
self.site = "http://example.com/"
|
6
|
+
self.element_name = "user"
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
class RemoteUserTest < Test::Unit::TestCase
|
11
|
+
include ActiveResourceTestHelper
|
12
|
+
active_resource_factories :basic_user
|
13
|
+
|
14
|
+
def setup
|
15
|
+
assert_equal 0, User.count
|
16
|
+
@users = []
|
17
|
+
20.times do |i|
|
18
|
+
@users << Factory.create(:basic_user)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_find_by_id
|
23
|
+
@users.each do |expected_user|
|
24
|
+
user = UserResource.find(expected_user.id)
|
25
|
+
assert_not_nil user
|
26
|
+
assert_equal expected_user.id.to_i, user.id
|
27
|
+
assert_equal expected_user.first_name, user.first_name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_return_404_if_user_does_not_exist
|
32
|
+
assert_raise ActiveResource::ResourceNotFound do
|
33
|
+
UserResource.find(@users.last.id.to_i + 100)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_should_get_all_users
|
38
|
+
current_users = UserResource.find(:all)
|
39
|
+
assert_equal current_users.size, @users.size
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_find_first_should_work
|
43
|
+
user = UserResource.find(:first)
|
44
|
+
assert_equal user.id, @users.first.id.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_find_last_should_work
|
48
|
+
user = UserResource.find(:last)
|
49
|
+
assert_equal user.id, @users.last.id.to_i
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_find_by_param
|
53
|
+
Factory.create(:basic_user, :age => 18, :first_name => "flavio")
|
54
|
+
Factory.create(:basic_user, :age => 28, :first_name => "flavio")
|
55
|
+
|
56
|
+
users = UserResource.find(:all, :params => {:first_name => "flavio"})
|
57
|
+
assert_equal 2, users.size
|
58
|
+
|
59
|
+
users = UserResource.find(:all, :params => {:first_name => "flavio", :age => 18})
|
60
|
+
assert_equal 1, users.size
|
61
|
+
assert_equal "flavio", users.first.first_name
|
62
|
+
assert_equal "18", users.first.age
|
63
|
+
|
64
|
+
users = UserResource.find(:all, :params => {:first_name => "flavio", :age => 68})
|
65
|
+
assert users.empty?
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
User.destroy_all
|
4
|
+
|
5
|
+
100.times do
|
6
|
+
Factory.create(:basic_user)
|
7
|
+
end
|
8
|
+
|
9
|
+
if User.count != 100
|
10
|
+
warn("Error: 100 users should have been created")
|
11
|
+
exit(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
class ActiveResourceTestHelperTest < Test::Unit::TestCase
|
15
|
+
include ActiveResourceTestHelper
|
16
|
+
active_resource_factories :basic_user
|
17
|
+
|
18
|
+
def setup
|
19
|
+
# make sure the User table is empty at the beginning of the test
|
20
|
+
assert_equal 0, User.count
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_create_some_users
|
24
|
+
100.times do
|
25
|
+
Factory.create(:basic_user)
|
26
|
+
end
|
27
|
+
assert_equal 100, User.count
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
Factory.define_active_resource_factory(:basic_user, :class => "User") do |u|
|
4
|
+
u.sequence(:first_name) {|n| "first_name#{n}"}
|
5
|
+
u.sequence(:last_name) {|n| "last_name#{n}"}
|
6
|
+
u.admin false
|
7
|
+
u.email {|u| "#{u.first_name}.#{u.last_name}@example.com" }
|
8
|
+
u.age {rand(30) + 18}
|
9
|
+
#u.association :pet, :factory => :pet
|
10
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Dog < Ohm::Model
|
4
|
+
attribute :name
|
5
|
+
attribute :breed
|
6
|
+
end
|
7
|
+
|
8
|
+
class OhmModelChangesTest < Test::Unit::TestCase
|
9
|
+
def setup
|
10
|
+
Dog.destroy_all
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
Dog.destroy_all
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_to_hash
|
18
|
+
my_dog = Dog.new(:name => "Baguette", :breed => "Dachshund").save
|
19
|
+
|
20
|
+
expected = {:breed=>"Dachshund", :name=>"Baguette", :id=>my_dog.id.to_i}
|
21
|
+
|
22
|
+
assert_equal expected, my_dog.to_hash
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_destroy_all
|
26
|
+
101.times do |i|
|
27
|
+
Dog.new(:name => "puppy #{i}", :breed => "Dalmatian").save
|
28
|
+
end
|
29
|
+
assert_equal 101, Dog.count
|
30
|
+
Dog.destroy_all
|
31
|
+
assert_equal 0, Dog.count
|
32
|
+
end
|
33
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_resource_test_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Flavio Castelli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-05-14 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: factory_girl
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.3
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: redis
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.4
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: ohm
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.0.35
|
44
|
+
version:
|
45
|
+
description: "active_resource_test_helper makes it easier to use ActiveResouce::HttpMock.\n Instead of declaring manually all the request-response pairs,\n it\xE2\x80\x99s possible to use dynamically generated contents.\n These contents are defined using factory_girl and are\n stored into a Redis database using ohm."
|
46
|
+
email: flavio@castelli.name
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README.rdoc
|
53
|
+
files:
|
54
|
+
- LICENSE
|
55
|
+
- README.rdoc
|
56
|
+
- Rakefile
|
57
|
+
- VERSION
|
58
|
+
- lib/active_resource_test_helper.rb
|
59
|
+
- lib/active_resource_test_helper/active_resource/http_mock.rb
|
60
|
+
- lib/active_resource_test_helper/factory.rb
|
61
|
+
- lib/active_resource_test_helper/ohm/model.rb
|
62
|
+
- test/active_resource_factories_test.rb
|
63
|
+
- test/active_resource_http_mock_test.rb
|
64
|
+
- test/active_resource_test_helper_test.rb
|
65
|
+
- test/factories/user_factory.rb
|
66
|
+
- test/ohm_model_changes_test.rb
|
67
|
+
- test/test_helper.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: http://github.com/flavio/active_resource_test_helper
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- --line-numbers
|
75
|
+
- --main
|
76
|
+
- README.rdoc
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
version:
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 1.3.5
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: ActiveResource test helper.
|
98
|
+
test_files:
|
99
|
+
- test/active_resource_test_helper_test.rb
|
100
|
+
- test/ohm_model_changes_test.rb
|
101
|
+
- test/active_resource_factories_test.rb
|
102
|
+
- test/active_resource_http_mock_test.rb
|