Skip to content

Commit

Permalink
Added a Cucumber scenerio to test add_link and incorporated appropria…
Browse files Browse the repository at this point in the history
…te Pickle changes
  • Loading branch information
visoft committed Nov 20, 2011
1 parent b863d30 commit 4bc9b89
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
5 changes: 2 additions & 3 deletions features/service_manage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ Scenario: Related entities shouldn't be recreated on a child add
When I run the query
Then the method "Id" on the first result's method "Category" should equal: "1"

@wip
Scenario: Entities should be able to be linked together
Given a category: "cat1" exists with Name: "New Category"
And a product: "prod1" exists with Name: "Cool New Product!"
Given a category: "cat1" exists
And a product: "prod1" exists
When I add a link between category: "cat1" and product: "prod1" on "Products"
And I save changes
Then the product: "prod1" should be one of category: "cat1"'s Products
2 changes: 1 addition & 1 deletion features/step_definitions/pickle_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

# assert model is in another model's has_many assoc
Then(/^#{capture_model} should be (?:in|one of|amongst) #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association|
model!(owner).send(association).should include(model!(target))
model_with_associations(owner).send(association).should include(model!(target))
end

# assert model is not in another model's has_many assoc
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/service_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,5 @@
end

When /^I add a link between #{capture_model} and #{capture_model} on "([^"]*)"$/ do |parent, child, property|
@service.add_link(model!(parent), property, model!(child))
@service.add_link(created_model(parent), property, created_model(child))
end
24 changes: 22 additions & 2 deletions features/support/pickle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ def self.column_names(klass)
end

# Get an instance by id of the model
def self.get_model(klass, id)
def self.get_model(klass, id, expand = false)
collection = klass.to_s.split('::').last.pluralize
@@service.send collection, id
query = @@service.send collection, id

if expand then
# Expand all navigation properties
navigation_properties = klass.properties.select { |k, v| v.nav_prop }
navigation_properties.keys.each do |prop|
query.expand(prop)
end
end

@@service.execute.first
end

Expand Down Expand Up @@ -57,4 +66,15 @@ def self.create_model(klass, attributes)
end

end
end

module Pickle
module Session
# return a newly selected model with the navigation properties expanded
def model_with_associations(name)
model = created_model(name)
return nil unless model
OData::PickleAdapter.get_model(model.class, model.id, true)
end
end
end
6 changes: 6 additions & 0 deletions lib/ruby_odata/class_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ def add_methods(klass)
end
end

# Override equals
klass.send :define_method, :== do |other|
self.class == other.class &&
self.id == other.id &&
self.__metadata == other.__metadata
end
end

def add_nav_props(klass)
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_odata/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def build_save_uri(operation)
uri
end
def build_add_link_uri(operation)
uri = operation.klass.send(:__metadata)[:uri]
uri = "#{operation.klass.send(:__metadata)[:uri]}"
uri << "/$links/#{operation.klass_name}"
uri
end
Expand Down Expand Up @@ -448,7 +448,7 @@ def single_save(operation)
child_collection = operation.klass.send("#{operation.klass_name}") || []
child_collection << operation.child_klass if (post_result.code == 204)
operation.klass.send("#{operation.klass_name}=", child_collection)

# TODO: Attach the parent to the child

return(post_result.code == 204)
Expand Down

0 comments on commit 4bc9b89

Please sign in to comment.