Skip to content

Commit

Permalink
Tests: coverage updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ewlarson committed Jan 16, 2025
1 parent 34ef2c1 commit 4e16c19
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 106 deletions.
39 changes: 1 addition & 38 deletions app/controllers/admin/document_data_dictionaries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions app/models/document_data_dictionary/csv_header_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 34 additions & 44 deletions test/controllers/document_data_dictionaries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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
19 changes: 0 additions & 19 deletions test/models/geoblacklight_admin_test.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/services/geoblacklight_admin/item_viewer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4e16c19

Please sign in to comment.