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
|
||||
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"
|
||||
|
||||
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
|
||||
|
||||
def seed(*constraints, &block)
|
||||
SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed!
|
||||
def seed(*constraints_and_objects, &block)
|
||||
SeedFuMongoid::DocumentSeeder.new(self, constraints_and_objects, block).seed!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -4,55 +4,94 @@ module SeedFuMongoid
|
||||
|
||||
attr_reader :constraints
|
||||
|
||||
def initialize(klass, constraints, block)
|
||||
@klass, @constraints, @block = klass, constraints, block
|
||||
def initialize(klass, constraints_and_objects, 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?
|
||||
@constraints = [ :id ]
|
||||
end
|
||||
end
|
||||
|
||||
def proxy
|
||||
return @proxy if @proxy
|
||||
class Document
|
||||
def initialize(klass, constraints, data_or_block)
|
||||
@klass, @constraints = klass, constraints
|
||||
|
||||
@proxy = SeedProxy.new
|
||||
|
||||
@block.call(@proxy)
|
||||
|
||||
@proxy
|
||||
end
|
||||
|
||||
def document
|
||||
@doc ||= begin
|
||||
@klass.find_by(constraint_search)
|
||||
rescue => e
|
||||
@klass.new
|
||||
end
|
||||
end
|
||||
|
||||
def constraint_search
|
||||
return @constraint_search if @constraint_search
|
||||
|
||||
@constraint_search = {}
|
||||
constraints.each do |constraint|
|
||||
if proxy[constraint]
|
||||
@constraint_search[constraint] = proxy[constraint]
|
||||
case data_or_block
|
||||
when Hash
|
||||
@data = data_or_block
|
||||
else
|
||||
raise ConstraintNotDefined.new(constraint)
|
||||
@block = data_or_block
|
||||
end
|
||||
end
|
||||
|
||||
@constraint_search
|
||||
def data
|
||||
return @data if @data
|
||||
|
||||
@data = SeedProxy.new
|
||||
|
||||
@block.call(@data)
|
||||
|
||||
@data
|
||||
end
|
||||
|
||||
def document
|
||||
@doc ||= begin
|
||||
@klass.find_by(constraint_search)
|
||||
rescue => e
|
||||
@klass.new
|
||||
end
|
||||
end
|
||||
|
||||
def constraint_search
|
||||
return @constraint_search if @constraint_search
|
||||
|
||||
@constraint_search = {}
|
||||
constraints.each do |constraint|
|
||||
if data[constraint]
|
||||
@constraint_search[constraint] = proxy[constraint]
|
||||
else
|
||||
raise ConstraintNotDefined.new(constraint)
|
||||
end
|
||||
end
|
||||
|
||||
@constraint_search
|
||||
end
|
||||
|
||||
def seed!
|
||||
data.each do |key, value|
|
||||
document[key] = value
|
||||
end
|
||||
|
||||
puts "#{@klass.name} #{document.attributes}"
|
||||
|
||||
document.upsert
|
||||
end
|
||||
end
|
||||
|
||||
def create_document(block_or_object)
|
||||
Document.new(@klass, @constraints, block_or_object)
|
||||
end
|
||||
|
||||
def seed!
|
||||
proxy.each do |key, value|
|
||||
document[key] = value
|
||||
if @objects.empty?
|
||||
create_document(@block).seed!
|
||||
else
|
||||
@objects.each do |object|
|
||||
create_document(object).seed!
|
||||
end
|
||||
end
|
||||
|
||||
puts "#{@klass.name} #{document.attributes}"
|
||||
|
||||
document.upsert
|
||||
end
|
||||
|
||||
def new?
|
||||
|
@ -17,5 +17,12 @@ Gem::Specification.new do |gem|
|
||||
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user