Skip to content

Commit

Permalink
Separated the delete logic from data extraction result job, modified …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
tomasdrga committed Oct 4, 2024
1 parent e338404 commit 7e0473a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
21 changes: 21 additions & 0 deletions app/jobs/metais/project_data_extraction_delete_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'net/http'
require 'uri'

class Metais::ProjectDataExtractionDeleteJob < ApplicationJob
queue_as :metais_data_extraction

def perform(project_uuid)
url = "#{ENV.fetch('API_URL')}/projects/#{project_uuid}"
uri = URI(url)

req = Net::HTTP::Delete.new(uri)
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(req)
end

unless res.is_a?(Net::HTTPSuccess)
error_message = "Failed to delete project: #{res.code}, body: #{res.body}"
raise RuntimeError, error_message
end
end
end
14 changes: 1 addition & 13 deletions app/jobs/metais/project_data_extraction_result_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def perform(project_uuid, location_header)
update_project_origin(project_origin, result)
process_harmonogram(result['harmonogram'], project_origin)

send_delete_request(URI(url))
Metais::ProjectDataExtractionDeleteJob.perform_later(project_uuid)
end

private
Expand Down Expand Up @@ -105,16 +105,4 @@ def parse_event_date(start_date)
rescue ArgumentError => e
raise RuntimeError, e
end

def send_delete_request(uri)
req = Net::HTTP::Delete.new(uri)
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(req)
end

unless res.is_a?(Net::HTTPSuccess)
error_message = "Failed to delete project: #{res.code}, body: #{res.body}"
raise RuntimeError, error_message
end
end
end
47 changes: 47 additions & 0 deletions spec/jobs/metais/project_data_extraction_delete_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'rails_helper'
require 'net/http'

RSpec.describe Metais::ProjectDataExtractionDeleteJob, type: :job do
include ActiveJob::TestHelper

let(:project_uuid) { 'sample-uuid' }
let(:api_url) { 'http://example.com/api' }
let(:url) { "#{api_url}/projects/#{project_uuid}" }
let(:uri) { URI(url) }
let(:response) { instance_double('Net::HTTPResponse') }

before do
allow(ENV).to receive(:fetch).with('API_URL').and_return(api_url)
allow(Net::HTTP).to receive(:start).and_return(response)
end

describe '#perform' do
context 'when the delete request is successful' do
before do
allow(response).to receive(:is_a?).with(Net::HTTPSuccess).and_return(true)
end

it 'sends a delete request' do
expect {
described_class.perform_now(project_uuid)
}.not_to raise_error

expect(Net::HTTP).to have_received(:start).with(uri.hostname, uri.port, use_ssl: uri.scheme == 'https')
end
end

context 'when the delete request fails' do
before do
allow(response).to receive(:is_a?).with(Net::HTTPSuccess).and_return(false)
allow(response).to receive(:code).and_return('500')
allow(response).to receive(:body).and_return('Internal Server Error')
end

it 'raises a RuntimeError with the correct message' do
expect {
described_class.perform_now(project_uuid)
}.to raise_error(RuntimeError, /Failed to delete project: 500, body: Internal Server Error/)
end
end
end
end
6 changes: 1 addition & 5 deletions spec/jobs/metais/project_data_extraction_result_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,12 @@
allow(Metais::ProjectEventType).to receive(:find_by).with(name: 'Predpoklad').and_return(event_type)

allow(Metais::ProjectEvent).to receive(:find_or_initialize_by).and_return(double('Metais::ProjectEvent', save!: true))

allow(Net::HTTP).to receive(:start).and_return(double('Net::HTTPResponse', is_a?: true, code: '200'))
end

it 'successfully processes the result and sends a delete request' do
expect {
described_class.perform_now(project_uuid, location_header)
}.not_to have_enqueued_job

expect(Net::HTTP).to have_received(:start)
}.to have_enqueued_job(Metais::ProjectDataExtractionDeleteJob).with(project_uuid)
end
end

Expand Down

0 comments on commit 7e0473a

Please sign in to comment.