Skip to content

Commit 28897d5

Browse files
bors[bot]jmks
andauthored
Merge #230
230: Make tests independent r=curquiza a=jmks Hello! This is a fix for #122 - Make the tests independent. My approach was this: * Delete all indexes at the start of each test example * Fix all the failures In the end, I was able to enable RSpec to run the specs in a random order, which should help detect any additional dependent tests. I've run the full test suite locally many times now and only seen a transient error; however, a dependent test could still be lurking in the suite somewhere. The transient error was that synonyms were being update then queried, without waiting for the updates to complete first. A notable change that is not obvious from the code is that the test suite is slower now (for me, 19s instead of 8s, or ~2.5x slower). I suppose that's a tradeoff for clearing all indexes before each example runs. The PR is also quite large 😬 Co-authored-by: Jason Schweier <[email protected]>
2 parents 95d3cbf + 9e316a7 commit 28897d5

24 files changed

+909
-1223
lines changed
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'MeiliSearch::Client - Dumps' do
4-
before(:all) do
5-
@client = MeiliSearch::Client.new(URL, MASTER_KEY)
6-
clear_all_indexes(@client)
7-
end
8-
94
it 'creates a new dump' do
10-
response = @client.create_dump
5+
response = client.create_dump
116
expect(response).to be_a(Hash)
127
expect(response['uid']).to_not be_nil
138
expect(response['status']).to_not be_nil
149
expect(response['status']).to eq('in_progress')
15-
wait_for_dump_creation(@client, response['uid'])
10+
wait_for_dump_creation(client, response['uid'])
1611
end
1712

1813
it 'gets dump status' do
19-
dump = @client.create_dump
20-
response = @client.dump_status(dump['uid'])
14+
dump = client.create_dump
15+
response = client.dump_status(dump['uid'])
2116
expect(response['status']).to_not be_nil
22-
wait_for_dump_creation(@client, dump['uid'])
17+
wait_for_dump_creation(client, dump['uid'])
2318
end
2419

2520
it 'fails to get dump status without uid' do
2621
expect do
27-
@client.dump_status('uid_not_exists')
22+
client.dump_status('uid_not_exists')
2823
end.to raise_meilisearch_api_error_with(404, 'not_found', 'invalid_request_error')
2924
end
3025

3126
it 'works with method aliases' do
32-
expect(@client.method(:dump_status) == @client.method(:get_dump_status)).to be_truthy
27+
expect(client.method(:dump_status) == client.method(:get_dump_status)).to be_truthy
3328
end
3429
end

spec/meilisearch/client/indexes_spec.rb

Lines changed: 74 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,133 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'MeiliSearch::Client - Indexes' do
4-
before(:all) do
5-
@client = MeiliSearch::Client.new(URL, MASTER_KEY)
6-
clear_all_indexes(@client)
7-
@uid1 = 'uid1'
8-
@uid2 = 'uid2'
9-
@uid3 = 'uid3'
10-
@uid4 = 'uid4'
11-
@uid5 = 'uid5'
12-
@uid6 = 'uid6'
13-
@uid7 = 'uid7'
14-
@primary_key = 'objectId'
15-
end
16-
174
it 'creates an index without primary-key' do
18-
index = @client.create_index(@uid1)
5+
index = client.create_index('index')
196
expect(index).to be_a(MeiliSearch::Index)
20-
expect(index.uid).to eq(@uid1)
7+
expect(index.uid).to eq('index')
218
expect(index.primary_key).to be_nil
229
expect(index.fetch_primary_key).to be_nil
2310
end
2411

2512
it 'creates an index with primary-key' do
26-
index = @client.create_index(@uid2, primaryKey: @primary_key)
13+
index = client.create_index('index', primaryKey: 'primary_key')
2714
expect(index).to be_a(MeiliSearch::Index)
28-
expect(index.uid).to eq(@uid2)
29-
expect(index.primary_key).to eq(@primary_key)
30-
expect(index.fetch_primary_key).to eq(@primary_key)
15+
expect(index.uid).to eq('index')
16+
expect(index.primary_key).to eq('primary_key')
17+
expect(index.fetch_primary_key).to eq('primary_key')
3118
end
3219

3320
it 'creates an index with uid in options - should not take it into account' do
34-
index = @client.create_index(@uid3, primaryKey: @primary_key, uid: 'wrong')
21+
index = client.create_index('index', primaryKey: 'primary_key', uid: 'wrong')
3522
expect(index).to be_a(MeiliSearch::Index)
36-
expect(index.uid).to eq(@uid3)
37-
expect(index.primary_key).to eq(@primary_key)
38-
expect(index.fetch_primary_key).to eq(@primary_key)
23+
expect(index.uid).to eq('index')
24+
expect(index.primary_key).to eq('primary_key')
25+
expect(index.fetch_primary_key).to eq('primary_key')
3926
end
4027

4128
it 'creates an new index with get_or_create_index method' do
42-
index = @client.get_or_create_index(@uid4)
43-
expect(index).to be_a(MeiliSearch::Index)
44-
expect(@client.indexes.count).to eq(4)
45-
expect(@client.fetch_index(@uid4).uid).to eq(index.uid)
46-
expect(@client.fetch_index(@uid4).uid).to eq(@uid4)
47-
expect(@client.fetch_index(@uid4).primary_key).to be_nil
48-
expect(@client.fetch_index(@uid4).primary_key).to eq(index.primary_key)
29+
expect do
30+
index = client.get_or_create_index('index')
31+
32+
expect(index).to be_a(MeiliSearch::Index)
33+
expect(index.uid).to eq('index')
34+
expect(index.primary_key).to be_nil
35+
end.to(change { client.indexes.length }.by(1))
36+
37+
fetched_index = client.fetch_index('index')
38+
expect(fetched_index.uid).to eq('index')
39+
expect(fetched_index.primary_key).to be_nil
4940
end
5041

5142
it 'creates an new index with get_or_create_index method and a primary-key' do
52-
index = @client.get_or_create_index(@uid5, primaryKey: 'title')
53-
expect(index).to be_a(MeiliSearch::Index)
54-
expect(@client.indexes.count).to eq(5)
55-
expect(@client.fetch_index(@uid5).uid).to eq(index.uid)
56-
expect(@client.fetch_index(@uid5).uid).to eq(@uid5)
57-
expect(@client.fetch_index(@uid5).primary_key).to eq(index.primary_key)
58-
expect(@client.fetch_index(@uid5).primary_key).to eq('title')
43+
expect do
44+
index = client.get_or_create_index('index', primaryKey: 'title')
45+
46+
expect(index).to be_a(MeiliSearch::Index)
47+
expect(index.uid).to eq('index')
48+
expect(index.primary_key).to eq('title')
49+
end.to(change { client.indexes.length }.by(1))
50+
51+
fetched_index = client.fetch_index('index')
52+
expect(fetched_index.uid).to eq('index')
53+
expect(fetched_index.primary_key).to eq('title')
5954
end
6055

6156
it 'get an already existing index with get_or_create_index method' do
62-
index = @client.get_or_create_index(@uid5)
63-
expect(index).to be_a(MeiliSearch::Index)
64-
expect(@client.indexes.count).to eq(5)
65-
expect(@client.fetch_index(@uid5).uid).to eq(index.uid)
66-
expect(@client.fetch_index(@uid5).uid).to eq(@uid5)
67-
expect(@client.fetch_index(@uid5).primary_key).to eq('title')
68-
expect(@client.fetch_index(@uid5).primary_key).to eq(index.primary_key)
57+
client.create_index(test_uid)
58+
59+
expect do
60+
index = client.get_or_create_index(test_uid)
61+
62+
expect(index).to be_a(MeiliSearch::Index)
63+
expect(index.uid).to eq(test_uid)
64+
expect(index.primary_key).to be_nil
65+
end.not_to(change { client.indexes.length })
6966
end
7067

7168
it 'fails to create an index with an uid already taken' do
69+
client.create_index(test_uid)
70+
7271
expect do
73-
@client.create_index(@uid1)
72+
client.create_index(test_uid)
7473
end.to raise_meilisearch_api_error_with(400, 'index_already_exists', 'invalid_request_error')
7574
end
7675

7776
it 'fails to create an index with bad UID format' do
7877
expect do
79-
@client.create_index('two words')
78+
client.create_index('two words')
8079
end.to raise_meilisearch_api_error_with(400, 'invalid_index_uid', 'invalid_request_error')
8180
end
8281

8382
it 'gets list of indexes' do
84-
response = @client.indexes
85-
expect(response).to be_a(Array)
86-
expect(response.count).to eq(5)
87-
uids = response.map { |elem| elem['uid'] }
88-
expect(uids).to contain_exactly(@uid1, @uid2, @uid3, @uid4, @uid5)
83+
['first_index', 'second_index', 'third_index'].each { |name| client.create_index(name) }
84+
85+
indexes = client.indexes
86+
87+
expect(indexes).to be_a(Array)
88+
expect(indexes.length).to eq(3)
89+
uids = indexes.map { |elem| elem['uid'] }
90+
expect(uids).to contain_exactly('first_index', 'second_index', 'third_index')
8991
end
9092

9193
it 'fetch a specific index' do
92-
response = @client.fetch_index(@uid2)
94+
client.create_index('specific_index', primaryKey: 'primary_key')
95+
96+
response = client.fetch_index('specific_index')
97+
9398
expect(response).to be_a(MeiliSearch::Index)
94-
expect(response.uid).to eq(@uid2)
95-
expect(response.primary_key).to eq(@primary_key)
96-
expect(response.fetch_primary_key).to eq(@primary_key)
99+
expect(response.uid).to eq('specific_index')
100+
expect(response.primary_key).to eq('primary_key')
101+
expect(response.fetch_primary_key).to eq('primary_key')
97102
end
98103

99104
it 'returns an index object based on uid' do
100-
index = @client.index(@uid2)
105+
client.create_index('index_with_pk', primaryKey: 'primary_key')
106+
107+
index = client.index('index_with_pk')
108+
101109
expect(index).to be_a(MeiliSearch::Index)
102-
expect(index.uid).to eq(@uid2)
110+
expect(index.uid).to eq('index_with_pk')
103111
expect(index.primary_key).to be_nil
104-
expect(index.fetch_primary_key).to eq(@primary_key)
105-
expect(index.primary_key).to eq(@primary_key)
112+
expect(index.fetch_primary_key).to eq('primary_key')
113+
expect(index.primary_key).to eq('primary_key')
106114
end
107115

108-
it 'deletes index' do
109-
expect(@client.delete_index(@uid1)).to be_nil
110-
expect { @client.fetch_index(@uid1) }.to raise_index_not_found_meilisearch_api_error
111-
expect(@client.delete_index(@uid2)).to be_nil
112-
expect { @client.fetch_index(@uid2) }.to raise_index_not_found_meilisearch_api_error
113-
expect(@client.delete_index(@uid3)).to be_nil
114-
expect { @client.fetch_index(@uid3) }.to raise_index_not_found_meilisearch_api_error
115-
expect(@client.delete_index(@uid4)).to be_nil
116-
expect { @client.fetch_index(@uid4) }.to raise_index_not_found_meilisearch_api_error
117-
expect(@client.delete_index(@uid5)).to be_nil
118-
expect { @client.fetch_index(@uid5) }.to raise_index_not_found_meilisearch_api_error
119-
expect(@client.indexes.count).to eq(0)
120-
end
116+
it 'deletes an index' do
117+
client.create_index('index')
121118

122-
it 'deletes index if index exists' do
123-
@client.create_index(@uid6)
124-
expect(@client.delete_index_if_exists(@uid6)).to eq(true)
125-
expect { @client.fetch_index(@uid6) }.to raise_index_not_found_meilisearch_api_error
126-
expect(@client.delete_index_if_exists(@uid6)).to eq(false)
127-
expect(@client.indexes.count).to eq(0)
119+
expect do
120+
expect(client.delete_index('index')).to be_nil
121+
end.to(change { client.indexes.length }.by(-1))
128122
end
129123

130124
context 'with snake_case options' do
131125
it 'creates an index without errors' do
132126
uid = SecureRandom.uuid
133127

134128
expect do
135-
@client.create_index(uid, primary_key: @primary_key)
136-
@client.fetch_index(uid)
129+
client.create_index(uid, primary_key: @primary_key)
130+
client.fetch_index(uid)
137131
end.to_not raise_error
138132
end
139133
end
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'MeiliSearch::Client - Keys' do
4-
before(:all) do
5-
@client = MeiliSearch::Client.new(URL, MASTER_KEY)
6-
@uid = 'uid'
7-
@client.create_index(@uid)
8-
end
9-
10-
after(:all) { clear_all_indexes(@client) }
11-
124
it 'gets the list of keys' do
13-
response = @client.keys
5+
response = client.keys
146
expect(response).to be_a(Hash)
157
expect(response.count).to eq(2)
168
expect(response.keys).to contain_exactly('private', 'public')
@@ -19,15 +11,15 @@
1911
end
2012

2113
it 'fails to get settings if public key used' do
22-
public_key = @client.keys['public']
14+
public_key = client.keys['public']
2315
new_client = MeiliSearch::Client.new(URL, public_key)
2416
expect do
25-
new_client.index(@uid).settings
17+
new_client.index(test_uid).settings
2618
end.to raise_meilisearch_api_error_with(403, 'invalid_token', 'authentication_error')
2719
end
2820

2921
it 'fails to get keys if private key used' do
30-
private_key = @client.keys['private']
22+
private_key = client.keys['private']
3123
new_client = MeiliSearch::Client.new(URL, private_key)
3224
expect do
3325
new_client.keys
@@ -37,25 +29,30 @@
3729
it 'fails to search if no key used' do
3830
new_client = MeiliSearch::Client.new(URL)
3931
expect do
40-
new_client.index(@uid).settings
32+
new_client.index(test_uid).settings
4133
end.to raise_meilisearch_api_error_with(401, 'missing_authorization_header', 'authentication_error')
4234
end
4335

4436
it 'succeeds to search when using public key' do
45-
public_key = @client.keys['public']
37+
public_key = client.keys['public']
38+
index = client.create_index(test_uid)
39+
response = index.add_documents(title: 'Test')
40+
index.wait_for_pending_update(response['updateId'])
41+
4642
new_client = MeiliSearch::Client.new(URL, public_key)
47-
response = new_client.index(@uid).search('test')
43+
response = new_client.index(test_uid).search('test')
4844
expect(response).to have_key('hits')
4945
end
5046

5147
it 'succeeds to get settings when using private key' do
52-
private_key = @client.keys['private']
48+
client.create_index(test_uid)
49+
private_key = client.keys['private']
5350
new_client = MeiliSearch::Client.new(URL, private_key)
54-
response = new_client.index(@uid).settings
51+
response = new_client.index(test_uid).settings
5552
expect(response).to have_key('rankingRules')
5653
end
5754

5855
it 'works with method aliases' do
59-
expect(@client.method(:keys) == @client.method(:get_keys)).to be_truthy
56+
expect(client.method(:keys) == client.method(:get_keys)).to be_truthy
6057
end
6158
end
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'MeiliSearch::Client - Stats' do
4-
before(:all) do
5-
@client = MeiliSearch::Client.new(URL, MASTER_KEY)
6-
end
7-
84
it 'gets version' do
9-
response = @client.version
5+
response = client.version
106
expect(response).to be_a(Hash)
117
expect(response).to have_key('commitSha')
128
expect(response).to have_key('commitDate')
139
expect(response).to have_key('pkgVersion')
1410
end
1511

1612
it 'gets stats' do
17-
response = @client.stats
13+
response = client.stats
1814
expect(response).to have_key('databaseSize')
1915
end
2016
end

0 commit comments

Comments
 (0)