From 9780d956cd9033e8d03d4cf378e8e6c4b011b860 Mon Sep 17 00:00:00 2001 From: "E. Lynette Rayle" Date: Tue, 27 Jul 2021 15:19:20 -0400 Subject: [PATCH 1/2] fix solr query in MultipleMembershipChecker and refactor to improve readability The code was complex and difficult to follow. I refactored so that each method has one and only one task and is clearly named for that task. Also, tests were heavily mocked and were hiding errors in the generated solr query. The tests are rewritten to organize them into clear test scenarios with real solr access. --- .../hyrax/multiple_membership_checker.rb | 78 +++-- .../hyrax/multiple_membership_checker_spec.rb | 272 ++++++++++-------- spec/spec_helper.rb | 1 + 3 files changed, 200 insertions(+), 151 deletions(-) diff --git a/app/services/hyrax/multiple_membership_checker.rb b/app/services/hyrax/multiple_membership_checker.rb index 291b4620ec..444baad28f 100644 --- a/app/services/hyrax/multiple_membership_checker.rb +++ b/app/services/hyrax/multiple_membership_checker.rb @@ -6,8 +6,7 @@ module Hyrax class MultipleMembershipChecker attr_reader :item - # @param [#member_of_collection_ids] item an object that belongs to - # collections + # @param [#member_of_collection_ids] item an object that belongs to collections def initialize(item:) @item = item end @@ -20,59 +19,78 @@ def initialize(item:) # `allow_multiple_membership` as `false` require that its members do not # also belong to other collections of the same type. # - # There are two contexts in which memberships are checked: when doing a - # wholesale replacement and when making an incremental change, such as - # adding a single collection membership to an object. In the former case, - # `#check` only scans the passed-in collection identifiers. In the latter, - # `#check` must also scan the collections to which an object currently - # belongs for potential conflicts. - # # @param collection_ids [Array] a list of collection identifiers - # @param include_current_members [Boolean] a flag to also scan an object's - # current collection memberships + # @param include_current_members [Boolean] if true, include item's existing + # collections in check; else if false, check passed in collections only + # * use `false` when collection_ids includes proposed new collections and existing + # collections (@see Hyrax::Actors::CollectionsMembershipActor #valid_membership?) + # * use `true` when collection_ids includes proposed new collections only + # (@see Hyrax::Collections::CollectionMemberService #add_member) # # @return [nil, String] nil if no conflicts; an error message string if so def check(collection_ids:, include_current_members: false) - # short-circuit if no single membership types have been created - return if collection_type_gids_that_disallow_multiple_membership.blank? - # short-circuit if no new single_membership_collections passed in - new_single_membership_collections = single_membership_collections(collection_ids) - return if new_single_membership_collections.blank? - collections_to_check = new_single_membership_collections - # No need to check current members when coming in from the ActorStack, which does a wholesale collection membership replacement - collections_to_check |= single_membership_collections(item.member_of_collection_ids) if include_current_members - problematic_collections = collections_to_check.uniq(&:id) - .group_by(&:collection_type_gid) - .select { |_gid, list| list.count > 1 } - return if problematic_collections.blank? + return unless single_membership_collection_types_exist? + + proposed_single_membership_collections = filter_to_single_membership_collections(collection_ids) + return if proposed_single_membership_collections.blank? + + collections_to_check = collections_to_check(proposed_single_membership_collections, + include_current_members) + problematic_collections = check_collections(collections_to_check) build_error_message(problematic_collections) end private - def single_membership_collections(collection_ids) + def single_membership_collection_types_exist? + single_membership_collection_types_gids.present? + end + + def single_membership_collection_types_gids + @single_membership_collection_types_gids ||= + Hyrax::CollectionType.gids_that_do_not_allow_multiple_membership&.map(&:to_s) + end + + def filter_to_single_membership_collections(collection_ids) return [] if collection_ids.blank? field_pairs = { - :id => Array(collection_ids).map(&:to_s), - Hyrax.config.collection_type_index_field.to_sym => collection_type_gids_that_disallow_multiple_membership&.map(&:to_s) + Hyrax.config.collection_type_index_field.to_sym => single_membership_collection_types_gids } Hyrax::SolrQueryService.new - .with_model(model: ::Collection) + .with_generic_type(generic_type: "Collection") + .with_ids(ids: Array[collection_ids]) .with_field_pairs(field_pairs: field_pairs, join_with: ' OR ') .get_objects(use_valkyrie: true).to_a end - def collection_type_gids_that_disallow_multiple_membership - Hyrax::CollectionType.gids_that_do_not_allow_multiple_membership + def collections_to_check(proposed, include_current_members) + # ActorStack does a wholesale collection membership replacement, such that + # proposed collections include existing and new collections. Parameter + # `include_current_members` will be false when coming from the actor stack + # to prevent member items being passed in and then added here as well. + return proposed unless include_current_members + proposed | filter_to_single_membership_collections(item.member_of_collection_ids) + end + + def check_collections(collections_to_check) + # uniq insures we include a collection only once when it is in the list multiple + # group_by groups collections of the same collection type together + # select keeps only collection type groups that have more than one collection + # of the single collection type + collections_to_check.uniq(&:id) + .group_by(&:collection_type_gid) + .select { |_gid, list| list.count > 1 } end def build_error_message(problematic_collections) + return if problematic_collections.blank? error_message_clauses = problematic_collections.map do |gid, list| I18n.t('hyrax.admin.collection_types.multiple_membership_checker.error_type_and_collections', type: collection_type_title_from_gid(gid), collections: collection_titles_from_list(list)) end - "#{I18n.t('hyrax.admin.collection_types.multiple_membership_checker.error_preamble')}#{error_message_clauses.join('; ')}" + "#{I18n.t('hyrax.admin.collection_types.multiple_membership_checker.error_preamble')}" \ + "#{error_message_clauses.join('; ')}" end def collection_type_title_from_gid(gid) diff --git a/spec/services/hyrax/multiple_membership_checker_spec.rb b/spec/services/hyrax/multiple_membership_checker_spec.rb index b6570eaac9..9435ee9c50 100644 --- a/spec/services/hyrax/multiple_membership_checker_spec.rb +++ b/spec/services/hyrax/multiple_membership_checker_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true RSpec.describe Hyrax::MultipleMembershipChecker, :clean_repo do - let(:item) { double } + let(:item) { create(:work, id: 'work-1', user: user) } + let(:user) { create(:user) } describe '#initialize' do subject { described_class.new(item: item) } @@ -11,35 +12,26 @@ end describe '#check' do + let(:base_errmsg) { "Error: You have specified more than one of the same single-membership collection type" } + let(:checker) { described_class.new(item: item) } let(:collection_ids) { ['foobar'] } let(:included) { false } let!(:collection_type) { create(:collection_type, title: 'Greedy', allow_multiple_membership: false) } let(:collection_types) { [collection_type] } let(:collection_type_gids) { [collection_type.to_global_id] } - let(:field_pairs) do - { - id: collection_ids, - collection_type_gid_ssim: collection_type_gids.map(&:to_s) - } - end - let(:field_pairs_for_col2) do - { - id: [collection2.id], - collection_type_gid_ssim: collection_type_gids.map(&:to_s) - } - end - let(:use_valkyrie) { true } before do - allow(Hyrax::CollectionType).to receive(:gids_that_do_not_allow_multiple_membership).and_return(collection_type_gids) + allow(Hyrax::CollectionType).to receive(:gids_that_do_not_allow_multiple_membership) + .and_return(collection_type_gids) end subject { checker.check(collection_ids: collection_ids, include_current_members: included) } context 'when there are no single-membership collection types' do it 'returns nil' do - expect(Hyrax::CollectionType).to receive(:gids_that_do_not_allow_multiple_membership).and_return([]) + expect(Hyrax::CollectionType).to receive(:gids_that_do_not_allow_multiple_membership) + .and_return([]) expect(subject).to be nil end end @@ -48,7 +40,8 @@ let(:collection_ids) { [] } it 'returns nil' do - expect(checker).to receive(:single_membership_collections).with(collection_ids).once.and_call_original + expect(checker).to receive(:filter_to_single_membership_collections) + .with(collection_ids).once.and_call_original expect(Hyrax::SolrQueryService).not_to receive(:new) expect(subject).to be nil end @@ -56,136 +49,173 @@ context 'when there are no single-membership collection instances' do it 'returns nil' do - expect(checker).to receive(:single_membership_collections).with(collection_ids).once.and_return([]) + expect(checker).to receive(:filter_to_single_membership_collections) + .with(collection_ids).once.and_return([]) expect(Hyrax::SolrQueryService).not_to receive(:new) expect(subject).to be nil end end - context 'when multiple single-membership collection instances are not in the list' do - let(:collection) { create(:collection_lw, id: 'collection0', collection_type: collection_type, with_solr_document: true) } - let(:collections) { [collection] } - let(:collection_ids) { collections.map(&:id) } + context 'when called from actor stack' do + # actor stack passes in all parent collections, existing and new; do not + # want to include item's existing collections or they will be in the checked + # list twice causing the check to always fail if the item is already in a + # single membership collection + let(:included) { false } + + context 'and multiple single-membership collections of the same type exist' do + let(:collection1) do + create(:collection_lw, id: 'collection1', title: ['Foo'], + collection_type: collection_type, + with_solr_document: true) + end + let(:collection2) do + create(:collection_lw, id: 'collection2', title: ['Bar'], + collection_type: collection_type, + with_solr_document: true) + end - it 'returns nil' do - expect(checker).to receive(:single_membership_collections).with(collection_ids).once.and_call_original - expect(Hyrax::SolrQueryService).to receive(:new).and_return(inst = double) - expect(inst).to receive(:with_model).with(model: ::Collection).once.and_return(inst_with_model = double) - expect(inst_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs, join_with: ' OR ').once.and_return(inst_with_full_query = double) - expect(inst_with_full_query).to receive(:get_objects).with(use_valkyrie: true).once.and_return(collections) - expect(subject).to be nil - end - end + before do + Hyrax.publisher.publish('object.metadata.updated', + object: item.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', + object: collection1.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', + object: collection2.valkyrie_resource, user: user) + end + + context 'and only one is in the list' do + let(:collections) { [collection1] } + let(:collection_ids) { collections.map(&:id) } + + it 'returns nil' do + expect(subject).to be nil + end + end + + context 'and both are in the list' do + let(:collections) { [collection1, collection2] } + let(:collection_ids) { collections.map(&:id) } - context 'when multiple single-membership collection instances are in the list, not including current members' do - let(:collection1) { create(:collection_lw, id: 'collection1', title: ['Foo'], collection_type: collection_type, with_solr_document: true) } - let(:collection2) { create(:collection_lw, id: 'collection2', title: ['Bar'], collection_type: collection_type, with_solr_document: true) } - let(:collections) { [collection1, collection2] } - let(:collection_ids) { collections.map(&:id) } - - it 'returns an error' do - expect(item).not_to receive(:member_of_collection_ids) - expect(checker).to receive(:single_membership_collections).with(collection_ids).once.and_call_original - expect(Hyrax::SolrQueryService).to receive(:new).and_return(inst = double) - expect(inst).to receive(:with_model).with(model: ::Collection).once.and_return(inst_with_model = double) - expect(inst_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs, join_with: ' OR ').once.and_return(inst_with_full_query = double) - expect(inst_with_full_query).to receive(:get_objects).with(use_valkyrie: true).once.and_return(collections) - expect(subject).to eq 'Error: You have specified more than one of the same single-membership collection type (type: Greedy, collections: Foo and Bar)' + it 'returns an error' do + regexp = /#{base_errmsg} \(type: Greedy, collections: (Foo and Bar|Bar and Foo)\)/ + expect(subject).to match regexp + end + end end - context 'with multiple single membership collection types' do + context 'and multiple single-membership collection instances of different types exist' do let!(:collection_type_2) { create(:collection_type, title: 'Doc', allow_multiple_membership: false) } - let(:collection_type_gids) { [collection_type.to_global_id, collection_type_2.to_global_id] } - - it 'returns an error' do - expect(item).not_to receive(:member_of_collection_ids) - expect(checker).to receive(:single_membership_collections).with(collection_ids).once.and_call_original - expect(Hyrax::SolrQueryService).to receive(:new).and_return(inst = double) - expect(inst).to receive(:with_model).with(model: ::Collection).once.and_return(inst_with_model = double) - expect(inst_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs, join_with: ' OR ').once.and_return(inst_with_full_query = double) - expect(inst_with_full_query).to receive(:get_objects).with(use_valkyrie: true).once.and_return(collections) - expect(subject).to eq 'Error: You have specified more than one of the same single-membership collection type (type: Greedy, collections: Foo and Bar)' + let(:collection1) { create(:collection_lw, title: ['Foo'], collection_type: collection_type, with_solr_document: true) } + let(:collection2) { create(:collection_lw, title: ['Bar'], collection_type: collection_type, with_solr_document: true) } + let(:collection3) { create(:collection_lw, title: ['Baz'], collection_type: collection_type_2, with_solr_document: true) } + + before do + Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', object: collection3.valkyrie_resource, user: user) end - end - end - context 'when multiple single-membership collection instances are in the list, including current members' do - let(:collection1) { create(:collection_lw, id: 'collection1', title: ['Foo'], collection_type: collection_type, with_solr_document: true) } - let(:collection2) { create(:collection_lw, id: 'collection2', title: ['Bar'], collection_type: collection_type, with_solr_document: true) } - let(:collections) { [collection1] } - let(:collection_ids) { collections.map(&:id) } - let(:included) { true } + context 'and collections of both types are passed in' do + let(:collections) { [collection1, collection3] } + let(:collection_ids) { collections.map(&:id) } - before do - allow(item).to receive(:member_of_collection_ids).once.and_return([collection2.id]) - end + it 'returns nil' do + expect(subject).to be nil + end + end - it 'returns an error' do - expect(item).to receive(:member_of_collection_ids) - expect(Hyrax::SolrQueryService).to receive(:new).and_return(inst1 = double, inst2 = double) - expect(inst1).to receive(:with_model).with(model: ::Collection).and_return(inst1_with_model = double) - expect(inst1_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs, join_with: ' OR ').and_return(inst1_with_full_query = double) - expect(inst1_with_full_query).to receive(:get_objects).with(use_valkyrie: true).and_return(collections) - expect(inst2).to receive(:with_model).with(model: ::Collection).and_return(inst2_with_model = double) - expect(inst2_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs_for_col2, join_with: ' OR ').and_return(inst2_with_full_query = double) - expect(inst2_with_full_query).to receive(:get_objects).with(use_valkyrie: true).and_return([collection2]) - expect(subject).to eq 'Error: You have specified more than one of the same single-membership collection type (type: Greedy, collections: Foo and Bar)' - end + context 'and collections of the same type are passed in' do + let(:collections) { [collection1, collection2] } + let(:collection_ids) { collections.map(&:id) } - context 'with multiple single membership collection types' do - let!(:collection_type_2) { create(:collection_type, title: 'Doc', allow_multiple_membership: false) } - let(:collection_type_gids) { [collection_type.to_global_id, collection_type_2.to_global_id] } - - it 'returns an error' do - expect(item).to receive(:member_of_collection_ids) - expect(Hyrax::SolrQueryService).to receive(:new).and_return(inst1 = double, inst2 = double) - expect(inst1).to receive(:with_model).with(model: ::Collection).and_return(inst1_with_model = double) - expect(inst1_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs, join_with: ' OR ').and_return(inst1_with_full_query = double) - expect(inst1_with_full_query).to receive(:get_objects).with(use_valkyrie: true).and_return(collections) - expect(inst2).to receive(:with_model).with(model: ::Collection).and_return(inst2_with_model = double) - expect(inst2_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs_for_col2, join_with: ' OR ').and_return(inst2_with_full_query = double) - expect(inst2_with_full_query).to receive(:get_objects).with(use_valkyrie: true).and_return([collection2]) - expect(subject).to eq 'Error: You have specified more than one of the same single-membership collection type (type: Greedy, collections: Foo and Bar)' + it 'returns an error' do + regexp = /#{base_errmsg} \(type: Greedy, collections: (Foo and Bar|Bar and Foo)\)/ + expect(subject).to match regexp + end end end end - context 'when multiple single-membership collection instances are in the list, but are different collection types' do - let(:collection1) { create(:collection_lw, title: ['Foo'], collection_type: collection_type, with_solr_document: true) } - let(:collection2) { create(:collection_lw, title: ['Bar'], collection_type: collection_type_2, with_solr_document: true) } - let(:collections) { [collection1, collection2] } - let(:collection_ids) { collections.map(&:id) } - let(:collection_type_2) { create(:collection_type, title: 'Doc', allow_multiple_membership: false) } - let(:collection_type_gids) { [collection_type.to_global_id, collection_type_2.to_global_id] } + context 'when incrementally adding collections' do + # for incremental add, the proposed collection list only includes the new collections, so need to include the existing collections + # in the checked list to catch the case where the item is already in a single membership collection of the same collection type + let(:included) { true } - it 'returns nil' do - expect(item).not_to receive(:member_of_collection_ids) - expect(checker).to receive(:single_membership_collections).with(collection_ids).once.and_call_original - expect(Hyrax::SolrQueryService).to receive(:new).and_return(inst = double) - expect(inst).to receive(:with_model).with(model: ::Collection).once.and_return(inst_with_model = double) - expect(inst_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs, join_with: ' OR ').once.and_return(inst_with_full_query = double) - expect(inst_with_full_query).to receive(:get_objects).with(use_valkyrie: true).once.and_return(collections) - expect(subject).to be nil + context 'and multiple single-membership collections of the same type exist' do + let(:collection1) { create(:collection_lw, id: 'collection1', title: ['Foo'], collection_type: collection_type, with_solr_document: true) } + let(:collection2) { create(:collection_lw, id: 'collection2', title: ['Bar'], collection_type: collection_type, with_solr_document: true) } + let(:item_2) { create(:work, id: 'work-2', user: user) } + + context 'and only one is in the list' do + let(:collections) { [collection1] } + let(:collection_ids) { collections.map(&:id) } + + context 'and the member is already in the other single membership collection' do + before do + [item, item_2].each do |work| + work.member_of_collections << collection2 + work.save! + Hyrax.publisher.publish('object.metadata.updated', object: work.valkyrie_resource, user: user) + end + Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user) + end + + it 'returns an error' do + regexp = /#{base_errmsg} \(type: Greedy, collections: (Foo and Bar|Bar and Foo)\)/ + expect(subject).to match regexp + end + end + + context 'and the member is not already in the other single membership collection' do + before do + [item_2].each do |work| + work.member_of_collections << collection2 + work.save! + Hyrax.publisher.publish('object.metadata.updated', object: work.valkyrie_resource, user: user) + end + Hyrax.publisher.publish('object.metadata.updated', object: item.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user) + end + + it 'returns nil' do + expect(subject).to be nil + end + end + end end - context 'when including current members' do - let(:collections) { [collection1] } - let(:included) { true } + context 'and multiple single-membership collection instances of different types exist' do + let!(:collection_type_2) { create(:collection_type, title: 'Doc', allow_multiple_membership: false) } + let(:collection1) { create(:collection_lw, title: ['Foo'], collection_type: collection_type, with_solr_document: true) } + let(:collection2) { create(:collection_lw, title: ['Bar'], collection_type: collection_type, with_solr_document: true) } + let(:collection3) { create(:collection_lw, title: ['Baz'], collection_type: collection_type_2, with_solr_document: true) } before do - allow(item).to receive(:member_of_collection_ids).once.and_return([collection2.id]) + Hyrax.publisher.publish('object.metadata.updated', object: collection1.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', object: collection2.valkyrie_resource, user: user) + Hyrax.publisher.publish('object.metadata.updated', object: collection3.valkyrie_resource, user: user) end - it 'returns nil' do - expect(item).to receive(:member_of_collection_ids) - expect(Hyrax::SolrQueryService).to receive(:new).and_return(inst1 = double, inst2 = double) - expect(inst1).to receive(:with_model).with(model: ::Collection).and_return(inst1_with_model = double) - expect(inst1_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs, join_with: ' OR ').and_return(inst1_with_full_query = double) - expect(inst1_with_full_query).to receive(:get_objects).with(use_valkyrie: true).and_return(collections) - expect(inst2).to receive(:with_model).with(model: ::Collection).and_return(inst2_with_model = double) - expect(inst2_with_model).to receive(:with_field_pairs).with(field_pairs: field_pairs_for_col2, join_with: ' OR ').and_return(inst2_with_full_query = double) - expect(inst2_with_full_query).to receive(:get_objects).with(use_valkyrie: true).and_return([collection2]) - expect(subject).to be nil + context 'and collections of both types are passed in' do + let(:collections) { [collection1, collection3] } + let(:collection_ids) { collections.map(&:id) } + + it 'returns nil' do + expect(subject).to be nil + end + end + + context 'and collections of the same type are passed in' do + let(:collections) { [collection1, collection2] } + let(:collection_ids) { collections.map(&:id) } + + it 'returns an error' do + regexp = /#{base_errmsg} \(type: Greedy, collections: (Foo and Bar|Bar and Foo)\)/ + expect(subject).to match regexp + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6272de5eff..bd213e1e6e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -108,6 +108,7 @@ def clean_active_fedora_repository # The JS is executed in a different thread, so that other thread # may think the root path has already been created: ActiveFedora.fedora.connection.send(:init_base_path) + Hyrax.persister.wipe! if Hyrax.config.query_index_from_valkyrie end RSpec.configure do |config| From da4e523c6a7a0460f7cea5ad2f677efba7fcd3df Mon Sep 17 00:00:00 2001 From: "E. Lynette Rayle" Date: Wed, 28 Jul 2021 07:50:24 -0400 Subject: [PATCH 2/2] only clear valkyrie solr when :clean_repo is specified in a spec --- app/services/hyrax/solr_service.rb | 9 ++++++++- spec/services/hyrax/solr_service_spec.rb | 19 +++++++++++++++++++ spec/spec_helper.rb | 5 ++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/services/hyrax/solr_service.rb b/app/services/hyrax/solr_service.rb index 7e429b33ac..74a1bc3ac9 100644 --- a/app/services/hyrax/solr_service.rb +++ b/app/services/hyrax/solr_service.rb @@ -37,7 +37,8 @@ def select_path 'Use `Hyrax.config.solr_select_path` instead' end - delegate :add, :commit, :count, :delete, :get, :instance, :ping, :post, :query, :delete_by_query, :search_by_id, to: :new + delegate :add, :commit, :count, :delete, :get, :instance, :ping, :post, + :query, :delete_by_query, :search_by_id, :wipe!, to: :new end # Wraps rsolr get @@ -100,6 +101,12 @@ def delete(id) connection.delete_by_id(id, params: COMMIT_PARAMS) end + # Deletes all solr documents + def wipe! + delete_by_query("*:*") + commit + end + # Wraps rsolr add # @return [Hash] the hash straight form rsolr def add(solr_doc, commit: true) diff --git a/spec/services/hyrax/solr_service_spec.rb b/spec/services/hyrax/solr_service_spec.rb index 76a03c92a5..0210188dbd 100644 --- a/spec/services/hyrax/solr_service_spec.rb +++ b/spec/services/hyrax/solr_service_spec.rb @@ -197,6 +197,25 @@ end end + describe ".wipe!" do + it "calls solr" do + expect(mock_conn).to receive(:delete_by_query).with("*:*", params: {}) + allow(described_class).to receive(:instance).and_return(double("instance", conn: mock_conn)) + expect(mock_conn).to receive(:commit) + described_class.wipe! + end + + context "when use_valkyrie: true" do + let(:service) { described_class.new(use_valkyrie: true) } + + it "uses valkyrie solr based on config query_index_from_valkyrie" do + expect(mock_conn).to receive(:delete_by_query).with("*:*", params: {}) + expect(mock_conn).to receive(:commit) + service.wipe! + end + end + end + describe '.instance' do let(:mock_instance) { double("instance", conn: mock_conn) } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bd213e1e6e..8d4ff388ca 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -108,7 +108,6 @@ def clean_active_fedora_repository # The JS is executed in a different thread, so that other thread # may think the root path has already been created: ActiveFedora.fedora.connection.send(:init_base_path) - Hyrax.persister.wipe! if Hyrax.config.query_index_from_valkyrie end RSpec.configure do |config| @@ -236,6 +235,10 @@ def clean_active_fedora_repository config.before(:example, :clean_repo) do clean_active_fedora_repository Hyrax::RedisEventStore.instance.redis.flushdb + + # Not needed to clean the Solr core used by ActiveFedora since + # clean_active_fedora_repository will wipe that core + Hyrax::SolrService.wipe! if Hyrax.config.query_index_from_valkyrie end # Use this example metadata when you want to perform jobs inline during testing.