Skip to content

Commit

Permalink
Add ServingConfig model
Browse files Browse the repository at this point in the history
This adds a `ServingConfig` model and a representation of the existing
default serving configuration, allowing us to remove the more specific
environment variable/app configuration.
  • Loading branch information
csutter committed Feb 7, 2025
1 parent 1ef8275 commit 32e0124
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
21 changes: 21 additions & 0 deletions app/models/serving_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Represents a serving config on Discovery Engine.
#
# A serving config is an endpoint on an engine that can be used for
# querying. Each serving config can have different configuration (in particular, different sets of
# active controls), which allows us to test out new configuration changes outside of the default
# serving config.
#
# see https://cloud.google.com/ruby/docs/reference/google-cloud-discovery_engine-v1beta/latest/Google-Cloud-DiscoveryEngine-V1beta-ServingConfig
ServingConfig = Data.define(:remote_resource_id) do
include DiscoveryEngineNameable

# The default serving config automatically available on an engine
def self.default
new("default_search")
end

def parent
# We only use a single engine in our architecture, so we can hardcode it here.
Engine.default
end
end
2 changes: 1 addition & 1 deletion app/services/discovery_engine/query/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def query
end

def serving_config
Rails.configuration.discovery_engine_serving_config
ServingConfig.default.name
end

def page_size
Expand Down
1 change: 0 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Application < Rails::Application

# Google Discovery Engine configuration
config.discovery_engine_default_collection_name = ENV.fetch("DISCOVERY_ENGINE_DEFAULT_COLLECTION_NAME")
config.discovery_engine_serving_config = ENV.fetch("DISCOVERY_ENGINE_SERVING_CONFIG")
config.google_cloud_project_id = ENV.fetch("GOOGLE_CLOUD_PROJECT_ID")

# Document sync configuration
Expand Down
15 changes: 15 additions & 0 deletions spec/models/serving_config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RSpec.describe ServingConfig do
subject(:serving_config) { described_class.new("my-serving-config") }

describe ".default" do
it "returns the default serving config" do
expect(described_class.default).to eq(described_class.new("default_search"))
end
end

describe "#name" do
it "returns the fully qualified name of the serving config" do
expect(subject.name).to eq("[collection]/engines/govuk/servingConfigs/my-serving-config")

Check failure on line 12 in spec/models/serving_config_spec.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

RSpec/NamedSubject: Name your test subject if you need to reference it explicitly. (https://rspec.rubystyle.guide/#use-subject, https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/NamedSubject)
end
end
end
6 changes: 2 additions & 4 deletions spec/services/discovery_engine/query/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
end

before do
allow(Rails.configuration).to receive(:discovery_engine_serving_config)
.and_return("serving-config-path")
allow(DiscoveryEngine::Query::Filters).to receive(:new).and_return(filters)
end

Expand Down Expand Up @@ -52,7 +50,7 @@

it "calls the client with the expected parameters" do
expect(client).to have_received(:search).with(
serving_config: "serving-config-path",
serving_config: ServingConfig.default.name,
query: "garden centres",
offset: 0,
page_size: 10,
Expand All @@ -76,7 +74,7 @@

it "calls the client with the expected parameters" do
expect(client).to have_received(:search).with(
serving_config: "serving-config-path",
serving_config: ServingConfig.default.name,
query: "garden centres",
offset: 11,
page_size: 22,
Expand Down

0 comments on commit 32e0124

Please sign in to comment.