Skip to content

Commit

Permalink
add failure case to write method rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
praneeth-ais committed May 6, 2024
1 parent c732f20 commit 7d818cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7d818cd

Please sign in to comment.