add some tests and the ability to create objects via hashes
This commit is contained in:
parent
f17f299abe
commit
2069b9f1e0
3
Gemfile
3
Gemfile
@ -2,3 +2,6 @@ source 'https://rubygems.org'
|
|||||||
|
|
||||||
# Specify your gem's dependencies in seed-fu-mongoid.gemspec
|
# Specify your gem's dependencies in seed-fu-mongoid.gemspec
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
|
gem 'guard-cucumber', :git => 'git://github.com/johnbintz/guard-cucumber', :branch => 'paths_from_profile'
|
||||||
|
|
||||||
|
10
Guardfile
Normal file
10
Guardfile
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# added by cuke-pack
|
||||||
|
|
||||||
|
group :wip do
|
||||||
|
guard 'cucumber', :env => :cucumber, :paths_from_profile => true, :cli => '--color -p wip' do
|
||||||
|
watch(%r{^features/.+.feature$})
|
||||||
|
watch(%r{^(app|lib).*}) { 'features' }
|
||||||
|
watch(%r{^features/support/.+$}) { 'features' }
|
||||||
|
watch(%r{^features/step_definitions/(.+).rb$}) { 'features' }
|
||||||
|
end
|
||||||
|
end
|
5
Rakefile
5
Rakefile
@ -1 +1,6 @@
|
|||||||
require "bundler/gem_tasks"
|
require "bundler/gem_tasks"
|
||||||
|
|
||||||
|
task :default do
|
||||||
|
system 'bundle exec cucumber'
|
||||||
|
exit $?.exitstatus if $?.exitstatus != 0
|
||||||
|
end
|
||||||
|
2
config/cucumber.yml
Normal file
2
config/cucumber.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<%= require 'cuke-pack/profiles' ; CukePack::Profiles.write %>
|
||||||
|
|
6
config/mongoid.yml
Normal file
6
config/mongoid.yml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
test:
|
||||||
|
sessions:
|
||||||
|
default:
|
||||||
|
database: seed_fu_mongoid_test
|
||||||
|
hosts:
|
||||||
|
- localhost:27017
|
4
features/seed_from_block.feature
Normal file
4
features/seed_from_block.feature
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Feature: Seed From Block
|
||||||
|
Scenario: Seed new records from block
|
||||||
|
When I seed records from a block
|
||||||
|
Then I should have the new records
|
9
features/seed_from_hash.feature
Normal file
9
features/seed_from_hash.feature
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
Feature: Seed From Hash
|
||||||
|
Scenario: Seed new records from hash
|
||||||
|
When I seed records from a hash
|
||||||
|
Then I should have the new records
|
||||||
|
|
||||||
|
Scenario: Update existing records from a hash
|
||||||
|
Given I have an existing record
|
||||||
|
When I seed records from a hash
|
||||||
|
Then I should have the new records
|
@ -0,0 +1,6 @@
|
|||||||
|
Given(/^I have an existing record$/) do
|
||||||
|
record = Record.new
|
||||||
|
record[:_id] = 1
|
||||||
|
record[:data] = 'other'
|
||||||
|
record.upsert
|
||||||
|
end
|
@ -0,0 +1,4 @@
|
|||||||
|
Then(/^I should have the new records$/) do
|
||||||
|
Record.count.should == 1
|
||||||
|
Record.find(1).data.should == 'data'
|
||||||
|
end
|
@ -0,0 +1,6 @@
|
|||||||
|
When(/^I seed records from a block$/) do
|
||||||
|
Record.seed :id do |s|
|
||||||
|
s.id = 1
|
||||||
|
s.data = 'data'
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,4 @@
|
|||||||
|
When(/^I seed records from a hash$/) do
|
||||||
|
Record.seed :id,
|
||||||
|
{ :id => 1, :data => 'data' }
|
||||||
|
end
|
6
features/support/cuke-pack.rb
Normal file
6
features/support/cuke-pack.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# write out missing steps automatically
|
||||||
|
require 'cuke-pack/support/step_writer'
|
||||||
|
|
||||||
|
# use advanced in_progress mode
|
||||||
|
require 'cuke-pack/support/in_progress'
|
||||||
|
|
25
features/support/env.rb
Normal file
25
features/support/env.rb
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
require 'database_cleaner'
|
||||||
|
require 'mongoid'
|
||||||
|
|
||||||
|
Mongoid.load!('config/mongoid.yml', :test)
|
||||||
|
|
||||||
|
DatabaseCleaner[:mongoid].strategy = :truncation
|
||||||
|
|
||||||
|
Before do
|
||||||
|
DatabaseCleaner[:mongoid].start
|
||||||
|
end
|
||||||
|
|
||||||
|
After do
|
||||||
|
DatabaseCleaner[:mongoid].clean
|
||||||
|
end
|
||||||
|
|
||||||
|
$: << File.expand_path('lib')
|
||||||
|
|
||||||
|
require 'seed-fu-mongoid'
|
||||||
|
|
||||||
|
class Record
|
||||||
|
include Mongoid::Document
|
||||||
|
|
||||||
|
field :data, type: String
|
||||||
|
end
|
||||||
|
|
@ -31,8 +31,8 @@ module Mongoid::Document
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def seed(*constraints, &block)
|
def seed(*constraints_and_objects, &block)
|
||||||
SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed!
|
SeedFuMongoid::DocumentSeeder.new(self, constraints_and_objects, block).seed!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,22 +4,46 @@ module SeedFuMongoid
|
|||||||
|
|
||||||
attr_reader :constraints
|
attr_reader :constraints
|
||||||
|
|
||||||
def initialize(klass, constraints, block)
|
def initialize(klass, constraints_and_objects, block)
|
||||||
@klass, @constraints, @block = klass, constraints, block
|
@klass, @block = klass, block
|
||||||
|
|
||||||
|
@constraints = []
|
||||||
|
@objects = []
|
||||||
|
|
||||||
|
constraints_and_objects.each do |constraint_or_object|
|
||||||
|
case constraint_or_object
|
||||||
|
when Symbol
|
||||||
|
@constraints << constraint_or_object
|
||||||
|
when Hash
|
||||||
|
@objects << constraint_or_object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if @constraints.empty?
|
if @constraints.empty?
|
||||||
@constraints = [ :id ]
|
@constraints = [ :id ]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def proxy
|
class Document
|
||||||
return @proxy if @proxy
|
def initialize(klass, constraints, data_or_block)
|
||||||
|
@klass, @constraints = klass, constraints
|
||||||
|
|
||||||
@proxy = SeedProxy.new
|
case data_or_block
|
||||||
|
when Hash
|
||||||
|
@data = data_or_block
|
||||||
|
else
|
||||||
|
@block = data_or_block
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@block.call(@proxy)
|
def data
|
||||||
|
return @data if @data
|
||||||
|
|
||||||
@proxy
|
@data = SeedProxy.new
|
||||||
|
|
||||||
|
@block.call(@data)
|
||||||
|
|
||||||
|
@data
|
||||||
end
|
end
|
||||||
|
|
||||||
def document
|
def document
|
||||||
@ -35,7 +59,7 @@ module SeedFuMongoid
|
|||||||
|
|
||||||
@constraint_search = {}
|
@constraint_search = {}
|
||||||
constraints.each do |constraint|
|
constraints.each do |constraint|
|
||||||
if proxy[constraint]
|
if data[constraint]
|
||||||
@constraint_search[constraint] = proxy[constraint]
|
@constraint_search[constraint] = proxy[constraint]
|
||||||
else
|
else
|
||||||
raise ConstraintNotDefined.new(constraint)
|
raise ConstraintNotDefined.new(constraint)
|
||||||
@ -46,7 +70,7 @@ module SeedFuMongoid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def seed!
|
def seed!
|
||||||
proxy.each do |key, value|
|
data.each do |key, value|
|
||||||
document[key] = value
|
document[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -54,6 +78,21 @@ module SeedFuMongoid
|
|||||||
|
|
||||||
document.upsert
|
document.upsert
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_document(block_or_object)
|
||||||
|
Document.new(@klass, @constraints, block_or_object)
|
||||||
|
end
|
||||||
|
|
||||||
|
def seed!
|
||||||
|
if @objects.empty?
|
||||||
|
create_document(@block).seed!
|
||||||
|
else
|
||||||
|
@objects.each do |object|
|
||||||
|
create_document(object).seed!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def new?
|
def new?
|
||||||
!document.persisted?
|
!document.persisted?
|
||||||
|
@ -17,5 +17,12 @@ Gem::Specification.new do |gem|
|
|||||||
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
||||||
gem.require_paths = ["lib"]
|
gem.require_paths = ["lib"]
|
||||||
|
|
||||||
gem.add_dependency 'mongoid'
|
gem.add_dependency 'mongoid', '>= 3.0.0'
|
||||||
|
gem.add_development_dependency 'cucumber'
|
||||||
|
gem.add_development_dependency 'rspec'
|
||||||
|
gem.add_development_dependency 'database_cleaner'
|
||||||
|
gem.add_development_dependency 'cuke-pack'
|
||||||
|
gem.add_development_dependency 'guard'
|
||||||
|
gem.add_development_dependency 'rb-fsevent'
|
||||||
|
gem.add_development_dependency 'guard-cucumber'
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user