Skip to content

Commit

Permalink
faker specs were targeting factory classes not instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip (flip) Kromer committed Jun 18, 2012
1 parent e1a9f25 commit e42ac26
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 50 deletions.
28 changes: 23 additions & 5 deletions examples/dataflow/parse_apache_logs.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path("../lib", File.realdirpath(File.dirname(__FILE__)))) $LOAD_PATH.unshift(File.expand_path("../lib", File.realdirpath(File.dirname(__FILE__))))

require 'wukong' require 'wukong'


Settings.use(:commandline) Settings.use(:commandline)
Expand All @@ -15,15 +14,34 @@
Parses an apache log line into a structured model, emits it as JSON Parses an apache log line into a structured model, emits it as JSON
DOC DOC


input :default, stdin # file_source(Pathname.path_to(:data, 'log/sample_apache_log.log')) source = ($0 == __FILE__) ? stdin : file_source(Pathname.path_to(:data, 'log/sample_apache_log.log'))
input :default, source
output :dump, stdout output :dump, stdout


input(:default) > input(:default) >
map{|line| ApacheLogLine.make(line) or bad_record(line) } > map{|line| ApacheLogLine.make(line) or bad_record(line) } >
# to_json >
to_tsv > to_tsv >
output(:dump) output(:dump)

end end


Wukong::LocalRunner.run(Wukong.dataflow(:parse_apache_logs), :default) if ($0 == __FILE__)
flow_name = :parse_apache_logs
if Settings.profiler
require 'perftools'
Pathname(Settings.profiler).dirname.mkpath
PerfTools::CpuProfiler.start(Settings.profiler) do
Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
end
else
Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
end

# require 'jruby/profiler'
# profile_data = JRuby::Profiler.profile do
# Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
# end
# profile_printer = JRuby::Profiler::GraphProfilePrinter.new(profile_data)
# profile_printer.printProfile($stderr)

# Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
end
36 changes: 18 additions & 18 deletions lib/wukong/model/faker.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -110,27 +110,27 @@ def fake_value(opts={})
end end
end end


def StringFactory.fake_value() ; receive(Wukong::Faker::Helpers.fake_word) ; end StringFactory.class_eval{ def fake_value() ; receive(Wukong::Faker::Helpers.fake_word) ; end }
def GuidFactory.fake_value() ; receive(UUIDTools::UUID.random_create.to_s) ; end GuidFactory.class_eval{ def fake_value() ; receive(UUIDTools::UUID.random_create.to_s) ; end }
def HostnameFactory.fake_value() ; receive(Wukong::Faker::Helpers.fake_hostname) ; end HostnameFactory.class_eval{ def fake_value() ; receive(Wukong::Faker::Helpers.fake_hostname) ; end }
def IpAddressFactory.fake_value() ; receive(Wukong::Faker::Helpers.fake_ip_addresss) ; end IpAddressFactory.class_eval{ def fake_value() ; receive(Wukong::Faker::Helpers.fake_ip_addresss) ; end }


def SymbolFactory.fake_value() ; receive(Wukong::Faker::Helpers.fake_identifier) end SymbolFactory.class_eval{ def fake_value() ; receive(Wukong::Faker::Helpers.fake_identifier) end }
def PathnameFactory.fake_value() ; receive(Wukong::Faker::Helpers.fake_filename) end PathnameFactory.class_eval{ def fake_value() ; receive(Wukong::Faker::Helpers.fake_filename) end }


def IntegerFactory.fake_value(opts={}) ; receive(Wukong::Faker::Helpers.fake_integer(opts)) ; end IntegerFactory.class_eval{ def fake_value(opts={}) ; receive(Wukong::Faker::Helpers.fake_integer(opts)) ; end }
def BignumFactory.fake_value(opts={}) ; super({:min => 2**68, :max => 2**90}.merge(opts)) ; end BignumFactory.class_eval{ def fake_value(opts={}) ; super({:min => 2**68, :max => 2**90}.merge(opts)) ; end }


def FloatFactory.fake_value(opts={}) ; receive(Wukong::Faker::Helpers.fake_float(opts)) ; end FloatFactory.class_eval{ def fake_value(opts={}) ; receive(Wukong::Faker::Helpers.fake_float(opts)) ; end }
def ComplexFactory.fake_value() ; receive(Kernel.rand, Kernel.rand) ; end ComplexFactory.class_eval{ def fake_value() ; receive(Kernel.rand, Kernel.rand) ; end }
def RationalFactory.fake_value() ; receive(Kernel.rand.to_r) ; end RationalFactory.class_eval{ def fake_value() ; receive(Kernel.rand.to_r) ; end }


def TimeFactory.fake_value() ; receive(Time.now) ; end TimeFactory.class_eval{ def fake_value() ; receive(Time.now) ; end }


def ExceptionFactory.fake_value() ; Exception.constants.sample ; end ExceptionFactory.class_eval{ def fake_value() ; Exception.constants.sample ; end }

def NilFactory.fake_value() ; nil ; end NilFactory.class_eval{ def fake_value() ; nil ; end }
def TrueFactory.fake_value() ; true ; end TrueFactory.class_eval{ def fake_value() ; true ; end }
def FalseFactory.fake_value() ; false ; end FalseFactory.class_eval{ def fake_value() ; false ; end }
def BooleanFactory.fake_value() ; [true, false].sample ; end BooleanFactory.class_eval{ def fake_value() ; [true, false].sample ; end }
end end
3 changes: 2 additions & 1 deletion spec/examples/dataflow/parsing_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@


describe_example_script(:parse_apache_logs, 'dataflow/parse_apache_logs.rb') do describe_example_script(:parse_apache_logs, 'dataflow/parse_apache_logs.rb') do
it 'runs' do it 'runs' do
subject = Wukong.dataflow(:parse_apache_logs)
out, err = Gorillib::TestHelpers.capture_output do out, err = Gorillib::TestHelpers.capture_output do
Wukong::LocalRunner.receive(:flow => subject) do Wukong::LocalRunner.receive(:flow => subject) do
run :default run :default
end end
end end
out.string.split("\n").first.should =~ /\{\"ip_address\":\"[\d\.]+\",.*\"}/ out.string.split("\n").first.should == "127.0.0.1 - - [10/Apr/2007:10:39:11 +0300] \"GET / HTTP/1.1\" 500 606 \"-\" \"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)\"\t"
end end
end end
3 changes: 3 additions & 0 deletions spec/support/wukong_test_helpers.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def self.the(class_method, &block)
let(:mock_stage ){ m = mock('mock stage') ; m } let(:mock_stage ){ m = mock('mock stage') ; m }
let(:mock_processor){ m = mock ; m.stub(:name => 'mock processor', :attributes => { :a => :b }) ; m } let(:mock_processor){ m = mock ; m.stub(:name => 'mock processor', :attributes => { :a => :b }) ; m }


let(:test_model_class){ Class.new{ include Gorillib::Model ; field :smurfiness, Integer } }
let(:test_model){ test_model_class.receive(:smurfiness => 99) }

let(:test_source){ Wukong::Integers.new(:name => :integers, :size => 100) } let(:test_source){ Wukong::Integers.new(:name => :integers, :size => 100) }
let(:test_sink){ Wukong::Sink::ArraySink.new(:name => :test_sink) } let(:test_sink){ Wukong::Sink::ArraySink.new(:name => :test_sink) }
let(:test_processor_class){ Wukong::AsIs } let(:test_processor_class){ Wukong::AsIs }
Expand Down
51 changes: 25 additions & 26 deletions spec/wukong/model/faker_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


it 'using type if no faker given' do it 'using type if no faker given' do
subject.field :last_name, String subject.field :last_name, String
Gorillib::Factory::StringFactory.should_receive(:fake_value) Gorillib::Factory(String).should_receive(:fake_value)
subject.fake_value subject.fake_value
end end


Expand All @@ -27,8 +27,8 @@
end end


it 'with explicit faker factory' do it 'with explicit faker factory' do
subject.field :longitude, String, :faker => Gorillib::Factory::IntegerFactory subject.field :longitude, String, :faker => Gorillib::Factory(Integer)
Gorillib::Factory::IntegerFactory.should_receive(:fake_value) Gorillib::Factory(Integer).should_receive(:fake_value)
subject.fake_value subject.fake_value
end end


Expand All @@ -54,41 +54,40 @@
be_ish_matcher :ip_address, /^(\d+\.){3}\d+$/ be_ish_matcher :ip_address, /^(\d+\.){3}\d+$/


context 'extensions to Gorillib::Factory' do context 'extensions to Gorillib::Factory' do
context Gorillib::Factory::StringFactory do context Gorillib::Factory(String) do
the(:fake_value){ should =~ /^\w+$/ } its(:fake_value){ should =~ /^\w+$/ }
end end
context(Gorillib::Factory::GuidFactory) do context(Gorillib::Factory(Guid)) do
the(:fake_value){ should be_guid_ish } its(:fake_value){ should be_guid_ish }
end end
context(Gorillib::Factory::IpAddressFactory) do context(Gorillib::Factory(IpAddress)) do
the(:fake_value){ should be_ip_address_ish } its(:fake_value){ should be_ip_address_ish }
end end
context(Gorillib::Factory::HostnameFactory) do context(Gorillib::Factory(Hostname)) do
the(:fake_value){ should be_hostname_ish } its(:fake_value){ should be_hostname_ish }
end end


context(Gorillib::Factory::SymbolFactory) do context(Gorillib::Factory(Symbol)) do
the(:fake_value){ should be_a Symbol } its(:fake_value){ should be_a Symbol }
the(:fake_value){ should be_identifier_ish } its(:fake_value){ should be_identifier_ish }
end end


context(Gorillib::Factory::IntegerFactory) do context(Gorillib::Factory(Integer)) do
the(:fake_value){ should be_a Integer } its(:fake_value){ should be_a Integer }
the(:fake_value){ should be < 100 } its(:fake_value){ should be < 100 }
end end


context(Gorillib::Factory::TimeFactory) do context(Gorillib::Factory(Time)) do
the(:fake_value){ should be_a Time } its(:fake_value){ should be_a Time }
the(:fake_value){ should be_within(5).of(Time.now) } its(:fake_value){ should be_within(5).of(Time.now) }
end end


context(Gorillib::Factory::NilFactory ){ the(:fake_value){ should equal(nil) } } context(Gorillib::Factory(NilClass) ){ its(:fake_value){ should equal(nil) } }
context(Gorillib::Factory::TrueFactory ){ the(:fake_value){ should equal(true) } } context(Gorillib::Factory(TrueClass) ){ its(:fake_value){ should equal(true) } }
context(Gorillib::Factory::FalseFactory ){ the(:fake_value){ should equal(false) } } context(Gorillib::Factory(FalseClass) ){ its(:fake_value){ should equal(false) } }
context(Gorillib::Factory::BooleanFactory){ the(:fake_value){ should be_in([true, false]) } } context(Gorillib::Factory(:boolean) ){ its(:fake_value){ should be_in([true, false]) } }
end end



context Wukong::Faker::Helpers do context Wukong::Faker::Helpers do
subject{ Wukong::Faker::Helpers } subject{ Wukong::Faker::Helpers }


Expand Down Expand Up @@ -129,6 +128,6 @@
its(:fake_ip_addresss){ should be_ip_address_ish } its(:fake_ip_addresss){ should be_ip_address_ish }
its(:fake_version_number){ should be_a(String) } its(:fake_version_number){ should be_a(String) }
its(:fake_version_number){ should =~ /^\d+\.\d+$/ } its(:fake_version_number){ should =~ /^\d+\.\d+$/ }

end end

end end

0 comments on commit e42ac26

Please sign in to comment.