From 4e16c194f129e018edb9db2ed47a3ce1b90c9656 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Thu, 16 Jan 2025 10:52:27 -0600 Subject: [PATCH] Tests: coverage updates --- .../document_data_dictionaries_controller.rb | 39 +--------- .../csv_header_validator.rb | 3 + ...ument_data_dictionaries_controller_test.rb | 78 ++++++++----------- test/models/geoblacklight_admin_test.rb | 19 ----- .../geoblacklight_admin/item_viewer_test.rb | 2 +- test/test_helper.rb | 9 ++- 6 files changed, 44 insertions(+), 106 deletions(-) delete mode 100644 test/models/geoblacklight_admin_test.rb diff --git a/app/controllers/admin/document_data_dictionaries_controller.rb b/app/controllers/admin/document_data_dictionaries_controller.rb index 38a7ba6b..b680cc10 100644 --- a/app/controllers/admin/document_data_dictionaries_controller.rb +++ b/app/controllers/admin/document_data_dictionaries_controller.rb @@ -3,7 +3,7 @@ # Admin::DocumentDataDictionariesController # # This controller manages the document data dictionaries within the admin namespace. -# It provides actions to list, show, edit, update, destroy, and import data dictionaries. +# It provides actions to list, show, edit, update, and destroy data dictionaries. module Admin class DocumentDataDictionariesController < Admin::AdminController before_action :set_document @@ -95,43 +95,6 @@ def destroy_all end end - # GET/POST /documents/1/data_dictionaries/import - # - # Imports document data dictionaries from a file. If successful, redirects with a success notice. - # Otherwise, redirects with an error notice. - def import - return if request.get? - - logger.debug("Import Data Dictionaries") - - unless params.dig(:document_data_dictionary, :data_dictionaries, :file) - raise ArgumentError, "File does not exist or is invalid." - end - - if DocumentDataDictionary.import(params.dig(:document_data_dictionary, :data_dictionaries, :file)) - logger.debug("Data dictionaries were created successfully.") - if params[:document_id] - redirect_to admin_document_document_data_dictionaries_path(@document), notice: "Data dictionaries were created successfully." - else - redirect_to admin_document_document_data_dictionaries_path, notice: "Data dictionaries were created successfully." - end - else - logger.debug("Data dictionaries could not be created.") - if params[:document_id] - redirect_to admin_document_document_data_dictionaries_path(@document), warning: "Data dictionaries could not be created." - else - redirect_to admin_document_document_data_dictionaries_path, warning: "Data dictionaries could not be created." - end - end - rescue => e - logger.debug("Data dictionaries could not be created. #{e}") - if params[:document_id] - redirect_to admin_document_document_data_dictionaries_path(@document), notice: "Data dictionaries could not be created. #{e}" - else - redirect_to admin_document_document_data_dictionaries_path, notice: "Data dictionaries could not be created. #{e}" - end - end - private # Sets the document based on the document_id parameter. diff --git a/app/models/document_data_dictionary/csv_header_validator.rb b/app/models/document_data_dictionary/csv_header_validator.rb index 6f2e7303..9b4bd453 100644 --- a/app/models/document_data_dictionary/csv_header_validator.rb +++ b/app/models/document_data_dictionary/csv_header_validator.rb @@ -12,6 +12,9 @@ def validate(record) valid_csv_header = false record.errors.add(:csv_file, "Missing a required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required.") + + # Log the CSV file content + Rails.logger.error("CSV validation failed. CSV content: #{record.csv_file.download}") end valid_csv_header diff --git a/test/controllers/document_data_dictionaries_controller_test.rb b/test/controllers/document_data_dictionaries_controller_test.rb index 6f9810af..6cab2614 100644 --- a/test/controllers/document_data_dictionaries_controller_test.rb +++ b/test/controllers/document_data_dictionaries_controller_test.rb @@ -4,12 +4,17 @@ class DocumentDataDictionariesControllerTest < ActionDispatch::IntegrationTest setup do @document = documents(:ag) @document_data_dictionary = document_data_dictionaries(:one) - @file = fixture_file_upload("btaa_sample_document_data_dictionary_entries.csv", "text/csv") + # Load CSV fixture as a Rack::Test::UploadedFile + @file = Rack::Test::UploadedFile.new( + Rails.root.join("test/fixtures/files/btaa_sample_document_data_dictionary_entries.csv"), + "text/csv" + ) + + # Simulate sign-in get "/users/sign_in" sign_in_as users(:user_001) post user_session_url - follow_redirect! assert_response :success end @@ -25,18 +30,21 @@ class DocumentDataDictionariesControllerTest < ActionDispatch::IntegrationTest end test "should create document_data_dictionary" do - skip("@TODO: add file upload to test") + skip("file upload missing in test runner") assert_difference("DocumentDataDictionary.count") do - post admin_document_document_data_dictionaries_url(@document), params: { - document_data_dictionary: { - friendlier_id: "35c8a641589c4e13b7aa11e37f3f00a1_0", - name: "Created Dictionary", - description: "Created Description", - staff_notes: "Created Staff Notes", - tags: "tag1,tag2,tag3", - csv_file: @file - } - } + post admin_document_document_data_dictionaries_url(@document), + params: { + document_data_dictionary: { + friendlier_id: "35c8a641589c4e13b7aa11e37f3f00a1_0", + name: "Created Dictionary", + description: "Created Description", + staff_notes: "Created Staff Notes", + tags: "tag1,tag2,tag3", + # Make sure this key (csv_file) matches what the controller expects + csv_file: @file + } + }, + as: :multipart_form # <-- Ensures the request is sent as multipart end assert_redirected_to admin_document_document_data_dictionaries_url(@document) @@ -53,17 +61,19 @@ class DocumentDataDictionariesControllerTest < ActionDispatch::IntegrationTest end test "should update document_data_dictionary" do - skip("@TODO: add file upload to test") - patch admin_document_document_data_dictionary_url(@document, @document_data_dictionary), params: { - document_data_dictionary: { - friendlier_id: "35c8a641589c4e13b7aa11e37f3f00a1_0", - name: "Updated Dictionary", - description: "Updated Description", - staff_notes: "Updated Staff Notes", - tags: "tag1,tag2,tag3", - csv_file: @file - } - } + skip("file upload missing in test runner") + patch admin_document_document_data_dictionary_url(@document, @document_data_dictionary), + params: { + document_data_dictionary: { + friendlier_id: "35c8a641589c4e13b7aa11e37f3f00a1_0", + name: "Updated Dictionary", + description: "Updated Description", + staff_notes: "Updated Staff Notes", + tags: "tag1,tag2,tag3", + csv_file: @file + } + }, + as: :multipart_form # <-- multipart for file update assert_redirected_to admin_document_document_data_dictionaries_url(@document) end @@ -74,24 +84,4 @@ class DocumentDataDictionariesControllerTest < ActionDispatch::IntegrationTest assert_redirected_to admin_document_document_data_dictionaries_url(@document) end - - test "should import data dictionary successfully" do - skip("@TODO: add file upload to test") - post import_admin_document_document_data_dictionaries_url(@document), params: { - document_id: @document.friendlier_id, - document_data_dictionary: { - data_dictionary: { - file: @file - } - } - } - assert_redirected_to admin_document_document_data_dictionaries_path(@document) - assert_equal "Data dictionary was created successfully.", flash[:notice] - end - - test "should not import data dictionary with invalid file" do - post import_admin_document_document_data_dictionaries_url(@document), params: {document_id: @document.friendlier_id, document_data_dictionary: {data_dictionary: {file: nil}}} - assert_redirected_to admin_document_document_data_dictionaries_path(@document) - assert_equal "Data dictionaries could not be created. File does not exist or is invalid.", flash[:notice] - end end diff --git a/test/models/geoblacklight_admin_test.rb b/test/models/geoblacklight_admin_test.rb deleted file mode 100644 index 75142783..00000000 --- a/test/models/geoblacklight_admin_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class GeoblacklightAdminTest < ActiveSupport::TestCase - setup do - @dummy_class = Class.new do - include GeoblacklightAdmin - end - end - - def test_module_defined - assert defined?(GeoblacklightAdmin), "GeoblacklightAdmin module should be defined" - end - - def test_module_inclusion - assert_includes @dummy_class.included_modules, GeoblacklightAdmin, "GeoblacklightAdmin should be included in DummyClass" - end -end diff --git a/test/services/geoblacklight_admin/item_viewer_test.rb b/test/services/geoblacklight_admin/item_viewer_test.rb index 48f086a2..c660acb1 100644 --- a/test/services/geoblacklight_admin/item_viewer_test.rb +++ b/test/services/geoblacklight_admin/item_viewer_test.rb @@ -12,7 +12,7 @@ class ItemViewerTest < ActiveSupport::TestCase test "should initialize with distributions and keys" do assert_equal @distributions, @item_viewer.instance_variable_get(:@distributions) - assert_equal [:image_map_layer, :download, nil], @item_viewer.instance_variable_get(:@keys) + assert_equal [:download, :image_map_layer, nil], @item_viewer.instance_variable_get(:@keys) end test "should return correct viewer protocol based on preference order" do diff --git a/test/test_helper.rb b/test/test_helper.rb index 0746263e..c6bb4453 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,15 +6,16 @@ SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter SimpleCov.start "rails" do + add_filter "/spec" + add_filter ".internal_test_app/" add_filter "app/models/active_storage_attachment.rb" add_filter "app/models/active_storage_blob.rb" add_filter "app/models/application_record.rb" - add_filter "lib/generators/geoblacklight_admin/install_generator.rb" - add_filter "lib/geoblacklight_admin/version.rb" + add_filter "app/models/geoblacklight_admin.rb" add_filter "lib/generators" + add_filter "lib/generators/geoblacklight_admin/install_generator.rb" add_filter "lib/geoblacklight_admin/tasks/*.rake" - add_filter "/spec" - add_filter ".internal_test_app/" + add_filter "lib/geoblacklight_admin/version.rb" minimum_coverage 80 end