<commit_before># frozen_string_literal: true
RSpec.describe Hyrax::CustomQueries::Navigators::ParentWorkNavigator, valkyrie_adapter: :test_adapter, clean_repo: true do
  let!(:parent_work) { FactoryBot.valkyrie_create(:hyrax_work, id: 'pw1', title: ['Parent Work 1'], member_ids: member_ids) }

  let(:child_work1) { FactoryBot.valkyrie_create(:hyrax_work, id: 'cw1', title: ['Child Work 1']) }
  let(:child_work2) { FactoryBot.valkyrie_create(:hyrax_work, id: 'cw2', title: ['Child Work 2']) }
  let(:fileset1)    { FactoryBot.valkyrie_create(:hyrax_file_set, id: 'fs1', title: ['Child File Set 1']) }
  let(:fileset2)    { FactoryBot.valkyrie_create(:hyrax_file_set, id: 'fs2', title: ['Child File Set 2']) }

  let(:member_ids) do
    [
      child_work1.id,
      child_work2.id,
      fileset1.id,
      fileset2.id
    ]
  end

  let(:custom_query_service) { Hyrax.custom_queries }

  describe '#find_parent_work' do
    context 'on a work' do
      it 'returns one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work(resource: child_work1).id).to eq parent_work.id
      end

      context 'when more than one parent' do
        let!(:work) { FactoryBot.valkyrie_create(:hyrax_work, id: 'pw2', title: ['Parent Work 2'], member_ids: member_ids) }
        let(:child_work3) { FactoryBot.valkyrie_create(:hyrax_work, id: 'cw3', title: ['Child Work 3']) }
        let(:member_ids) { [child_work3.id] }
        it 'logs warning about more than one parent and returns the first parent work as Valkyrie resources' do
          expect(Hyrax.logger).to receive(:warn).with("Work cw3 is in 2 works when it should be in no more than one.")
          parent = custom_query_service.find_parent_work(resource: child_work3)
          # There is no guarantee which of the parents will be returned.
          expect([work.id, parent_work.id]).to include parent.id
        end
      end
    end

    context 'on a fileset' do
      it 'returns one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work(resource: fileset1).id).to eq parent_work.id
      end

      context 'when more than one parent' do
        let!(:work) { FactoryBot.valkyrie_create(:hyrax_work, id: 'pw2', title: ['Parent Work 2'], member_ids: member_ids) }
        let(:fileset3) { FactoryBot.valkyrie_create(:hyrax_file_set, id: 'fs3', title: ['Child File Set 3']) }
        let(:member_ids) { [fileset3.id] }
        it 'logs warning about more than one parent and returns the first parent work as Valkyrie resources' do
          expect(Hyrax.logger).to receive(:warn).with("File set fs3 is in 2 works when it should be in no more than one.")
          parent = custom_query_service.find_parent_work(resource: fileset3)
          # There is no guarantee which of the parents will be returned.
          expect([work.id, parent_work.id]).to include parent.id
        end
      end
    end

    context 'when no parents' do
      let(:member_ids) { [] }
      it 'returns nil' do
        expect(custom_query_service.find_parent_work(resource: child_work1)).to be_nil
        expect(custom_query_service.find_parent_work(resource: fileset1)).to be_nil
      end
    end
  end

  describe '#find_parent_work_id' do
    context 'on a work' do
      it 'returns the id of one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work_id(resource: child_work1)).to eq parent_work.id
      end
    end

    context 'on a fileset' do
      it 'returns the id of one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work_id(resource: fileset1)).to eq parent_work.id
      end
    end
  end
end<commit_msg>add test for find_parent_work_id when no members<commit_after># frozen_string_literal: true
RSpec.describe Hyrax::CustomQueries::Navigators::ParentWorkNavigator, valkyrie_adapter: :test_adapter, clean_repo: true do
  let!(:parent_work) { FactoryBot.valkyrie_create(:hyrax_work, id: 'pw1', title: ['Parent Work 1'], member_ids: member_ids) }

  let(:child_work1) { FactoryBot.valkyrie_create(:hyrax_work, id: 'cw1', title: ['Child Work 1']) }
  let(:child_work2) { FactoryBot.valkyrie_create(:hyrax_work, id: 'cw2', title: ['Child Work 2']) }
  let(:fileset1)    { FactoryBot.valkyrie_create(:hyrax_file_set, id: 'fs1', title: ['Child File Set 1']) }
  let(:fileset2)    { FactoryBot.valkyrie_create(:hyrax_file_set, id: 'fs2', title: ['Child File Set 2']) }

  let(:member_ids) do
    [
      child_work1.id,
      child_work2.id,
      fileset1.id,
      fileset2.id
    ]
  end

  let(:custom_query_service) { Hyrax.custom_queries }

  describe '#find_parent_work' do
    context 'on a work' do
      it 'returns one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work(resource: child_work1).id).to eq parent_work.id
      end

      context 'when more than one parent' do
        let!(:work) { FactoryBot.valkyrie_create(:hyrax_work, id: 'pw2', title: ['Parent Work 2'], member_ids: member_ids) }
        let(:child_work3) { FactoryBot.valkyrie_create(:hyrax_work, id: 'cw3', title: ['Child Work 3']) }
        let(:member_ids) { [child_work3.id] }
        it 'logs warning about more than one parent and returns the first parent work as Valkyrie resources' do
          expect(Hyrax.logger).to receive(:warn).with("Work cw3 is in 2 works when it should be in no more than one.")
          parent = custom_query_service.find_parent_work(resource: child_work3)
          # There is no guarantee which of the parents will be returned.
          expect([work.id, parent_work.id]).to include parent.id
        end
      end
    end

    context 'on a fileset' do
      it 'returns one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work(resource: fileset1).id).to eq parent_work.id
      end

      context 'when more than one parent' do
        let!(:work) { FactoryBot.valkyrie_create(:hyrax_work, id: 'pw2', title: ['Parent Work 2'], member_ids: member_ids) }
        let(:fileset3) { FactoryBot.valkyrie_create(:hyrax_file_set, id: 'fs3', title: ['Child File Set 3']) }
        let(:member_ids) { [fileset3.id] }
        it 'logs warning about more than one parent and returns the first parent work as Valkyrie resources' do
          expect(Hyrax.logger).to receive(:warn).with("File set fs3 is in 2 works when it should be in no more than one.")
          parent = custom_query_service.find_parent_work(resource: fileset3)
          # There is no guarantee which of the parents will be returned.
          expect([work.id, parent_work.id]).to include parent.id
        end
      end
    end

    context 'when no parents' do
      let(:member_ids) { [] }
      it 'returns nil' do
        expect(custom_query_service.find_parent_work(resource: child_work1)).to be_nil
        expect(custom_query_service.find_parent_work(resource: fileset1)).to be_nil
      end
    end
  end

  describe '#find_parent_work_id' do
    context 'on a work' do
      it 'returns the id of one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work_id(resource: child_work1)).to eq parent_work.id
      end
    end

    context 'on a fileset' do
      it 'returns the id of one parent work as Valkyrie resources' do
        expect(custom_query_service.find_parent_work_id(resource: fileset1)).to eq parent_work.id
      end
    end

    context 'when no parents' do
      let(:member_ids) { [] }
      it 'returns nil' do
        expect(custom_query_service.find_parent_work_id(resource: child_work1)).to be_nil
        expect(custom_query_service.find_parent_work_id(resource: fileset1)).to be_nil
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10106107660455486, 'fraction_numerical': 0.011128364389233954, 'mean_word_length': 3.4190966266437965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7489970', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2451, 'n_words': 703}