Skip to content

Commit

Permalink
fixed failing rspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasdrga committed Sep 30, 2024
1 parent bc05039 commit 0ace20a
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 93 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def preview
@phase_revision = @phase.revisions.find_by(revision: @page.revisions.find_by(version: params['version']))
end
@ratings_by_type = @phase_revision.ratings.index_by(&:rating_type)
@project = @phase.project

else
if params['version'] == 'latest'
Expand Down
3 changes: 1 addition & 2 deletions app/jobs/metais/sync_project_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def perform(project, metais_project)

project_origin.save!

# TODO uncomment later if needed but mind the request price
# Metais::ProjectDataExtractionJob.set(wait: 5.minutes).perform_later(metais_project.uuid)
Metais::ProjectDataExtractionJob.set(wait: 5.minutes).perform_later(metais_project.uuid)
Metais::SyncProjectSuppliersJob.perform_later(project_origin, metais_project)
Metais::SyncProjectDocumentsJob.perform_later(project_origin, metais_project)
Metais::SyncProjectEventsJob.perform_later(project_origin, metais_project)
Expand Down
58 changes: 34 additions & 24 deletions app/jobs/metais/sync_project_suppliers_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
class Metais::SyncProjectSuppliersJob < ApplicationJob
queue_as :metais

ORIGIN_TYPE = Metais::OriginType.find_by(name: 'MetaIS')

def perform(project_origin, metais_project)
origin_type = Metais::OriginType.find_by(name: 'MetaIS')

if metais_project.latest_version.link_nfp.present?
supplier_type = Metais::SupplierType.find_by(name: "NFP")

Expand All @@ -20,7 +20,7 @@ def perform(project_origin, metais_project)
value: url,
date: metais_project.latest_version.datum_nfp,
project_origin: project_origin,
origin_type: ORIGIN_TYPE,
origin_type: origin_type,
supplier_type: supplier_type)

project_supplier.save!
Expand All @@ -41,7 +41,7 @@ def perform(project_origin, metais_project)
value: url,
date: metais_project.latest_version.vyhlasenie_vo,
project_origin: project_origin,
origin_type: ORIGIN_TYPE,
origin_type: origin_type,
supplier_type: supplier_type)

project_supplier.save!
Expand All @@ -54,24 +54,13 @@ def perform(project_origin, metais_project)

link = metais_project.latest_version.zmluva_o_dielo_crz

document = Nokogiri::HTML(open(link))

li_elements = document.css('li.py-2.border-top')

li_elements.each_with_index do |li_element, i|
supplier_label = li_element.at_css('strong.col-sm-3.text-sm-end')&.text&.strip
if supplier_label == 'Dodávateľ:'
supplier_info = li_element.at_css('span.col-sm-9')&.text&.strip
cin_label = li_elements[i + 1].at_css('strong.col-sm-3.text-sm-end')&.text&.strip
if cin_label == 'IČO:'
cin = li_elements[i + 1].at_css('span.col-sm-9')&.text&.strip
if supplier_info && cin
project_origin.supplier = supplier_info
project_origin.supplier_cin = cin
project_origin.save!
end
end
end
document = Nokogiri::HTML(open(link).read.force_encoding('UTF-8'))
crz_data = parse_crz_document(document)

if crz_data[:supplier] && crz_data[:cin]
project_origin.supplier = crz_data[:supplier]
project_origin.supplier_cin = crz_data[:cin]
project_origin.save!
end

links = extract_links(link)
Expand All @@ -82,7 +71,7 @@ def perform(project_origin, metais_project)
value: url,
date: metais_project.latest_version.zmluva_o_dielo,
project_origin: project_origin,
origin_type: ORIGIN_TYPE,
origin_type: origin_type,
supplier_type: supplier_type)

project_supplier.save!
Expand All @@ -102,6 +91,27 @@ def valid_url?(url)
end

def extract_links(str)
str.to_s.scan(URI.regexp(['http','https']))
uri_regexp = /\b(?:https?|ftp):\/\/\S+\b/
str.to_s.scan(uri_regexp)
end

def parse_crz_document(document)
li_elements = document.css('li.py-2.border-top')
supplier_info = nil
cin = nil

li_elements.each_with_index do |li_element, i|
supplier_label = li_element.at_css('strong.col-sm-3.text-sm-end')&.text&.strip
if supplier_label == 'Dodávateľ:'
supplier_info = li_element.at_css('span.col-sm-9')&.text&.strip
cin_label = li_elements[i + 1].at_css('strong.col-sm-3.text-sm-end')&.text&.strip
if cin_label == 'IČO:'
cin = li_elements[i + 1].at_css('span.col-sm-9')&.text&.strip
break if supplier_info && cin
end
end
end

{ supplier: supplier_info, cin: cin }
end
end
1 change: 0 additions & 1 deletion app/views/admin/pages/preview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</div>

<% if @phase.present? %>
<% @project = @phase_revision %>
<%= render file: 'phase_revision/show' %>
<% else %>
<% @page = @phase_revision %>
Expand Down
5 changes: 4 additions & 1 deletion app/views/metais/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<%= form_with url: metais_projects_path, method: :get, class: 'form my-2 my-lg-0', local: true, id: "form" do |form| %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= hidden_field_tag :sort_direction, params[:sort_direction] %>

<%= hidden_field_tag :status, params[:status] %>
<%= hidden_field_tag :guarantor, params[:guarantor] %>
<%= hidden_field_tag :has_evaluation, params[:has_evaluation] %>

<div class="row">
<div class="col-md-6">
<div class="form-group d-flex flex-row">
Expand Down
41 changes: 33 additions & 8 deletions spec/jobs/metais/daily_sync_projects_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
require 'rails_helper'

RSpec.describe Metais::SyncProjectSuppliersJob, type: :job do
let!(:origin_type) { Metais::OriginType.create!(name: 'MetaIS') }

let(:supplier_type_nfp) { Metais::SupplierType.create!(name: "NFP") }
let(:supplier_type_vo) { Metais::SupplierType.create!(name: "VO") }
let(:supplier_type_crz) { Metais::SupplierType.create!(name: "CRZ") }

let(:project_origin) { Metais::ProjectOrigin.create! }
let!(:metais_project) { create(:metais_project) }
let(:project_origin) { Metais::ProjectOrigin.create!(project: metais_project, origin_type: origin_type, title: "Test") }

let(:latest_version) { double('LatestVersion', link_nfp: 'http://example.com/nfp', vo: 'http://example.com/vo', zmluva_o_dielo_crz: 'http://example.com/crz', datum_nfp: Date.today, vyhlasenie_vo: Date.today, zmluva_o_dielo: Date.today) }
let(:metais_project) { instance_double(Metais::Project, latest_version: latest_version) }
let(:latest_version) { double('LatestVersion', kod_metais: 'code1', link_nfp: "http://example.com/nfp", datum_nfp: Date.today, vo: "http://example.com/vo", vyhlasenie_vo: Date.today, zmluva_o_dielo_crz: "http://example.com/crz", zmluva_o_dielo: Date.today) }
let(:datahub_project) { instance_double(Datahub::Metais::Project, uuid: 'uuid1', latest_version: latest_version) }

before do
allow(datahub_project).to receive(:latest_version).and_return(latest_version)

allow(Metais::SupplierType).to receive(:find_by).with(name: "NFP").and_return(supplier_type_nfp)
allow(Metais::SupplierType).to receive(:find_by).with(name: "VO").and_return(supplier_type_vo)
allow(Metais::SupplierType).to receive(:find_by).with(name: "CRZ").and_return(supplier_type_crz)
allow_any_instance_of(Metais::SyncProjectSuppliersJob).to receive(:extract_links).and_return([])

crz_html = <<-HTML
<html>
<body>
<ul>
<li class='py-2 border-top'>
<strong class='col-sm-3 text-sm-end'>Dodávateľ:</strong>
<span class='col-sm-9'>Supplier Name</span>
</li>
<li class='py-2 border-top'>
<strong class='col-sm-3 text-sm-end'>IČO:</strong>
<span class='col-sm-9'>12345678</span>
</li>
</ul>
</body>
</html>
HTML

stub_request(:get, "http://example.com/crz").to_return(body: crz_html, headers: { 'Content-Type' => 'text/html' })
allow_any_instance_of(Metais::SyncProjectSuppliersJob).to receive(:open).with("http://example.com/crz").and_return(StringIO.new(crz_html))
end

it 'creates ProjectSupplier records for NFP links' do
allow_any_instance_of(Metais::SyncProjectSuppliersJob).to receive(:extract_links).with('http://example.com/nfp').and_return(['http://supplier1.com', 'http://supplier2.com'])
expect { described_class.perform_now(project_origin, metais_project) }.to change(Metais::ProjectSupplier, :count).by(2)
expect { described_class.perform_now(project_origin, datahub_project) }.to change(Metais::ProjectSupplier, :count).by(2)

suppliers = Metais::ProjectSupplier.where(supplier_type: supplier_type_nfp)
expect(suppliers.map(&:name)).to contain_exactly('http://supplier1.com', 'http://supplier2.com')
end

it 'creates ProjectSupplier records for VO links' do
allow_any_instance_of(Metais::SyncProjectSuppliersJob).to receive(:extract_links).with('http://example.com/vo').and_return(['http://supplier3.com'])
expect { described_class.perform_now(project_origin, metais_project) }.to change(Metais::ProjectSupplier, :count).by(1)
expect { described_class.perform_now(project_origin, datahub_project) }.to change(Metais::ProjectSupplier, :count).by(1)

supplier = Metais::ProjectSupplier.find_by(name: 'http://supplier3.com')
expect(supplier).to be_present
Expand All @@ -39,11 +64,11 @@
allow_any_instance_of(Metais::SyncProjectSuppliersJob).to receive(:valid_url?).and_return(true)

# Mock the parsing to return specific values
allow_any_instance_of(Metais::SyncProjectSuppliersJob).to receive(:parse_crz_document).and_return({ supplier: 'Supplier Info', cin: '12345678' })
allow_any_instance_of(Metais::SyncProjectSuppliersJob).to receive(:parse_crz_document).and_return({ supplier: 'Supplier Info', cin: 12345678 })

expect { described_class.perform_now(project_origin, metais_project) }.to change(Metais::ProjectSupplier, :count).by(1)
expect { described_class.perform_now(project_origin, datahub_project) }.to change(Metais::ProjectSupplier, :count).by(1)

expect(project_origin.supplier).to eq('Supplier Info')
expect(project_origin.supplier_cin).to eq('12345678')
expect(project_origin.supplier_cin).to eq(12345678)
end
end
9 changes: 6 additions & 3 deletions spec/jobs/metais/sync_project_events_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
include ActiveJob::TestHelper

let(:project_origin) { instance_double('Metais::ProjectOrigin') }
let(:metais_project) { instance_double('Metais::Project', latest_version: 'v1') }

let(:origin_type) { instance_double('Metais::OriginType') }
let(:event_type) { instance_double('Metais::ProjectEventType') }

let(:latest_version) { double('LatestVersion', kod_metais: 'code1') }
let(:datahub_project) { instance_double(Datahub::Metais::Project, uuid: 'uuid1', latest_version: latest_version) }

let(:status_change) do
instance_double('Datahub::Metais::ProjectChange',
field: 'status',
Expand All @@ -33,7 +36,7 @@
allow(Metais::OriginType).to receive(:find_by).with(name: 'MetaIS').and_return(origin_type)
allow(Metais::ProjectEventType).to receive(:find_by).with(name: 'Realita').and_return(event_type)

allow(Datahub::Metais::ProjectChange).to receive(:where).with(project_version: metais_project.latest_version).and_return([status_change, phase_change])
allow(Datahub::Metais::ProjectChange).to receive(:where).with(project_version: datahub_project.latest_version).and_return([status_change, phase_change])

allow(Datahub::Metais::CodelistProjectState).to receive(:find_by).with(code: 'old_status').and_return(codelist_project_state_old)
allow(Datahub::Metais::CodelistProjectState).to receive(:find_by).with(code: 'new_status').and_return(codelist_project_state_new)
Expand All @@ -47,7 +50,7 @@

describe '#perform' do
it 'processes all changes and creates events correctly' do
described_class.perform_now(project_origin, metais_project)
described_class.perform_now(project_origin, datahub_project)

expect(Metais::ProjectEvent).to have_received(:find_or_initialize_by).with(
project_origin: project_origin,
Expand Down
20 changes: 8 additions & 12 deletions spec/jobs/metais/sync_project_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
schvalene_rocne_naklady: 25_000)
end

let(:metais_project) { double(Datahub::Metais::Project, latest_version: latest_version, uuid: 'uuid1') }
let(:datahub_project) { double(Datahub::Metais::Project, latest_version: latest_version, uuid: 'uuid1') }
let(:project) { double(Metais::Project) }
let(:project_origin) { instance_double(Metais::ProjectOrigin, save!: true) }

Expand Down Expand Up @@ -60,17 +60,13 @@
end

it 'updates or creates a ProjectOrigin and enqueues subsequent jobs' do
Metais::SyncProjectJob.perform_now(project, metais_project)
Metais::SyncProjectJob.perform_now(project, datahub_project)

expect(enqueued_jobs).to include(
a_hash_including(
job: Metais::ProjectDataExtractionJob,
args: [metais_project.uuid],
queue: 'metais_data_extraction'
)
)
expect(Metais::SyncProjectSuppliersJob).to have_received(:perform_later).with(project_origin, metais_project)
expect(Metais::SyncProjectDocumentsJob).to have_received(:perform_later).with(project_origin, metais_project)
expect(Metais::SyncProjectEventsJob).to have_received(:perform_later).with(project_origin, metais_project)
expect(enqueued_jobs).to include(a_hash_including(job: Metais::ProjectDataExtractionJob,
args: [datahub_project.uuid],
queue: 'metais_data_extraction'))
expect(Metais::SyncProjectSuppliersJob).to have_received(:perform_later).with(project_origin, datahub_project)
expect(Metais::SyncProjectDocumentsJob).to have_received(:perform_later).with(project_origin, datahub_project)
expect(Metais::SyncProjectEventsJob).to have_received(:perform_later).with(project_origin, datahub_project)
end
end
Loading

0 comments on commit 0ace20a

Please sign in to comment.