Skip to content

Commit

Permalink
add new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Tooyosi committed Dec 4, 2024
1 parent 764236d commit 74fd273
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 7 deletions.
10 changes: 5 additions & 5 deletions app/services/batch/prediction/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def initialize(prediction_job, bajor_client = Bajor::Client.new)
def run
begin
subject_set_id = prediction_job.subject_set_id
context = Context.where(active_subject_set_id: subject_set_id).or(Context.where(pool_subject_set_id: subject_set_id)).order(Arel.sql("CASE WHEN active_subject_set_id = #{subject_set_id} THEN 0 ELSE 1 END"))
context = Context
.where(active_subject_set_id: subject_set_id)
.or(Context.where(pool_subject_set_id: subject_set_id))
.order(Arel.sql("CASE WHEN active_subject_set_id = #{subject_set_id} THEN 0 ELSE 1 END"))

workflow_name = nil
if context.first.present?
workflow_name = context.first.extractor_name
end
workflow_name = context.first&.extractor_name

bajor_job_url = bajor_client.create_prediction_job(prediction_job.manifest_url, workflow_name)
prediction_job.update(state: :submitted, service_job_url: bajor_job_url, message: '')
Expand Down
31 changes: 30 additions & 1 deletion spec/fixtures/contexts.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
galaxy_zoo_active_learning_project:
galaxy_zoo_cosmic_active_learning_project:
id: 1
workflow_id: 123
project_id: 39
Expand All @@ -7,3 +7,32 @@ galaxy_zoo_active_learning_project:
module_name: 'galaxy_zoo'
extractor_name: 'cosmic_dawn'

galaxy_zoo_euclid_active_learning_project:
id: 2
workflow_id: 133
project_id: 40
active_subject_set_id: 55
pool_subject_set_id: 67
module_name: 'galaxy_zoo'
extractor_name: 'euclid'


third_workflow_context:
id: 3
workflow_id: 143
project_id: 41
active_subject_set_id: 70
pool_subject_set_id: 80
module_name: 'galaxy_zoo'
extractor_name: 'third_workflow'

fourth_workflow_context:
id: 4
workflow_id: 153
project_id: 42
active_subject_set_id: 80
pool_subject_set_id: 70
module_name: 'galaxy_zoo'
extractor_name: 'fourth_workflow'


65 changes: 65 additions & 0 deletions spec/lib/bajor/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@
end
end

context 'with specific workflow_name' do
let(:workflow_name) { 'euclid' }
let(:run_opts) { '--schema euclid' }

let(:request_body) do
{
manifest_path: catalogue_manifest_path,
opts: {
run_opts: run_opts,
workflow_name: workflow_name
}
}
end
let(:request) do
stub_request(:post, request_url)
.with(
body: request_body,
headers: request_headers
)
end

before do
request.to_return(status: 201, body: bajor_response_body.to_json, headers: { content_type: 'application/json' })
end

it 'sends the right workflow name' do
bajor_client.create_training_job(catalogue_manifest_path, workflow_name)
expect(
a_request(:post, request_url).with(body: request_body, headers: request_headers)
).to have_been_made.once
end

end

context 'with a failed repsonse' do
let(:error_message) do
'Active Jobs are running in the batch system - please wait till they are fininshed processing.'
Expand Down Expand Up @@ -133,6 +167,37 @@
end
end

context 'with specific workflow_name' do
let(:workflow_name) { 'euclid' }
let(:run_opts) { '--schema euclid' }

let(:request_body) do
{ manifest_url: manifest_url, opts: { workflow_name: workflow_name} }
end

let(:request) do
stub_request(:post, request_url)
.with(
body: request_body,
headers: request_headers
)
end

before do
request.to_return(status: 201, body: bajor_response_body.to_json, headers: { content_type: 'application/json' })
end

it 'sends the right workflow name' do

bajor_client.create_prediction_job(manifest_url, 'euclid')


expect(
a_request(:post, request_url).with(body: request_body, headers: request_headers)
).to have_been_made.once
end
end

context 'with a failed repsonse' do
let(:error_message) do
'Active Jobs are running in the batch system - please wait till they are fininshed processing.'
Expand Down
42 changes: 41 additions & 1 deletion spec/services/batch/prediction/create_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

RSpec.describe Batch::Prediction::CreateJob do
describe '#run' do
fixtures :contexts

let(:manifest_url) { 'https://manifest-host.zooniverse.org/manifest.csv' }
let(:context){Context.first}
let(:context){ contexts(:galaxy_zoo_cosmic_active_learning_project) }
let(:prediction_job) do
PredictionJob.new(
manifest_url: manifest_url,
Expand Down Expand Up @@ -37,6 +39,44 @@
expect(bajor_client_double).to have_received(:create_prediction_job).with(manifest_url, context.extractor_name).once
end

describe 'prediction_job with pool_subject_set_id' do
let(:context){ contexts(:galaxy_zoo_euclid_active_learning_project) }
let(:prediction_job) do
PredictionJob.new(
manifest_url: manifest_url,
state: :pending,
subject_set_id: context.pool_subject_set_id,
probability_threshold: 0.5,
randomisation_factor: 0.5
)
end

it 'calls the bajor client service with workflow name from pool_subject_set_id' do
described_class.new(prediction_job, bajor_client_double).run
expect(bajor_client_double).to have_received(:create_prediction_job).with(manifest_url, context.extractor_name).once
end
end

describe 'with same active_subject_id and pool_subject_set_id' do
let(:context1){ contexts(:third_workflow_context) }
let(:context2){ contexts(:fourth_workflow_context) }
let(:prediction_job) do
PredictionJob.new(
manifest_url: manifest_url,
state: :pending,
subject_set_id: context2.pool_subject_set_id,
probability_threshold: 0.5,
randomisation_factor: 0.5
)
end

it 'calls the bajor client service with workflow name from an active_subject_set_id' do

described_class.new(prediction_job, bajor_client_double).run
expect(bajor_client_double).to have_received(:create_prediction_job).with(manifest_url, context1.extractor_name).once
end
end

it 'updates the state tracking info on the prediction job resource' do
expect {
prediction_create_job.run
Expand Down

0 comments on commit 74fd273

Please sign in to comment.