Skip to content

Commit

Permalink
Merge #385
Browse files Browse the repository at this point in the history
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 <thicolares@gmail.com>
  • Loading branch information
meili-bors[bot] and thicolares committed Oct 31, 2022
2 parents e4ba373 + 1fe584b commit 4cc6c2a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 82 deletions.
112 changes: 56 additions & 56 deletions spec/meilisearch/client/indexes_spec.rb
Expand Up @@ -4,100 +4,100 @@
describe '#create_index' do
context 'without a primary key' do
it 'creates an index' do
task = client.create_index('new_index')
task = client.create_index('books')

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

client.wait_for_task(task['taskUid'])
index = client.fetch_index('new_index')
index = client.fetch_index('books')

expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('new_index')
expect(index.uid).to eq('books')
expect(index.primary_key).to be_nil
end

it 'creates an index synchronously' do
task = client.create_index!('new_index')
task = client.create_index!('books')

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

index = client.fetch_index('new_index')
index = client.fetch_index('books')

expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('new_index')
expect(index.uid).to eq('books')
expect(index.primary_key).to be_nil
end
end

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

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

client.wait_for_task(task['taskUid'])
index = client.fetch_index('new_index')
index = client.fetch_index('books')

expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('new_index')
expect(index.primary_key).to eq('primary_key')
expect(index.fetch_primary_key).to eq('primary_key')
expect(index.uid).to eq('books')
expect(index.primary_key).to eq('reference_code')
expect(index.fetch_primary_key).to eq('reference_code')
end

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

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

index = client.fetch_index('new_index')
index = client.fetch_index('books')

expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('new_index')
expect(index.primary_key).to eq('primary_key')
expect(index.fetch_primary_key).to eq('primary_key')
expect(index.uid).to eq('books')
expect(index.primary_key).to eq('reference_code')
expect(index.fetch_primary_key).to eq('reference_code')
end

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

index = client.fetch_index('new_index')
index = client.fetch_index('books')
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('new_index')
expect(index.primary_key).to eq('primary_key')
expect(index.fetch_primary_key).to eq('primary_key')
expect(index.uid).to eq('books')
expect(index.primary_key).to eq('reference_code')
expect(index.fetch_primary_key).to eq('reference_code')
end
end

context 'when uid is provided as an option' do
it 'ignores the uid option' do
task = client.create_index(
'new_index',
primaryKey: 'primary_key',
uid: 'not_primary_key'
'books',
primaryKey: 'reference_code',
uid: 'publications'
)

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

client.wait_for_task(task['taskUid'])
index = client.fetch_index('new_index')
index = client.fetch_index('books')

expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('new_index')
expect(index.primary_key).to eq('primary_key')
expect(index.fetch_primary_key).to eq('primary_key')
expect(index.uid).to eq('books')
expect(index.primary_key).to eq('reference_code')
expect(index.fetch_primary_key).to eq('reference_code')
end
end
end

context 'when an index with a given uid already exists' do
it 'returns a failing task' do
initial_task = client.create_index!('existing_index')
last_task = client.create_index!('existing_index')
initial_task = client.create_index!('books')
last_task = client.create_index!('books')

expect(initial_task['type']).to eq('indexCreation')
expect(last_task['type']).to eq('indexCreation')
Expand All @@ -110,42 +110,42 @@
context 'when the uid format is invalid' do
it 'raises an error' do
expect do
client.create_index('two words')
client.create_index('ancient books')
end.to raise_meilisearch_api_error_with(400, 'invalid_index_uid', 'invalid_request')
end
end
end

describe '#indexes' do
it 'returns MeiliSearch::Index objects' do
client.create_index!('index')
client.create_index!('books')

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

expect(index).to be_a(MeiliSearch::Index)
end

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

indexes = client.indexes['results']

expect(indexes).to be_a(Array)
expect(indexes.length).to eq(3)
uids = indexes.map(&:uid)
expect(uids).to contain_exactly('first_index', 'second_index', 'third_index')
expect(uids).to contain_exactly('books', 'colors', 'artists')
end

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

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

expect(indexes['results']).to be_a(Array)
expect(indexes['total']).to eq(3)
expect(indexes['limit']).to eq(1)
expect(indexes['offset']).to eq(2)
expect(indexes['results'].map(&:uid)).to eq(['third_index'])
expect(indexes['results'].map(&:uid)).to eq(['colors'])
end
end

Expand All @@ -160,39 +160,39 @@
end

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

indexes = client.raw_indexes['results']

expect(indexes).to be_a(Array)
expect(indexes.length).to eq(3)
uids = indexes.map { |elem| elem['uid'] }
expect(uids).to contain_exactly('first_index', 'second_index', 'third_index')
expect(uids).to contain_exactly('books', 'colors', 'artists')
end
end

describe '#fetch_index' do
it 'fetches index by uid' do
client.create_index!('new_index', primaryKey: 'primary_key')
client.create_index!('books', primaryKey: 'reference_code')

fetched_index = client.fetch_index('new_index')
fetched_index = client.fetch_index('books')

expect(fetched_index).to be_a(MeiliSearch::Index)
expect(fetched_index.uid).to eq('new_index')
expect(fetched_index.primary_key).to eq('primary_key')
expect(fetched_index.fetch_primary_key).to eq('primary_key')
expect(fetched_index.uid).to eq('books')
expect(fetched_index.primary_key).to eq('reference_code')
expect(fetched_index.fetch_primary_key).to eq('reference_code')
end
end

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

expect(raw_response).to be_a(Hash)
expect(raw_response['uid']).to eq('specific_index_fetch_raw')
expect(raw_response['primaryKey']).to eq('primary_key')
expect(raw_response['uid']).to eq('books')
expect(raw_response['primaryKey']).to eq('reference_code')
expect(Time.parse(raw_response['createdAt'])).to be_a(Time)
expect(Time.parse(raw_response['createdAt'])).to be_within(60).of(Time.now)
expect(Time.parse(raw_response['updatedAt'])).to be_a(Time)
Expand All @@ -202,38 +202,38 @@

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

expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('existing_index')
expect(index.uid).to eq('books')
expect(index.primary_key).to be_nil

# fetch primary key metadata from server
expect(index.fetch_primary_key).to eq('primary_key')
expect(index.primary_key).to eq('primary_key')
expect(index.fetch_primary_key).to eq('reference_code')
expect(index.primary_key).to eq('reference_code')
end
end

describe '#delete_index' do
context 'when the index exists' do
it 'deletes the index' do
client.create_index!('existing_index')
task = client.delete_index('existing_index')
client.create_index!('books')
task = client.delete_index('books')

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

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

expect(achieved_task['status']).to eq('succeeded')
expect { client.fetch_index('existing_index') }.to raise_index_not_found_meilisearch_api_error
expect { client.fetch_index('books') }.to raise_index_not_found_meilisearch_api_error
end
end

context 'when the index does not exist' do
it 'raises an index not found error' do
expect { client.fetch_index('index_does_not_exist') }.to raise_index_not_found_meilisearch_api_error
expect { client.fetch_index('books') }.to raise_index_not_found_meilisearch_api_error
end
end
end
Expand Down
38 changes: 19 additions & 19 deletions spec/meilisearch/index/base_spec.rb
Expand Up @@ -2,11 +2,11 @@

RSpec.describe MeiliSearch::Index do
it 'fetch the info of the index' do
client.create_index!('new_index')
client.create_index!('books')

index = client.fetch_index('new_index')
index = client.fetch_index('books')
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('new_index')
expect(index.uid).to eq('books')
expect(index.created_at).to be_a(Time)
expect(index.created_at).to be_within(60).of(Time.now)
expect(index.updated_at).to be_a(Time)
Expand All @@ -15,13 +15,13 @@
end

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

raw_index = client.fetch_raw_index('specific_index_fetch_raw')
raw_index = client.fetch_raw_index('books')

expect(raw_index).to be_a(Hash)
expect(raw_index['uid']).to eq('specific_index_fetch_raw')
expect(raw_index['primaryKey']).to eq('primary_key')
expect(raw_index['uid']).to eq('books')
expect(raw_index['primaryKey']).to eq('reference_number')
expect(Time.parse(raw_index['createdAt'])).to be_a(Time)
expect(Time.parse(raw_index['createdAt'])).to be_within(60).of(Time.now)
expect(Time.parse(raw_index['updatedAt'])).to be_a(Time)
Expand Down Expand Up @@ -70,17 +70,17 @@
end

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

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

index = client.fetch_index('uid')
index = client.fetch_index('books')
expect(index).to be_a(MeiliSearch::Index)
expect(index.uid).to eq('uid')
expect(index.primary_key).to eq('new_primary_key')
expect(index.fetch_primary_key).to eq('new_primary_key')
expect(index.uid).to eq('books')
expect(index.primary_key).to eq('international_standard_book_number')
expect(index.fetch_primary_key).to eq('international_standard_book_number')
expect(index.created_at).to be_a(Time)
expect(index.created_at).to be_within(60).of(Time.now)
expect(index.updated_at).to be_a(Time)
Expand All @@ -107,12 +107,12 @@
}

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

expect(MeiliSearch::Index).to receive(:get).with(
"#{URL}/indexes/options",
"#{URL}/indexes/books",
{
headers: expected_headers,
body: 'null',
Expand All @@ -135,12 +135,12 @@
}

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

expect(MeiliSearch::Index).to receive(:get).with(
"#{URL}/indexes/options",
"#{URL}/indexes/books",
{
headers: expected_headers,
body: 'null',
Expand Down

0 comments on commit 4cc6c2a

Please sign in to comment.