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,111 +4,96 @@ 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
d.string :avatar_file_name
d.string :avatar_content_type
end
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))
before do
reset_table("dummies") do |d|
d.string :title
d.string :avatar_file_name
d.string :avatar_content_type
end
reset_class "Dummy"
Dummy.do_not_validate_attachment_file_type :avatar
Dummy.has_attached_file :avatar
end
context "given a class with no validation" do
should_reject_dummy_class
end
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
Dummy.validates_attachment_content_type :avatar, content_type: %r{audio/.*}
end
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
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
context "given a class with a matching validation" do
before do
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
end
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
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 = plain_matcher.allowing(%w(image/png image/jpeg))
context "given a class with other validations but matching types" do
before do
Dummy.validates_presence_of :title
Dummy.validates_attachment_content_type :avatar, content_type: %r{image/.*}
end
expect(matcher).to accept(Dummy)
end
should_accept_dummy_class
end
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 = plain_matcher.allowing(%w(image/png image/jpeg))
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/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg))
end
expect(matcher).to_not accept(Dummy)
end
should_accept_dummy_class
end
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 = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
context "given 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/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg))
end
expect(matcher).to accept(Dummy)
end
should_reject_dummy_class
end
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 = plain_matcher.rejecting(%w(audio/mp3 application/octet-stream))
context "given a class that matches and a matcher that only specifies 'rejecting'" do
before 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
expect(matcher).to_not accept(Dummy)
end
should_accept_dummy_class
context "using an :if to control the validation" do
before do
Dummy.class_eval do
validates_attachment_content_type :avatar, content_type: %r{image/*} , if: :go
attr_accessor :go
end
end
context "given 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/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
rejecting(%w(audio/mp3 application/octet-stream))
end
it "run the validation if the control is true" do
dummy = Dummy.new
dummy.go = true
expect(matcher).to accept(dummy)
end
should_reject_dummy_class
it "not run the validation if the control is false" do
dummy = Dummy.new
dummy.go = false
expect(matcher).to_not accept(dummy)
end
end
context "using an :if to control the validation" do
before do
Dummy.class_eval 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
private
it "run the validation if the control is true" do
@dummy.go = true
expect(@matcher).to accept(@dummy)
end
def plain_matcher
self.class.validate_attachment_content_type(:avatar)
end
it "not run the validation if the control is false" do
@dummy.go = false
expect(@matcher).to_not accept(@dummy)
end
end
def matcher
plain_matcher.
allowing(%w(image/png image/jpeg)).
rejecting(%w(audio/mp3 application/octet-stream))
end
end
......@@ -4,70 +4,66 @@ 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
end
reset_class "Dummy"
Dummy.has_attached_file :avatar
Dummy.do_not_validate_attachment_file_type :avatar
@matcher = self.class.validate_attachment_presence(:avatar)
before do
reset_table("dummies") do |d|
d.string :avatar_file_name
end
reset_class "Dummy"
Dummy.has_attached_file :avatar
Dummy.do_not_validate_attachment_file_type :avatar
end
context "given a class with no validation" do
should_reject_dummy_class
end
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
Dummy.validates_attachment_presence :avatar
end
it "accepts a class with a matching validation" do
Dummy.validates_attachment_presence :avatar
expect(matcher).to accept(Dummy)
end
should_accept_dummy_class
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
context "given an instance with other attachment validations" do
before do
reset_table("dummies") do |d|
d.string :avatar_file_name
d.string :avatar_content_type
end
dummy.avatar = File.new fixture_file('5k.png')
Dummy.class_eval do
validates_attachment_presence :avatar
validates_attachment_content_type :avatar, content_type: 'image/gif'
end
expect(matcher).to accept(dummy)
end
@dummy = Dummy.new
@matcher = self.class.validate_attachment_presence(:avatar)
context "using an :if to control the validation" do
before do
Dummy.class_eval do
validates_attachment_presence :avatar, if: :go
attr_accessor :go
end
end
it "it should validate properly" do
@dummy.avatar = File.new fixture_file('5k.png')
expect(@matcher).to accept(@dummy)
end
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
context "using an :if to control the validation" do
before do
Dummy.class_eval do
validates_attachment_presence :avatar, if: :go
attr_accessor :go
end
@dummy = Dummy.new
@dummy.avatar = nil
end
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
it "run the validation if the control is true" do
@dummy.go = true
expect(@matcher).to accept(@dummy)
end
private
it "not run the validation if the control is false" do
@dummy.go = false
expect(@matcher).to_not accept(@dummy)
end
end
def matcher
self.class.validate_attachment_presence(:avatar)
end
end
......@@ -4,91 +4,85 @@ 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
d.integer :avatar_file_size
end
reset_class "Dummy"
Dummy.do_not_validate_attachment_file_type :avatar
Dummy.has_attached_file :avatar
before do
reset_table("dummies") do |d|
d.string :avatar_file_name
d.integer :avatar_file_size
end
reset_class "Dummy"
Dummy.do_not_validate_attachment_file_type :avatar
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
end
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
end
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
end
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
end
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)
end
context "allowing anything" do
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
it "given a class with a lower limit" do
Dummy.validates_attachment_size :avatar, greater_than: 1
expect(matcher).to accept(Dummy)
end
end
context "given a class with a lower limit" do
before { Dummy.validates_attachment_size :avatar, greater_than: 1 }
should_accept_dummy_class
context "using an :if to control the validation" do
before do
Dummy.class_eval do
validates_attachment_size :avatar, greater_than: 1024, if: :go
attr_accessor :go
end
end
context "using an :if to control the validation" do
before do
Dummy.class_eval 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 = Dummy.new
dummy.go = true
expect(matcher.greater_than(1024)).to accept(dummy)
end
it "run the validation if the control is true" do
@dummy.go = true
expect(@matcher).to accept(@dummy)
end
it "not run the validation if the control is false" do
dummy = Dummy.new
dummy.go = false
expect(matcher.greater_than(1024)).to_not accept(dummy)
end
end
it "not run the validation if the control is false" do
@dummy.go = false
expect(@matcher).to_not accept(@dummy)
end
context "post processing" do
before do
Dummy.validates_attachment_size :avatar, greater_than: 1024
end
context "post processing" do
before do
Dummy.validates_attachment_size :avatar, greater_than: 1024
it "be skipped" do
dummy = Dummy.new
dummy.avatar.expects(:post_process).never
expect(matcher.greater_than(1024)).to accept(dummy)
end
end
@dummy = Dummy.new
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
end
private
it "be skipped" do
@dummy.avatar.expects(:post_process).never
expect(@matcher).to accept(@dummy)
end
end
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