Skip to content

Commit

Permalink
Better stubbing
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgriffiniii committed Jul 30, 2019
1 parent 586f2d5 commit 8a965f1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 35 deletions.
21 changes: 17 additions & 4 deletions app/services/hydra_editor/field_metadata_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ class FieldMetadataService
# If the field is a property, delegate to the property.
# Otherwise return false
def self.multiple?(model_class, field)
if reflection = model_class.reflect_on_association(field)
reflection.collection?
elsif model_class.attribute_names.include?(field.to_s)
model_class.multiple?(field)
if model_class.respond_to?(:reflect_on_association)
# For ActiveFedora reflections

if reflection = model_class.reflect_on_association(field)
reflection.collection?
elsif model_class.attribute_names.include?(field.to_s)
model_class.multiple?(field)
else
false
end
elsif false
# For Valkyrie schemata

elsif model_class.respond_to?(:reflections)
# For ActiveRecord reflections
model_class.reflections.key?(field.to_s)
else
# The ORM for one-to-many/many-to-many cardinality is not supported
false
end
end
Expand Down
96 changes: 65 additions & 31 deletions spec/presenters/hydra_editor_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
require 'spec_helper'
require 'pry-byebug'

describe Hydra::Presenter do
before do
class Contributor < ActiveRecord::Base
def self.table_name_prefix
'hydra_editor'
end

# has_and_belongs_to_many :books, predicate: ::RDF::Vocab::DC.title
has_and_belongs_to_many :books
end
let(:model_class) { TestModel }
let(:object) { instance_double(model_class, title: ['foo', 'bar'], creator: 'baz') }
let(:presenter) { TestPresenter.new(object) }

class Publisher < ActiveRecord::Base
def self.table_name_prefix
'hydra_editor'
end
before(:all) do
# For stubbing the API for ActiveRecord models
class Contributor < ActiveRecord::Base; end

# has_many :books, predicate: ::RDF::Vocab::DC.title
has_many :books
end
class Publisher < ActiveRecord::Base; end

class TestModel < ActiveRecord::Base
def self.table_name_prefix
'hydra_editor'
end

attr_accessor :title, :creator
# property :title, predicate: ::RDF::Vocab::DC.title
# property :creator, predicate: ::RDF::Vocab::DC.creator, multiple: false
# has_and_belongs_to_many :contributors, predicate: ::RDF::Vocab::DC.contributor
has_and_belongs_to_many :contributors
# belongs_to :publisher, predicate: ::RDF::Vocab::DC.publisher
belongs_to :publisher

# This needs to change
def self.unique?(field); end
def title; end
def creator; end
# This needs to change
def new_record?; end
end

# For stubbing the API for ActiveFedora models
class TestFedoraModel

def self.unique?(field); end
def self.reflect_on_association(field); end
def title; end
def creator; end
def new_record?; end
def persisted?; end
def model_name; end
end

class TestPresenter
include Hydra::Presenter
self.model_class = TestModel
# TODO: This needs to be removed
# self.model_class = model_class
# Terms is the list of fields displayed by app/views/generic_files/_show_descriptions.html.erb
self.terms = [:title, :creator]

Expand All @@ -46,20 +49,26 @@ class TestPresenter
end
end

after do
after(:all) do
Object.send(:remove_const, :TestPresenter)
Object.send(:remove_const, :TestFedoraModel)
Object.send(:remove_const, :TestModel)
end

before do
TestPresenter.model_class = model_class
allow(model_class).to receive(:unique?).and_return(false)
allow(object).to receive(:new_record?).and_return(false)
allow(object).to receive(:persisted?).and_return(true)
allow(object).to receive(:model_name).and_return('TestModel')
end

describe 'class methods' do
subject { TestPresenter.model_name }
it { is_expected.to eq 'TestModel' }
end

let(:object) { TestModel.new(title: ['foo', 'bar'], creator: 'baz') }
let(:presenter) { TestPresenter.new(object) }

describe '#model_name' do
describe '#model_name' do
subject { presenter.model_name }
it { is_expected.to eq 'TestModel' }
end
Expand All @@ -71,15 +80,30 @@ class TestPresenter

describe 'new_record?' do
subject { presenter.new_record? }

before do
allow(object).to receive(:new_record?).and_return(true)
end

it { is_expected.to be true }
end

describe 'persisted?' do
subject { presenter.persisted? }

before do
allow(object).to receive(:persisted?).and_return(false)
end

it { is_expected.to be false }
end

describe 'the term accessors' do
before do
allow(object).to receive(:[]).with(:title).and_return(['foo', 'bar'])
allow(object).to receive(:[]).with(:creator).and_return('baz')
end

it 'has the accessors' do
expect(presenter.title).to match_array ['foo', 'bar']
expect(presenter.creator).to eq 'baz'
Expand Down Expand Up @@ -112,6 +136,12 @@ def count
subject { presenter.multiple?(field) }

context 'for a multivalue string' do
let(:model_class) { TestFedoraModel }
let(:reflection) { double }
before do
allow(reflection).to receive(:collection?).and_return(true)
allow(model_class).to receive(:reflect_on_association).with(field).and_return(reflection)
end
let(:field) { :title }
it { is_expected.to be true }
end
Expand Down Expand Up @@ -173,6 +203,10 @@ def count
end

context 'for a single value string' do
before do
allow(model_class).to receive(:unique?).with(field).and_return(true)
end

let(:field) { :creator }
it { is_expected.to be true }
end
Expand Down

0 comments on commit 8a965f1

Please sign in to comment.