-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathror_reference_store_spec.rb
More file actions
110 lines (87 loc) · 4.45 KB
/
ror_reference_store_spec.rb
File metadata and controls
110 lines (87 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true
require "rails_helper"
RSpec.describe RorReferenceStore, type: :service do
let(:sample_json) { JSON.generate({ "100010552" => "https://ror.org/04ttjf776" }) }
let(:s3_client) { instance_double(Aws::S3::Client) }
let(:s3_response) { instance_double(Aws::S3::Types::GetObjectOutput, body: StringIO.new(sample_json)) }
before do
# Clear all ROR reference cache keys before each test (per-key + populated sentinel)
RorReferenceStore::MAPPING_FILES.each_key do |mapping|
Rails.cache.delete("ror_ref/#{mapping}/#{RorReferenceStore::POPULATED_KEY_SUFFIX}")
# Clear the value key used in funder_to_ror tests so cold-cache behavior is consistent
Rails.cache.delete("ror_ref/#{mapping}/100010552")
end
end
describe "per-key cache write and read" do
it "stores and retrieves data via per-key cache entries after explicit refresh" do
allow(Aws::S3::Client).to receive(:new).and_return(s3_client)
allow(s3_client).to receive(:get_object).and_return(s3_response)
stub_const("ENV", ENV.to_h.merge("ROR_ANALYSIS_S3_BUCKET" => "test-bucket"))
described_class.refresh_all!
result = described_class.funder_to_ror("100010552")
expect(result).to eq("https://ror.org/04ttjf776")
expect(Rails.cache.read("ror_ref/funder_to_ror/populated")).to eq(true)
end
it "returns cached data on subsequent calls without hitting S3" do
allow(Aws::S3::Client).to receive(:new).and_return(s3_client)
allow(s3_client).to receive(:get_object).and_return(s3_response)
stub_const("ENV", ENV.to_h.merge("ROR_ANALYSIS_S3_BUCKET" => "test-bucket"))
described_class.refresh_all!
expect(s3_client).to have_received(:get_object).exactly(3).times
result = described_class.funder_to_ror("100010552")
second_result = described_class.funder_to_ror("100010552")
expect(result).to eq("https://ror.org/04ttjf776")
expect(second_result).to eq("https://ror.org/04ttjf776")
expect(s3_client).to have_received(:get_object).exactly(3).times
end
end
describe "cold cache lookup" do
it "returns nil and does not fetch from S3 when populated key is missing" do
allow(Aws::S3::Client).to receive(:new).and_return(s3_client)
allow(s3_client).to receive(:get_object).and_return(s3_response)
stub_const("ENV", ENV.to_h.merge("ROR_ANALYSIS_S3_BUCKET" => "test-bucket"))
Rails.cache.delete("ror_ref/funder_to_ror/populated")
Rails.cache.delete("ror_ref/funder_to_ror/100010552")
result = described_class.funder_to_ror("100010552")
expect(result).to be_nil
expect(s3_client).not_to have_received(:get_object)
end
end
describe "refresh_all!" do
it "downloads all three mappings from S3" do
allow(Aws::S3::Client).to receive(:new).and_return(s3_client)
allow(s3_client).to receive(:get_object).and_return(s3_response)
stub_const("ENV", ENV.to_h.merge("ROR_ANALYSIS_S3_BUCKET" => "test-bucket"))
described_class.refresh_all!
expect(s3_client).to have_received(:get_object).exactly(3).times
end
it "overwrites existing cache entries" do
allow(Aws::S3::Client).to receive(:new).and_return(s3_client)
allow(s3_client).to receive(:get_object).and_return(s3_response)
stub_const("ENV", ENV.to_h.merge("ROR_ANALYSIS_S3_BUCKET" => "test-bucket"))
described_class.funder_to_ror("100010552")
described_class.refresh_all!
result = described_class.funder_to_ror("100010552")
expect(result).to eq("https://ror.org/04ttjf776")
end
end
describe "S3 failure handling" do
it "handles S3 errors during refresh and lookup remains nil on cold cache" do
allow(Aws::S3::Client).to receive(:new).and_return(s3_client)
allow(s3_client).to receive(:get_object).and_raise(
Aws::S3::Errors::ServiceError.new(nil, "Access Denied")
)
stub_const("ENV", ENV.to_h.merge("ROR_ANALYSIS_S3_BUCKET" => "test-bucket"))
expect { described_class.refresh_all! }.not_to raise_error
expect(described_class.funder_to_ror("100010552")).to be_nil
end
end
describe "cache key helpers" do
it "uses expected value cache key format" do
expect(described_class.send(:value_cache_key, :funder_to_ror, "100010552")).to eq("ror_ref/funder_to_ror/100010552")
end
it "uses expected populated cache key format" do
expect(described_class.send(:populated_cache_key, :funder_to_ror)).to eq("ror_ref/funder_to_ror/populated")
end
end
end