|
| 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