features/step_definitions/document_steps.rb in mongodoc-0.1.2 vs features/step_definitions/document_steps.rb in mongodoc-0.2.0
- old
+ new
@@ -43,17 +43,30 @@
Given /^I set the id on the document '(.*)' to (.*)$/ do |doc_name, value|
doc = instance_variable_get("@#{doc_name}")
doc._id = Mongo::ObjectID.new([value.to_i])
end
+Given /^'(.+)' has one (.+?) as (.+?) \(identified by '(.+)'\):$/ do |doc_name, class_name, assoc_name, var_name, table|
+ doc = instance_variable_get("@#{doc_name}")
+ obj = class_name.constantize.new
+ table.hashes.each do |hash|
+ hash.each do |key, value|
+ obj.send("#{key.underscore.gsub(' ', '_')}=", value)
+ end
+ end
+ instance_variable_set("@#{var_name}", obj)
+ doc.send("#{assoc_name.underscore.gsub(' ', '_')}=", obj)
+ @last = obj
+end
+
When /^I save the document '(.*)'$/ do |name|
object = instance_variable_get("@#{name}")
- @last_return = object.save
+ @last_return = object.save
end
When /^I save the last document$/ do
- @last_return = @last.save
+ @last_return = @last.save
end
When /^I create an (.*) '(.*)' from the hash '(.*)'$/ do |doc, name, hash|
klass = doc.constantize
attrs = instance_variable_get("@#{hash}")
@@ -61,13 +74,24 @@
end
When /^I update the document '(.*)' with the hash named '(.*)'$/ do |doc_name, hash_name|
doc = instance_variable_get("@#{doc_name}")
attrs = instance_variable_get("@#{hash_name}")
- @last_return = doc.update_attributes(attrs)
+ @last_return = doc.update_attributes(attrs)
end
+When /^I query (.*) with find (.*)$/ do |doc, criteria_text|
+ klass = doc.singularize.camelize.constantize
+ criteria = eval(criteria_text)
+ @last_return = klass.find(:all, criteria)
+end
+
+When /^'(.+)' is the first (.+?) of '(.+)'$/ do |var_name, single_assoc, doc_name|
+ doc = instance_variable_get("@#{doc_name}")
+ instance_variable_set("@#{var_name}", doc.send(single_assoc.pluralize).first)
+end
+
Then /^'(.*)' is not a new record$/ do |name|
instance_variable_get("@#{name}").new_record?.should be_false
end
Then /the (.*) collection should have (\d+) documents?/ do |doc, count|
@@ -80,8 +104,35 @@
from_db = object.class.find_one(object._id)
from_db.should == object
instance_variable_set("@#{name}", from_db)
end
-Then /^the last return value is false$/ do
- @last_return.should be_false
+Then /^the document '(.+)' does not roundtrip$/ do |name|
+ object = instance_variable_get("@#{name}")
+ from_db = object.class.find_one(object._id)
+ from_db.should_not == object
+end
+
+Then /^the last return value is (.+)$/ do |bool_val|
+ @last_return.should send("be_#{bool_val}")
+end
+
+Then /^the first (.*) of '(.*)' is not a new record$/ do |assoc, name|
+ object = instance_variable_get("@#{name}")
+ plural = assoc.pluralize
+ object.send(plural).first.should_not be_new_record
+end
+
+Then /^the (\w*) of '(.*)' is not a new record$/ do |assoc, name|
+ object = instance_variable_get("@#{name}")
+ object.send(assoc).should_not be_new_record
+end
+
+Then /^the (\w*) of '(.*)' roundtrips$/ do |assoc, name|
+ object = instance_variable_get("@#{name}")
+ from_db = object.class.find_one(object._id)
+ object.send(assoc).id.should == from_db.send(assoc).id
+end
+
+Then /^the size of the last return value is (.*)$/ do |count|
+ @last_return.size.should == count.to_i
end