Skip to content

Commit 85e829c

Browse files
committed
Added tests
1 parent 9280a58 commit 85e829c

4 files changed

Lines changed: 117 additions & 6 deletions

File tree

lib/typesense/overrides.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def initialize(collection_name, api_call)
1010
@overrides = {}
1111
end
1212

13-
def create(params)
14-
@api_call.put(endpoint_path, params)
13+
def upsert(override_id, params)
14+
@api_call.put(endpoint_path(override_id), params)
1515
end
1616

1717
def retrieve

spec/typesense/overrides_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@
2020
}
2121
end
2222

23-
describe '#create' do
23+
describe '#upsert' do
2424
it 'creates an override rule and returns it' do
25-
stub_request(:put, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/overrides', typesense.configuration.nodes[0]))
25+
stub_request(:put, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/overrides/lex-exact', typesense.configuration.nodes[0]))
2626
.with(body: override,
2727
headers: {
2828
'X-Typesense-Api-Key' => typesense.configuration.api_key,
2929
'Content-Type' => 'application/json'
3030
})
3131
.to_return(status: 201, body: JSON.dump(override), headers: { 'Content-Type': 'application/json' })
3232

33-
result = companies_overrides.create(override)
33+
result = companies_overrides.upsert(override['id'], override)
3434

3535
expect(result).to eq(override)
3636
end
3737
end
3838

3939
describe '#retrieve' do
40-
it 'creates an override rule and returns it' do
40+
it 'retrieves all overrides' do
4141
stub_request(:get, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/overrides', typesense.configuration.nodes[0]))
4242
.with(headers: {
4343
'X-Typesense-Api-Key' => typesense.configuration.api_key,

spec/typesense/synonym_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require_relative 'shared_configuration_context'
5+
6+
describe Typesense::Synonym do
7+
subject(:synonym) { typesense.collections['companies'].synonyms['synonym-set-1'] }
8+
9+
include_context 'with Typesense configuration'
10+
11+
let(:synonym_data) do
12+
{
13+
'id' => 'synonym-set-1',
14+
'synonyms' => %w[
15+
lex
16+
luthor
17+
businessman
18+
]
19+
}
20+
end
21+
22+
describe '#retrieve' do
23+
it 'returns the specified synonym' do
24+
stub_request(:get, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/synonyms/synonym-set-1', typesense.configuration.nodes[0]))
25+
.with(headers: {
26+
'X-Typesense-Api-Key' => typesense.configuration.api_key,
27+
'Content-Type': 'application/json'
28+
})
29+
.to_return(status: 200, body: JSON.dump(synonym_data), headers: { 'Content-Type': 'application/json' })
30+
31+
result = synonym.retrieve
32+
33+
expect(result).to eq(synonym_data)
34+
end
35+
end
36+
37+
describe '#delete' do
38+
it 'deletes the specified synonym' do
39+
stub_request(:delete, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/synonyms/synonym-set-1', typesense.configuration.nodes[0]))
40+
.with(headers: {
41+
'X-Typesense-Api-Key' => typesense.configuration.api_key
42+
})
43+
.to_return(status: 200, body: JSON.dump('id' => 'synonym-set-1'), headers: { 'Content-Type': 'application/json' })
44+
45+
result = synonym.delete
46+
47+
expect(result).to eq('id' => 'synonym-set-1')
48+
end
49+
end
50+
end

spec/typesense/synonyms_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require_relative 'shared_configuration_context'
5+
6+
describe Typesense::Synonyms do
7+
subject(:companies_synonyms) { typesense.collections['companies'].synonyms }
8+
9+
include_context 'with Typesense configuration'
10+
11+
let(:synonym) do
12+
{
13+
'id' => 'synonym-set-1',
14+
'synonyms' => %w[
15+
lex
16+
luthor
17+
businessman
18+
]
19+
}
20+
end
21+
22+
describe '#upsert' do
23+
it 'creates an synonym rule and returns it' do
24+
stub_request(:put, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/synonyms/synonym-set-1', typesense.configuration.nodes[0]))
25+
.with(body: synonym,
26+
headers: {
27+
'X-Typesense-Api-Key' => typesense.configuration.api_key,
28+
'Content-Type' => 'application/json'
29+
})
30+
.to_return(status: 201, body: JSON.dump(synonym), headers: { 'Content-Type': 'application/json' })
31+
32+
result = companies_synonyms.upsert(synonym['id'], synonym)
33+
34+
expect(result).to eq(synonym)
35+
end
36+
end
37+
38+
describe '#retrieve' do
39+
it 'retrieves all synonyms' do
40+
stub_request(:get, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/synonyms', typesense.configuration.nodes[0]))
41+
.with(headers: {
42+
'X-Typesense-Api-Key' => typesense.configuration.api_key,
43+
'Content-Type' => 'application/json'
44+
})
45+
.to_return(status: 201, body: JSON.dump([synonym]), headers: { 'Content-Type': 'application/json' })
46+
47+
result = companies_synonyms.retrieve
48+
49+
expect(result).to eq([synonym])
50+
end
51+
end
52+
53+
describe '#[]' do
54+
it 'creates an synonym object and returns it' do
55+
result = companies_synonyms['synonym-set-1']
56+
57+
expect(result).to be_a_kind_of(Typesense::Synonym)
58+
expect(result.instance_variable_get(:@synonym_id)).to eq('synonym-set-1')
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)