Skip to content

Commit 8569ed2

Browse files
Add event_index_handler specs
1 parent 9216886 commit 8569ed2

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

app/models/concerns/event_index_handler.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
module EventIndexHandler
44
extend ActiveSupport::Concern
55

6-
# Used to prepare the event record for indexing
6+
# Used to prepare the event record for indexing.
7+
# Invoked implicitely when document indexing occurs.
78
def as_indexed_json(_options = {})
89
{
910
"uuid" => uuid,

spec/models/concerns/event_index_handler_spec.rb

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,75 @@
33
require "rails_helper"
44

55
RSpec.describe(EventIndexHandler, type: :concern) do
6-
it "must do the tings :)" do
7-
one = 1
8-
expect(one).to(eq(1))
6+
include ActiveSupport::Testing::TimeHelpers
7+
8+
describe ".subj_cache_key" do
9+
it "when subj_hash has 'dateModified' returns expected result" do
10+
event = Event.new(
11+
subj_id: "00.0000/zenodo.00000000",
12+
subj: {
13+
"@id" => "00.0000/zenodo.00000000",
14+
"@type" => "Organization",
15+
"name" => "DataCite",
16+
"dateModified" => "2025-01-01 00:00:00",
17+
"location" => {
18+
"type" => "postalAddress",
19+
"addressCountry" => "Germany",
20+
},
21+
}.to_json,
22+
)
23+
24+
expected = "objects/00.0000/zenodo.00000000-2025-01-01 00:00:00"
25+
26+
expect(event.subj_cache_key).to(eq(expected))
27+
end
28+
29+
it "when subj_hash does not have a 'dateModified' returns expected result" do
30+
travel_to(Time.zone.parse("2025-01-01T00:00:00Z")) do
31+
event = Event.new(
32+
subj_id: "00.0000/zenodo.00000000",
33+
subj: { "field": "value" }.to_json,
34+
)
35+
36+
expected = "objects/00.0000/zenodo.00000000-2025-01-01T00:00:00Z"
37+
38+
expect(event.subj_cache_key).to(eq(expected))
39+
end
40+
end
41+
end
42+
43+
describe ".obj_cache_key" do
44+
it "when obj_hash has 'dateModified' returns expected result" do
45+
event = Event.new(
46+
obj_id: "00.0000/zenodo.00000000",
47+
obj: {
48+
"@id" => "00.0000/zenodo.00000000",
49+
"@type" => "Organization",
50+
"name" => "DataCite",
51+
"dateModified" => "2025-01-01 00:00:00",
52+
"location" => {
53+
"type" => "postalAddress",
54+
"addressCountry" => "Germany",
55+
},
56+
}.to_json,
57+
)
58+
59+
expected = "objects/00.0000/zenodo.00000000-2025-01-01 00:00:00"
60+
61+
expect(event.obj_cache_key).to(eq(expected))
62+
end
63+
64+
it "when obj_hash does not have a 'dateModified' returns expected result" do
65+
travel_to(Time.zone.parse("2025-01-01T00:00:00Z")) do
66+
event = Event.new(
67+
obj_id: "00.0000/zenodo.00000000",
68+
obj: { "field": "value" }.to_json,
69+
)
70+
71+
expected = "objects/00.0000/zenodo.00000000-2025-01-01T00:00:00Z"
72+
73+
expect(event.obj_cache_key).to(eq(expected))
74+
end
75+
end
976
end
1077
end

spec/models/event_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,28 @@
286286

287287
expect(Rails.logger).to(have_received(:error).with("JSON parsing failed for event.subj: error"))
288288
end
289+
290+
it "returns the expected hash when subj is valid JSON" do
291+
event.subj = {
292+
"@id": "https://doi.org/00.0000/zenodo.00000000",
293+
"@type": "Organization",
294+
"name": "DataCite",
295+
"location": {
296+
"type": "postalAddress",
297+
"addressCountry": "France",
298+
},
299+
}.to_json
300+
301+
expect(event.subj_hash).to(eql({
302+
"@id" => "https://doi.org/00.0000/zenodo.00000000",
303+
"@type" => "Organization",
304+
"name" => "DataCite",
305+
"location" => {
306+
"type" => "postalAddress",
307+
"addressCountry" => "France",
308+
},
309+
}))
310+
end
289311
end
290312

291313
describe "#obj_hash" do
@@ -310,6 +332,28 @@
310332

311333
expect(Rails.logger).to(have_received(:error).with("JSON parsing failed for event.obj: error"))
312334
end
335+
336+
it "returns the expected hash when obj is valid JSON" do
337+
event.obj = {
338+
"@id": "https://doi.org/00.0000/zenodo.00000000",
339+
"@type": "Organization",
340+
"name": "DataCite",
341+
"location": {
342+
"type": "postalAddress",
343+
"addressCountry": "France",
344+
},
345+
}.to_json
346+
347+
expect(event.obj_hash).to(eql({
348+
"@id" => "https://doi.org/00.0000/zenodo.00000000",
349+
"@type" => "Organization",
350+
"name" => "DataCite",
351+
"location" => {
352+
"type" => "postalAddress",
353+
"addressCountry" => "France",
354+
},
355+
}))
356+
end
313357
end
314358
end
315359

0 commit comments

Comments
 (0)