Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds some specs for loading collections

  • Loading branch information...
commit b83cc309f5b3ed190c25a99711e5502c3557a223 1 parent 2c548c0
@hassox hassox authored
View
22 merb-rest-client/lib/merb_rest_adapter.rb
@@ -37,22 +37,30 @@ def create(resources)
# created
end
- # def read_one(query)
- # end
+ def read_one(query)
+ query.limit = 1
+ read_many(query).first
+ end
def read_many(query)
resource = resource_name(query)
Collection.new(query) do |collection|
parameters = condition_parameters(query.conditions)
- parameters.merge!(order_parameters(query.order))
- parameters.merge!(field_parameters(query.fields))
+ parameters.merge!(order_parameters(query.order)) unless query.order.blank?
+ parameters.merge!("fields" => query.fields.map{|f| f.name.to_s}) unless query.fields.blank?
parameters.merge!("limit" => query.limit) if query.limit
parameters.merge!("offset" => query.offset) if query.offset && query.offset > 1
+ parameters.merge!("unique" => query.unique?) if query.unique?
result = api_get(resource_name(query).to_s, parameters)
- hash = parse_results(result.body)
-
- collection.load(hash)
+ values_array =[]
+ fields = query.fields.map{|f| f.name.to_s}
+ results_array = parse_results(result.body).map do |result|
+ fields.map{|f| result[f]}
+ end
+ results_array.each do |result|
+ collection.load(result)
+ end
end
end
View
39 merb-rest-client/spec/merb_rest_adapter_spec.rb
@@ -69,7 +69,7 @@ def self.default_repository_name
describe "read_many" do
before(:all) do
- @hash = [{"title" => "title", "body" => "body", "id" => 3}]
+ @hash = [{"title" => "title", "body" => "body", "id" => 3},{"title" => "another title", "body" => "another body", "id" => 42}]
@json = JSON.generate(@hash)
end
@@ -83,12 +83,12 @@ def self.default_repository_name
it{@adapter.should respond_to(:read_many)}
it "should send a get request to the Post resource" do
- @adapter.should_receive(:api_get).with("posts", {"order" => ["id.asc"], "fields" => [:id, :title, :body]}).and_return(@response)
+ @adapter.should_receive(:api_get).with("posts", {"order" => ["id.asc"], "fields" => ["id", "title", "body"]}).and_return(@response)
Post.all.inspect
end
it "should return instantiated objects" do
- @adapter.should_receive(:api_get).with("posts", {"order" => ["id.asc"], "fields" => [:id, :title, :body]}).and_return(@response)
+ @adapter.should_receive(:api_get).with("posts", {"order" => ["id.asc"], "fields" => ["id", "title", "body"]}).and_return(@response)
@adapter.should_receive(:parse_results).and_return(@hash)
Post.all.inspect
end
@@ -97,13 +97,31 @@ def self.default_repository_name
Post.all.each{|p| p.should be_a_kind_of(Post)}
end
+ it "should load the objects correctly" do
+ post = Post.all.map{|p| p}.first
+ post.title.should == "title"
+ post.body.should == "body"
+ post.id.should == 3
+ end
+
+ it "Should load all the objects correctly" do
+ posts = Post.all(:order => [:id.asc]).map{|p| p}
+ posts.should have(2).items
+ posts[0].id.should == 3
+ posts[0].title.should == "title"
+ posts[0].body.should == "body"
+ posts[1].id.should == 42
+ posts[1].title.should == "another title"
+ posts[1].body.should == "another body"
+ end
+
describe "read many with conditions" do
it "should use a get with conditional parameters" do
@adapter.should_receive(:api_get).with("posts", { "title.like" => "tit%",
"body.eql" => "body",
"order" => ["id.asc"],
- "fields" => [:id, :title, :body]
+ "fields" => ["id", "title", "body"]
}).and_return(@response)
Post.all(:title.like => "tit%", :body.eql => "body").each{}
end
@@ -111,24 +129,31 @@ def self.default_repository_name
it "should add a fields option for fields" do
@adapter.should_receive(:api_get).with("posts", "title.like" => "tit%",
"order" => ["id.asc"],
- "fields" => [:id, :body]).and_return(@response)
+ "fields" => ["id", "body"]).and_return(@response)
Post.all(:title.like => "tit%", :fields => [:id, :body]).each{}
end
it "should add the options for limit" do
@adapter.should_receive(:api_get).with("posts", "title.like" => "tit%",
"order" => ["id.asc"],
- "fields" => [:id, :title, :body],
+ "fields" => ["id", "title", "body"],
"limit" => 5).and_return(@response)
Post.all(:title.like => "tit%", :limit => 5).each{}
end
it "should allow for options with offset" do
@adapter.should_receive(:api_get).with("posts", "order" => ["id.asc"],
- "fields" => [:id, :title, :body],
+ "fields" => ["id", "title", "body"],
"offset" => 23).and_return(@response)
Post.all(:offset => 23).each{}
end
+
+ it "should allow for unique flag" do
+ @adapter.should_receive(:api_get).with("posts", "order" => ["id.asc"],
+ "fields" => ["id", "title", "body"],
+ "unique" => true).and_return(@response)
+ Post.all(:unique => true).each{}
+ end
end
describe "load many with associations" do
Please sign in to comment.
Something went wrong with that request. Please try again.