Skip to content

Commit

Permalink
Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ewlarson committed Jan 15, 2025
1 parent e2bf038 commit 047f95f
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@ module Admin
class DocumentDataDictionaryEntriesController < Admin::AdminController
before_action :set_document
before_action :set_document_data_dictionary
before_action :set_document_data_dictionary_entry, only: %i[show edit update destroy]

# GET /document_data_dictionaries/1/entries or /document_data_dictionaries/1/entries.json
def index
redirect_to admin_document_document_data_dictionary_path(@document, @document_data_dictionary)
end

# GET /document_data_dictionaries/1/entries/1 or /document_data_dictionaries/1/entries/1.json
def show
end
before_action :set_document_data_dictionary_entry, only: %i[edit update destroy]

# GET /document_data_dictionaries/1/entries/new
def new
Expand Down Expand Up @@ -51,6 +42,7 @@ def update
format.html { redirect_to admin_document_document_data_dictionary_path(@document, @document_data_dictionary), notice: "Document data dictionary entry was successfully updated." }
format.json { render :show, status: :ok, location: @document_data_dictionary_entry }
else
logger.debug("Document data dictionary entry could not be updated. #{@document_data_dictionary_entry.errors.full_messages}")
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @document_data_dictionary_entry.errors, status: :unprocessable_entity }
end
Expand Down
1 change: 1 addition & 0 deletions lib/generators/geoblacklight_admin/config_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ def add_vite_rails_config
def add_test_fixture_files
copy_file "btaa_sample_records.csv", "test/fixtures/files/btaa_sample_records.csv", force: true
copy_file "btaa_sample_document_distributions.csv", "test/fixtures/files/btaa_sample_document_distributions.csv", force: true
copy_file "btaa_sample_document_data_dictionary_entries.csv", "test/fixtures/files/btaa_sample_document_data_dictionary_entries.csv", force: true
end

# Run bundle with vite install
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
friendlier_id,field_name,field_type,values,definition,definition_source,parent_field_name
35c8a641589c4e13b7aa11e37f3f00a1_0,AREA,Real,6,Area of polygon,Software generated,
35c8a641589c4e13b7aa11e37f3f00a1_0,PERIMETER,Real,6,Perimeter of polygon,Software generated,
35c8a641589c4e13b7aa11e37f3f00a1_0,NAME_U,String,10,County names in upper case letters,USGS 7.5-minute quadrangles,
35c8a641589c4e13b7aa11e37f3f00a1_0,NAME_L,String,10,County names in upper and lower case letters,IGS standard,
35c8a641589c4e13b7aa11e37f3f00a1_0,NCAPC,Integer,2,Numeric county alphabetic prefix code,Indiana Bureau of Motor Vehicles,
35c8a641589c4e13b7aa11e37f3f00a1_0,Subtable A,String,A,Definition,Definition source,PERIMETER
35c8a641589c4e13b7aa11e37f3f00a1_0,Subtable B,String,B,Definition,Definition Source,PERIMETER
35c8a641589c4e13b7aa11e37f3f00a1_0,Subtable C,String,C,Definition,Definition Source,PERIMETER
97 changes: 97 additions & 0 deletions test/controllers/document_data_dictionaries_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require "test_helper"

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")

get "/users/sign_in"
sign_in_as users(:user_001)
post user_session_url

follow_redirect!
assert_response :success
end

test "should get index" do
get admin_document_document_data_dictionaries_url(@document)
assert_response :success
end

test "should get new" do
get new_admin_document_document_data_dictionary_url(@document)
assert_response :success
end

test "should create document_data_dictionary" do
skip("@TODO: add file upload to test")
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
}
}
end

assert_redirected_to admin_document_document_data_dictionaries_url(@document)
end

test "should show document_data_dictionary" do
get admin_document_document_data_dictionary_url(@document, @document_data_dictionary)
assert_response :success
end

test "should get edit" do
get edit_admin_document_document_data_dictionary_url(@document, @document_data_dictionary)
assert_response :success
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
}
}
assert_redirected_to admin_document_document_data_dictionaries_url(@document)
end

test "should destroy document_data_dictionary" do
assert_difference("DocumentDataDictionary.count", -1) do
delete admin_document_document_data_dictionary_url(@document, @document_data_dictionary)
end

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
79 changes: 0 additions & 79 deletions test/controllers/document_data_dictionary_controller_test.rb

This file was deleted.

113 changes: 113 additions & 0 deletions test/controllers/document_data_dictionary_entries_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# frozen_string_literal: true

require "test_helper"

module Admin
class DocumentDataDictionaryEntriesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

setup do
@document = documents(:ag)
@document_data_dictionary = document_data_dictionaries(:one)
@document_data_dictionary_entry = document_data_dictionary_entries(:one)

get "/users/sign_in"
sign_in_as users(:user_001)
post user_session_url

follow_redirect!
assert_response :success
end

test "should get new" do
get new_admin_document_document_data_dictionary_document_data_dictionary_entry_url(
@document,
@document_data_dictionary
)
assert_response :success
end

test "should create document_data_dictionary_entry" do
assert_difference("DocumentDataDictionaryEntry.count") do
post admin_document_document_data_dictionary_document_data_dictionary_entries_url(
@document,
@document_data_dictionary
), params: {
document_data_dictionary_entry: {
document_data_dictionary_id: @document_data_dictionary.id,
friendlier_id: @document.friendlier_id,
field_name: "test_field",
field_type: "string",
values: "test_values",
definition: "Test definition"
}
}
end

assert_redirected_to admin_document_document_data_dictionary_path(@document, @document_data_dictionary)
end

test "should get edit" do
get edit_admin_document_document_data_dictionary_document_data_dictionary_entry_url(
@document,
@document_data_dictionary,
@document_data_dictionary_entry
)
assert_response :success
end

test "should update document_data_dictionary_entry" do
patch admin_document_document_data_dictionary_document_data_dictionary_entry_url(
@document,
@document_data_dictionary,
@document_data_dictionary_entry
), params: {
document_data_dictionary_entry: {
friendlier_id: @document.friendlier_id,
field_name: "updated_field",
field_type: "string",
values: "updated_values"
}
}
assert_redirected_to admin_document_document_data_dictionary_path(@document, @document_data_dictionary)
end

test "should destroy document_data_dictionary_entry" do
assert_difference("DocumentDataDictionaryEntry.count", -1) do
delete admin_document_document_data_dictionary_document_data_dictionary_entry_url(
@document,
@document_data_dictionary,
@document_data_dictionary_entry
)
end

assert_redirected_to admin_document_document_data_dictionary_path(@document, @document_data_dictionary)
end

test "should destroy all entries" do
assert_difference("DocumentDataDictionaryEntry.count", -DocumentDataDictionaryEntry.count) do
post destroy_all_admin_document_document_data_dictionary_document_data_dictionary_entries_path(
@document,
@document_data_dictionary
)
end

assert_redirected_to admin_document_document_data_dictionary_document_data_dictionary_entries_path(
@document_data_dictionary.friendlier_id,
@document_data_dictionary
)
end

test "should sort entries" do
entry1 = document_data_dictionary_entries(:one)
entry2 = document_data_dictionary_entries(:two)

post sort_admin_document_document_data_dictionary_document_data_dictionary_entries_url(
@document,
@document_data_dictionary
), params: { id_list: [entry2.id, entry1.id] }

assert_response :success
end
end
end
7 changes: 7 additions & 0 deletions test/fixtures/document_data_dictionaries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
one:
friendlier_id: 35c8a641589c4e13b7aa11e37f3f00a1_0
name: Sample Dictionary
description: Description
staff_notes: Staff notes
tags: tag1,tag2,tag3
position: 1
13 changes: 13 additions & 0 deletions test/fixtures/document_data_dictionary_entries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
one:
document_data_dictionary: one
field_name: "field_one"
field_type: "string"
definition: "First test field"
position: 1

two:
document_data_dictionary: one
field_name: "field_two"
field_type: "string"
definition: "Second test field"
position: 2

0 comments on commit 047f95f

Please sign in to comment.