Skip to content

Commit 4cc6c2a

Browse files
Merge #385
385: Improve indexes names in tests r=brunoocasali a=thicolares # Pull Request ## Related issue Fixes #355 ## What does this PR do? - Choose better names to index in tests. - I decided to use static index names instead of randomized ones -- as suggested. - It communicates the intent clearer and mitigates the chances of introducing a flaky test. - Using `books` in all cases seems tedious, but helps keep the same mental context across all tests. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Thiago Colares <[email protected]>
2 parents e4ba373 + 1fe584b commit 4cc6c2a

File tree

3 files changed

+82
-82
lines changed

3 files changed

+82
-82
lines changed

spec/meilisearch/client/indexes_spec.rb

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,100 @@
44
describe '#create_index' do
55
context 'without a primary key' do
66
it 'creates an index' do
7-
task = client.create_index('new_index')
7+
task = client.create_index('books')
88

99
expect(task['type']).to eq('indexCreation')
1010

1111
client.wait_for_task(task['taskUid'])
12-
index = client.fetch_index('new_index')
12+
index = client.fetch_index('books')
1313

1414
expect(index).to be_a(MeiliSearch::Index)
15-
expect(index.uid).to eq('new_index')
15+
expect(index.uid).to eq('books')
1616
expect(index.primary_key).to be_nil
1717
end
1818

1919
it 'creates an index synchronously' do
20-
task = client.create_index!('new_index')
20+
task = client.create_index!('books')
2121

2222
expect(task['type']).to eq('indexCreation')
2323
expect(task['status']).to eq('succeeded')
2424

25-
index = client.fetch_index('new_index')
25+
index = client.fetch_index('books')
2626

2727
expect(index).to be_a(MeiliSearch::Index)
28-
expect(index.uid).to eq('new_index')
28+
expect(index.uid).to eq('books')
2929
expect(index.primary_key).to be_nil
3030
end
3131
end
3232

3333
context 'with a primary key' do
3434
it 'creates an index' do
35-
task = client.create_index('new_index', primaryKey: 'primary_key')
35+
task = client.create_index('books', primaryKey: 'reference_code')
3636

3737
expect(task['type']).to eq('indexCreation')
3838

3939
client.wait_for_task(task['taskUid'])
40-
index = client.fetch_index('new_index')
40+
index = client.fetch_index('books')
4141

4242
expect(index).to be_a(MeiliSearch::Index)
43-
expect(index.uid).to eq('new_index')
44-
expect(index.primary_key).to eq('primary_key')
45-
expect(index.fetch_primary_key).to eq('primary_key')
43+
expect(index.uid).to eq('books')
44+
expect(index.primary_key).to eq('reference_code')
45+
expect(index.fetch_primary_key).to eq('reference_code')
4646
end
4747

4848
it 'creates an index synchronously' do
49-
task = client.create_index!('new_index', primaryKey: 'primary_key')
49+
task = client.create_index!('books', primaryKey: 'reference_code')
5050

5151
expect(task['type']).to eq('indexCreation')
5252
expect(task['status']).to eq('succeeded')
5353

54-
index = client.fetch_index('new_index')
54+
index = client.fetch_index('books')
5555

5656
expect(index).to be_a(MeiliSearch::Index)
57-
expect(index.uid).to eq('new_index')
58-
expect(index.primary_key).to eq('primary_key')
59-
expect(index.fetch_primary_key).to eq('primary_key')
57+
expect(index.uid).to eq('books')
58+
expect(index.primary_key).to eq('reference_code')
59+
expect(index.fetch_primary_key).to eq('reference_code')
6060
end
6161

6262
context 'when primary key option in snake_case' do
6363
it 'creates an index' do
64-
task = client.create_index('new_index', primary_key: 'primary_key')
64+
task = client.create_index('books', primary_key: 'reference_code')
6565
expect(task['type']).to eq('indexCreation')
6666
client.wait_for_task(task['taskUid'])
6767

68-
index = client.fetch_index('new_index')
68+
index = client.fetch_index('books')
6969
expect(index).to be_a(MeiliSearch::Index)
70-
expect(index.uid).to eq('new_index')
71-
expect(index.primary_key).to eq('primary_key')
72-
expect(index.fetch_primary_key).to eq('primary_key')
70+
expect(index.uid).to eq('books')
71+
expect(index.primary_key).to eq('reference_code')
72+
expect(index.fetch_primary_key).to eq('reference_code')
7373
end
7474
end
7575

7676
context 'when uid is provided as an option' do
7777
it 'ignores the uid option' do
7878
task = client.create_index(
79-
'new_index',
80-
primaryKey: 'primary_key',
81-
uid: 'not_primary_key'
79+
'books',
80+
primaryKey: 'reference_code',
81+
uid: 'publications'
8282
)
8383

8484
expect(task['type']).to eq('indexCreation')
8585

8686
client.wait_for_task(task['taskUid'])
87-
index = client.fetch_index('new_index')
87+
index = client.fetch_index('books')
8888

8989
expect(index).to be_a(MeiliSearch::Index)
90-
expect(index.uid).to eq('new_index')
91-
expect(index.primary_key).to eq('primary_key')
92-
expect(index.fetch_primary_key).to eq('primary_key')
90+
expect(index.uid).to eq('books')
91+
expect(index.primary_key).to eq('reference_code')
92+
expect(index.fetch_primary_key).to eq('reference_code')
9393
end
9494
end
9595
end
9696

9797
context 'when an index with a given uid already exists' do
9898
it 'returns a failing task' do
99-
initial_task = client.create_index!('existing_index')
100-
last_task = client.create_index!('existing_index')
99+
initial_task = client.create_index!('books')
100+
last_task = client.create_index!('books')
101101

102102
expect(initial_task['type']).to eq('indexCreation')
103103
expect(last_task['type']).to eq('indexCreation')
@@ -110,42 +110,42 @@
110110
context 'when the uid format is invalid' do
111111
it 'raises an error' do
112112
expect do
113-
client.create_index('two words')
113+
client.create_index('ancient books')
114114
end.to raise_meilisearch_api_error_with(400, 'invalid_index_uid', 'invalid_request')
115115
end
116116
end
117117
end
118118

119119
describe '#indexes' do
120120
it 'returns MeiliSearch::Index objects' do
121-
client.create_index!('index')
121+
client.create_index!('books')
122122

123123
index = client.indexes['results'].first
124124

125125
expect(index).to be_a(MeiliSearch::Index)
126126
end
127127

128128
it 'gets a list of indexes' do
129-
['first_index', 'second_index', 'third_index'].each { |name| client.create_index!(name) }
129+
['books', 'colors', 'artists'].each { |name| client.create_index!(name) }
130130

131131
indexes = client.indexes['results']
132132

133133
expect(indexes).to be_a(Array)
134134
expect(indexes.length).to eq(3)
135135
uids = indexes.map(&:uid)
136-
expect(uids).to contain_exactly('first_index', 'second_index', 'third_index')
136+
expect(uids).to contain_exactly('books', 'colors', 'artists')
137137
end
138138

139139
it 'paginates indexes list with limit and offset' do
140-
['first_index', 'second_index', 'third_index'].each { |name| client.create_index!(name) }
140+
['books', 'colors', 'artists'].each { |name| client.create_index!(name) }
141141

142142
indexes = client.indexes(limit: 1, offset: 2)
143143

144144
expect(indexes['results']).to be_a(Array)
145145
expect(indexes['total']).to eq(3)
146146
expect(indexes['limit']).to eq(1)
147147
expect(indexes['offset']).to eq(2)
148-
expect(indexes['results'].map(&:uid)).to eq(['third_index'])
148+
expect(indexes['results'].map(&:uid)).to eq(['colors'])
149149
end
150150
end
151151

@@ -160,39 +160,39 @@
160160
end
161161

162162
it 'gets a list of raw indexes' do
163-
['first_index', 'second_index', 'third_index'].each { |name| client.create_index!(name) }
163+
['books', 'colors', 'artists'].each { |name| client.create_index!(name) }
164164

165165
indexes = client.raw_indexes['results']
166166

167167
expect(indexes).to be_a(Array)
168168
expect(indexes.length).to eq(3)
169169
uids = indexes.map { |elem| elem['uid'] }
170-
expect(uids).to contain_exactly('first_index', 'second_index', 'third_index')
170+
expect(uids).to contain_exactly('books', 'colors', 'artists')
171171
end
172172
end
173173

174174
describe '#fetch_index' do
175175
it 'fetches index by uid' do
176-
client.create_index!('new_index', primaryKey: 'primary_key')
176+
client.create_index!('books', primaryKey: 'reference_code')
177177

178-
fetched_index = client.fetch_index('new_index')
178+
fetched_index = client.fetch_index('books')
179179

180180
expect(fetched_index).to be_a(MeiliSearch::Index)
181-
expect(fetched_index.uid).to eq('new_index')
182-
expect(fetched_index.primary_key).to eq('primary_key')
183-
expect(fetched_index.fetch_primary_key).to eq('primary_key')
181+
expect(fetched_index.uid).to eq('books')
182+
expect(fetched_index.primary_key).to eq('reference_code')
183+
expect(fetched_index.fetch_primary_key).to eq('reference_code')
184184
end
185185
end
186186

187187
describe '#fetch_raw_index' do
188188
it 'fetch a specific index raw Hash response based on uid' do
189-
client.create_index!('specific_index_fetch_raw', primaryKey: 'primary_key')
190-
index = client.fetch_index('specific_index_fetch_raw')
189+
client.create_index!('books', primaryKey: 'reference_code')
190+
index = client.fetch_index('books')
191191
raw_response = index.fetch_raw_info
192192

193193
expect(raw_response).to be_a(Hash)
194-
expect(raw_response['uid']).to eq('specific_index_fetch_raw')
195-
expect(raw_response['primaryKey']).to eq('primary_key')
194+
expect(raw_response['uid']).to eq('books')
195+
expect(raw_response['primaryKey']).to eq('reference_code')
196196
expect(Time.parse(raw_response['createdAt'])).to be_a(Time)
197197
expect(Time.parse(raw_response['createdAt'])).to be_within(60).of(Time.now)
198198
expect(Time.parse(raw_response['updatedAt'])).to be_a(Time)
@@ -202,38 +202,38 @@
202202

203203
describe '#index' do
204204
it 'returns an index object with the provided uid' do
205-
client.create_index!('existing_index', primaryKey: 'primary_key')
205+
client.create_index!('books', primaryKey: 'reference_code')
206206
# this index is in memory, without metadata from server
207-
index = client.index('existing_index')
207+
index = client.index('books')
208208

209209
expect(index).to be_a(MeiliSearch::Index)
210-
expect(index.uid).to eq('existing_index')
210+
expect(index.uid).to eq('books')
211211
expect(index.primary_key).to be_nil
212212

213213
# fetch primary key metadata from server
214-
expect(index.fetch_primary_key).to eq('primary_key')
215-
expect(index.primary_key).to eq('primary_key')
214+
expect(index.fetch_primary_key).to eq('reference_code')
215+
expect(index.primary_key).to eq('reference_code')
216216
end
217217
end
218218

219219
describe '#delete_index' do
220220
context 'when the index exists' do
221221
it 'deletes the index' do
222-
client.create_index!('existing_index')
223-
task = client.delete_index('existing_index')
222+
client.create_index!('books')
223+
task = client.delete_index('books')
224224

225225
expect(task['type']).to eq('indexDeletion')
226226

227227
achieved_task = client.wait_for_task(task['taskUid'])
228228

229229
expect(achieved_task['status']).to eq('succeeded')
230-
expect { client.fetch_index('existing_index') }.to raise_index_not_found_meilisearch_api_error
230+
expect { client.fetch_index('books') }.to raise_index_not_found_meilisearch_api_error
231231
end
232232
end
233233

234234
context 'when the index does not exist' do
235235
it 'raises an index not found error' do
236-
expect { client.fetch_index('index_does_not_exist') }.to raise_index_not_found_meilisearch_api_error
236+
expect { client.fetch_index('books') }.to raise_index_not_found_meilisearch_api_error
237237
end
238238
end
239239
end

spec/meilisearch/index/base_spec.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
RSpec.describe MeiliSearch::Index do
44
it 'fetch the info of the index' do
5-
client.create_index!('new_index')
5+
client.create_index!('books')
66

7-
index = client.fetch_index('new_index')
7+
index = client.fetch_index('books')
88
expect(index).to be_a(MeiliSearch::Index)
9-
expect(index.uid).to eq('new_index')
9+
expect(index.uid).to eq('books')
1010
expect(index.created_at).to be_a(Time)
1111
expect(index.created_at).to be_within(60).of(Time.now)
1212
expect(index.updated_at).to be_a(Time)
@@ -15,13 +15,13 @@
1515
end
1616

1717
it 'fetch the raw Hash info of the index' do
18-
client.create_index!('specific_index_fetch_raw', primaryKey: 'primary_key')
18+
client.create_index!('books', primaryKey: 'reference_number')
1919

20-
raw_index = client.fetch_raw_index('specific_index_fetch_raw')
20+
raw_index = client.fetch_raw_index('books')
2121

2222
expect(raw_index).to be_a(Hash)
23-
expect(raw_index['uid']).to eq('specific_index_fetch_raw')
24-
expect(raw_index['primaryKey']).to eq('primary_key')
23+
expect(raw_index['uid']).to eq('books')
24+
expect(raw_index['primaryKey']).to eq('reference_number')
2525
expect(Time.parse(raw_index['createdAt'])).to be_a(Time)
2626
expect(Time.parse(raw_index['createdAt'])).to be_within(60).of(Time.now)
2727
expect(Time.parse(raw_index['updatedAt'])).to be_a(Time)
@@ -70,17 +70,17 @@
7070
end
7171

7272
it 'updates primary-key of index if has been defined before but there is not docs' do
73-
client.create_index!('uid', primaryKey: 'primary_key')
73+
client.create_index!('books', primaryKey: 'reference_number')
7474

75-
task = client.index('uid').update(primaryKey: 'new_primary_key')
75+
task = client.index('books').update(primaryKey: 'international_standard_book_number')
7676
expect(task['type']).to eq('indexUpdate')
7777
client.wait_for_task(task['taskUid'])
7878

79-
index = client.fetch_index('uid')
79+
index = client.fetch_index('books')
8080
expect(index).to be_a(MeiliSearch::Index)
81-
expect(index.uid).to eq('uid')
82-
expect(index.primary_key).to eq('new_primary_key')
83-
expect(index.fetch_primary_key).to eq('new_primary_key')
81+
expect(index.uid).to eq('books')
82+
expect(index.primary_key).to eq('international_standard_book_number')
83+
expect(index.fetch_primary_key).to eq('international_standard_book_number')
8484
expect(index.created_at).to be_a(Time)
8585
expect(index.created_at).to be_within(60).of(Time.now)
8686
expect(index.updated_at).to be_a(Time)
@@ -107,12 +107,12 @@
107107
}
108108

109109
new_client = MeiliSearch::Client.new(URL, MASTER_KEY, options)
110-
new_client.create_index!('options')
111-
index = new_client.fetch_index('options')
110+
new_client.create_index!('books')
111+
index = new_client.fetch_index('books')
112112
expect(index.options).to eq({ max_retries: 1, timeout: 2, convert_body?: true })
113113

114114
expect(MeiliSearch::Index).to receive(:get).with(
115-
"#{URL}/indexes/options",
115+
"#{URL}/indexes/books",
116116
{
117117
headers: expected_headers,
118118
body: 'null',
@@ -135,12 +135,12 @@
135135
}
136136

137137
new_client = MeiliSearch::Client.new(URL, MASTER_KEY, options)
138-
new_client.create_index!('options')
139-
index = new_client.fetch_index('options')
138+
new_client.create_index!('books')
139+
index = new_client.fetch_index('books')
140140
expect(index.options).to eq(options.merge({ convert_body?: true }))
141141

142142
expect(MeiliSearch::Index).to receive(:get).with(
143-
"#{URL}/indexes/options",
143+
"#{URL}/indexes/books",
144144
{
145145
headers: expected_headers,
146146
body: 'null',

0 commit comments

Comments
 (0)