Permalink
Browse files

Added specs, but not complete yet

  • Loading branch information...
1 parent 522f5ae commit 2cfb9989e0530313612a0131cf3eee0318049dae @iain committed Sep 25, 2009
Showing with 194 additions and 17 deletions.
  1. +17 −6 lib/fill.rb
  2. +20 −5 lib/fill/presenter.rb
  3. +13 −6 lib/fill/procedure.rb
  4. +104 −0 spec/fill_spec.rb
  5. +40 −0 spec/spec_helper.rb
View
@@ -1,18 +1,29 @@
require 'activesupport'
-require 'fill/configure'
-require 'fill/presenter'
-require 'fill/procedure'
+require File.dirname(__FILE__) + '/fill/configure'
+require File.dirname(__FILE__) + '/fill/presenter'
+require File.dirname(__FILE__) + '/fill/procedure'
module Fill
class << self
+
+ attr_writer :out
+ def out
+ @out ||= STDOUT
+ end
+
def database
db = Configure.new
yield db
- bm = time { db.perform! }
- puts Presenter
- puts "Database filled in %.2f seconds" % bm
+ perform!(db)
+ end
+
+ def perform!(configuration)
+ bm = time { configuration.perform! }
+ out.puts Presenter
+ out.puts "Database filled in %.2f seconds" % bm
+ Presenter.clear!
end
def time
View
@@ -14,15 +14,23 @@ def self.to_s
presenter.to_s
end
+ def self.hirb?
+ require 'hirb'
+ true
+ rescue LoadError
+ false
+ end
+
+ def self.clear!
+ @presenter = nil
+ end
+
def add(data)
presented.push(data) if data && !presented.include?(data)
end
def hirb?
- require 'hirb'
- true
- rescue LoadError
- false
+ self.class.hirb?
end
def presented
@@ -43,7 +51,14 @@ def to_s
def present_hash
presentable.map do |row|
- row.map { |key, value| "#{key}: #{value}" }.join(" - ")
+ format_row(row).join(" - ")
+ end
+ end
+
+ def format_row(row)
+ row.map do |key, value|
+ value = "%.2f" % value if key == "Time"
+ "#{key}: #{value}"
end
end
View
@@ -2,7 +2,7 @@ module Fill
class Procedure
- attr_accessor :block
+ attr_accessor :block, :options
def initialize(models, options = {}, &block)
@block = block
@@ -19,12 +19,19 @@ def to_hash
end
def human_models
- @options[:name] ||
- models.map { |model| model.respond_to?(:human_name) ? model.human_name : model.to_s }.join(', ')
+ @human_models ||= (options[:name] || humanize_models)
+ end
+
+ def humanize_models
+ models.map { |model| i18n_name(model) }.join(', ')
+ end
+
+ def i18n_name(model)
+ model.respond_to?(:human_name) ? model.human_name : model.to_s
end
def delete_all
- models.each { |model| model.delete_all } if @options[:delete]
+ models.map { |model| model.delete_all }
end
def models
@@ -36,8 +43,8 @@ def count
end
def perform
- @before = count
- @time = Fill.time { self.delete_all; block.call }
+ @before = options[:delete] ? delete_all : count
+ @time = Fill.time { block.call }
@after = count
Presenter.present self
true
View
@@ -0,0 +1,104 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe Fill do
+
+ before do
+ Fill.out = Output
+ Output.output = []
+ end
+
+ describe "the the internal workings of the produce method" do
+
+ it "should perform the content only after the block is closed" do
+ # This is so that all dependencies can be initialized
+ # before anything is executed.
+ lambda {
+ Fill.database do |db|
+ lambda {
+ db.produce :projects do
+ throw :fill_executed
+ end
+ }.should_not throw_symbol(:fill_executed)
+ end
+ }.should throw_symbol(:fill_executed)
+ end
+
+ it "should count the records" do
+ mock(Project).count
+ produce!
+ end
+
+ it "should delete all the records" do
+ mock(Project).delete_all
+ produce!
+ end
+
+ it "should get the human name of a model" do
+ mock(Project).human_name
+ produce!
+ end
+
+ it "should report the total time" do
+ produce!
+ Output.output.last.should =~ /\ADatabase filled in (.*) seconds\Z/
+ end
+
+ context "without hirb" do
+
+ before { mock(Fill::Presenter).hirb? { false } }
+
+ describe "output per model" do
+
+ before do
+ stub(Project).human_name { "FOOBAR" }
+ produce!
+ end
+
+ subject { Output.output.first }
+
+ it { should =~ /Time: ([0-9.]+)/ }
+ it { should include("Before: 1") }
+ it { should include("After: 2") }
+ it { should include("Models: FOOBAR") }
+
+ end
+
+ it "should count the time of the block" do
+
+ # Fill itself shouldn't take up more than 1 second
+ # so this test should return just about 1 second
+ # I _am_ sorry about this crude example ;)
+
+ produce! { sleep 1 } # zzzzzzzzzz
+
+ Output.output.first =~ /Time: ([0-9.]+)/
+ $1.to_i.should == 1
+
+ # Test total time, not in a seperate example to shorten testrun
+ Output.output.last =~ /([0-9.]+) seconds/
+ $1.to_i.should == 1
+ end
+
+ end
+
+ it "should use hirb when possible" do
+ out = "---HIRB OUTPUT---"
+ mock(Fill::Presenter).hirb? { true }
+ mock(Hirb::Helpers::Table).render(is_a(Array), :description => false) { out }
+ produce!
+ Output.output.first.should == out
+ end
+
+ end
+
+ describe "usage of paramaters" do
+
+ it "should accept multiple parameters" do
+ mock(Project).delete_all
+ mock(User).delete_all
+ produce! :projects, :users
+ end
+
+ end
+
+end
View
@@ -0,0 +1,40 @@
+require File.dirname(__FILE__) + '/../lib/fill'
+
+require 'spec'
+Spec::Runner.configure do |config|
+ config.mock_with :rr
+end
+
+def produce!(*args)
+ args = [:projects] if args.empty?
+ Fill.database do |db|
+ db.produce *args do
+ yield if block_given?
+ end
+ end
+end
+
+class ActiveRecordMimic
+ class << self
+ def delete_all; 1; end
+ def count; 2; end
+ def human_name; self.inspect; end
+ def create!; end
+ end
+end
+
+class Output
+ class << self
+ attr_accessor :output
+ def puts(string)
+ @output ||= []
+ @output << string.to_s
+ end
+ end
+end
+
+class User < ActiveRecordMimic; end
+
+class Project < ActiveRecordMimic; end
+
+class Membership < ActiveRecordMimic; end

0 comments on commit 2cfb998

Please sign in to comment.