Skip to content

Commit

Permalink
Rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jjromeo committed Apr 25, 2023
1 parent a6e5226 commit 157f106
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module WasteCarriersEngine
class AssignSiteDetailsService < BaseService
attr_reader :address
Expand Down
2 changes: 2 additions & 0 deletions app/services/waste_carriers_engine/determine_area_service.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module WasteCarriersEngine
class DetermineAreaService < BaseService
def run(easting:, northing:)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module WasteCarriersEngine
class DetermineEastingAndNorthingService < BaseService
def run(postcode:)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
require 'rails_helper'
# frozen_string_literal: true

require 'rails_helper'
require "rails_helper"

module WasteCarriersEngine
RSpec.describe AssignSiteDetailsService, type: :service do
describe '#run' do
let(:x) { 123456 }
let(:y) { 654321 }
let(:area) { 'Area Name' }
describe "#run" do
let(:x) { 123_456 }
let(:y) { 654_321 }
let(:area) { "Area Name" }

context 'when address has a postcode and area is not present' do
context "when address has a postcode and area is not present" do
let(:address) { build(:address, :has_required_data) }

before do
allow(DetermineEastingAndNorthingService).to receive(:run).and_return(easting: x, northing: y)
allow(DetermineAreaService).to receive(:run).and_return(area)
end

it 'assigns area' do
it "assigns area" do
described_class.run(address: address)
expect(address.area).to eq(area)
end
end

context 'when address has an area' do
context "when address has an area" do
let(:address) { build(:address, :has_required_data, area: area) }

it 'does not change the area' do
expect(DetermineEastingAndNorthingService).not_to receive(:run)
expect(DetermineAreaService).not_to receive(:run)
it "does not change the area" do
allow(DetermineEastingAndNorthingService).to receive(:run)
allow(DetermineAreaService).to receive(:run)

described_class.run(address: address)

expect(DetermineEastingAndNorthingService).not_to have_received(:run)
expect(DetermineAreaService).not_to have_received(:run)
expect(address.area).to eq(area)
end
end

context 'when address does not have a postcode' do
context "when address does not have a postcode" do
let(:address) { build(:address, :has_required_data, postcode: nil) }

it 'does not assign area' do
expect(DetermineEastingAndNorthingService).not_to receive(:run)
expect(DetermineAreaService).not_to receive(:run)
it "does not assign area" do
allow(DetermineEastingAndNorthingService).to receive(:run)
allow(DetermineAreaService).to receive(:run)

described_class.run(address: address)

expect(DetermineEastingAndNorthingService).not_to have_received(:run)
expect(DetermineAreaService).not_to have_received(:run)
expect(address.area).to be_nil
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
# frozen_string_literal: true

# spec/services/waste_carriers_engine/determine_easting_and_northing_service_spec.rb

require 'rails_helper'
require "rails_helper"

module WasteCarriersEngine
RSpec.describe DetermineEastingAndNorthingService, type: :service do
let(:service) { DetermineEastingAndNorthingService.new }
let(:valid_postcode) { 'SW1A 1AA' }
let(:invalid_postcode) { 'INVALID' }
let(:service) { described_class.new }
let(:valid_postcode) { "SW1A 1AA" }
let(:invalid_postcode) { "INVALID" }

describe '#run' do
context 'when given a valid postcode' do
describe "#run" do
context "when given a valid postcode" do
before do
# Stub AddressLookupService to return a valid response
allow(AddressLookupService).to receive(:run).with(valid_postcode).and_return(
instance_double(DefraRuby::Address::Response, successful?: true, results: [{ "x" => 529_090, "y" => 179_645 }])
)
end

it 'returns the correct easting and northing values' do
it "returns the correct easting and northing values" do
result = service.run(postcode: valid_postcode)

expect(result[:easting]).to eq(529_090.0)
expect(result[:northing]).to eq(179_645.0)
end
end

context 'when given an invalid postcode' do
context "when given an invalid postcode" do
before do
# Stub AddressLookupService to return a NoMatchError
allow(AddressLookupService).to receive(:run).with(invalid_postcode).and_return(
instance_double(DefraRuby::Address::Response, successful?: false, error: DefraRuby::Address::NoMatchError.new)
)
end

it 'returns the default easting and northing values' do
it "returns the default easting and northing values" do
result = service.run(postcode: invalid_postcode)

expect(result[:easting]).to eq(0.0)
expect(result[:northing]).to eq(0.0)
end
end

context 'when the postcode lookup service returns an error' do
context "when the postcode lookup service returns an error" do
before do
# Stub AddressLookupService to return a generic error
allow(AddressLookupService).to receive(:run).with(invalid_postcode).and_return(
instance_double(DefraRuby::Address::Response, successful?: false, error: StandardError.new('An error occurred'))
instance_double(DefraRuby::Address::Response, successful?: false, error: StandardError.new("An error occurred"))
)
end

it 'returns the default easting and northing values' do
it "returns the default easting and northing values" do
result = service.run(postcode: invalid_postcode)

expect(result[:easting]).to eq(0.0)
Expand Down

0 comments on commit 157f106

Please sign in to comment.