Browse Source

Create tests

pull/2539/head
Manny Dinssa 2 years ago
parent
commit
dd698e45ab
  1. 9
      spec/factories/csv_variable_definitions.rb
  2. 41
      spec/lib/tasks/log_variable_definitions_spec.rb
  3. 62
      spec/services/imports/variable_definitions_service_spec.rb

9
spec/factories/csv_variable_definitions.rb

@ -0,0 +1,9 @@
FactoryBot.define do
factory :csv_variable_definition do
variable { "variable" }
definition { "definition" }
log_type { "log" }
user_type { "support" }
year { 2024 }
end
end

41
spec/lib/tasks/log_variable_definitions_spec.rb

@ -0,0 +1,41 @@
require "rails_helper"
require "rake"
RSpec.describe "log_variable_definitions" do
describe ":add_variable_definitions", type: :task do
subject(:task) { Rake::Task["data_import:add_variable_definitions"] }
let(:path) { "spec/fixtures/variable_definitions" }
before do
Rake.application.rake_require("tasks/log_variable_definitions")
Rake::Task.define_task(:environment)
task.reenable
end
it "adds CsvVariableDefinition records from each file in the specified directory" do
expect { task.invoke(path) }.to change(CsvVariableDefinition, :count).by(419)
end
it "handles an empty directory without errors" do
empty_path = "spec/fixtures/empty_directory"
FileUtils.mkdir_p(empty_path)
expect { task.invoke(empty_path) }.not_to raise_error
expect(CsvVariableDefinition.count).to eq(0)
end
it "does not create duplicate records if run multiple times" do
CsvVariableDefinition.delete_all
initial_count = CsvVariableDefinition.count
task.invoke(path)
first_run_count = CsvVariableDefinition.count
task.invoke(path)
second_run_count = CsvVariableDefinition.count
expect(first_run_count).to eq(initial_count + 419)
expect(second_run_count).to eq(first_run_count)
end
end
end

62
spec/services/imports/variable_definitions_service_spec.rb

@ -0,0 +1,62 @@
require "rails_helper"
RSpec.describe Imports::VariableDefinitionsService, type: :service do
let(:path) { "spec/fixtures/variable_definitions" }
let(:service) { described_class.new(path:) }
describe "#initialize" do
it "initializes with the correct path and count" do
expect(service.path).to eq(path)
expect(service.count).to eq(0)
end
end
describe "#call" do
before do
allow(Dir).to receive(:glob).and_return(%w[lettings_support_download_23_24.csv lettings_support_download_24_25.csv lettings_user_download_23_24.csv lettings_user_download_24_25.csv sales_support_download_23_24.csv sales_support_download_24_25.csv sales_user_download_23_24.csv sales_user_download_24_25.csv])
allow(service).to receive(:process_file)
end
it "processes each file in the directory" do
service.call
%w[lettings_support_download_23_24.csv lettings_support_download_24_25.csv lettings_user_download_23_24.csv lettings_user_download_24_25.csv sales_support_download_23_24.csv sales_support_download_24_25.csv sales_user_download_23_24.csv sales_user_download_24_25.csv].each do |file|
expect(service).to have_received(:process_file).with(file)
end
end
end
describe "#process_file" do
let(:file) { "spec/fixtures/variable_definitions/lettings_user_download_23_24.csv" }
let(:csv_content) { [["id", "Log ID"], ["status", "Status of log"], ["duplicate_set_id", "ID of a set of duplicate logs"]] }
before do
allow(CSV).to receive(:foreach).and_yield(csv_content[0]).and_yield(csv_content[1]).and_yield(csv_content[2])
end
context "when no existing record" do
it "creates new records" do
expect {
service.send(:process_file, file)
}.to change(CsvVariableDefinition, :count).by(3)
end
end
context "when existing record with user_type support" do
let!(:existing_record) { create(:csv_variable_definition, variable: "id", definition: "Log ID", log_type: "lettings", user_type: "support") }
it "updates the user_type to user" do
service.send(:process_file, file)
expect(existing_record.reload.user_type).to eq("user")
end
end
context "when existing record with user_type user" do
let!(:existing_record) { create(:csv_variable_definition, variable: "id", definition: "Log ID", log_type: "lettings", user_type: "user") }
it "does not update the user_type" do
service.send(:process_file, file)
expect(existing_record.reload.user_type).to eq("user")
end
end
end
end
Loading…
Cancel
Save