Skip to content

Commit

Permalink
More complete tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kookster committed Dec 15, 2024
1 parent 57a8701 commit 8d08165
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
4 changes: 4 additions & 0 deletions test/factories/media_resource_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
segmentation { [[1.23, 4.56]] }
task_count { 0 }
end

factory :external_media_resource, class: ExternalMediaResource do
original_url { "https://prx.org/audio.mp3" }
end
end
end
14 changes: 14 additions & 0 deletions test/factories/porter_callback_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@
initialize_with { attributes.with_indifferent_access }
end

factory :porter_job_analyze_results, class: Hash do
Time { "2012-12-21T12:34:56Z" }
Timestamp { 1356093296.0 }
JobResult do
{
Job: {Id: "the-job-id"},
State: "DONE",
TaskResults: [build(:porter_inspect_audio_result)],
FailedTasks: []
}
end
initialize_with { attributes.with_indifferent_access }
end

factory :porter_image_job_results, class: Hash do
Time { "2012-12-21T12:34:56Z" }
Timestamp { 1356093296.0 }
Expand Down
7 changes: 7 additions & 0 deletions test/factories/task_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
end
end

factory :analyze_media_task, class: Tasks::AnalyzeMediaTask do
association :owner, factory: :external_media_resource
status { :complete }
job_id { "1234" }
result { build(:porter_job_analyze_results) }
end

factory :copy_image_task, class: Tasks::CopyImageTask do
association :owner, factory: :episode_image_with_episode
status { :complete }
Expand Down
25 changes: 24 additions & 1 deletion test/models/concerns/episode_media_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class EpisodeMediaTest < ActiveSupport::TestCase
refute episode.feed_ready?

episode.medium = :override
episode.enclosure_override_url = :nil
episode.enclosure_override_url = nil
assert episode.override?
refute episode.feed_ready?

Expand All @@ -179,6 +179,29 @@ class EpisodeMediaTest < ActiveSupport::TestCase
end
end

it "creates override media when override url set" do
episode = create(:episode, segment_count: 2)
assert_nil episode.external_media_resource
episode.enclosure_override_url = "https://prx.org/a.mp3"
ExternalMediaResource.stub_any_instance(:analyze_media, true) do
episode.save!
end
assert_equal episode.external_media_resource.original_url, "https://prx.org/a.mp3"
end

it "creates override media when override url updated" do
ExternalMediaResource.stub_any_instance(:analyze_media, true) do
episode = create(:episode, enclosure_override_url: "https://prx.org/a.mp3")
assert_equal episode.external_media_resource.original_url, "https://prx.org/a.mp3"
emr_id = episode.external_media_resource.id
episode.external_media_resource

episode.update(enclosure_override_url: "https://prx.org/new.mp3")
assert_equal episode.external_media_resource.original_url, "https://prx.org/new.mp3"
assert emr_id != episode.external_media_resource.id
end
end

it "overrides media content type" do
c1.mime_type = "some/thing1"
assert_equal "some/thing1", ep.media_content_type
Expand Down
34 changes: 30 additions & 4 deletions test/models/external_media_resource_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
require "test_helper"

class ExternalMediaResourceTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
describe ExternalMediaResource do
let(:episode) { create(:episode, external_media_resource: media, enclosure_override_url: media.original_url) }
let(:media) { build(:external_media_resource) }

describe "#validate_episode_medium" do
it "does not have a guid" do
assert_nil media.guid
end

it "urls are the same as original" do
assert_equal media.url, media.original_url
assert_equal media.media_url, media.original_url
end

it "doesn't copy external media" do
# copy is a no-op
refute media.copy_media
end

it "analyzes the external media" do
Tasks::AnalyzeMediaTask.stub_any_instance(:porter_start!, true) do
media.analyze_media
media.reload
task = media.task
assert task.is_a?(Tasks::AnalyzeMediaTask)
assert_equal task.options["Source"]["URL"], "https://prx.org/audio.mp3"
assert_equal task.options["Tasks"].first["Type"], "Inspect"
end
end
end
end
66 changes: 66 additions & 0 deletions test/models/tasks/analyze_media_task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require "test_helper"

describe Tasks::AnalyzeMediaTask do
let(:task) { build_stubbed(:analyze_media_task) }

describe "#source_url" do
it "is the media resource href" do
task.media_resource.stub(:original_url, "whatev") do
assert_equal "whatev", task.source_url
end
end
end

describe "#porter_tasks" do
it "runs an inspect task" do
assert_equal "Inspect", task.porter_tasks[0][:Type]
end
end

describe "#update_media_resource" do
let(:task) { create(:copy_media_task) }

it "updates status before save" do
assert_equal task.status, "complete"
assert_equal task.media_resource.status, "complete"
task.update(status: "processing")
assert_equal task.status, "processing"
assert_equal task.media_resource.status, "processing"
end

it "replaces resources and publishes on complete" do
publish = Minitest::Mock.new

task.media_resource.episode.stub(:publish!, publish) do
task.update(status: "created")
assert publish.verify

publish.expect(:call, nil)
task.update(status: "complete")
assert publish.verify
end
end

it "updates audio metadata on complete" do
task.media_resource.reset_media_attributes

task.update(status: "created")
assert_nil task.media_resource.mime_type

task.update(status: "complete")
assert_equal 192, task.media_resource.bit_rate
assert_equal 2, task.media_resource.channels
assert_equal 1371.437, task.media_resource.duration
assert_equal 32980032, task.media_resource.file_size
assert_equal "audio", task.media_resource.medium
assert_equal "audio/mpeg", task.media_resource.mime_type
assert_equal 48000, task.media_resource.sample_rate

# this audio has a video stream (the ID3 image) - but we should
# not be parsing the metadata for it
assert_nil task.media_resource.frame_rate
assert_nil task.media_resource.height
assert_nil task.media_resource.width
end
end
end

0 comments on commit 8d08165

Please sign in to comment.