|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +class MockFile |
| 4 | + def initialize(path) |
| 5 | + @filepath = OpenStruct.new(path: path) |
| 6 | + end |
| 7 | + |
| 8 | + def uploader |
| 9 | + @filepath |
| 10 | + end |
| 11 | +end |
| 12 | + |
| 13 | +class MockIO |
| 14 | + def initialize(path) |
| 15 | + @file = MockFile.new(path) |
| 16 | + end |
| 17 | + |
| 18 | + def uploaded_file |
| 19 | + @file |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +class MockRepo |
| 24 | + def initialize(id) |
| 25 | + @id = id |
| 26 | + end |
| 27 | + |
| 28 | + def id |
| 29 | + @id |
| 30 | + end |
| 31 | + |
| 32 | + def restore_version(revision_id) |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +class MockFileSet |
| 37 | + def initialize(id, save, label, created) |
| 38 | + @id = id |
| 39 | + @save = save |
| 40 | + @label = label |
| 41 | + @created = created |
| 42 | + end |
| 43 | + |
| 44 | + def id |
| 45 | + @id |
| 46 | + end |
| 47 | + |
| 48 | + def save |
| 49 | + @save |
| 50 | + end |
| 51 | + |
| 52 | + def latest_version |
| 53 | + OpenStruct.new(label:@label, created: @created) |
| 54 | + end |
| 55 | + |
| 56 | + def provenance_update_version(current_user:, event_note:, new_create_date:, new_revision_id:, prior_create_date:, prior_revision_id:, revision_id:) |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +class MockPublicSend |
| 61 | + |
| 62 | + def initialize(sent) |
| 63 | + @sent = sent |
| 64 | + end |
| 65 | + |
| 66 | + def public_send(relation) |
| 67 | + @sent |
| 68 | + end |
| 69 | + |
| 70 | + def id () |
| 71 | + "FileSet ID" |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +RSpec.describe Hyrax::Actors::FileActor do |
| 79 | + |
| 80 | + describe "#initialize" do |
| 81 | + it "creates instance variables" do |
| 82 | + actor_file = Hyrax::Actors::FileActor.new("file set", "relation", "user") |
| 83 | + |
| 84 | + actor_file.instance_variable_get(:@file_set) == "file set" |
| 85 | + actor_file.instance_variable_get(:@relation) == :relation |
| 86 | + actor_file.instance_variable_get(:@user) == "user" |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + |
| 91 | + describe "#ingest_file" do |
| 92 | + before { |
| 93 | + allow(::Deepblue::LoggingHelper).to receive(:here).and_return "here" |
| 94 | + allow(::Deepblue::LoggingHelper).to receive(:called_from).and_return "called from" |
| 95 | + } |
| 96 | + |
| 97 | + context "when bypass_fedora and file_set.save is negative" do |
| 98 | + fileset = OpenStruct.new(save: false) |
| 99 | + subject { described_class.new(fileset, "relational", "the user") } |
| 100 | + |
| 101 | + before { |
| 102 | + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "io=io)", "user=the user", "continue_job_chain=true", "continue_job_chain_later=true", |
| 103 | + "delete_input_file=true", "uploaded_file_ids=[11, 22, 33]", "bypass_fedora=true", ""]) |
| 104 | + allow(Hydra::Works::AddExternalFileToFileSet).to receive(:call).with(fileset, true, :relational, versioning: false) |
| 105 | + } |
| 106 | + it "logs arguments and calls AddExternalFileToFileSet" do |
| 107 | + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "io=io)", "user=the user", "continue_job_chain=true", "continue_job_chain_later=true", |
| 108 | + "delete_input_file=true", "uploaded_file_ids=[11, 22, 33]", "bypass_fedora=true", ""]) |
| 109 | + expect(Hydra::Works::AddExternalFileToFileSet).to receive(:call).with(fileset, true, :relational, versioning: false) |
| 110 | + |
| 111 | + expect(subject.ingest_file("io", current_user: "the user", uploaded_file_ids: [11,22,33], bypass_fedora: true)).to eq false |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + |
| 116 | + context "when not bypass_fedora" do |
| 117 | + fileset = OpenStruct.new(save: true) |
| 118 | + mockRepo = MockRepo.new(id: "repo") |
| 119 | + |
| 120 | + subject { described_class.new(fileset, "relational", "the user") } |
| 121 | + |
| 122 | + before { |
| 123 | + allow(subject).to receive(:related_file).and_return mockRepo |
| 124 | + allow(Hyrax::VersioningService).to receive(:create).with(mockRepo, "user") |
| 125 | + } |
| 126 | + |
| 127 | + context "when continue_job_chain_later is true" do |
| 128 | + io = MockIO.new("one true path") |
| 129 | + |
| 130 | + before { |
| 131 | + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "io=#{io})", "user=the user", "continue_job_chain=true", "continue_job_chain_later=true", |
| 132 | + "delete_input_file=true", "uploaded_file_ids=[11, 22, 33]", "bypass_fedora=false", ""]) |
| 133 | + allow(Hydra::Works::AddFileToFileSet).to receive(:call).with(fileset, "one true path", :relational, versioning: false) |
| 134 | + |
| 135 | + allow(CharacterizeJob).to receive(:perform_later).with(fileset, {:id=>"repo"}, "one true path", current_user: "the user", uploaded_file_ids: [11,22,33]) |
| 136 | + } |
| 137 | + it "logs arguments and calls AddFileToFileSet" do |
| 138 | + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "io=#{io})", "user=the user", "continue_job_chain=true", "continue_job_chain_later=true", |
| 139 | + "delete_input_file=true", "uploaded_file_ids=[11, 22, 33]", "bypass_fedora=false", ""]) |
| 140 | + expect(Hydra::Works::AddFileToFileSet).to receive(:call).with(fileset, io, :relational, versioning: false) |
| 141 | + expect(Hyrax::VersioningService).to receive(:create).with(mockRepo, "the user") |
| 142 | + expect(CharacterizeJob).to receive(:perform_later).with(fileset, {:id=>"repo"}, "one true path", current_user: "the user", uploaded_file_ids: [11,22,33]) |
| 143 | + subject.ingest_file(io, current_user: "the user", uploaded_file_ids: [11,22,33]) |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + |
| 148 | + context "when continue_job_chain_later is false" do |
| 149 | + io = OpenStruct.new(uploaded_file: false, path: "path finder") |
| 150 | + |
| 151 | + before { |
| 152 | + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "io=#{io})", "user=the user", "continue_job_chain=true", "continue_job_chain_later=false", |
| 153 | + "delete_input_file=true", "uploaded_file_ids=[11, 22, 33]", "bypass_fedora=false", ""]) |
| 154 | + allow(Hydra::Works::AddFileToFileSet).to receive(:call).with(fileset, io, :relational, versioning: false) |
| 155 | + allow(CharacterizeJob).to receive(:perform_now).with(fileset, {:id=>"repo"}, "path finder", continue_job_chain: true, continue_job_chain_later:false, current_user: "user", |
| 156 | + delete_input_file: true, uploaded_file_ids: [11,22,33]) |
| 157 | + } |
| 158 | + it "logs arguments and calls AddFileToFileSet" do |
| 159 | + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "io=#{io})", "user=the user", "continue_job_chain=true", "continue_job_chain_later=false", |
| 160 | + "delete_input_file=true", "uploaded_file_ids=[11, 22, 33]", "bypass_fedora=false", ""]) |
| 161 | + expect(Hydra::Works::AddFileToFileSet).to receive(:call).with(fileset, io, :relational, versioning: false) |
| 162 | + expect(Hyrax::VersioningService).to receive(:create).with(mockRepo, "the user") |
| 163 | + expect(CharacterizeJob).to receive(:perform_now).with(fileset, {:id=>"repo"}, "path finder", continue_job_chain: true, continue_job_chain_later:false, |
| 164 | + current_user: "the user", delete_input_file: true, uploaded_file_ids: [11,22,33]) |
| 165 | + subject.ingest_file(io, continue_job_chain_later: false, current_user: "the user", uploaded_file_ids: [11,22,33]) |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + |
| 172 | + describe "#revert_to" do |
| 173 | + before { |
| 174 | + allow(Deepblue::LoggingHelper).to receive(:here).and_return "here" |
| 175 | + allow(Deepblue::LoggingHelper).to receive(:called_from).and_return "called from" |
| 176 | + } |
| 177 | + |
| 178 | + context "when file_set.save is negative" do |
| 179 | + repo = MockRepo.new(id: "repo") |
| 180 | + fileset = MockFileSet.new("a great file", false, "brand new release", "hot off the presses") |
| 181 | + subject { described_class.new(fileset, "relational", "the user") } |
| 182 | + |
| 183 | + before { |
| 184 | + allow(subject).to receive(:related_file).and_return repo |
| 185 | + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "user=the user", "file_set.id=a great file", "relation=relational", |
| 186 | + "revision_id=revise revise revise" ]) |
| 187 | + } |
| 188 | + it "returns false" do |
| 189 | + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "user=the user", "file_set.id=a great file", "relation=relational", |
| 190 | + "revision_id=revise revise revise" ]) |
| 191 | + expect(repo).to receive(:restore_version).with("revise revise revise") |
| 192 | + expect(subject.revert_to("revise revise revise")).to eq false |
| 193 | + end |
| 194 | + end |
| 195 | + |
| 196 | + context "when file_set.save is positive" do |
| 197 | + repo = MockRepo.new(id: "repo") |
| 198 | + file_set = MockFileSet.new("a classic file", true, "oldie but goodie", "long ago") |
| 199 | + subject { described_class.new(file_set, "relational", "the user") } |
| 200 | + |
| 201 | + before { |
| 202 | + allow(subject).to receive(:related_file).and_return repo |
| 203 | + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "user=the user", "file_set.id=a classic file", "relation=relational", |
| 204 | + "revision_id=publish or perish" ]) |
| 205 | + allow(file_set).to receive(:provenance_update_version).with(current_user: "the user", event_note: "revert_to", new_create_date: "long ago", |
| 206 | + new_revision_id: "oldie but goodie", prior_create_date: "long ago", prior_revision_id: "oldie but goodie", |
| 207 | + revision_id: "publish or perish") |
| 208 | + allow(Hyrax::VersioningService).to receive(:create).with(repo, "the user") |
| 209 | + allow(CharacterizeJob).to receive(:perform_later).with(file_set, "repo", current_user: "the user") |
| 210 | + } |
| 211 | + it "returns blank" do |
| 212 | + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with(["here", "called from", "user=the user", "file_set.id=a classic file", "relation=relational", |
| 213 | + "revision_id=publish or perish" ]) |
| 214 | + expect(repo).to receive(:restore_version).with("publish or perish") |
| 215 | + expect(file_set).to receive(:provenance_update_version).with(current_user: "the user", event_note: "revert_to", new_create_date: "long ago", |
| 216 | + new_revision_id: "oldie but goodie", prior_create_date: "long ago", prior_revision_id: "oldie but goodie", |
| 217 | + revision_id: "publish or perish") |
| 218 | + expect(Hyrax::VersioningService).to receive(:create).with(repo, "the user") |
| 219 | + expect(CharacterizeJob).to receive(:perform_later).with(file_set, {:id=>"repo"}, current_user: "the user") |
| 220 | + |
| 221 | + expect(subject.revert_to("publish or perish")).to be_blank |
| 222 | + end |
| 223 | + end |
| 224 | + end |
| 225 | + |
| 226 | + |
| 227 | + describe "#==" do |
| 228 | + context "when argument is not a self.class" do |
| 229 | + subject { described_class.new(double, "relativity", double) } |
| 230 | + |
| 231 | + it "returns false" do |
| 232 | + expect(subject.== "plot twist").to eq false |
| 233 | + end |
| 234 | + end |
| 235 | + |
| 236 | + context "when argument is a self.class" do |
| 237 | + context "when values are equal" do |
| 238 | + subject { described_class.new(OpenStruct.new(id: "111"), "relativity", "just a guy") } |
| 239 | + |
| 240 | + it "returns true" do |
| 241 | + expect(subject.== Hyrax::Actors::FileActor.new(OpenStruct.new(id: "111"), "relativity", "just a guy")).to eq true |
| 242 | + end |
| 243 | + end |
| 244 | + |
| 245 | + context "when values are not equal" do |
| 246 | + subject { described_class.new(OpenStruct.new(id: "22"), "distant", "someone else") } |
| 247 | + |
| 248 | + it "returns false" do |
| 249 | + expect(subject.== Hyrax::Actors::FileActor.new(OpenStruct.new(id: "111"), "relativity", "just a guy")).to eq false |
| 250 | + end |
| 251 | + end |
| 252 | + end |
| 253 | + |
| 254 | + end |
| 255 | + |
| 256 | + |
| 257 | + describe "#related_file" do |
| 258 | + |
| 259 | + context "sends file successfully" do |
| 260 | + subject { described_class.new(MockPublicSend.new(true), "relational", "user dude" ) } |
| 261 | + |
| 262 | + it "returns true" do |
| 263 | + expect(subject.send(:related_file)).to eq true |
| 264 | + end |
| 265 | + end |
| 266 | + |
| 267 | + context "fails to send file" do |
| 268 | + subject { described_class.new(MockPublicSend.new(false), "relational", "user dude" ) } |
| 269 | + |
| 270 | + it "raises error" do |
| 271 | + |
| 272 | + begin |
| 273 | + subject.send(:related_file) |
| 274 | + rescue RuntimeError => e |
| 275 | + expect(e.message).to eq "No relational returned for FileSet FileSet ID" |
| 276 | + end |
| 277 | + |
| 278 | + end |
| 279 | + end |
| 280 | + end |
| 281 | + |
| 282 | +end |
0 commit comments