From 2a714a22ad8ed0e398f3ebd88d162aca4fd15071 Mon Sep 17 00:00:00 2001 From: fumimowdan Date: Mon, 2 Oct 2023 17:02:41 +0100 Subject: [PATCH] Fix specs for urn --- .../admin_console/applications_list_spec.rb | 6 ++ spec/fixtures/urns.yml | 90 +++++++++++++++++++ spec/models/urn_spec.rb | 5 +- spec/rails_helper.rb | 3 +- spec/services/generate_urns_spec.rb | 22 +++-- 5 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 spec/fixtures/urns.yml diff --git a/spec/features/admin_console/applications_list_spec.rb b/spec/features/admin_console/applications_list_spec.rb index 85a51dee..8cb032cd 100644 --- a/spec/features/admin_console/applications_list_spec.rb +++ b/spec/features/admin_console/applications_list_spec.rb @@ -59,6 +59,8 @@ def given_there_are_few_applications # Create 2 specific applications for search tests + create_list(:urn, 5, code: "TE") + create_list(:urn, 5, code: "ST") unique_applicant = create(:applicant, given_name: "Unique Given Name", middle_name: "Unique Middle Name", family_name: "Unique Family Name", email_address: "unique@example.com") create(:application, applicant: unique_applicant, urn: "Unique Urn 1") @@ -70,12 +72,16 @@ def given_there_are_few_applications end def given_there_is_an_application_that_breached_sla + create_list(:urn, 5, code: "TE") + create_list(:urn, 5, code: "ST") applicant = create(:applicant) application = create(:application, applicant:) application.application_progress.update(initial_checks_completed_at: 4.days.ago) end def given_there_are_applications_with_different_dates + create_list(:urn, 5, code: "TE") + create_list(:urn, 5, code: "ST") create(:application, application_progress: build(:application_progress, :initial_checks_completed, status: :initial_checks)) create(:application, application_progress: build(:application_progress, :home_office_checks_completed, status: :home_office_checks)) end diff --git a/spec/fixtures/urns.yml b/spec/fixtures/urns.yml new file mode 100644 index 00000000..4cedf718 --- /dev/null +++ b/spec/fixtures/urns.yml @@ -0,0 +1,90 @@ +te_one: + suffix: 5668 + prefix: IRP + code: TE + +te_two: + suffix: 21368 + prefix: IRP + code: TE + +te_three: + suffix: 5 + prefix: IRP + code: TE + +te_four: + suffix: 76998 + prefix: IRP + code: TE + +te_five: + suffix: 6559 + prefix: IRP + code: TE + +te_six: + suffix: 6 + prefix: IRP + code: TE + +te_seven: + suffix: 2298 + prefix: IRP + code: TE + +te_eight: + suffix: 1159 + prefix: IRP + code: TE + +te_nine: + suffix: 79298 + prefix: IRP + code: TE + +te_ten: + suffix: 19549 + prefix: IRP + code: TE + +st_one: + suffix: 5668 + prefix: IRP + code: ST + +st_two: + suffix: 29968 + prefix: IRP + code: ST + +st_three: + suffix: 5 + prefix: IRP + code: ST + +st_four: + suffix: 76998 + prefix: IRP + code: ST + +st_five: + suffix: 6559 + prefix: IRP + code: ST + +st_six: + suffix: 6 + prefix: IRP + code: ST + +st_seven: + suffix: 28 + prefix: IRP + code: ST + +st_eight: + suffix: 159 + prefix: IRP + code: ST + diff --git a/spec/models/urn_spec.rb b/spec/models/urn_spec.rb index 02466d4c..eb150367 100644 --- a/spec/models/urn_spec.rb +++ b/spec/models/urn_spec.rb @@ -12,7 +12,6 @@ require "rails_helper" RSpec.describe Urn do - describe "next" do subject(:next_urn) { described_class.next(route) } @@ -41,6 +40,10 @@ context "when there is no more urn available to assign" do let(:route) { "salaried_trainee" } + before do + allow(described_class).to receive(:find_by!).and_raise(ActiveRecord::RecordNotFound) + end + it { expect { next_urn }.to raise_error(Urn::NoUrnAvailableError) } end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6ba490c8..edfe2b50 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -42,7 +42,8 @@ end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - config.fixture_path = Rails.root.join("/spec/fixtures") + config.fixture_path = Rails.root.join("spec/fixtures") + config.global_fixtures = :urns # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false diff --git a/spec/services/generate_urns_spec.rb b/spec/services/generate_urns_spec.rb index ebde4c31..cbe58507 100644 --- a/spec/services/generate_urns_spec.rb +++ b/spec/services/generate_urns_spec.rb @@ -1,13 +1,23 @@ require "rails_helper" RSpec.describe GenerateUrns do - subject(:service) { described_class } + describe ".generate" do + subject(:generate) { described_class.new(code:).generate } - describe ".call" do - it { fail } - end + before do + allow(Urn).to receive(:insert_all) + stub_const "Urn::MAX_SUFFIX", 3 + generate + end - describe ".generate" do - it { fail } + let(:code) { "TE" } + let(:expected_data) do + [ + { prefix: "IRP", code: code, suffix: 1 }, + { prefix: "IRP", code: code, suffix: 2 }, + ] + end + + it { expect(Urn).to have_received(:insert_all).with(match_array(expected_data)) } end end