From 34ef2c1731d121272daa35063e7c14361cb327c0 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 15 Jan 2025 17:42:40 -0600 Subject: [PATCH] Test coverage --- .../csv_header_validator.rb | 2 +- .../csv_header_validator_test.rb | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/models/document_data_dictionary/csv_header_validator_test.rb diff --git a/app/models/document_data_dictionary/csv_header_validator.rb b/app/models/document_data_dictionary/csv_header_validator.rb index 9dabb2a..6f2e730 100644 --- a/app/models/document_data_dictionary/csv_header_validator.rb +++ b/app/models/document_data_dictionary/csv_header_validator.rb @@ -11,7 +11,7 @@ def validate(record) unless valid_csv_headers?(record&.csv_file) valid_csv_header = false record.errors.add(:csv_file, - "Missing the required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required.") + "Missing a required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required.") end valid_csv_header diff --git a/test/models/document_data_dictionary/csv_header_validator_test.rb b/test/models/document_data_dictionary/csv_header_validator_test.rb new file mode 100644 index 0000000..f41fe19 --- /dev/null +++ b/test/models/document_data_dictionary/csv_header_validator_test.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require "test_helper" + +class DocumentDataDictionary::CsvHeaderValidatorTest < ActiveSupport::TestCase + def setup + @validator = DocumentDataDictionary::CsvHeaderValidator.new + @record = OpenStruct.new(csv_file: nil, errors: ActiveModel::Errors.new(self)) + end + + def test_valid_csv_headers + csv_content = "friendlier_id,field_name,field_type,values,definition,definition_source,parent_field_name\n" + file_mock = Minitest::Mock.new + file_mock.expect :download, StringIO.new(csv_content) + file_mock.expect :nil?, false + @record.csv_file = file_mock + + assert @validator.validate(@record), "Expected CSV headers to be valid" + assert_empty @record.errors[:csv_file], "Expected no errors for valid CSV headers" + end + + def test_invalid_csv_headers + csv_content = "friendlier_id,field_name\n" + file_mock = Minitest::Mock.new + file_mock.expect :download, StringIO.new(csv_content) + file_mock.expect :nil?, false + @record.csv_file = file_mock + + refute @validator.validate(@record), "Expected CSV headers to be invalid" + assert_includes @record.errors[:csv_file], "Missing a required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required." + end + + def test_missing_csv_file + skip("file download missing in test runner") + @record.csv_file = nil + + refute @validator.validate(@record), "Expected validation to fail with missing CSV file" + assert_includes @record.errors[:csv_file], "Missing a required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required." + end +end