Browse files

Specs are complete now

  • Loading branch information...
1 parent 2cfb998 commit c47842c6d64ffde0552c91bfcdeddc0835f7ee9d @iain committed Sep 25, 2009
Showing with 185 additions and 27 deletions.
  1. +20 −9 Rakefile
  2. +1 −0 VERSION
  3. +2 −0 lib/fill.rb
  4. +154 −9 spec/fill_spec.rb
  5. +4 −0 spec/spec.opts
  6. +4 −9 spec/spec_helper.rb
View
29 Rakefile
@@ -1,13 +1,24 @@
require 'rake'
require 'rake/rdoctask'
-require 'echoe'
-Echoe.new('fill', '0.0.1') do |p|
- p.description = "Fill your database"
- p.url = "http://iain.nl"
- p.author = "iain"
- p.email = "iain@iain.nl"
- p.ignore_pattern = []
- p.development_dependencies = []
- p.runtime_dependencies = []
+require 'spec/rake/spectask'
+desc "Run all specs in spec directory"
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_opts = ['--options spec/spec.opts']
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+
+begin
+ require 'echoe'
+ Echoe.new('fill', File.read('VERSION').chomp) do |p|
+ p.description = "Fill your database"
+ p.url = "http://iain.nl"
+ p.author = "iain"
+ p.email = "iain@iain.nl"
+ p.ignore_pattern = []
+ p.development_dependencies = []
+ p.runtime_dependencies = []
+ end
+rescue LoadError
end
View
1 VERSION
@@ -0,0 +1 @@
+0.0.2
View
2 lib/fill.rb
@@ -5,6 +5,8 @@
module Fill
+ VERSION = File.read(File.dirname(__FILE__) + '/../VERSION').chomp
+
class << self
View
163 spec/fill_spec.rb
@@ -3,11 +3,10 @@
describe Fill do
before do
- Fill.out = Output
Output.output = []
end
- describe "the the internal workings of the produce method" do
+ describe "the the internal workings of db.produce" do
it "should perform the content only after the block is closed" do
# This is so that all dependencies can be initialized
@@ -51,14 +50,16 @@
before do
stub(Project).human_name { "FOOBAR" }
+ stub(Project).delete_all { 123456789 }
+ stub(Project).count { 987654321 }
produce!
end
subject { Output.output.first }
it { should =~ /Time: ([0-9.]+)/ }
- it { should include("Before: 1") }
- it { should include("After: 2") }
+ it { should include("Before: 123456789") }
+ it { should include("After: 987654321") }
it { should include("Models: FOOBAR") }
end
@@ -91,12 +92,156 @@
end
- describe "usage of paramaters" do
+ describe "arguments for db.produce" do
+
+ describe "multiple models" do
+
+ after do
+ produce! :projects, :users, :memberships do
+ User.create
+ end
+ end
+
+ it "calls delete_all on all models" do
+ mock(Project).delete_all
+ mock(User).delete_all
+ mock(Membership).delete_all
+ end
+
+ it "calls count on all models" do
+ mock(Project).count
+ mock(User).count
+ mock(Membership).count
+ end
+
+ it "calls human_name on all models" do
+ mock(Project).human_name
+ mock(User).human_name
+ mock(Membership).human_name
+ end
+
+ it "should only execute the procedure once" do
+ mock(User).create.times(1)
+ end
+
+ end
+
+ describe "dependencies" do
+
+ it "should run dependencies first" do
+ # This works because the dont_allow should be executed AFTER
+ # the call was actually done! Similarly, the mock should be
+ # defined BEFORE it was done.
+ Fill.database do |db|
+ db.produce :users, :needs => :projects do
+ dont_allow(Project).create
+ User.create
+ end
+ db.produce :projects do
+ Project.create
+ mock(User).create
+ end
+ end
+ end
+
+ it "should execute once, even with multiple dependencies" do
+ mock(User).create.times(1)
+ Fill.database do |db|
+ db.produce(:memberships, :needs => :users) {}
+ db.produce(:projects, :needs => :users) {}
+ db.produce(:users) { User.create }
+ end
+ end
+
+ it "should handle multiple dependencies" do
+ Fill.database do |db|
+ db.produce(:memberships, :needs => [:users, :projects]) do
+ # Again, don't allow, because these should already have
+ # been executed before this block is executed.
+ dont_allow(Project).create
+ dont_allow(User).create
+ Membership.create # twice because mocked twice
+ Membership.create
+ end
+ db.produce(:projects, :needs => :users) do
+ Project.create
+ mock(Membership).create
+ dont_allow(User).create
+ end
+ db.produce(:users) do
+ User.create
+ mock(Project).create
+ mock(Membership).create
+ end
+ end
+ end
+
+ context "without hirb" do
+
+ before { stub(Fill::Presenter).hirb? { false } }
+
+ it "should join model names" do
+ mock(Project).human_name { "Projects" }
+ mock(User).human_name { "Accounts" }
+ produce! :projects, :users
+ Output.output.first.should include("Models: Projects, Accounts")
+ end
+
+ it "should join before counts" do
+ mock(Project).delete_all { 1234 }
+ mock(User).delete_all { 4321 }
+ produce! :projects, :users
+ Output.output.first.should include("Before: 1234, 4321")
+ end
+
+ it "should join before counts" do
+ mock(Project).count { 789 }
+ mock(User).count { 456 }
+ produce! :projects, :users
+ Output.output.first.should include("After: 789, 456")
+ end
+
+ it "should add up times" do
+ produce! :projects, :users
+ Output.output.first.should =~ /Time: [0-9.][^,]/ # no comma, no join(',')
+ end
+
+ end
+
+ end
+
+ describe "names" do
+
+ before { stub(Fill::Presenter).hirb? { false } }
+
+ it "should override name" do
+ name = ":::MYNAME:::"
+ dont_allow(Project).human_name
+ produce! :projects, :name => name
+ Output.output.first.should include("Models: #{name}")
+ end
+
+ it "should override name with multiple models" do
+ name = ":::FOOBAR:::"
+ dont_allow(Project).human_name
+ dont_allow(User).human_name
+ dont_allow(Membership).human_name
+ produce! :projects, :users, :memberships, :name => name
+ Output.output.first.should include("Models: #{name}")
+ end
+
+ end
+
+ describe "delete option" do
+
+ it "should not delete with delete option to false" do
+ dont_allow(Project).delete_all
+ dont_allow(User).delete_all
+ mock(Project).count.times(2) # before and after
+ mock(User).count.times(2) # before and after
+ produce! :users, :projects, :delete => false
+ end
- it "should accept multiple parameters" do
- mock(Project).delete_all
- mock(User).delete_all
- produce! :projects, :users
end
end
View
4 spec/spec.opts
@@ -0,0 +1,4 @@
+--colour
+--format progress
+--loadby mtime
+--reverse
View
13 spec/spec_helper.rb
@@ -1,6 +1,5 @@
require File.dirname(__FILE__) + '/../lib/fill'
-require 'spec'
Spec::Runner.configure do |config|
config.mock_with :rr
end
@@ -16,10 +15,7 @@ def produce!(*args)
class ActiveRecordMimic
class << self
- def delete_all; 1; end
- def count; 2; end
- def human_name; self.inspect; end
- def create!; end
+ attr_reader :delete_all, :count, :create, :human_name
end
end
@@ -32,9 +28,8 @@ def puts(string)
end
end
end
+Fill.out = Output
-class User < ActiveRecordMimic; end
-
-class Project < ActiveRecordMimic; end
-
+class User < ActiveRecordMimic; end
+class Project < ActiveRecordMimic; end
class Membership < ActiveRecordMimic; end

0 comments on commit c47842c

Please sign in to comment.