Commit c9e3df3f by Jon Yurek

Put methods from spec/spec_helper in spec/support

There were a bunch of free-floating methods in spec_helper. They were
pulled over from test/helper, but they shouldn't be there. Move them
into appropriate modules in spec/support. This commit also cleans up the
"should_accept/reject_dummy_class methods in favor of using proper
matchers directly. This commit should probably not get squashed or git
will likely forget that those matcher specs are the same file because of
how much changed.
parent 40010c4d
...@@ -4,7 +4,6 @@ require 'paperclip/matchers' ...@@ -4,7 +4,6 @@ require 'paperclip/matchers'
describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
extend Paperclip::Shoulda::Matchers extend Paperclip::Shoulda::Matchers
context "validate_attachment_content_type" do
before do before do
reset_table("dummies") do |d| reset_table("dummies") do |d|
d.string :title d.string :title
...@@ -14,78 +13,54 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -14,78 +13,54 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
reset_class "Dummy" reset_class "Dummy"
Dummy.do_not_validate_attachment_file_type :avatar Dummy.do_not_validate_attachment_file_type :avatar
Dummy.has_attached_file :avatar Dummy.has_attached_file :avatar
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg)).
rejecting(%w(audio/mp3 application/octet-stream))
end end
context "given a class with no validation" do it "rejects a class with no validation" do
should_reject_dummy_class expect(matcher).to_not accept(Dummy)
end end
context "given a class with a validation that doesn't match" do it 'rejects a class when the validation fails' do
before do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
expect(matcher).to_not accept(Dummy)
end end
should_reject_dummy_class it "accepts a class with a matching validation" do
end
context "given a class with a matching validation" do
before do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy)
end end
should_accept_dummy_class it "accepts a class with other validations but matching types" do
end
context "given a class with other validations but matching types" do
before do
Dummy.validates_presence_of :title Dummy.validates_presence_of :title
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy)
end end
should_accept_dummy_class it "accepts a class that matches and a matcher that only specifies 'allowing'" do
end
context "given a class that matches and a matcher that only specifies 'allowing'" do
before do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
@matcher = self.class.validate_attachment_content_type(:avatar). matcher = plain_matcher.allowing(%w(image/png image/jpeg))
allowing(%w(image/png image/jpeg))
end
should_accept_dummy_class expect(matcher).to accept(Dummy)
end end
context "given a class that does not match and a matcher that only specifies 'allowing'" do it "rejects a class that does not match and a matcher that only specifies 'allowing'" do
before do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
@matcher = self.class.validate_attachment_content_type(:avatar). matcher = plain_matcher.allowing(%w(image/png image/jpeg))
allowing(%w(image/png image/jpeg))
end
should_reject_dummy_class expect(matcher).to_not accept(Dummy)
end end
context "given a class that matches and a matcher that only specifies 'rejecting'" do it "accepts a class that matches and a matcher that only specifies 'rejecting'" do
before do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
@matcher = self.class.validate_attachment_content_type(:avatar). matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
rejecting(%w(audio/mp3 application/octet-stream))
end
should_accept_dummy_class expect(matcher).to accept(Dummy)
end end
context "given a class that does not match and a matcher that only specifies 'rejecting'" do it "rejects a class that does not match and a matcher that only specifies 'rejecting'" do
before do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*} Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
@matcher = self.class.validate_attachment_content_type(:avatar). matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
rejecting(%w(audio/mp3 application/octet-stream))
end
should_reject_dummy_class expect(matcher).to_not accept(Dummy)
end end
context "using an :if to control the validation" do context "using an :if to control the validation" do
...@@ -94,21 +69,31 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do ...@@ -94,21 +69,31 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
validates_attachment_content_type :avatar, content_type: %r{image/*} , if: :go validates_attachment_content_type :avatar, content_type: %r{image/*} , if: :go
attr_accessor :go attr_accessor :go
end end
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg)).
rejecting(%w(audio/mp3 application/octet-stream))
@dummy = Dummy.new
end end
it "run the validation if the control is true" do it "run the validation if the control is true" do
@dummy.go = true dummy = Dummy.new
expect(@matcher).to accept(@dummy) dummy.go = true
expect(matcher).to accept(dummy)
end end
it "not run the validation if the control is false" do it "not run the validation if the control is false" do
@dummy.go = false dummy = Dummy.new
expect(@matcher).to_not accept(@dummy) dummy.go = false
expect(matcher).to_not accept(dummy)
end end
end end
private
def plain_matcher
self.class.validate_attachment_content_type(:avatar)
end end
def matcher
plain_matcher.
allowing(%w(image/png image/jpeg)).
rejecting(%w(audio/mp3 application/octet-stream))
end
end end
...@@ -4,7 +4,6 @@ require 'paperclip/matchers' ...@@ -4,7 +4,6 @@ require 'paperclip/matchers'
describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do
extend Paperclip::Shoulda::Matchers extend Paperclip::Shoulda::Matchers
context "validate_attachment_presence" do
before do before do
reset_table("dummies") do |d| reset_table("dummies") do |d|
d.string :avatar_file_name d.string :avatar_file_name
...@@ -12,41 +11,31 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do ...@@ -12,41 +11,31 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do
reset_class "Dummy" reset_class "Dummy"
Dummy.has_attached_file :avatar Dummy.has_attached_file :avatar
Dummy.do_not_validate_attachment_file_type :avatar Dummy.do_not_validate_attachment_file_type :avatar
@matcher = self.class.validate_attachment_presence(:avatar)
end end
context "given a class with no validation" do it "rejects a class with no validation" do
should_reject_dummy_class expect(matcher).to_not accept(Dummy)
end end
context "given a class with a matching validation" do it "accepts a class with a matching validation" do
before do
Dummy.validates_attachment_presence :avatar Dummy.validates_attachment_presence :avatar
expect(matcher).to accept(Dummy)
end end
should_accept_dummy_class it "accepts an instance with other attachment validations" do
end
context "given an instance with other attachment validations" do
before do
reset_table("dummies") do |d| reset_table("dummies") do |d|
d.string :avatar_file_name d.string :avatar_file_name
d.string :avatar_content_type d.string :avatar_content_type
end end
Dummy.class_eval do Dummy.class_eval do
validates_attachment_presence :avatar validates_attachment_presence :avatar
validates_attachment_content_type :avatar, content_type: 'image/gif' validates_attachment_content_type :avatar, content_type: 'image/gif'
end end
dummy = Dummy.new
@dummy = Dummy.new dummy.avatar = File.new fixture_file('5k.png')
@matcher = self.class.validate_attachment_presence(:avatar)
end
it "it should validate properly" do expect(matcher).to accept(dummy)
@dummy.avatar = File.new fixture_file('5k.png')
expect(@matcher).to accept(@dummy)
end
end end
context "using an :if to control the validation" do context "using an :if to control the validation" do
...@@ -55,19 +44,26 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do ...@@ -55,19 +44,26 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do
validates_attachment_presence :avatar, if: :go validates_attachment_presence :avatar, if: :go
attr_accessor :go attr_accessor :go
end end
@dummy = Dummy.new
@dummy.avatar = nil
end end
it "run the validation if the control is true" do it "runs the validation if the control is true" do
@dummy.go = true dummy = Dummy.new
expect(@matcher).to accept(@dummy) dummy.avatar = nil
dummy.go = true
expect(matcher).to accept(dummy)
end end
it "not run the validation if the control is false" do it "does not run the validation if the control is false" do
@dummy.go = false dummy = Dummy.new
expect(@matcher).to_not accept(@dummy) dummy.avatar = nil
dummy.go = false
expect(matcher).to_not accept(dummy)
end end
end end
private
def matcher
self.class.validate_attachment_presence(:avatar)
end end
end end
...@@ -4,7 +4,6 @@ require 'paperclip/matchers' ...@@ -4,7 +4,6 @@ require 'paperclip/matchers'
describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do
extend Paperclip::Shoulda::Matchers extend Paperclip::Shoulda::Matchers
context "validate_attachment_size" do
before do before do
reset_table("dummies") do |d| reset_table("dummies") do |d|
d.string :avatar_file_name d.string :avatar_file_name
...@@ -15,44 +14,36 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do ...@@ -15,44 +14,36 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do
Dummy.has_attached_file :avatar Dummy.has_attached_file :avatar
end end
context "of limited size" do context "Limiting size" do
before do it "rejects a class with no validation" do
@matcher = self.class.validate_attachment_size(:avatar).in(256..1024) expect(matcher.in(256..1024)).to_not accept(Dummy)
end
context "given a class with no validation" do
should_reject_dummy_class
end end
context "given a class with a validation that's too high" do it "rejects a class with a validation that's too high" do
before { Dummy.validates_attachment_size :avatar, in: 256..2048 } Dummy.validates_attachment_size :avatar, in: 256..2048
should_reject_dummy_class expect(matcher.in(256..1024)).to_not accept(Dummy)
end end
context "given a class with a validation that's too low" do it "accepts a class with a validation that's too low" do
before { Dummy.validates_attachment_size :avatar, in: 0..1024 } Dummy.validates_attachment_size :avatar, in: 0..1024
should_reject_dummy_class expect(matcher.in(256..1024)).to_not accept(Dummy)
end end
context "given a class with a validation that matches" do it "accepts a class with a validation that matches" do
before { Dummy.validates_attachment_size :avatar, in: 256..1024 } Dummy.validates_attachment_size :avatar, in: 256..1024
should_accept_dummy_class expect(matcher.in(256..1024)).to accept(Dummy)
end end
end end
context "allowing anything" do context "allowing anything" do
before do it "given a class with an upper limit" do
@matcher = self.class.validate_attachment_size(:avatar) Dummy.validates_attachment_size :avatar, less_than: 1
expect(matcher).to accept(Dummy)
end end
context "given a class with an upper limit" do it "given a class with a lower limit" do
before { Dummy.validates_attachment_size :avatar, less_than: 1 } Dummy.validates_attachment_size :avatar, greater_than: 1
should_accept_dummy_class expect(matcher).to accept(Dummy)
end
context "given a class with a lower limit" do
before { Dummy.validates_attachment_size :avatar, greater_than: 1 }
should_accept_dummy_class
end end
end end
...@@ -62,33 +53,36 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do ...@@ -62,33 +53,36 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do
validates_attachment_size :avatar, greater_than: 1024, if: :go validates_attachment_size :avatar, greater_than: 1024, if: :go
attr_accessor :go attr_accessor :go
end end
@dummy = Dummy.new
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
end end
it "run the validation if the control is true" do it "run the validation if the control is true" do
@dummy.go = true dummy = Dummy.new
expect(@matcher).to accept(@dummy) dummy.go = true
expect(matcher.greater_than(1024)).to accept(dummy)
end end
it "not run the validation if the control is false" do it "not run the validation if the control is false" do
@dummy.go = false dummy = Dummy.new
expect(@matcher).to_not accept(@dummy) dummy.go = false
expect(matcher.greater_than(1024)).to_not accept(dummy)
end end
end end
context "post processing" do context "post processing" do
before do before do
Dummy.validates_attachment_size :avatar, greater_than: 1024 Dummy.validates_attachment_size :avatar, greater_than: 1024
@dummy = Dummy.new
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
end end
it "be skipped" do it "be skipped" do
@dummy.avatar.expects(:post_process).never dummy = Dummy.new
expect(@matcher).to accept(@dummy) dummy.avatar.expects(:post_process).never
expect(matcher.greater_than(1024)).to accept(dummy)
end end
end end
private
def matcher
self.class.validate_attachment_size(:avatar)
end end
end end
...@@ -27,164 +27,11 @@ ActiveSupport::Deprecation.silenced = true ...@@ -27,164 +27,11 @@ ActiveSupport::Deprecation.silenced = true
RSpec.configure do |config| RSpec.configure do |config|
config.include Assertions config.include Assertions
config.include ModelReconstruction
config.include TestData
config.extend RailsHelpers::ClassMethods
config.mock_framework = :mocha config.mock_framework = :mocha
config.before(:all) do config.before(:all) do
rebuild_model rebuild_model
end end
end end
def using_protected_attributes?
ActiveRecord::VERSION::MAJOR < 4
end
def reset_class class_name
ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, class_name) rescue nil
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass.class_eval do
include Paperclip::Glue
end
klass.reset_column_information
klass.connection_pool.clear_table_cache!(klass.table_name) if klass.connection_pool.respond_to?(:clear_table_cache!)
klass.connection.schema_cache.clear_table_cache!(klass.table_name) if klass.connection.respond_to?(:schema_cache)
klass
end
def reset_table table_name, &block
block ||= lambda { |table| true }
ActiveRecord::Base.connection.create_table :dummies, {force: true}, &block
end
def modify_table table_name, &block
ActiveRecord::Base.connection.change_table :dummies, &block
end
def rebuild_model options = {}
ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
table.column :title, :string
table.column :other, :string
table.column :avatar_file_name, :string
table.column :avatar_content_type, :string
table.column :avatar_file_size, :integer
table.column :avatar_updated_at, :datetime
table.column :avatar_fingerprint, :string
end
rebuild_class options
end
def rebuild_class options = {}
reset_class("Dummy").tap do |klass|
klass.has_attached_file :avatar, options
klass.do_not_validate_attachment_file_type :avatar
Paperclip.reset_duplicate_clash_check!
end
end
def rebuild_meta_class_of obj, options = {}
(class << obj; self; end).tap do |metaklass|
metaklass.has_attached_file :avatar, options
metaklass.do_not_validate_attachment_file_type :avatar
Paperclip.reset_duplicate_clash_check!
end
end
class FakeModel
attr_accessor :avatar_file_name,
:avatar_file_size,
:avatar_updated_at,
:avatar_content_type,
:avatar_fingerprint,
:id
def errors
@errors ||= []
end
def run_paperclip_callbacks name, *args
end
def valid?
errors.empty?
end
end
def attachment(options={})
Paperclip::Attachment.new(:avatar, FakeModel.new, options)
end
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end
def should_accept_dummy_class
it "accepts the class" do
expect(@matcher).to accept(Dummy)
end
it "accepts an instance of that class" do
expect(@matcher).to accept(Dummy.new)
end
end
def should_reject_dummy_class
it "rejects the class" do
expect(@matcher).to_not accept(Dummy)
end
it "rejects an instance of that class" do
expect(@matcher).to_not accept(Dummy.new)
end
end
def with_exitstatus_returning(code)
saved_exitstatus = $?.nil? ? 0 : $?.exitstatus
begin
`ruby -e 'exit #{code.to_i}'`
yield
ensure
`ruby -e 'exit #{saved_exitstatus.to_i}'`
end
end
def stringy_file
StringIO.new('.\n')
end
def fixture_file(filename)
File.join(File.dirname(__FILE__), 'fixtures', filename)
end
def assert_success_response(url)
Net::HTTP.get_response(URI.parse(url)) do |response|
assert_equal "200", response.code,
"Expected HTTP response code 200, got #{response.code}"
end
end
def assert_not_found_response(url)
Net::HTTP.get_response(URI.parse(url)) do |response|
assert_equal "404", response.code,
"Expected HTTP response code 404, got #{response.code}"
end
end
def assert_file_exists(path)
assert File.exists?(path), %(Expect "#{path}" to be exists.)
end
def assert_file_not_exists(path)
assert !File.exists?(path), %(Expect "#{path}" to not exists.)
end
def assert_frame_dimensions(range, frames)
frames.each_with_index do |frame, frame_index|
frame.split('x').each_with_index do |dimension, dimension_index |
assert range.include?(dimension.to_i), "Frame #{frame_index}[#{dimension_index}] should have been within #{range.inspect}, but was #{dimension}"
end
end
end
...@@ -46,4 +46,26 @@ module Assertions ...@@ -46,4 +46,26 @@ module Assertions
def assert_empty(object) def assert_empty(object)
expect(object).to be_empty expect(object).to be_empty
end end
def assert_success_response(url)
Net::HTTP.get_response(URI.parse(url)) do |response|
assert_equal "200", response.code,
"Expected HTTP response code 200, got #{response.code}"
end
end
def assert_not_found_response(url)
Net::HTTP.get_response(URI.parse(url)) do |response|
assert_equal "404", response.code,
"Expected HTTP response code 404, got #{response.code}"
end
end
def assert_frame_dimensions(range, frames)
frames.each_with_index do |frame, frame_index|
frame.split('x').each_with_index do |dimension, dimension_index |
assert range.include?(dimension.to_i), "Frame #{frame_index}[#{dimension_index}] should have been within #{range.inspect}, but was #{dimension}"
end
end
end
end end
class FakeModel
attr_accessor(
:avatar_file_name,
:avatar_file_size,
:avatar_updated_at,
:avatar_content_type,
:avatar_fingerprint,
:id
)
def errors
@errors ||= []
end
def run_paperclip_callbacks name, *args
end
def valid?
errors.empty?
end
end
module ModelReconstruction
def reset_class class_name
ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, class_name) rescue nil
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass.class_eval do
include Paperclip::Glue
end
klass.reset_column_information
klass.connection_pool.clear_table_cache!(klass.table_name) if klass.connection_pool.respond_to?(:clear_table_cache!)
klass.connection.schema_cache.clear_table_cache!(klass.table_name) if klass.connection.respond_to?(:schema_cache)
klass
end
def reset_table table_name, &block
block ||= lambda { |table| true }
ActiveRecord::Base.connection.create_table :dummies, {force: true}, &block
end
def modify_table table_name, &block
ActiveRecord::Base.connection.change_table :dummies, &block
end
def rebuild_model options = {}
ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
table.column :title, :string
table.column :other, :string
table.column :avatar_file_name, :string
table.column :avatar_content_type, :string
table.column :avatar_file_size, :integer
table.column :avatar_updated_at, :datetime
table.column :avatar_fingerprint, :string
end
rebuild_class options
end
def rebuild_class options = {}
reset_class("Dummy").tap do |klass|
klass.has_attached_file :avatar, options
klass.do_not_validate_attachment_file_type :avatar
Paperclip.reset_duplicate_clash_check!
end
end
def rebuild_meta_class_of obj, options = {}
meta_class_of(obj).tap do |metaklass|
metaklass.has_attached_file :avatar, options
metaklass.do_not_validate_attachment_file_type :avatar
Paperclip.reset_duplicate_clash_check!
end
end
def meta_class_of(obj)
class << obj
self
end
end
end
module RailsHelpers
module ClassMethods
def using_protected_attributes?
ActiveRecord::VERSION::MAJOR < 4
end
end
end
module TestData
def attachment(options={})
Paperclip::Attachment.new(:avatar, FakeModel.new, options)
end
def stringy_file
StringIO.new('.\n')
end
def fixture_file(filename)
File.join(File.dirname(__FILE__), 'fixtures', filename)
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment