From dc499293b3a4e48a2c2fba528bfe2ad2f86efc3b Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Sun, 20 Feb 2011 00:35:51 -0800 Subject: [PATCH] Took 90 seconds off the build. Fixture builder FTFW. Cleaned up spec_helper - moved stuff into support files. --- spec/misc_spec.rb | 3 +- spec/models/data_point_spec.rb | 20 -------- spec/models/statistic_spec.rb | 24 +-------- spec/models/user/attack_vectors_spec.rb | 4 +- spec/models/user/connecting_spec.rb | 1 + spec/spec_helper.rb | 66 ++----------------------- spec/support/fake_http_request.rb | 42 ++++++++++++++++ spec/support/fake_redis.rb | 17 +++++++ spec/support/fixture_builder.rb | 36 +++++++++++++- 9 files changed, 105 insertions(+), 108 deletions(-) create mode 100644 spec/support/fake_http_request.rb create mode 100644 spec/support/fake_redis.rb diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index 12b22d20f..3ea7bbc6c 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -10,9 +10,10 @@ describe 'making sure the spec runner works' do loaded_user = User.find(user.id) loaded_user.person.owner_id.should == user.id end + describe 'fixtures' do it 'loads fixtures' do - User.count.should == 3 + User.count.should_not == 0 end end diff --git a/spec/models/data_point_spec.rb b/spec/models/data_point_spec.rb index 476316638..33b68bbe7 100644 --- a/spec/models/data_point_spec.rb +++ b/spec/models/data_point_spec.rb @@ -7,26 +7,6 @@ describe DataPoint do end describe '.users_with_posts_on_day' do - before do - 1.times do |n| - p = alice.post(:status_message, :message => 'hi', :to => alice.aspects.first) - p.created_at = @time - p.save - end - - 5.times do |n| - p = bob.post(:status_message, :message => 'hi', :to => bob.aspects.first) - p.created_at = @time - p.save - end - - 10.times do |n| - p = eve.post(:status_message, :message => 'hi', :to => eve.aspects.first) - p.created_at = @time - p.save - end - end - it 'returns a DataPoint object' do DataPoint.users_with_posts_on_day(@time, 1).class.should == DataPoint end diff --git a/spec/models/statistic_spec.rb b/spec/models/statistic_spec.rb index f01c9d23a..343c31787 100644 --- a/spec/models/statistic_spec.rb +++ b/spec/models/statistic_spec.rb @@ -2,30 +2,8 @@ require 'spec_helper' describe Statistic do before do - @stat = Statistic.new + @stat = Statistic.first @time = Time.now - - 1.times do |n| - p = alice.post(:status_message, :message => 'hi', :to => alice.aspects.first) - p.created_at = @time - p.save - end - - 5.times do |n| - p = bob.post(:status_message, :message => 'hi', :to => bob.aspects.first) - p.created_at = @time - p.save - end - - 10.times do |n| - p = eve.post(:status_message, :message => 'hi', :to => eve.aspects.first) - p.created_at = @time - p.save - end - - (0..10).each do |n| - @stat.data_points << DataPoint.users_with_posts_on_day(@time, n) - end end describe '#compute_average' do diff --git a/spec/models/user/attack_vectors_spec.rb b/spec/models/user/attack_vectors_spec.rb index 476fc26d0..d40d7d23b 100644 --- a/spec/models/user/attack_vectors_spec.rb +++ b/spec/models/user/attack_vectors_spec.rb @@ -112,6 +112,7 @@ describe "attack vectors" do end it "ignores retractions on a post not owned by the retraction's sender" do + StatusMessage.delete_all original_message = user2.post :status_message, :message => 'store this!', :to => aspect2.id salmon_xml = user2.salmon(original_message).xml_for(user.person) @@ -135,6 +136,7 @@ describe "attack vectors" do end it "disregards retractions for non-existent posts that are from someone other than the post's author" do + StatusMessage.delete_all original_message = user2.post :status_message, :message => 'store this!', :to => aspect2.id id = original_message.reload.id @@ -145,7 +147,7 @@ describe "attack vectors" do original_message.delete - StatusMessage.count.should be 0 + StatusMessage.count.should == 0 proc { salmon_xml = user3.salmon(ret).xml_for(user.person) zord = Postzord::Receiver.new(user, :salmon_xml => salmon_xml) diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index d5547cf67..477049da2 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -275,6 +275,7 @@ describe Diaspora::UserModules::Connecting do context 'with a post' do before do + StatusMessage.delete_all @message = user.post(:status_message, :message => "hi", :to => aspect.id) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f2a30b80e..33067cfe4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,8 +15,9 @@ include Devise::TestHelpers include WebMock::API include HelperMethods +# Force fixture rebuild `rm #{File.join(Rails.root, 'tmp', 'fixture_builder.yml')}` -# + # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} @@ -34,6 +35,8 @@ RSpec.configure do |config| end end +ImageUploader.enable_processing = false + def set_up_friends local_luke = Factory(:user_with_aspect, :username => "luke") local_leia = Factory(:user_with_aspect, :username => "leia") @@ -45,7 +48,6 @@ def set_up_friends [local_luke, local_leia, remote_raphael] end - def alice #users(:alice) User.where(:username => 'alice').first @@ -60,64 +62,4 @@ def eve #users(:eve) User.where(:username => 'eve').first end -module Diaspora::WebSocket - def self.redis - FakeRedis.new - end -end -class FakeRedis - def rpop(*args) - true - end - def llen(*args) - true - end - def lpush(*args) - true - end -end -ImageUploader.enable_processing = false - -class FakeHttpRequest - def initialize(callback_wanted) - @callback = callback_wanted - @callbacks = [] - end - - def callbacks=(rs) - @callbacks += rs.reverse - end - - def response - @callbacks.pop unless @callbacks.nil? || @callbacks.empty? - end - - def response_header - self - end - - def method_missing(method) - self - end - - def post(opts = nil); - self - end - - def get(opts = nil) - self - end - - def publish(opts = nil) - self - end - - def callback(&b) - b.call if @callback == :success - end - - def errback(&b) - b.call if @callback == :failure - end -end diff --git a/spec/support/fake_http_request.rb b/spec/support/fake_http_request.rb new file mode 100644 index 000000000..f4f56e868 --- /dev/null +++ b/spec/support/fake_http_request.rb @@ -0,0 +1,42 @@ +class FakeHttpRequest + def initialize(callback_wanted) + @callback = callback_wanted + @callbacks = [] + end + + def callbacks=(rs) + @callbacks += rs.reverse + end + + def response + @callbacks.pop unless @callbacks.nil? || @callbacks.empty? + end + + def response_header + self + end + + def method_missing(method) + self + end + + def post(opts = nil); + self + end + + def get(opts = nil) + self + end + + def publish(opts = nil) + self + end + + def callback(&b) + b.call if @callback == :success + end + + def errback(&b) + b.call if @callback == :failure + end +end diff --git a/spec/support/fake_redis.rb b/spec/support/fake_redis.rb new file mode 100644 index 000000000..97dda9585 --- /dev/null +++ b/spec/support/fake_redis.rb @@ -0,0 +1,17 @@ +module Diaspora::WebSocket + def self.redis + FakeRedis.new + end +end + +class FakeRedis + def rpop(*args) + true + end + def llen(*args) + true + end + def lpush(*args) + true + end +end diff --git a/spec/support/fixture_builder.rb b/spec/support/fixture_builder.rb index 79fbf6279..85440e887 100644 --- a/spec/support/fixture_builder.rb +++ b/spec/support/fixture_builder.rb @@ -1,10 +1,12 @@ -# I usually put this file in spec/support/fixture_builder.rb +require File.join(File.dirname(__FILE__), "user_methods.rb") + FixtureBuilder.configure do |fbuilder| # rebuild fixtures automatically when these files change: fbuilder.files_to_check += Dir["app/models/*.rb", "lib/**/*.rb", "spec/factories/*.rb", "spec/support/fixture_builder.rb"] # now declare objects fbuilder.factory do + # Users alice = Factory(:user_with_aspect, :username => "alice") eve = Factory(:user_with_aspect, :username => "eve") bob = Factory(:user_with_aspect, :username => "bob") @@ -12,6 +14,38 @@ FixtureBuilder.configure do |fbuilder| connect_users(bob, bob.aspects.first, alice, alice.aspects.first) connect_users(bob, bob.aspects.first, eve, eve.aspects.first) + + # Statistics + frodo = Factory(:user_with_aspect, :username => "frodo") + sam = Factory(:user_with_aspect, :username => "sam") + bilbo = Factory(:user_with_aspect, :username => "bilbo") + + stat = Statistic.new + time = Time.now + + 1.times do + p = frodo.post(:status_message, :message => 'hi', :to => frodo.aspects.first) + p.created_at = time + p.save! + end + + 5.times do + p = sam.post(:status_message, :message => 'hi', :to => sam.aspects.first) + p.created_at = time + p.save! + end + + 10.times do + p = bilbo.post(:status_message, :message => 'hi', :to => bilbo.aspects.first) + p.created_at = time + p.save! + end + + (0..10).each do |n| + stat.data_points << DataPoint.users_with_posts_on_day(time, n) + end + stat.time = time + stat.save! end end