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'
describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
extend Paperclip::Shoulda::Matchers
context "validate_attachment_content_type" do
before do
reset_table("dummies") do |d|
d.string :title
......@@ -14,78 +13,54 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
reset_class "Dummy"
Dummy.do_not_validate_attachment_file_type :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
context "given a class with no validation" do
should_reject_dummy_class
it "rejects a class with no validation" do
expect(matcher).to_not accept(Dummy)
end
context "given a class with a validation that doesn't match" do
before do
it 'rejects a class when the validation fails' do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
expect(matcher).to_not accept(Dummy)
end
should_reject_dummy_class
end
context "given a class with a matching validation" do
before do
it "accepts a class with a matching validation" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy)
end
should_accept_dummy_class
end
context "given a class with other validations but matching types" do
before do
it "accepts a class with other validations but matching types" do
Dummy.validates_presence_of :title
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
expect(matcher).to accept(Dummy)
end
should_accept_dummy_class
end
context "given a class that matches and a matcher that only specifies 'allowing'" do
before do
it "accepts a class that matches and a matcher that only specifies 'allowing'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg))
end
matcher = plain_matcher.allowing(%w(image/png image/jpeg))
should_accept_dummy_class
expect(matcher).to accept(Dummy)
end
context "given a class that does not match and a matcher that only specifies 'allowing'" do
before do
it "rejects a class that does not match and a matcher that only specifies 'allowing'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg))
end
matcher = plain_matcher.allowing(%w(image/png image/jpeg))
should_reject_dummy_class
expect(matcher).to_not accept(Dummy)
end
context "given a class that matches and a matcher that only specifies 'rejecting'" do
before do
it "accepts a class that matches and a matcher that only specifies 'rejecting'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
rejecting(%w(audio/mp3 application/octet-stream))
end
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
should_accept_dummy_class
expect(matcher).to accept(Dummy)
end
context "given a class that does not match and a matcher that only specifies 'rejecting'" do
before do
it "rejects a class that does not match and a matcher that only specifies 'rejecting'" do
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
rejecting(%w(audio/mp3 application/octet-stream))
end
matcher = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
should_reject_dummy_class
expect(matcher).to_not accept(Dummy)
end
context "using an :if to control the validation" do
......@@ -94,21 +69,31 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentContentTypeMatcher do
validates_attachment_content_type :avatar, content_type: %r{image/*} , if: :go
attr_accessor :go
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
it "run the validation if the control is true" do
@dummy.go = true
expect(@matcher).to accept(@dummy)
dummy = Dummy.new
dummy.go = true
expect(matcher).to accept(dummy)
end
it "not run the validation if the control is false" do
@dummy.go = false
expect(@matcher).to_not accept(@dummy)
dummy = Dummy.new
dummy.go = false
expect(matcher).to_not accept(dummy)
end
end
private
def plain_matcher
self.class.validate_attachment_content_type(:avatar)
end
def matcher
plain_matcher.
allowing(%w(image/png image/jpeg)).
rejecting(%w(audio/mp3 application/octet-stream))
end
end
......@@ -4,7 +4,6 @@ require 'paperclip/matchers'
describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do
extend Paperclip::Shoulda::Matchers
context "validate_attachment_presence" do
before do
reset_table("dummies") do |d|
d.string :avatar_file_name
......@@ -12,41 +11,31 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do
reset_class "Dummy"
Dummy.has_attached_file :avatar
Dummy.do_not_validate_attachment_file_type :avatar
@matcher = self.class.validate_attachment_presence(:avatar)
end
context "given a class with no validation" do
should_reject_dummy_class
it "rejects a class with no validation" do
expect(matcher).to_not accept(Dummy)
end
context "given a class with a matching validation" do
before do
it "accepts a class with a matching validation" do
Dummy.validates_attachment_presence :avatar
expect(matcher).to accept(Dummy)
end
should_accept_dummy_class
end
context "given an instance with other attachment validations" do
before do
it "accepts an instance with other attachment validations" do
reset_table("dummies") do |d|
d.string :avatar_file_name
d.string :avatar_content_type
end
Dummy.class_eval do
validates_attachment_presence :avatar
validates_attachment_content_type :avatar, content_type: 'image/gif'
end
dummy = Dummy.new
@dummy = Dummy.new
@matcher = self.class.validate_attachment_presence(:avatar)
end
dummy.avatar = File.new fixture_file('5k.png')
it "it should validate properly" do
@dummy.avatar = File.new fixture_file('5k.png')
expect(@matcher).to accept(@dummy)
end
expect(matcher).to accept(dummy)
end
context "using an :if to control the validation" do
......@@ -55,19 +44,26 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentPresenceMatcher do
validates_attachment_presence :avatar, if: :go
attr_accessor :go
end
@dummy = Dummy.new
@dummy.avatar = nil
end
it "run the validation if the control is true" do
@dummy.go = true
expect(@matcher).to accept(@dummy)
it "runs the validation if the control is true" do
dummy = Dummy.new
dummy.avatar = nil
dummy.go = true
expect(matcher).to accept(dummy)
end
it "not run the validation if the control is false" do
@dummy.go = false
expect(@matcher).to_not accept(@dummy)
it "does not run the validation if the control is false" do
dummy = Dummy.new
dummy.avatar = nil
dummy.go = false
expect(matcher).to_not accept(dummy)
end
end
private
def matcher
self.class.validate_attachment_presence(:avatar)
end
end
......@@ -4,7 +4,6 @@ require 'paperclip/matchers'
describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do
extend Paperclip::Shoulda::Matchers
context "validate_attachment_size" do
before do
reset_table("dummies") do |d|
d.string :avatar_file_name
......@@ -15,44 +14,36 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do
Dummy.has_attached_file :avatar
end
context "of limited size" do
before do
@matcher = self.class.validate_attachment_size(:avatar).in(256..1024)
end
context "given a class with no validation" do
should_reject_dummy_class
context "Limiting size" do
it "rejects a class with no validation" do
expect(matcher.in(256..1024)).to_not accept(Dummy)
end
context "given a class with a validation that's too high" do
before { Dummy.validates_attachment_size :avatar, in: 256..2048 }
should_reject_dummy_class
it "rejects a class with a validation that's too high" do
Dummy.validates_attachment_size :avatar, in: 256..2048
expect(matcher.in(256..1024)).to_not accept(Dummy)
end
context "given a class with a validation that's too low" do
before { Dummy.validates_attachment_size :avatar, in: 0..1024 }
should_reject_dummy_class
it "accepts a class with a validation that's too low" do
Dummy.validates_attachment_size :avatar, in: 0..1024
expect(matcher.in(256..1024)).to_not accept(Dummy)
end
context "given a class with a validation that matches" do
before { Dummy.validates_attachment_size :avatar, in: 256..1024 }
should_accept_dummy_class
it "accepts a class with a validation that matches" do
Dummy.validates_attachment_size :avatar, in: 256..1024
expect(matcher.in(256..1024)).to accept(Dummy)
end
end
context "allowing anything" do
before do
@matcher = self.class.validate_attachment_size(:avatar)
it "given a class with an upper limit" do
Dummy.validates_attachment_size :avatar, less_than: 1
expect(matcher).to accept(Dummy)
end
context "given a class with an upper limit" do
before { Dummy.validates_attachment_size :avatar, less_than: 1 }
should_accept_dummy_class
end
context "given a class with a lower limit" do
before { Dummy.validates_attachment_size :avatar, greater_than: 1 }
should_accept_dummy_class
it "given a class with a lower limit" do
Dummy.validates_attachment_size :avatar, greater_than: 1
expect(matcher).to accept(Dummy)
end
end
......@@ -62,33 +53,36 @@ describe Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher do
validates_attachment_size :avatar, greater_than: 1024, if: :go
attr_accessor :go
end
@dummy = Dummy.new
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
end
it "run the validation if the control is true" do
@dummy.go = true
expect(@matcher).to accept(@dummy)
dummy = Dummy.new
dummy.go = true
expect(matcher.greater_than(1024)).to accept(dummy)
end
it "not run the validation if the control is false" do
@dummy.go = false
expect(@matcher).to_not accept(@dummy)
dummy = Dummy.new
dummy.go = false
expect(matcher.greater_than(1024)).to_not accept(dummy)
end
end
context "post processing" do
before do
Dummy.validates_attachment_size :avatar, greater_than: 1024
@dummy = Dummy.new
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
end
it "be skipped" do
@dummy.avatar.expects(:post_process).never
expect(@matcher).to accept(@dummy)
dummy = Dummy.new
dummy.avatar.expects(:post_process).never
expect(matcher.greater_than(1024)).to accept(dummy)
end
end
private
def matcher
self.class.validate_attachment_size(:avatar)
end
end
......@@ -27,164 +27,11 @@ ActiveSupport::Deprecation.silenced = true
RSpec.configure do |config|
config.include Assertions
config.include ModelReconstruction
config.include TestData
config.extend RailsHelpers::ClassMethods
config.mock_framework = :mocha
config.before(:all) do
rebuild_model
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
def assert_empty(object)
expect(object).to be_empty
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
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