From 7d818cd9d74aae49180ff65e916a8e1f33771f21 Mon Sep 17 00:00:00 2001 From: Praneeth AIS Date: Sun, 5 May 2024 22:10:28 -0400 Subject: [PATCH] add failure case to write method rspec --- .../integrations/destination/zendesk/client.rb | 18 ++++++++++++------ .../destination/zendesk/client_spec.rb | 13 +++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/integrations/lib/multiwoven/integrations/destination/zendesk/client.rb b/integrations/lib/multiwoven/integrations/destination/zendesk/client.rb index 1fafc88c..88a4bf73 100644 --- a/integrations/lib/multiwoven/integrations/destination/zendesk/client.rb +++ b/integrations/lib/multiwoven/integrations/destination/zendesk/client.rb @@ -52,8 +52,8 @@ def authenticate_client end def process_records(records, stream) - success_count = 0 - failure_count = 0 + write_success = 0 + write_failure = 0 records.each do |record| zendesk_data = prepare_record_data(record, stream.name) @@ -66,13 +66,13 @@ def process_records(records, stream) existing_record.update!(zendesk_data) end - success_count += 1 - rescue ZendeskAPI::Error => e + write_success += 1 + rescue StandardError => e handle_exception("ZENDESK:WRITE_RECORD:EXCEPTION", "error", e) - failure_count += 1 + write_failure += 1 end - { success: success_count, failures: failure_count } + tracking_message(write_success, write_failure) end def pluralize_stream_name(name) @@ -113,6 +113,12 @@ def failure_status(error) def load_catalog read_json(CATALOG_SPEC_PATH) end + + def tracking_message(success, failure) + Multiwoven::Integrations::Protocol::TrackingMessage.new( + success: success, failed: failure + ).to_multiwoven_message + end end end end diff --git a/integrations/spec/multiwoven/integrations/destination/zendesk/client_spec.rb b/integrations/spec/multiwoven/integrations/destination/zendesk/client_spec.rb index 3dbb87b4..26cb1d78 100644 --- a/integrations/spec/multiwoven/integrations/destination/zendesk/client_spec.rb +++ b/integrations/spec/multiwoven/integrations/destination/zendesk/client_spec.rb @@ -158,20 +158,21 @@ allow(zendesk_client_double).to receive(:method_missing).with(:tickets).and_return(resource_double) allow(resource_double).to receive(:create!).and_return(true) result = client.write(sync_config, records) - expect(result[:success]).to eq(records.size) - expect(result[:failures]).to eq(0) + expect(result.tracking.success).to eq(records.size) + expect(result.tracking.failed).to eq(0) end end + context "when the write operation fails" do it "returns a failure status message" do expect(ZendeskAPI::Client).to receive(:new).and_return(zendesk_client_double) allow(zendesk_client_double).to receive(:method_missing).with(:tickets).and_return(resource_double) - allow(resource_double).to receive(:create!).and_raise(StandardError.new("Failed to create ticket")) + allow(resource_double).to receive(:create!).and_raise(StandardError.new("connection failed")) result = client.write(sync_config, records) - expect(result.type).to eq("connection_status") - expect(result.connection_status.status).to eq("failed") - expect(result.connection_status.message).to include("Failed to create ticket") + expect(result.type).to eq("tracking") # Adjust based on your response handling + expect(result.tracking.success).to eq(0) + expect(result.tracking.failed).to eq(records.size) end end end