Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
Improving validation, adding test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
escowles committed Jun 9, 2017
1 parent fa04ad5 commit a3d051e
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 54 deletions.
2 changes: 0 additions & 2 deletions app/helpers/ephemera_projects_helper.rb

This file was deleted.

3 changes: 2 additions & 1 deletion app/models/ephemera_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ class EphemeraField < ApplicationRecord
belongs_to :ephemera_project
belongs_to :vocabulary

validates :name, uniqueness: { scope: :ephemera_project }
validates :name, presence: true, uniqueness: { scope: :ephemera_project }
validates :vocabulary, presence: true
end
2 changes: 2 additions & 0 deletions app/models/ephemera_project.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class EphemeraProject < ApplicationRecord
has_many :ephemera_fields

validates :name, presence: true
end
2 changes: 1 addition & 1 deletion app/models/vocabulary.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Vocabulary < ApplicationRecord
belongs_to :parent, class_name: "Vocabulary"
validates :label, presence: true
validates :label, presence: true, uniqueness: true
after_create :register_authority
has_many :vocabulary_terms

Expand Down
10 changes: 3 additions & 7 deletions app/services/authority_finder.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
class AuthorityFinder
def self.for(property:, project:)
return unless property.starts_with?("EphemeraFolder.") && project
begin
fields = EphemeraField.where name: "#{property}", ephemera_project: project
Qa::Authorities::Local.subauthority_for(fields.first.vocabulary.label) if fields.first
rescue Qa::InvalidSubAuthority
Rails.logger.debug("Non-existent sub-authority requested for property #{property}")
nil
end

fields = EphemeraField.where name: "#{property}", ephemera_project: project
VocabularySubauthority.new(fields.first.vocabulary.label, fields.first.vocabulary) if fields.first
end
end
18 changes: 10 additions & 8 deletions spec/controllers/ephemera_fields_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
RSpec.describe EphemeraFieldsController, type: :controller do
let(:project) { FactoryGirl.create :ephemera_project }
let(:vocab) { FactoryGirl.create :vocabulary }
let(:valid_attributes) { { name: 'EphemeraFolder.language', vocabulary: vocab } }
let(:invalid_attributes) { { name: 'EphemeraFolder.language' } }
let(:valid_attributes) { { name: 'EphemeraFolder.language', vocabulary_id: vocab.id } }
let(:invalid_attributes) { { name: nil, vocabulary_id: nil } }

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
Expand Down Expand Up @@ -65,9 +65,10 @@
end

context "with invalid params" do
it "assigns a newly created but unsaved ephemera_field as @ephemera_field" do
post :create, params: { ephemera_field: invalid_attributes, ephemera_project_id: project.id }, session: valid_session
expect(response).not_to be_successful
it "fails" do
expect {
post :create, params: { ephemera_field: invalid_attributes, ephemera_project_id: project.id }, session: valid_session
}.not_to change(EphemeraField, :count)
end
end
end
Expand Down Expand Up @@ -97,10 +98,11 @@
end

context "with invalid params" do
it "assigns the ephemera_field as @ephemera_field" do
it "does not change the ephemera_field" do
ephemera_field = EphemeraField.create! valid_attributes
put :update, params: { id: ephemera_field.to_param, ephemera_field: invalid_attributes, ephemera_project_id: project.id }, session: valid_session
expect(response).not_to be_successful
expect {
put :update, params: { id: ephemera_field.to_param, ephemera_field: invalid_attributes, ephemera_project_id: project.id }, session: valid_session
}.not_to change { ephemera_field.updated_at }
end
end
end
Expand Down
19 changes: 4 additions & 15 deletions spec/controllers/ephemera_projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
# This should return the minimal set of attributes required to create a valid
# EphemeraProject. As you add validations to EphemeraProject, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:valid_attributes) { { name: "Test Project" } }

let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
let(:invalid_attributes) { { name: nil } }

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
Expand Down Expand Up @@ -91,25 +87,18 @@
post :create, params: { ephemera_project: invalid_attributes }, session: valid_session
expect(assigns(:ephemera_project)).to be_a_new(EphemeraProject)
end

it "re-renders the 'new' template" do
post :create, params: { ephemera_project: invalid_attributes }, session: valid_session
expect(response).to render_template("new")
end
end
end

describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:new_attributes) { { name: "Updated Name" } }

it "updates the requested ephemera_project" do
ephemera_project = EphemeraProject.create! valid_attributes
put :update, params: { id: ephemera_project.to_param, ephemera_project: new_attributes }, session: valid_session
ephemera_project.reload
skip("Add assertions for updated state")
expect(ephemera_project.name).to eq("Updated Name")
end

it "assigns the requested ephemera_project as @ephemera_project" do
Expand Down
13 changes: 12 additions & 1 deletion spec/helpers/ephemera_fields_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,16 @@
# end
# end
RSpec.describe EphemeraFieldsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
let(:fields) { [
"EphemeraFolder.genre",
"EphemeraFolder.language",
"EphemeraFolder.geographic_origin",
"EphemeraFolder.subject"
] }

describe "#controllable_fields" do
it "lists the controllable fields" do
expect(helper.controllable_fields).to match_array(fields)
end
end
end
15 changes: 0 additions & 15 deletions spec/helpers/ephemera_projects_helper_spec.rb

This file was deleted.

17 changes: 16 additions & 1 deletion spec/models/ephemera_field_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
require 'rails_helper'

RSpec.describe EphemeraField, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let(:project) { FactoryGirl.build :ephemera_project }
let(:vocab) { FactoryGirl.build :vocabulary }

subject { described_class.new name: "EphemeraFolder.subject", ephemera_project: project, vocabulary: vocab }

it "has a name" do
expect(subject.name).to eq("EphemeraFolder.subject")
end

it "has a project" do
expect(subject.ephemera_project).to eq(project)
end

it "has a vocabulary" do
expect(subject.vocabulary).to eq(vocab)
end
end
3 changes: 2 additions & 1 deletion spec/models/ephemera_folder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
end
end
let(:folder) { FactoryGirl.build(:ephemera_folder, language: [VocabularyTerm.first.id.to_s]) }
let(:solr_doc) { folder.to_solr }
it "Returns the label for the IDs" do
expect(folder.to_solr["language_sim"]).to eq ["English"]
expect(solr_doc["language_sim"]).to eq ["English"]
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion spec/models/ephemera_project_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'rails_helper'

RSpec.describe EphemeraProject, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
it "has a name" do
project = described_class.new name: "Test Project"
expect(project.name).to eq("Test Project")
end
end
11 changes: 10 additions & 1 deletion spec/models/solr_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
hasRelatedImage_ssim: ["Image"],
member_of_collections_ssim: ["Collection1"],
member_of_collection_ids_ssim: ["1"],
folder_number_ssim: ["1"]
folder_number_ssim: ["1"],
ephemera_project_ssim: ["2"],
ephemera_project_name_ssim: ["Test Ephemera Project"]
}
end

Expand Down Expand Up @@ -156,6 +158,13 @@
end
end

describe "#ephemera_project_name" do
it "has accessors for the project id and name" do
expect(subject.ephemera_project).to eq("2")
expect(subject.ephemera_project_name).to eq("Test Ephemera Project")
end
end

describe '#method_missing' do
it 'passes through to super' do
expect { subject.send :foo }.to raise_error NoMethodError, /undefined method/
Expand Down

0 comments on commit a3d051e

Please sign in to comment.