forked from typesense/typesense-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollections_spec.rb
More file actions
184 lines (164 loc) · 5.14 KB
/
collections_spec.rb
File metadata and controls
184 lines (164 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# frozen_string_literal: true
require_relative '../spec_helper'
require_relative 'shared_configuration_context'
describe Typesense::Collections do
subject(:collections) { typesense.collections }
include_context 'with Typesense configuration'
let(:company_schema) do
{
'name' => 'companies',
'num_documents' => 0,
'fields' => [
{
'name' => 'company_name',
'type' => 'string',
'facet' => false
},
{
'name' => 'num_employees',
'type' => 'int32',
'facet' => false
},
{
'name' => 'country',
'type' => 'string',
'facet' => true
}
],
'token_ranking_field' => 'num_employees'
}
end
describe '#create' do
it 'creates a collection and returns it' do
# since num_documents is a read-only attribute
schema_for_creation = company_schema.reject { |key, _| key == 'num_documents' }
stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections', typesense.configuration.nodes[0]))
.with(body: schema_for_creation,
headers: {
'X-Typesense-Api-Key' => typesense.configuration.api_key,
'Content-Type' => 'application/json'
})
.to_return(status: 200, body: JSON.dump(company_schema), headers: { 'Content-Type': 'application/json' })
result = collections.create(schema_for_creation)
expect(result).to eq(company_schema)
end
context 'with integration', :integration do
let(:integration_schema) do
{
'name' => 'integration_companies',
'fields' => [
{
'name' => 'company_name',
'type' => 'string',
'facet' => false
},
{
'name' => 'num_employees',
'type' => 'int32',
'facet' => false
},
{
'name' => 'country',
'type' => 'string',
'facet' => true
}
],
'default_sorting_field' => 'num_employees'
}
end
let(:integration_client) do
Typesense::Client.new(
nodes: [{
host: 'localhost',
port: '8108',
protocol: 'http'
}],
api_key: 'xyz',
connection_timeout_seconds: 10
)
end
let(:expected_fields) do
[
{
'name' => 'company_name',
'type' => 'string',
'facet' => false,
'index' => true,
'infix' => false,
'locale' => '',
'optional' => false,
'sort' => false,
'stem' => false,
'store' => true
},
{
'name' => 'num_employees',
'type' => 'int32',
'facet' => false,
'index' => true,
'infix' => false,
'locale' => '',
'optional' => false,
'sort' => true,
'stem' => false,
'store' => true
},
{
'name' => 'country',
'type' => 'string',
'facet' => true,
'index' => true,
'infix' => false,
'locale' => '',
'optional' => false,
'sort' => false,
'stem' => false,
'store' => true
}
]
end
before do
WebMock.disable!
begin
integration_client.collections['integration_companies'].delete
rescue Typesense::Error::ObjectNotFound
# Collection doesn't exist, which is fine
end
end
after do
begin
integration_client.collections['integration_companies'].delete
rescue Typesense::Error::ObjectNotFound
# Collection doesn't exist, which is fine
end
WebMock.enable!
end
it 'creates a collection on a real Typesense server' do
result = integration_client.collections.create(integration_schema)
expect(result['name']).to eq('integration_companies')
expect(result['fields']).to eq(expected_fields)
expect(result['default_sorting_field']).to eq(integration_schema['default_sorting_field'])
expect(result['num_documents']).to eq(0)
end
end
end
describe '#retrieve' do
it 'returns all collections' do
stub_request(:get, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections', typesense.configuration.nodes[0]))
.with(headers: {
'X-Typesense-Api-Key' => typesense.configuration.api_key,
'Content-Type' => 'application/json'
})
.to_return(status: 200, body: JSON.dump([company_schema]), headers: { 'Content-Type': 'application/json' })
result = collections.retrieve
expect(result).to eq([company_schema])
end
end
describe '#[]' do
it 'returns a collection object' do
result = collections['companies']
expect(result).to be_a(Typesense::Collection)
expect(result.instance_variable_get(:@name)).to eq('companies')
end
end
end