Skip to content

Commit

Permalink
✅ test tags related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
just-the-v committed Oct 25, 2024
1 parent 96586b3 commit 906a79a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/zoho_hub/base_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ def exists?(id)
false
end

def associate_tags(ressource_ids, tag_names = [])
def associate_tags(resource_ids, tag_names = [])
responses = []
ressource_ids.each_slice(100) do |ids|
resource_ids.each_slice(100) do |ids|
parameters = "add_tags?tag_names=#{tag_names.join(',')}&ids=#{ids.join(',')}"
url = File.join(self.class.request_path, 'actions', parameters)
url = File.join(request_path, 'actions', parameters)
responses << build_response(post(url)).data.first
end
responses
Expand Down
47 changes: 47 additions & 0 deletions spec/zoho_hub/base_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@
end
end

describe '.associate_tags' do
before { allow(test_class).to receive(:request_path).and_return('Leads') }

let!(:stub_add_tags_request) do
stub_request(:post, 'https://crmsandbox.zoho.eu/crm/v2/Leads/actions/add_tags?tag_names=tag1,tag2&ids=1,2')
.to_return(status: 200, body: '', headers: { "Content-Type": 'application/json' })
end

it 'associate tags to records' do
test_class.associate_tags([1, 2], %w[tag1 tag2])
expect(stub_add_tags_request).to have_been_requested
end

context 'when the request is too big' do
let!(:stub_add_tags_request_first_batch) do
stub_request(:post, "https://crmsandbox.zoho.eu/crm/v2/Leads/actions/add_tags?tag_names=tag1,tag2&ids=#{(1..100).to_a.join(',')}")
.to_return(status: 200, body: '', headers: { "Content-Type": 'application/json' })
end
let!(:stub_add_tags_request_second_batch) do
stub_request(:post, "https://crmsandbox.zoho.eu/crm/v2/Leads/actions/add_tags?tag_names=tag1,tag2&ids=#{(101..200).to_a.join(',')}")
.to_return(status: 200, body: '', headers: { "Content-Type": 'application/json' })
end

it 'splits the request in batches' do
test_class.associate_tags((1..200).to_a, %w[tag1 tag2])
expect(stub_add_tags_request_first_batch).to have_been_requested
expect(stub_add_tags_request_second_batch).to have_been_requested
end
end
end

describe '#build_response' do
context 'with an empty string and a "false" boolean' do
let(:body) { { data: [{ My_String: '', My_Bool: false }] } }
Expand Down Expand Up @@ -83,6 +114,22 @@
end
end

describe '#associate_tags' do
let(:test_instance) { test_class.new(id: '123456789') }
let(:tag_names) { %w[tag1 tag2] }
let!(:associate_tags_stub) do
stub_request(:post, "https://crmsandbox.zoho.eu/crm/v2/Leads/#{test_instance.id}/actions/add_tags?tag_names=tag1,tag2")
.to_return(status: 200, body: {}.to_json, headers: { "Content-Type": 'application/json' })
end

before { allow(test_class).to receive(:request_path).and_return('Leads') }

it 'associates tags to the record' do
test_instance.associate_tags(tag_names)
expect(associate_tags_stub).to have_been_requested
end
end

describe '#notes' do
let(:test_instance) { test_class.new(id: '123456789') }
let(:data_notes) { { data: [{ Note_Title: 'Title', Note_Content: 'content' }] } }
Expand Down

0 comments on commit 906a79a

Please sign in to comment.