From 2cfb9989e0530313612a0131cf3eee0318049dae Mon Sep 17 00:00:00 2001 From: Iain Hecker Date: Fri, 25 Sep 2009 19:03:02 +0200 Subject: [PATCH] Added specs, but not complete yet --- lib/fill.rb | 23 +++++++--- lib/fill/presenter.rb | 25 ++++++++-- lib/fill/procedure.rb | 19 +++++--- spec/fill_spec.rb | 104 ++++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 40 ++++++++++++++++ 5 files changed, 194 insertions(+), 17 deletions(-) create mode 100644 spec/fill_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/lib/fill.rb b/lib/fill.rb index 7699ec3..4fa7937 100644 --- a/lib/fill.rb +++ b/lib/fill.rb @@ -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 diff --git a/lib/fill/presenter.rb b/lib/fill/presenter.rb index 07e0b6c..8696be7 100644 --- a/lib/fill/presenter.rb +++ b/lib/fill/presenter.rb @@ -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 diff --git a/lib/fill/procedure.rb b/lib/fill/procedure.rb index 3c72500..447eeb6 100644 --- a/lib/fill/procedure.rb +++ b/lib/fill/procedure.rb @@ -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 diff --git a/spec/fill_spec.rb b/spec/fill_spec.rb new file mode 100644 index 0000000..d2ecb68 --- /dev/null +++ b/spec/fill_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..a09230f --- /dev/null +++ b/spec/spec_helper.rb @@ -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