Skip to content

Commit

Permalink
Merge pull request #732 from learningtapestry/688-ignore-bnode-ids
Browse files Browse the repository at this point in the history
(#688) Ignore bnode IDs when fetching single envelopes
  • Loading branch information
excelsior authored Oct 17, 2024
2 parents 24acdd5 + 1f5c058 commit f8dcd32
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'https://rubygems.org'

# API
gem 'api-pagination', '~> 6.0'
gem 'aws-sdk-s3', '~> 1.166'
gem 'aws-sdk-s3', '~> 1.167'
gem 'bundler', '= 2.5.16'
gem 'fiddle', '~> 1.1'
gem 'grape', '~> 2.2'
Expand Down Expand Up @@ -77,7 +77,7 @@ group :development do
gem 'grape-raketasks'
# Code quality tools
gem 'overcommit', '~> 0.64'
gem 'rubocop', '~> 1.66', require: false
gem 'rubocop', '~> 1.67', require: false
gem 'rubocop-faker', '~> 1.1', require: false
gem 'rubocop-performance', '~> 1.22'
gem 'rubocop-rspec', '~> 3.1', require: false
Expand Down
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ GEM
ast (2.4.2)
attribute_normalizer (1.2.0)
aws-eventstream (1.3.0)
aws-partitions (1.981.0)
aws-partitions (1.991.0)
aws-sdk-core (3.209.1)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.651.0)
Expand All @@ -68,7 +68,7 @@ GEM
aws-sdk-kms (1.94.0)
aws-sdk-core (~> 3, >= 3.207.0)
aws-sigv4 (~> 1.5)
aws-sdk-s3 (1.166.0)
aws-sdk-s3 (1.168.0)
aws-sdk-core (~> 3, >= 3.207.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
Expand Down Expand Up @@ -289,7 +289,7 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.66.1)
rubocop (1.67.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand Down Expand Up @@ -370,7 +370,7 @@ DEPENDENCIES
airbrake (~> 13.0)
api-pagination (~> 6.0)
attribute_normalizer (~> 1.2)
aws-sdk-s3 (~> 1.166)
aws-sdk-s3 (~> 1.167)
bundler (= 2.5.16)
byebug (~> 11.1)
chronic (~> 0.10.2)
Expand Down Expand Up @@ -416,7 +416,7 @@ DEPENDENCIES
redis (~> 4.8)
rest-client (~> 2.1)
rspec (~> 3.13)
rubocop (~> 1.66)
rubocop (~> 1.67)
rubocop-faker (~> 1.1)
rubocop-performance (~> 1.22)
rubocop-rspec (~> 3.1)
Expand Down
2 changes: 1 addition & 1 deletion app/api/v1/envelopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def find_community_envelopes
.where(envelope_resources: { resource_id: id })
.last

if @envelope.nil?
if id.starts_with?('_:') || @envelope.nil?
error!({ errors: ['Couldn\'t find Envelope'] }, 404)
end
end
Expand Down
1 change: 1 addition & 0 deletions app/services/ctdl_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class CtdlQuery

delegate :columns_hash, to: IndexedEnvelopeResource
delegate :context, to: JsonContext
delegate :to_sql, to: :data_query

def initialize(
query,
Expand Down
4 changes: 2 additions & 2 deletions app/services/run_ctdl_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def self.call(
}

ctids = rows.map { |r| r.fetch('ceterms:ctid') }.compact
result.merge!(sql: query.data_query.to_sql ) if debug
result.merge!(sql: query.to_sql) if debug

if include_description_set_resources || include_description_sets
description_set_data = FetchDescriptionSetData.call(
Expand Down Expand Up @@ -93,7 +93,7 @@ def self.call(
)
end

query_log&.update(query: query.data_query.to_sql)
query_log&.update(query: query.to_sql)
query_log&.complete(result)
OpenStruct.new(result: result, status: 200)
rescue => e
Expand Down
42 changes: 22 additions & 20 deletions spec/api/v1/ctdl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
.at_least(:once).times
.and_return(ctdl_query)

expect(ctdl_query).to receive(:execute).and_raise(error)
expect(ctdl_query).to receive(:rows).and_raise(error)
end

it 'returns the error' do
Expand Down Expand Up @@ -80,7 +80,6 @@

context 'success' do
let(:ctdl_query) { double('ctdl_query') }
let(:count) { rand(100..1_000) }
let(:ctid1) { Faker::Lorem.characters }
let(:ctid2) { Faker::Lorem.characters }
let(:payload1) { JSON(Faker::Json.shallow_json).symbolize_keys }
Expand All @@ -89,6 +88,7 @@
let(:skip) { 0 }
let(:sql) { Faker::Lorem.paragraph }
let(:take) { 10 }
let(:total_count) { rand(100..1_000) }

before do
allow(CtdlQuery).to receive(:new) do |*args|
Expand All @@ -104,24 +104,26 @@

context 'without results metadata' do
before do
allow(ctdl_query).to receive(:execute)
allow(ctdl_query).to receive(:rows)
.and_return([
{
'@id' => Faker::Internet.url,
'ceterms:ctid' => ctid1,
'payload' => payload1.to_json,
'total_count' => count
'payload' => payload1.to_json
},
{
'@id' => Faker::Internet.url,
'ceterms:ctid' => ctid2,
'payload' => payload2.to_json,
'total_count' => count
'payload' => payload2.to_json
},
{
'@id' => Faker::Internet.url,
'ceterms:ctid' => nil,
'payload' => payload3.to_json,
'total_count' => count
'payload' => payload3.to_json
}
])

allow(ctdl_query).to receive(:total_count).and_return(total_count)
end

context 'default params' do
Expand All @@ -137,7 +139,7 @@

expect_status(:ok)
expect_json('data', [payload1, payload2, payload3])
expect_json('total', count)
expect_json('total', total_count)
expect_json('sql', nil)

query_log = QueryLog.last
Expand Down Expand Up @@ -175,7 +177,7 @@

expect_status(:ok)
expect_json('data', [payload1, payload2, payload3])
expect_json('total', count)
expect_json('total', total_count)
expect_json('sql', sql)
end
end
Expand Down Expand Up @@ -227,7 +229,7 @@
expect_json('data', [payload1, payload2, payload3])
expect_json('description_set_resources', nil)
expect_json('description_sets', description_sets)
expect_json('total', count)
expect_json('total', total_count)
expect_json('sql', nil)
end
end
Expand Down Expand Up @@ -257,7 +259,7 @@
expect_json('data', [payload1, payload2, payload3])
expect_json('description_set_resources', description_set_resources)
expect_json('description_sets', description_sets)
expect_json('total', count)
expect_json('total', total_count)
expect_json('sql', sql)
end
end
Expand Down Expand Up @@ -287,7 +289,7 @@
[graph_resource1, graph_resource2]
)
expect_json('description_sets', nil)
expect_json('total', count)
expect_json('total', total_count)
expect_json('sql', nil)
end
end
Expand All @@ -307,7 +309,7 @@
let(:updated_at2) { Faker::Time.backward(days: 30) }

before do
allow(ctdl_query).to receive(:execute)
allow(ctdl_query).to receive(:rows)
.and_return([
{
'@id' => resource_uri1,
Expand All @@ -316,8 +318,7 @@
'search:recordCreated' => created_at1,
'search:recordOwnedBy' => owner1,
'search:recordPublishedBy' => publisher1,
'search:recordUpdated' => updated_at1,
'total_count' => count
'search:recordUpdated' => updated_at1
},
{
'@id' => resource_uri2,
Expand All @@ -326,10 +327,11 @@
'search:recordCreated' => created_at2,
'search:recordOwnedBy' => owner2,
'search:recordPublishedBy' => publisher2,
'search:recordUpdated' => updated_at2,
'total_count' => count
'search:recordUpdated' => updated_at2
}
])

allow(ctdl_query).to receive(:total_count).and_return(total_count)
end

it 'returns query results with metadata' do
Expand All @@ -342,7 +344,7 @@

expect_status(:ok)
expect_json('data', [payload1, payload2])
expect_json('total', count)
expect_json('total', total_count)
expect_json('sql', nil)
expect_json('results_metadata.0.resource_uri', resource_uri1)
expect_json('results_metadata.0.search:recordCreated', created_at1.as_json)
Expand Down
55 changes: 35 additions & 20 deletions spec/api/v1/single_envelope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,8 @@
end

context 'by resource ID' do
let(:resource) { create(:envelope_resource, envelope: subject) }
let(:resource_id) { resource.resource_id }

include_examples 'missing envelope', :get

subject { envelopes.first }

before(:each) do
Expand All @@ -95,27 +92,45 @@
end
end

it { expect_status(:ok) }
context 'bnode ID' do
let(:resource) do
create(
:envelope_resource,
envelope: subject,
resource_id: "_:#{Faker::Internet.uuid}"
)
end

it 'retrieves the desired envelope' do
expect_json(envelope_community: subject.envelope_community.name)
expect_json(envelope_id: subject.envelope_id)
expect_json(resource_format: 'json')
expect_json(resource_encoding: 'jwt')
it { expect_status(:not_found) }
end

it 'displays the appended node headers' do
base_url = "/learning-registry/envelopes/#{subject.envelope_id}"
context 'full ID' do
let(:resource) { create(:envelope_resource, envelope: subject) }

expect_json_keys('node_headers', %i[resource_digest revision_history
created_at updated_at deleted_at])
expect_json('node_headers.revision_history.1', head: true)
expect_json('node_headers.revision_history.1', url: base_url)
expect_json('node_headers.revision_history.0', head: false)
expect_json(
'node_headers.revision_history.0',
url: "#{base_url}/revisions/#{subject.versions.last.id}"
)
include_examples 'missing envelope', :get

it { expect_status(:ok) }

it 'retrieves the desired envelope' do
expect_json(envelope_community: subject.envelope_community.name)
expect_json(envelope_id: subject.envelope_id)
expect_json(resource_format: 'json')
expect_json(resource_encoding: 'jwt')
end

it 'displays the appended node headers' do
base_url = "/learning-registry/envelopes/#{subject.envelope_id}"

expect_json_keys('node_headers', %i[resource_digest revision_history
created_at updated_at deleted_at])
expect_json('node_headers.revision_history.1', head: true)
expect_json('node_headers.revision_history.1', url: base_url)
expect_json('node_headers.revision_history.0', head: false)
expect_json(
'node_headers.revision_history.0',
url: "#{base_url}/revisions/#{subject.versions.last.id}"
)
end
end
end
end
Expand Down

0 comments on commit f8dcd32

Please sign in to comment.