Permalink
Browse files

Start some specification

  • Loading branch information...
mike-burns committed May 12, 2012
1 parent d6a3a71 commit 42241d54ecfe09ce5b0ee7de61e87b4e0a95b6c7
View
@@ -5,3 +5,5 @@ gemspec
gem "jruby-openssl", :platform => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
gem "sqlite3", :platform => :ruby
+
+gem 'railties'
View
@@ -11,6 +11,16 @@ PATH
GEM
remote: http://rubygems.org/
specs:
+ actionpack (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
activemodel (3.2.3)
activesupport (= 3.2.3)
builder (~> 3.0.0)
@@ -58,6 +68,7 @@ GEM
json (>= 1.4.6)
term-ansicolor (>= 1.0.6)
diff-lcs (1.1.3)
+ erubis (2.7.0)
excon (0.13.4)
fakeweb (1.3.0)
ffi (1.0.11)
@@ -74,10 +85,12 @@ GEM
formatador (0.2.1)
gherkin (2.9.3)
json (>= 1.4.6)
+ hike (1.2.1)
httparty (0.8.3)
multi_json (~> 1.0)
multi_xml
i18n (0.6.0)
+ journey (1.0.3)
json (1.6.6)
launchy (2.1.0)
addressable (~> 2.2.6)
@@ -99,9 +112,22 @@ GEM
method_source (~> 0.7.1)
slop (>= 2.4.4, < 3)
rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
rack-test (0.6.1)
rack (>= 1.0)
+ railties (3.2.3)
+ actionpack (= 3.2.3)
+ activesupport (= 3.2.3)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
@@ -124,8 +150,14 @@ GEM
shoulda-context (1.0.0)
shoulda-matchers (1.0.0)
slop (2.4.4)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
term-ansicolor (1.0.7)
+ thor (0.14.6)
+ tilt (1.3.3)
tzinfo (0.3.33)
uuidtools (2.1.2)
xpath (0.1.4)
@@ -152,6 +184,8 @@ DEPENDENCIES
nokogiri
paperclip!
pry
+ railties
rake
+ rspec
shoulda
sqlite3
@@ -1,4 +1,5 @@
require 'uri'
+require 'active_support/core_ext/object/blank'
module Paperclip
class UrlGenerator
View
@@ -48,4 +48,5 @@ Gem::Specification.new do |s|
s.add_development_dependency('launchy')
s.add_development_dependency('rake')
s.add_development_dependency('fakeweb')
+ s.add_development_dependency('rspec')
end
@@ -0,0 +1,24 @@
+require 'spec_helper'
+require 'paperclip/attachment_options'
+
+describe Paperclip::AttachmentOptions, 'as a concept' do
+ subject { Paperclip::AttachmentOptions.new({}) }
+
+ it { should be_kind_of(Hash) }
+ it { should respond_to(:[]) }
+ it { should respond_to(:[]=) }
+end
+
+describe Paperclip::AttachmentOptions do
+ it "remembers options set with []=" do
+ attachment_options = Paperclip::AttachmentOptions.new({})
+ attachment_options[:foo] = "bar"
+ attachment_options[:foo].should == "bar"
+ end
+
+ it "delivers the specified options through []" do
+ intended_options = {specific_key: "specific value"}
+ attachment_options = Paperclip::AttachmentOptions.new(intended_options)
+ attachment_options[:specific_key].should == "specific value"
+ end
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+require 'paperclip/processor'
+
+describe Paperclip::Processor do
+ it 'instantiates and calls #make when sent .make' do
+ result = InheritedProcessor.make(:one, :two, :three)
+ result.should == [:one, :two, :three]
+ end
+end
+
+class InheritedProcessor < Paperclip::Processor
+ def make
+ [@file, @options, @attachment]
+ end
+end
@@ -0,0 +1,44 @@
+require 'spec_helper'
+require 'paperclip/schema'
+
+describe Paperclip::Schema, 'migrating up' do
+ subject do
+ MockSchema.new.tap do |mock_schema|
+ mock_schema.has_attached_file(:avatar)
+ end
+ end
+
+ it { should have_column(:avatar_file_name) }
+ it { should have_column(:avatar_content_type) }
+ it { should have_column(:avatar_file_size) }
+ it { should have_column(:avatar_updated_at) }
+
+ it 'makes the file_name column a string' do
+ subject.type_of(:avatar_file_name).should == :string
+ end
+
+ it 'makes the content_type column a string' do
+ subject.type_of(:avatar_content_type).should == :string
+ end
+
+ it 'makes the file_size column an integer' do
+ subject.type_of(:avatar_file_size).should == :integer
+ end
+
+ it 'makes the updated_at column a datetime' do
+ subject.type_of(:avatar_updated_at).should == :datetime
+ end
+end
+
+describe Paperclip::Schema, 'migrating down' do
+ subject do
+ MockSchema.new(:users).tap do |mock_schema|
+ mock_schema.drop_attached_file(:users, :avatar)
+ end
+ end
+
+ it { should have_deleted_column(:avatar_file_name) }
+ it { should have_deleted_column(:avatar_content_type) }
+ it { should have_deleted_column(:avatar_file_size) }
+ it { should have_deleted_column(:avatar_updated_at) }
+end
@@ -0,0 +1,169 @@
+require 'spec_helper'
+require 'paperclip/url_generator'
+
+describe Paperclip::UrlGenerator do
+ it "uses the given interpolator" do
+ expected = "the expected result"
+ mock_attachment = MockAttachment.new
+ mock_interpolator = MockInterpolator.new(result: expected)
+
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment,
+ interpolator: mock_interpolator)
+ result = url_generator.for(:style_name, {})
+
+ result.should == expected
+ mock_interpolator.should have_interpolated_attachment(mock_attachment)
+ mock_interpolator.should have_interpolated_style_name(:style_name)
+ end
+
+ it "uses the default URL when no file is assigned" do
+ mock_attachment = MockAttachment.new
+ mock_interpolator = MockInterpolator.new
+ default_url = "the default url"
+ options = { interpolator: mock_interpolator, default_url: default_url }
+
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+ url_generator.for(:style_name, {})
+
+ mock_interpolator.should have_interpolated_pattern(default_url)
+ end
+
+ it "executes the default URL lambda when no file is assigned" do
+ mock_attachment = MockAttachment.new
+ mock_interpolator = MockInterpolator.new
+ default_url = lambda {|attachment| "the #{attachment.class.name} default url" }
+ options = { interpolator: mock_interpolator, default_url: default_url }
+
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+ url_generator.for(:style_name, {})
+
+ mock_interpolator.should have_interpolated_pattern("the MockAttachment default url")
+ end
+
+ it "executes the method named by the symbol as the default URL when no file is assigned" do
+ mock_model = MockModel.new
+ mock_attachment = MockAttachment.new(model: mock_model)
+ mock_interpolator = MockInterpolator.new
+ default_url = :to_s
+ options = { interpolator: mock_interpolator, default_url: default_url }
+
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+ url_generator.for(:style_name, {})
+
+ mock_interpolator.should have_interpolated_pattern(mock_model.to_s)
+ end
+
+ it "URL-escapes spaces if asked to" do
+ expected = "the expected result"
+ mock_attachment = MockAttachment.new
+ mock_interpolator = MockInterpolator.new(result: expected)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, {escape: true})
+
+ result.should == "the%20expected%20result"
+ end
+
+ it "escapes the result of the interpolator using a method on the object, if asked to escape" do
+ expected = Class.new do
+ def escape
+ "the escaped result"
+ end
+ end.new
+ mock_attachment = MockAttachment.new
+ mock_interpolator = MockInterpolator.new(result: expected)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, {escape: true})
+
+ result.should == "the escaped result"
+ end
+
+ it "leaves spaces unescaped as asked to" do
+ expected = "the expected result"
+ mock_attachment = MockAttachment.new
+ mock_interpolator = MockInterpolator.new(result: expected)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, {escape: false})
+
+ result.should == "the expected result"
+ end
+
+ it "defaults to leaving spaces unescaped" do
+ expected = "the expected result"
+ mock_attachment = MockAttachment.new
+ mock_interpolator = MockInterpolator.new(result: expected)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, {})
+
+ result.should == "the expected result"
+ end
+
+ it "produces URLs without the updated_at value when the object does not respond to updated_at" do
+ expected = "the expected result"
+ mock_interpolator = MockInterpolator.new(result: expected)
+ mock_attachment = MockAttachment.new(responds_to_updated_at: false)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, timestamp: true)
+
+ result.should == expected
+ end
+
+ it "produces URLs without the updated_at value when the updated_at value is nil" do
+ expected = "the expected result"
+ mock_interpolator = MockInterpolator.new(result: expected)
+ mock_attachment = MockAttachment.new(responds_to_updated_at: true, updated_at: nil)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, timestamp: true)
+
+ result.should == expected
+ end
+
+ it "produces URLs with the updated_at when it exists, separated with a & if a ? follow by = already exists" do
+ expected = "the?expected=result"
+ updated_at = 1231231234
+ mock_interpolator = MockInterpolator.new(result: expected)
+ mock_attachment = MockAttachment.new(updated_at: updated_at)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, timestamp: true)
+
+ result.should == "#{expected}&#{updated_at}"
+ end
+
+ it "produces URLs without the updated_at when told to do as much" do
+ expected = "the expected result"
+ updated_at = 1231231234
+ mock_interpolator = MockInterpolator.new(result: expected)
+ mock_attachment = MockAttachment.new(updated_at: updated_at)
+ options = { interpolator: mock_interpolator }
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+
+ result = url_generator.for(:style_name, timestamp: false)
+
+ result.should == expected
+ end
+
+ it "produces the correct URL when the instance has a file name" do
+ expected = "the expected result"
+ mock_attachment = MockAttachment.new(original_filename: 'exists')
+ mock_interpolator = MockInterpolator.new
+ options = { interpolator: mock_interpolator, url: expected}
+
+ url_generator = Paperclip::UrlGenerator.new(mock_attachment, options)
+ url_generator.for(:style_name, {})
+
+ mock_interpolator.should have_interpolated_pattern(expected)
+ end
+end
View
@@ -0,0 +1,3 @@
+spec_dir = File.expand_path(File.dirname(__FILE__))
+helpers = Dir[File.join(spec_dir, 'support', '*')]
+helpers.each {|helper| require helper}
@@ -0,0 +1,22 @@
+class MockAttachment
+ attr_accessor :updated_at, :original_filename
+
+ def initialize(options = {})
+ @model = options[:model]
+ @responds_to_updated_at = options[:responds_to_updated_at]
+ @updated_at = options[:updated_at]
+ @original_filename = options[:original_filename]
+ end
+
+ def instance
+ @model
+ end
+
+ def respond_to?(meth)
+ if meth.to_s == "updated_at"
+ @responds_to_updated_at || @updated_at
+ else
+ super
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 42241d5

Please sign in to comment.