Skip to content

Commit

Permalink
lose the test doubles, use real models
Browse files Browse the repository at this point in the history
Still I don't understand most of this. Paging doctor Tim! @tf
  • Loading branch information
tilsammans committed Dec 31, 2017
1 parent 98b3ef2 commit 518acca
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 76 deletions.
12 changes: 4 additions & 8 deletions spec/models/pageflow/url_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@ module Pageflow
let(:video_file) { create(:video_file, :on_filesystem) }

it 'returns url with :id_partition placeholder' do
VideoFile.has_attached_file :attachment_on_filesystem, {
VideoFile.has_attached_file :attachment_on_filesystem,
url: '/:class/:attachment/:id_partition/:style/:filename'
}
result = described_class.from_attachment(video_file.attachment_on_filesystem)

expect(result).to eq('/pageflow/video_files/attachment_on_filesystems/:id_partition/original/video.mp4?1356994800')
end

it 'only replaces last set of digits' do
VideoFile.has_attached_file :attachment_on_filesystem, {
VideoFile.has_attached_file :attachment_on_filesystem,
url: '/123/:class/:id_partition/:style/:filename'
}
result = described_class.from_attachment(video_file.attachment_on_filesystem)

expect(result).to eq('/123/pageflow/video_files/:id_partition/original/video.mp4?1356994800')
end

it 'ignores other numeric values in url pattern' do
VideoFile.has_attached_file :attachment_on_filesystem, {
VideoFile.has_attached_file :attachment_on_filesystem,
url: '/_a123/_e1001/:class/:id_partition/:style/:filename'
}
result = described_class.from_attachment(video_file.attachment_on_filesystem)

expect(result).to eq('/_a123/_e1001/pageflow/video_files/:id_partition/original/video.mp4?1356994800')
end

it 'ignores numeric file names' do
VideoFile.has_attached_file :attachment_on_filesystem, {
VideoFile.has_attached_file :attachment_on_filesystem,
url: '/:class/:id_partition/:style/:filename',
filename: '123'
}
video_file = create(:video_file, :numeric_filename_on_filesystem)
result = described_class.from_attachment(video_file.attachment_on_filesystem)

Expand Down
112 changes: 48 additions & 64 deletions spec/models/pageflow/zencoder_attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,168 +18,157 @@ module Pageflow
zencoder_options[:attachments_version]
end

def file_double(id:)
double('File', id: id, class: double(to_s: 'File'))
let(:video_file) do
create :video_file, :on_filesystem
end

let(:id_partition) do
("%09d" % video_file.id).scan(/\d{3}/).join("/")
end

describe '#original_filename' do
it 'defaults to file_name_pattern' do
attachment = ZencoderAttachment.new(file_double(id: 5), 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

expect(attachment.original_filename).to eq('video.mp4')
end

it 'replaces {{number}} in with 0' do
attachment = ZencoderAttachment.new(file_double(id: 5), 'thumbnail-{{number}}.jpg')
attachment = ZencoderAttachment.new(video_file, 'thumbnail-{{number}}.jpg')

expect(attachment.original_filename).to eq('thumbnail-0.jpg')
end

it 'appends format option' do
attachment = ZencoderAttachment.new(file_double(id: 5), 'thumbnail', format: 'jpg')
attachment = ZencoderAttachment.new(video_file, 'thumbnail', format: 'jpg')

expect(attachment.original_filename).to eq('thumbnail.jpg')
end

it 'supports slashes in file name pattern' do
attachment = ZencoderAttachment.new(file_double(id: 5), 'dash/video.mp4')
attachment = ZencoderAttachment.new(video_file, 'dash/video.mp4')

expect(attachment.original_filename).to eq('dash/video.mp4')
end
end

describe '#path' do
it 'uses paperclip for interpolation' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

expect(attachment.path).to eq("/#{version}/test-host/files/000/000/005/video.mp4")
expect(attachment.path).to eq("/#{version}/test-host/pageflow/video_files/#{id_partition}/video.mp4")
end

it 'replaces {{number}} in with 0' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'thumbnail-{{number}}.jpg')
attachment = ZencoderAttachment.new(video_file, 'thumbnail-{{number}}.jpg')

expect(attachment.path).to eq("/#{version}/test-host/files/000/000/005/thumbnail-0.jpg")
expect(attachment.path).to eq("/#{version}/test-host/pageflow/video_files/#{id_partition}/thumbnail-0.jpg")
end

it 'appends format option' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'thumbnail', format: 'jpg')
attachment = ZencoderAttachment.new(video_file, 'thumbnail', format: 'jpg')

expect(attachment.path).to eq("/#{version}/test-host/files/000/000/005/thumbnail.jpg")
expect(attachment.path).to eq("/#{version}/test-host/pageflow/video_files/#{id_partition}/thumbnail.jpg")
end

it 'supports slashed in file name pattern' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'dash/video.mp4')
attachment = ZencoderAttachment.new(video_file, 'dash/video.mp4')

expect(attachment.path).to eq("/#{version}/test-host/files/000/000/005/dash/video.mp4")
expect(attachment.path).to eq("/#{version}/test-host/pageflow/video_files/#{id_partition}/dash/video.mp4")
end
end

describe '#url' do
it 'uses s3_protocol and s3_host_alias from zencoder config' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

expect(attachment.url).to eq("#{s3_protocol}://#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/video.mp4')
"test-host/pageflow/video_files/#{id_partition}/video.mp4")
end

it 'supports protocol relative urls' do
Pageflow.config.zencoder_options[:s3_protocol] = ''
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

expect(attachment.url).to eq("//#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/video.mp4')
"test-host/files/#{id_partition}/video.mp4")
end

it 'supports slashes file name pattern' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'dash/video.mp4')
attachment = ZencoderAttachment.new(video_file, 'dash/video.mp4')

expect(attachment.url).to eq("#{s3_protocol}://#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/dash/video.mp4')
"test-host/pageflow/video_files/#{id_partition}/dash/video.mp4")
end

context 'with default_protocol options ' do
it 'prepends protocol if url would be protocol relative' do
Pageflow.config.zencoder_options[:s3_protocol] = ''
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

result = attachment.url(default_protocol: 'http')

expect(result).to eq("http://#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/video.mp4')
"test-host/pageflow/video_files/#{id_partition}/video.mp4")
end

it 'does not alter relative urls' do
Pageflow.config.zencoder_options[:s3_protocol] = ''
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4', url: ':filename')
attachment = ZencoderAttachment.new(video_file, 'video.mp4', url: ':filename')

expect(attachment.url(default_protocol: 'http')).to eq('video.mp4')
end

it 'does not alter protocol if configured' do
Pageflow.config.zencoder_options[:s3_protocol] = 'https'
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

result = attachment.url(default_protocol: 'http')

expect(result).to eq("https://#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/video.mp4')
"test-host/pageflow/video_files/#{id_partition}/video.mp4")
end
end

it 'can append unique id to url' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

result = attachment.url(unique_id: 3)

expect(result).to eq("#{s3_protocol}://#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/video.mp4?n=3')
"test-host/pageflow/video_files/#{id_partition}/video.mp4?n=3")
end

it 'can append url suffix to url' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'playlist.smil', url_suffix: '/master.m3u8')
attachment = ZencoderAttachment.new(video_file, 'playlist.smil', url_suffix: '/master.m3u8')

expect(attachment.url).to eq("#{s3_protocol}://#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/playlist.smil/master.m3u8')
"test-host/pageflow/video_files/#{id_partition}/playlist.smil/master.m3u8")
end

it 'can append url suffix and unique id to url' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'playlist.smil', url_suffix: '/master.m3u8')
attachment = ZencoderAttachment.new(video_file, 'playlist.smil', url_suffix: '/master.m3u8')

result = attachment.url(unique_id: 3)

expect(result).to eq("#{s3_protocol}://#{s3_host_alias}/#{version}/" \
'test-host/files/000/000/005/playlist.smil/master.m3u8?n=3')
"test-host/pageflow/video_files/#{id_partition}/playlist.smil/master.m3u8?n=3")
end
end

describe '#url_relative_to' do
it 'returns url which is relative to given other ZencoderAttachment' do
file = file_double(id: 5)
manifest = ZencoderAttachment.new(file, 'manifest.mpd')
attachment = ZencoderAttachment.new(file, 'high/rendition.mpd')
manifest = ZencoderAttachment.new(video_file, 'manifest.mpd')
attachment = ZencoderAttachment.new(video_file, 'high/rendition.mpd')

result = attachment.url_relative_to(manifest)

expect(result).to eq('high/rendition.mpd')
end

it 'handles common sub directory' do
file = file_double(id: 5)
manifest = ZencoderAttachment.new(file, 'dash/manifest.mpd')
attachment = ZencoderAttachment.new(file, 'dash/high/rendition.mpd')
manifest = ZencoderAttachment.new(video_file, 'dash/manifest.mpd')
attachment = ZencoderAttachment.new(video_file, 'dash/high/rendition.mpd')

result = attachment.url_relative_to(manifest)

Expand All @@ -188,19 +177,17 @@ def file_double(id:)

it 'handles protocol relative urls correctly' do
Pageflow.config.zencoder_options[:s3_protocol] = ''
file = file_double(id: 5)
manifest = ZencoderAttachment.new(file, 'dash/manifest.mpd')
attachment = ZencoderAttachment.new(file, 'dash/high/rendition.mpd')
manifest = ZencoderAttachment.new(video_file, 'dash/manifest.mpd')
attachment = ZencoderAttachment.new(video_file, 'dash/high/rendition.mpd')

result = attachment.url_relative_to(manifest)

expect(result).to eq('high/rendition.mpd')
end

it 'fails if other attachment is in other directory' do
file = file_double(id: 5)
manifest = ZencoderAttachment.new(file, 'other/manifest.mpd')
attachment = ZencoderAttachment.new(file, 'dash/high/rendition.mpd')
manifest = ZencoderAttachment.new(video_file, 'other/manifest.mpd')
attachment = ZencoderAttachment.new(video_file, 'dash/high/rendition.mpd')

expect {
attachment.url_relative_to(manifest)
Expand All @@ -210,24 +197,21 @@ def file_double(id:)

describe '#dir_name' do
it 'returns directory path' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'video.mp4')
attachment = ZencoderAttachment.new(video_file, 'video.mp4')

expect(attachment.dir_name).to eq("/#{version}/test-host/files/000/000/005")
expect(attachment.dir_name).to eq("/#{version}/test-host/pageflow/video_files/#{id_partition}")
end

it 'includes dir components from file_name_pattern' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'dir/video.mp4')
attachment = ZencoderAttachment.new(video_file, 'dir/video.mp4')

expect(attachment.dir_name).to eq("/#{version}/test-host/files/000/000/005/dir")
expect(attachment.dir_name).to eq("/#{version}/test-host/pageflow/video_files/#{id_partition}/dir")
end
end

describe '#base_name_pattern' do
it 'removes dirs from pattern' do
file = file_double(id: 5)
attachment = ZencoderAttachment.new(file, 'dir/video-{{number}}')
attachment = ZencoderAttachment.new(video_file, 'dir/video-{{number}}')

expect(attachment.base_name_pattern).to eq('video-{{number}}')
end
Expand Down
6 changes: 3 additions & 3 deletions spec/pageflow/paperclip_interpolations/support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module PaperclipInterpolations

it 'has pageflow_placeholder' do
support = described_class.new(video_file.attachment_on_filesystem, 'thumbnailxl')
expect(support.pageflow_placeholder).to eq("pageflow/placeholder_thumbnailxl.jpg")
expect(support.pageflow_placeholder).to eq('pageflow/placeholder_thumbnailxl.jpg')
end

describe 'pageflow_attachments_version' do
Expand All @@ -17,7 +17,7 @@ module PaperclipInterpolations
end

support = described_class.new(video_file.attachment_on_filesystem, 'thumbnailxl')
expect(support.pageflow_attachments_version).to eq("303/")
expect(support.pageflow_attachments_version).to eq('303/')
end

it 'is nil for :original style' do
Expand All @@ -41,7 +41,7 @@ module PaperclipInterpolations

it 'has class_basename' do
support = described_class.new(video_file.attachment_on_filesystem, 'thumbnailxs')
expect(support.class_basename).to eq("video_files")
expect(support.class_basename).to eq('video_files')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/pageflow/zencoder_video_output_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Pageflow
describe ZencoderVideoOutputDefinition do
describe '#outputs' do
it 'contains all definitions except highdef per default' do
video_file = build(:video_file)
video_file = create(:video_file)
definition = ZencoderVideoOutputDefinition.new(video_file)
definition.skip_hls = false
definition.skip_smil = false
Expand Down

0 comments on commit 518acca

Please sign in to comment.