From 6dfa01168cfdaa839c4f3c9ee147c2a551f7a4eb Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 11 Dec 2023 14:26:21 +0000 Subject: [PATCH] feat: add scheme csv service spec --- spec/fixtures/files/locations_csv_export.csv | 2 + .../schemes_and_locations_csv_export.csv | 2 + spec/fixtures/files/schemes_csv_export.csv | 2 + spec/services/csv/scheme_csv_service_spec.rb | 142 ++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 spec/fixtures/files/locations_csv_export.csv create mode 100644 spec/fixtures/files/schemes_and_locations_csv_export.csv create mode 100644 spec/fixtures/files/schemes_csv_export.csv create mode 100644 spec/services/csv/scheme_csv_service_spec.rb diff --git a/spec/fixtures/files/locations_csv_export.csv b/spec/fixtures/files/locations_csv_export.csv new file mode 100644 index 000000000..6ef80bfa1 --- /dev/null +++ b/spec/fixtures/files/locations_csv_export.csv @@ -0,0 +1,2 @@ +scheme_code,location_code,location_postcode,location_name,location_status,location_local_authority,location_units,location_type_of_unit,location_mobility_type,location_admin_district,location_active_dates +,,SW1A 2AA,Downing Street,active,E09000033,20,Self-contained house,Fitted with equipment and adaptations,Westminster,Active from 1 April 2022 diff --git a/spec/fixtures/files/schemes_and_locations_csv_export.csv b/spec/fixtures/files/schemes_and_locations_csv_export.csv new file mode 100644 index 000000000..a273b73c9 --- /dev/null +++ b/spec/fixtures/files/schemes_and_locations_csv_export.csv @@ -0,0 +1,2 @@ +scheme_code,scheme_service_name,scheme_status,scheme_sensitive,scheme_type,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_support_services_provided_by,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,scheme_active_dates,location_code,location_postcode,location_name,location_status,location_local_authority,location_units,location_type_of_unit,location_mobility_type,location_admin_district,location_active_dates +,Test name,active,Yes,Housing for older people,No,DLUHC,The same organisation that owns the housing stock,People with alcohol problems,Yes,Older people with support needs,High level,Medium stay,2021-04-01T00:00:00+01:00,Active from 1 April 2020,,SW1A 2AA,Downing Street,active,E09000033,20,Self-contained house,Fitted with equipment and adaptations,Westminster,Active from 1 April 2022 diff --git a/spec/fixtures/files/schemes_csv_export.csv b/spec/fixtures/files/schemes_csv_export.csv new file mode 100644 index 000000000..4bdca4e3d --- /dev/null +++ b/spec/fixtures/files/schemes_csv_export.csv @@ -0,0 +1,2 @@ +scheme_code,scheme_service_name,scheme_status,scheme_sensitive,scheme_type,scheme_registered_under_care_act,scheme_owning_organisation_name,scheme_support_services_provided_by,scheme_primary_client_group,scheme_has_other_client_group,scheme_secondary_client_group,scheme_support_type,scheme_intended_stay,scheme_created_at,scheme_active_dates +,Test name,active,Yes,Housing for older people,No,DLUHC,The same organisation that owns the housing stock,People with alcohol problems,Yes,Older people with support needs,High level,Medium stay,2021-04-01T00:00:00+01:00,Active from 1 April 2020 diff --git a/spec/services/csv/scheme_csv_service_spec.rb b/spec/services/csv/scheme_csv_service_spec.rb new file mode 100644 index 000000000..855fa221e --- /dev/null +++ b/spec/services/csv/scheme_csv_service_spec.rb @@ -0,0 +1,142 @@ +require "rails_helper" + +RSpec.describe Csv::SchemeCsvService do + let(:organisation) { create(:organisation) } + let(:fixed_time) { Time.zone.local(2023, 6, 26) } + let(:scheme) { create(:scheme, :export, owning_organisation: organisation, service_name: "Test name") } + let(:service) { described_class.new(download_type:) } + let(:download_type) { "combined" } + let(:csv) { CSV.parse(service.prepare_csv(Scheme.where(id: schemes.map(&:id)))) } + let(:schemes) { [scheme] } + let(:headers) { csv.first } + + before do + Timecop.freeze(fixed_time) + create(:location, :export, scheme:) + end + + after do + Timecop.return + end + + it "returns a string" do + result = service.prepare_csv(Scheme.all) + expect(result).to be_a String + end + + it "returns a csv with headers" do + expect(csv.first.first).to eq "scheme_code" + end + + it "returns the correctly formatted scheme code" do + expect(csv.second.first.first).to eq "S" + end + + context "when download type is schemes" do + let(:download_type) { "schemes" } + let(:scheme_attributes) { %w[scheme_code scheme_service_name scheme_status scheme_sensitive scheme_type scheme_registered_under_care_act scheme_owning_organisation_name scheme_support_services_provided_by scheme_primary_client_group scheme_has_other_client_group scheme_secondary_client_group scheme_support_type scheme_intended_stay scheme_created_at scheme_active_dates] } + + it "has the correct headers" do + expect(headers).to eq(scheme_attributes) + end + + it "exports the CSV with all values correct" do + expected_content = CSV.read("spec/fixtures/files/schemes_csv_export.csv") + values_to_delete = %w[scheme_code] + values_to_delete.each do |attribute| + index = csv.first.index(attribute) + csv.second[index] = nil + end + expect(csv).to eq expected_content + end + + context "when there are many schemes and locations" do + let(:schemes) { create_list(:scheme, scheme_count) } + let(:scheme_count) { 5 } + let(:locations_per_scheme) { 2 } + + before do + schemes.each do |scheme| + create_list(:location, locations_per_scheme, scheme:) + end + end + + it "creates a CSV with the correct number of schemes" do + expected_row_count_with_headers = scheme_count + 1 + expect(csv.size).to be expected_row_count_with_headers + end + end + end + + context "when download type is locations" do + let(:download_type) { "locations" } + let(:location_attributes) { %w[scheme_code location_code location_postcode location_name location_status location_local_authority location_units location_type_of_unit location_mobility_type location_admin_district location_active_dates] } + + + it "has the correct headers" do + expect(headers).to eq(location_attributes) + end + + it "exports the CSV with all values correct" do + expected_content = CSV.read("spec/fixtures/files/locations_csv_export.csv") + values_to_delete = %w[scheme_code location_code] + values_to_delete.each do |attribute| + index = csv.first.index(attribute) + csv.second[index] = nil + end + expect(csv).to eq expected_content + end + + context "when there are many schemes and locations" do + let(:schemes) { create_list(:scheme, scheme_count) } + let(:scheme_count) { 5 } + let(:locations_per_scheme) { 2 } + + before do + schemes.each do |scheme| + create_list(:location, locations_per_scheme, scheme:) + end + end + + it "creates a CSV with the correct number of locations" do + expected_row_count_with_headers = locations_per_scheme * scheme_count + 1 + expect(csv.size).to be expected_row_count_with_headers + end + end + end + + context "when download type is combined" do + let(:combined_attributes) { %w[scheme_code scheme_service_name scheme_status scheme_sensitive scheme_type scheme_registered_under_care_act scheme_owning_organisation_name scheme_support_services_provided_by scheme_primary_client_group scheme_has_other_client_group scheme_secondary_client_group scheme_support_type scheme_intended_stay scheme_created_at scheme_active_dates location_code location_postcode location_name location_status location_local_authority location_units location_type_of_unit location_mobility_type location_admin_district location_active_dates] } + + it "has the correct headers" do + expect(headers).to eq(combined_attributes) + end + + it "exports the CSV with all values correct" do + expected_content = CSV.read("spec/fixtures/files/schemes_and_locations_csv_export.csv") + values_to_delete = %w[scheme_code location_code] + values_to_delete.each do |attribute| + index = csv.first.index(attribute) + csv.second[index] = nil + end + expect(csv).to eq expected_content + end + + context "when there are many schemes and locations" do + let(:schemes) { create_list(:scheme, scheme_count) } + let(:scheme_count) { 5 } + let(:locations_per_scheme) { 2 } + + before do + schemes.each do |scheme| + create_list(:location, locations_per_scheme, scheme:) + end + end + + it "creates a CSV with the correct number of locations" do + expected_row_count_with_headers = locations_per_scheme * scheme_count + 1 + expect(csv.size).to be expected_row_count_with_headers + end + end + end +end