Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve indexes names in tests #385

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks strange but is correct. Apparently, we sort this before returning and the last one, in this case, is colors. third_index was also the third by coincidence 😉

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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have some unclear names in the indexes_spec.rb and documents_spec.rb, can you update them as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brunoocasali good call! It's done! ✅

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