|
| 1 | +RSpec.describe DiscoveryEngine::Autocomplete::UpdateDenylist do |
| 2 | + subject(:update_denylist) { described_class.new(client:) } |
| 3 | + |
| 4 | + let(:client) do |
| 5 | + instance_double( |
| 6 | + ::Google::Cloud::DiscoveryEngine::V1::CompletionService::Client, |
| 7 | + purge_suggestion_deny_list_entries: purge_operation, |
| 8 | + import_suggestion_deny_list_entries: import_operation, |
| 9 | + ) |
| 10 | + end |
| 11 | + let(:purge_operation) { instance_double(Gapic::Operation, wait_until_done!: nil, error?: false, results: purge_results) } |
| 12 | + let(:purge_results) { double("results") } |
| 13 | + let(:import_operation) { instance_double(Gapic::Operation, wait_until_done!: nil, error?: false, results: import_results) } |
| 14 | + let(:import_results) { double("results", failed_entries_count: 0, imported_entries_count: 100) } |
| 15 | + |
| 16 | + before do |
| 17 | + allow(Rails.configuration).to receive_messages( |
| 18 | + discovery_engine_datastore: "data/store", |
| 19 | + google_cloud_project_id: "my-fancy-project", |
| 20 | + ) |
| 21 | + end |
| 22 | + |
| 23 | + describe "#call" do |
| 24 | + it "purges existing suggestion deny list entries" do |
| 25 | + update_denylist.call |
| 26 | + |
| 27 | + expect(client).to have_received(:purge_suggestion_deny_list_entries) |
| 28 | + .with(parent: "data/store") |
| 29 | + expect(purge_operation).to have_received(:wait_until_done!) |
| 30 | + end |
| 31 | + |
| 32 | + it "imports new suggestion deny list entries from GCS" do |
| 33 | + update_denylist.call |
| 34 | + |
| 35 | + expect(client).to have_received(:import_suggestion_deny_list_entries).with( |
| 36 | + gcs_source: { |
| 37 | + data_schema: "suggestion_deny_list", |
| 38 | + input_uris: ["gs://my-fancy-project_vais_artifacts/denylist.jsonl"], |
| 39 | + }, |
| 40 | + parent: "data/store", |
| 41 | + ) |
| 42 | + expect(import_operation).to have_received(:wait_until_done!) |
| 43 | + end |
| 44 | + |
| 45 | + context "when an error occurs during purge" do |
| 46 | + let(:purge_results) { double("results", message: "Purge failed") } |
| 47 | + |
| 48 | + before do |
| 49 | + allow(purge_operation).to receive(:error?).and_return(true) |
| 50 | + end |
| 51 | + |
| 52 | + it "raises an error" do |
| 53 | + expect { update_denylist.call }.to raise_error("Purge failed") |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context "when an error occurs during import" do |
| 58 | + let(:import_results) { double("results", message: "Import failed") } |
| 59 | + |
| 60 | + before do |
| 61 | + allow(import_operation).to receive(:error?).and_return(true) |
| 62 | + end |
| 63 | + |
| 64 | + it "raises an error" do |
| 65 | + expect { update_denylist.call }.to raise_error("Import failed") |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "when there are failed entries during import" do |
| 70 | + let(:import_results) { double("results", failed_entries_count: 2, imported_entries_count: 0) } |
| 71 | + |
| 72 | + it "raises an error" do |
| 73 | + expect { update_denylist.call }.to raise_error("Failed to import 2 entries to autocomplete denylist") |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments