Skip to content

Commit

Permalink
Added mongoid steps for features/pickle_api/example_code
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Aug 28, 2010
1 parent ec6570b commit 7b49bd8
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 33 deletions.
@@ -1,11 +1,10 @@
Feature: Example of the pickle api, testing that it works (in exactly the same way) with different combinations of orm and factory
Feature: Example of the pickle api, testing that it works with different combinations of orm and factory

Scenario Outline:
Given I create an example <ORM> app
And I am using <FACTORY> for generating test data
And I am writing a test using the pickle dsl
And notice the code is the same even though the orm is <ORM> and the factory is <FACTORY>

And I am using <FACTORY> (<ORM>) for generating test data
And I am writing a test using the pickle dsl, with <FACTORY> (<ORM>)

Then I can make and store a user (code):
"""
user = pickle.make_and_store 'a user'
Expand All @@ -32,7 +31,7 @@ Feature: Example of the pickle api, testing that it works (in exactly the same w
pickle.model('the user').should == user
"""

When some other process changes the db (code):
When something happens that changes the db (code):
"""
user.create_welcome_note
"""
Expand All @@ -55,13 +54,15 @@ Feature: Example of the pickle api, testing that it works (in exactly the same w
pickle.model('the note').should == welcome_note
pickle.model('note "welcome note"').should == welcome_note
pickle.model('"welcome note"').should == welcome_note
pickle.model('1st note').should == welcome_note
pickle.model(:factory => 'note').should == welcome_note
pickle.model(:label => 'welcome note').should == welcome_note
pickle.model(:factory => 'note', :index => 0).should == welcome_note
"""

When we create another note manually, we can store it in pickle ourselves (code):
When we create another note, we can store it in pickle ourselves (code):
"""
another_note = user.notes.create!
another_note = user.create_note("another note")
pickle.store another_note
"""

Expand All @@ -79,6 +80,6 @@ Feature: Example of the pickle api, testing that it works (in exactly the same w
| data_mapper | machinist |
| data_mapper | factory_girl |
| data_mapper | orm |
# | mongoid | orm |
# | mongoid | factory_girl |
# | mongoid | machinist |
| mongoid | machinist |
| mongoid | factory_girl |
| mongoid | orm |
6 changes: 5 additions & 1 deletion features/step_definitions/example_active_record_app_steps.rb
Expand Up @@ -26,7 +26,11 @@ class User < ActiveRecord::Base
has_many :notes, :as => :owner
def create_welcome_note
notes.create! :body => "Welcome \#{name}!"
create_note("Welcome \#{name}!")
end
def create_note(body)
notes.create! :body => body
end
end
FILE
Expand Down
23 changes: 13 additions & 10 deletions features/step_definitions/example_app_steps.rb
Expand Up @@ -38,8 +38,13 @@
FILE
end

Given /I am writing a test using the pickle dsl/ do
@test = <<-FILE
Given /I am writing a test using the pickle dsl, with (\w+) \((\w+)\)/ do |factory, orm|
@code = ""

# for some reason to do with mongoid & machinist combo, the following line needs to be first
@code += "require 'rubygems'; require 'machinist/mongoid'\n" if factory == 'machinist' && orm == 'mongoid'

@code += <<-FILE
require 'app'
require 'factory'
require 'spec/expectations'
Expand All @@ -50,15 +55,13 @@
include Pickle::Dsl
include Spec::Matchers
FILE
end

Given /notice the code is the same even though the orm is (\w+) and the factory is (\w+)/ do |orm, factory|
announce "\n\n**\n** Commence test using orm: #{orm} with factory: #{factory}\n**\n"
announce "\n\n**\n** testing using orm: #{orm} with factory: #{factory}\n**\n" if @anncounce_code
end

Then /^(.*) \(code\):$/ do |intention, code|
@test += "\n" + code
create_file 'test.rb', @test
announce "\n# #{intention}\n#{code}\n"
run("ruby test.rb")
announce "\n# #{intention}\n#{code}\n" if @anncounce_code
$stdout.flush
@code += "\n# #{intention}\n#{code}\n"
create_file 'code.rb', @code
run("ruby code.rb")
end
6 changes: 5 additions & 1 deletion features/step_definitions/example_data_mapper_app_steps.rb
Expand Up @@ -16,7 +16,11 @@ class User
has n, :notes, :child_key => [:owner_id]
def create_welcome_note
notes.create! :body => "Welcome \#{name}!"
create_note "Welcome \#{name}!"
end
def create_note(body)
notes.create! :body => body
end
end
FILE
Expand Down
12 changes: 4 additions & 8 deletions features/step_definitions/example_factory_orm_steps.rb
@@ -1,8 +1,8 @@
Given "I am using orm for generating test data" do
Given /^I am using orm \((\w+)\) for generating test data$/ do |orm|
create_file "lib/factory.rb", '# nothing to do for orm as factory adapter'
end

Given "I am using factory_girl for generating test data" do
Given /^I am using factory_girl \((\w+)\) for generating test data$/ do |orm|
create_file "lib/factory.rb", <<-FILE
require 'factory_girl'
Expand All @@ -16,13 +16,9 @@
FILE
end

Given "I am using machinist for generating test data" do
Given /^I am using machinist \((\w+)\) for generating test data$/ do |orm|
create_file "lib/factory.rb", <<-FILE
if defined?(DataMapper)
require 'machinist/data_mapper'
else
require 'machinist/active_record'
end
require 'machinist/#{orm}'
User.blueprint do
name "made by machinist"
Expand Down
42 changes: 42 additions & 0 deletions features/step_definitions/example_mongoid_app_steps.rb
@@ -0,0 +1,42 @@
Given "I create a database setup for the example mongoid app" do
require 'mongo'
pending("Install and start mongodb to run the mongoid features") unless (Mongo::Connection.new.db('pickle_test') rescue nil)
create_file "lib/db.rb", <<-FILE
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db('pickle-test')
end
Mongoid.database.collections.each(&:remove)
FILE
end

Given "I create the example mongoid app models" do
create_file "lib/user.rb", <<-FILE
class User
include Mongoid::Document
field :name, :default => "made by orm"
has_many_related :notes, :foreign_key => :owner_id
def create_welcome_note
create_note("Welcome \#{name}!")
end
def create_note(body)
Note.create :owner_id => id, :body => body
end
end
FILE

create_file "lib/note.rb", <<-FILE
class Note
include Mongoid::Document
field :body, :default => "made by orm"
belongs_to_related :owner, :class_name => 'User'
end
FILE

create_file "lib/models.rb", <<-FILE
require 'user'
require 'note'
FILE
end
16 changes: 14 additions & 2 deletions lib/pickle/orm_adapters/mongoid.rb
Expand Up @@ -27,18 +27,30 @@ def self.get_model(klass, id)

# Find the first instance matching conditions
def self.find_first_model(klass, conditions)
klass.first(:conditions => conditions)
klass.first(:conditions => conditions_to_fields(klass, conditions))
end

# Find all models matching conditions
def self.find_all_models(klass, conditions)
klass.all(:conditions => conditions)
klass.all(:conditions => conditions_to_fields(klass, conditions))
end

# Create a model with given attributes
def self.create_model(klass, attributes)
klass.create!(attributes)
end

protected
# converts and documents to ids
def self.conditions_to_fields(klass, conditions)
conditions.inject({}) do |fields, (key, value)|
if value.is_a?(Mongoid::Document) && klass.fields.keys.include?("#{key}_id")
fields.merge("#{key}_id" => value.id)
else
fields.merge(key => value)
end
end
end
end
end
end

0 comments on commit 7b49bd8

Please sign in to comment.