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