Reduce factory creation in `MediaAttachment` model spec (#31058)

This commit is contained in:
Matt Jankowski 2024-07-18 11:23:46 -04:00 committed by GitHub
parent 41b7281b56
commit 848b59c8ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 38 deletions

View File

@ -90,7 +90,7 @@ RSpec.describe MediaAttachment, :attachment_processing do
media.destroy media.destroy
end end
it 'saves media attachment with correct file metadata' do it 'saves media attachment with correct file and size metadata' do
expect(media) expect(media)
.to be_persisted .to be_persisted
.and be_processing_complete .and be_processing_complete
@ -103,14 +103,12 @@ RSpec.describe MediaAttachment, :attachment_processing do
# Rack::Mime (used by PublicFileServerMiddleware) recognizes file extension # Rack::Mime (used by PublicFileServerMiddleware) recognizes file extension
expect(Rack::Mime.mime_type(extension, nil)).to eq content_type expect(Rack::Mime.mime_type(extension, nil)).to eq content_type
end
it 'saves media attachment with correct size metadata' do # Strip original file name
# strips original file name
expect(media.file_file_name) expect(media.file_file_name)
.to_not start_with '600x400' .to_not start_with '600x400'
# sets meta for original and thumbnail # Set meta for original and thumbnail
expect(media.file.meta.deep_symbolize_keys) expect(media.file.meta.deep_symbolize_keys)
.to include( .to include(
original: include( original: include(
@ -174,10 +172,18 @@ RSpec.describe MediaAttachment, :attachment_processing do
let(:media) { Fabricate(:media_attachment, file: attachment_fixture('avatar.gif')) } let(:media) { Fabricate(:media_attachment, file: attachment_fixture('avatar.gif')) }
it 'sets correct file metadata' do it 'sets correct file metadata' do
expect(media.type).to eq 'gifv' expect(media)
expect(media.file_content_type).to eq 'video/mp4' .to have_attributes(
expect(media.file.meta['original']['width']).to eq 128 type: eq('gifv'),
expect(media.file.meta['original']['height']).to eq 128 file_content_type: eq('video/mp4')
)
expect(media_metadata)
.to include(
original: include(
width: eq(128),
height: eq(128)
)
)
end end
end end
@ -192,11 +198,19 @@ RSpec.describe MediaAttachment, :attachment_processing do
let(:media) { Fabricate(:media_attachment, file: attachment_fixture(fixture[:filename])) } let(:media) { Fabricate(:media_attachment, file: attachment_fixture(fixture[:filename])) }
it 'sets correct file metadata' do it 'sets correct file metadata' do
expect(media.type).to eq 'image' expect(media)
expect(media.file_content_type).to eq 'image/gif' .to have_attributes(
expect(media.file.meta['original']['width']).to eq fixture[:width] type: eq('image'),
expect(media.file.meta['original']['height']).to eq fixture[:height] file_content_type: eq('image/gif')
expect(media.file.meta['original']['aspect']).to eq fixture[:aspect] )
expect(media_metadata)
.to include(
original: include(
width: eq(fixture[:width]),
height: eq(fixture[:height]),
aspect: eq(fixture[:aspect])
)
)
end end
end end
end end
@ -204,39 +218,42 @@ RSpec.describe MediaAttachment, :attachment_processing do
describe 'ogg with cover art' do describe 'ogg with cover art' do
let(:media) { Fabricate(:media_attachment, file: attachment_fixture('boop.ogg')) } let(:media) { Fabricate(:media_attachment, file: attachment_fixture('boop.ogg')) }
let(:expected_media_duration) { 0.235102 }
# The libvips and ImageMagick implementations produce different results
let(:expected_background_color) { Rails.configuration.x.use_vips ? '#268cd9' : '#3088d4' }
it 'sets correct file metadata' do it 'sets correct file metadata' do
expect(media.type).to eq 'audio' expect(media)
expect(media.file.meta['original']['duration']).to be_within(0.05).of(0.235102) .to have_attributes(
expect(media.thumbnail.present?).to be true type: eq('audio'),
thumbnail: be_present,
file_file_name: not_eq('boop.ogg')
)
expect(media.file.meta['colors']['background']).to eq(expected_background_color) expect(media_metadata)
expect(media.file_file_name).to_not eq 'boop.ogg' .to include(
end original: include(duration: be_within(0.05).of(expected_media_duration)),
colors: include(background: eq(expected_background_color))
def expected_background_color )
# The libvips and ImageMagick implementations produce different results
Rails.configuration.x.use_vips ? '#268cd9' : '#3088d4'
end end
end end
describe 'mp3 with large cover art' do describe 'mp3 with large cover art' do
let(:media) { Fabricate(:media_attachment, file: attachment_fixture('boop.mp3')) } let(:media) { Fabricate(:media_attachment, file: attachment_fixture('boop.mp3')) }
let(:expected_media_duration) { 0.235102 }
it 'detects it as an audio file' do it 'detects file type and sets correct metadata' do
expect(media.type).to eq 'audio' expect(media)
end .to have_attributes(
type: eq('audio'),
it 'sets meta for the duration' do thumbnail: be_present,
expect(media.file.meta['original']['duration']).to be_within(0.05).of(0.235102) file_file_name: not_eq('boop.mp3')
end )
expect(media_metadata)
it 'extracts thumbnail' do .to include(
expect(media.thumbnail.present?).to be true original: include(duration: be_within(0.05).of(expected_media_duration))
end )
it 'gives the file a random name' do
expect(media.file_file_name).to_not eq 'boop.mp3'
end end
end end
@ -274,4 +291,10 @@ RSpec.describe MediaAttachment, :attachment_processing do
expect(media.valid?).to be true expect(media.valid?).to be true
end end
end end
private
def media_metadata
media.file.meta.deep_symbolize_keys
end
end end

View File

@ -161,6 +161,7 @@ RSpec::Sidekiq.configure do |config|
end end
RSpec::Matchers.define_negated_matcher :not_change, :change RSpec::Matchers.define_negated_matcher :not_change, :change
RSpec::Matchers.define_negated_matcher :not_eq, :eq
RSpec::Matchers.define_negated_matcher :not_include, :include RSpec::Matchers.define_negated_matcher :not_include, :include
def request_fixture(name) def request_fixture(name)