|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | RSpec.describe 'MeiliSearch::Client - Indexes' do |
4 | | - it 'creates an index without primary-key' do |
5 | | - index = client.create_index('index') |
6 | | - expect(index).to be_a(MeiliSearch::Index) |
7 | | - expect(index.uid).to eq('index') |
8 | | - expect(index.primary_key).to be_nil |
9 | | - expect(index.fetch_primary_key).to be_nil |
10 | | - end |
11 | | - |
12 | | - it 'creates an index with primary-key' do |
13 | | - index = client.create_index('index', primaryKey: 'primary_key') |
14 | | - expect(index).to be_a(MeiliSearch::Index) |
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') |
18 | | - end |
| 4 | + describe '#create_index' do |
| 5 | + context 'without a primary key' do |
| 6 | + it 'creates an index' do |
| 7 | + index = client.create_index('new_index') |
| 8 | + |
| 9 | + expect(index).to be_a(MeiliSearch::Index) |
| 10 | + expect(index.uid).to eq('new_index') |
| 11 | + expect(index.primary_key).to be_nil |
| 12 | + expect(index.fetch_primary_key).to be_nil |
| 13 | + end |
| 14 | + end |
19 | 15 |
|
20 | | - it 'creates an index with uid in options - should not take it into account' do |
21 | | - index = client.create_index('index', primaryKey: 'primary_key', uid: 'wrong') |
22 | | - expect(index).to be_a(MeiliSearch::Index) |
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') |
26 | | - end |
| 16 | + context 'with a primary key' do |
| 17 | + it 'creates an index' do |
| 18 | + index = client.create_index('new_index', primaryKey: 'primary_key') |
| 19 | + |
| 20 | + expect(index).to be_a(MeiliSearch::Index) |
| 21 | + expect(index.uid).to eq('new_index') |
| 22 | + expect(index.primary_key).to eq('primary_key') |
| 23 | + expect(index.fetch_primary_key).to eq('primary_key') |
| 24 | + end |
| 25 | + |
| 26 | + context 'when primary key option in snake_case' do |
| 27 | + it 'creates an index' do |
| 28 | + index = client.create_index('new_index', primary_key: 'primary_key') |
| 29 | + |
| 30 | + expect(index).to be_a(MeiliSearch::Index) |
| 31 | + expect(index.uid).to eq('new_index') |
| 32 | + expect(index.primary_key).to eq('primary_key') |
| 33 | + expect(index.fetch_primary_key).to eq('primary_key') |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context 'when uid is provided as an option' do |
| 38 | + it 'ignores the uid option' do |
| 39 | + index = client.create_index( |
| 40 | + 'new_index', |
| 41 | + primaryKey: 'primary_key', |
| 42 | + uid: 'not_primary_key' |
| 43 | + ) |
| 44 | + |
| 45 | + expect(index).to be_a(MeiliSearch::Index) |
| 46 | + expect(index.uid).to eq('new_index') |
| 47 | + expect(index.primary_key).to eq('primary_key') |
| 48 | + expect(index.fetch_primary_key).to eq('primary_key') |
| 49 | + end |
| 50 | + end |
| 51 | + end |
27 | 52 |
|
28 | | - it 'creates an new index with get_or_create_index method' do |
29 | | - expect do |
30 | | - index = client.get_or_create_index('index') |
| 53 | + context 'when an index with a given uid already exists' do |
| 54 | + it 'raises an error' do |
| 55 | + client.create_index('existing_index') |
31 | 56 |
|
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)) |
| 57 | + expect do |
| 58 | + client.create_index('existing_index') |
| 59 | + end.to raise_meilisearch_api_error_with(400, 'index_already_exists', 'invalid_request_error') |
| 60 | + end |
| 61 | + end |
36 | 62 |
|
37 | | - fetched_index = client.fetch_index('index') |
38 | | - expect(fetched_index.uid).to eq('index') |
39 | | - expect(fetched_index.primary_key).to be_nil |
| 63 | + context 'when the uid format is invalid' do |
| 64 | + it 'raises an error' do |
| 65 | + expect do |
| 66 | + client.create_index('two words') |
| 67 | + end.to raise_meilisearch_api_error_with(400, 'invalid_index_uid', 'invalid_request_error') |
| 68 | + end |
| 69 | + end |
40 | 70 | end |
41 | 71 |
|
42 | | - it 'creates an new index with get_or_create_index method and a primary-key' do |
43 | | - expect do |
44 | | - index = client.get_or_create_index('index', primaryKey: 'title') |
| 72 | + describe '#get_or_create_index' do |
| 73 | + it 'creates a new index' do |
| 74 | + expect do |
| 75 | + new_index = client.get_or_create_index('new_index') |
45 | 76 |
|
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)) |
| 77 | + expect(new_index).to be_a(MeiliSearch::Index) |
| 78 | + end.to change { client.indexes.length }.by(1) |
50 | 79 |
|
51 | | - fetched_index = client.fetch_index('index') |
52 | | - expect(fetched_index.uid).to eq('index') |
53 | | - expect(fetched_index.primary_key).to eq('title') |
54 | | - end |
| 80 | + found_index = client.fetch_index('new_index') |
| 81 | + expect(found_index.uid).to eq('new_index') |
| 82 | + expect(found_index.primary_key).to be_nil |
| 83 | + end |
55 | 84 |
|
56 | | - it 'get an already existing index with get_or_create_index method' do |
57 | | - client.create_index(test_uid) |
| 85 | + it 'gets an index that already exists' do |
| 86 | + client.create_index('existing_index') |
58 | 87 |
|
59 | | - expect do |
60 | | - index = client.get_or_create_index(test_uid) |
| 88 | + expect do |
| 89 | + client.get_or_create_index('existing_index') |
| 90 | + end.not_to(change { client.indexes.length }) |
| 91 | + end |
61 | 92 |
|
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 }) |
| 93 | + context 'when a primary key is provided' do |
| 94 | + it 'creates a new index' do |
| 95 | + expect do |
| 96 | + index = client.get_or_create_index('new_index', primaryKey: 'primary_key') |
| 97 | + |
| 98 | + expect(index).to be_a(MeiliSearch::Index) |
| 99 | + end.to change { client.indexes.length }.by(1) |
| 100 | + end |
| 101 | + end |
66 | 102 | end |
67 | 103 |
|
68 | | - it 'fails to create an index with an uid already taken' do |
69 | | - client.create_index(test_uid) |
| 104 | + describe '#indexes' do |
| 105 | + it 'returns MeiliSearch::Index objects' do |
| 106 | + client.create_index('index') |
70 | 107 |
|
71 | | - expect do |
72 | | - client.create_index(test_uid) |
73 | | - end.to raise_meilisearch_api_error_with(400, 'index_already_exists', 'invalid_request_error') |
74 | | - end |
| 108 | + response = client.indexes.first |
75 | 109 |
|
76 | | - it 'fails to create an index with bad UID format' do |
77 | | - expect do |
78 | | - client.create_index('two words') |
79 | | - end.to raise_meilisearch_api_error_with(400, 'invalid_index_uid', 'invalid_request_error') |
80 | | - end |
| 110 | + expect(response).to be_a(MeiliSearch::Index) |
| 111 | + end |
81 | 112 |
|
82 | | - it 'gets list of indexes as MeiliSearch::Index' do |
83 | | - client.create_index('index') |
| 113 | + it 'gets a list of indexes' do |
| 114 | + ['first_index', 'second_index', 'third_index'].each { |name| client.create_index(name) } |
84 | 115 |
|
85 | | - response = client.indexes.first |
| 116 | + indexes = client.indexes |
86 | 117 |
|
87 | | - expect(response).to be_a(MeiliSearch::Index) |
| 118 | + expect(indexes).to be_a(Array) |
| 119 | + expect(indexes.length).to eq(3) |
| 120 | + uids = indexes.map(&:uid) |
| 121 | + expect(uids).to contain_exactly('first_index', 'second_index', 'third_index') |
| 122 | + end |
88 | 123 | end |
89 | 124 |
|
90 | | - it 'gets list of raw indexes' do |
91 | | - ['first_index', 'second_index', 'third_index'].each { |name| client.create_index(name) } |
| 125 | + describe '#raw_indexes' do |
| 126 | + it 'returns raw indexes' do |
| 127 | + client.create_index('index') |
92 | 128 |
|
93 | | - indexes = client.raw_indexes |
| 129 | + response = client.raw_indexes.first |
94 | 130 |
|
95 | | - expect(indexes).to be_a(Array) |
96 | | - expect(indexes.length).to eq(3) |
97 | | - uids = indexes.map { |elem| elem['uid'] } |
98 | | - expect(uids).to contain_exactly('first_index', 'second_index', 'third_index') |
99 | | - end |
| 131 | + expect(response).to be_a(Hash) |
| 132 | + expect(response['uid']).to eq('index') |
| 133 | + end |
100 | 134 |
|
101 | | - it 'fetch a specific index' do |
102 | | - client.create_index('specific_index', primaryKey: 'primary_key') |
| 135 | + it 'gets a list of raw indexes' do |
| 136 | + ['first_index', 'second_index', 'third_index'].each { |name| client.create_index(name) } |
103 | 137 |
|
104 | | - response = client.fetch_index('specific_index') |
| 138 | + indexes = client.raw_indexes |
105 | 139 |
|
106 | | - expect(response).to be_a(MeiliSearch::Index) |
107 | | - expect(response.uid).to eq('specific_index') |
108 | | - expect(response.primary_key).to eq('primary_key') |
109 | | - expect(response.fetch_primary_key).to eq('primary_key') |
| 140 | + expect(indexes).to be_a(Array) |
| 141 | + expect(indexes.length).to eq(3) |
| 142 | + uids = indexes.map { |elem| elem['uid'] } |
| 143 | + expect(uids).to contain_exactly('first_index', 'second_index', 'third_index') |
| 144 | + end |
110 | 145 | end |
111 | 146 |
|
112 | | - it 'returns an index object based on uid' do |
113 | | - client.create_index('index_with_pk', primaryKey: 'primary_key') |
| 147 | + describe '#fetch_index' do |
| 148 | + it 'fetches index by uid' do |
| 149 | + client.create_index('new_index', primaryKey: 'primary_key') |
114 | 150 |
|
115 | | - index = client.index('index_with_pk') |
| 151 | + fetched_index = client.fetch_index('new_index') |
116 | 152 |
|
117 | | - expect(index).to be_a(MeiliSearch::Index) |
118 | | - expect(index.uid).to eq('index_with_pk') |
119 | | - expect(index.primary_key).to be_nil |
120 | | - expect(index.fetch_primary_key).to eq('primary_key') |
121 | | - expect(index.primary_key).to eq('primary_key') |
| 153 | + expect(fetched_index).to be_a(MeiliSearch::Index) |
| 154 | + expect(fetched_index.uid).to eq('new_index') |
| 155 | + expect(fetched_index.primary_key).to eq('primary_key') |
| 156 | + expect(fetched_index.fetch_primary_key).to eq('primary_key') |
| 157 | + end |
122 | 158 | end |
123 | 159 |
|
124 | | - it 'deletes an index' do |
125 | | - client.create_index('index') |
| 160 | + describe '#index' do |
| 161 | + it 'returns an index object with the provided uid' do |
| 162 | + client.create_index('existing_index', primaryKey: 'primary_key') |
| 163 | + |
| 164 | + # this index is in memory, without metadata from server |
| 165 | + index = client.index('existing_index') |
| 166 | + |
| 167 | + expect(index).to be_a(MeiliSearch::Index) |
| 168 | + expect(index.uid).to eq('existing_index') |
| 169 | + expect(index.primary_key).to be_nil |
126 | 170 |
|
127 | | - expect do |
128 | | - expect(client.delete_index('index')).to be_nil |
129 | | - end.to(change { client.indexes.length }.by(-1)) |
| 171 | + # fetch primary key metadata from server |
| 172 | + expect(index.fetch_primary_key).to eq('primary_key') |
| 173 | + expect(index.primary_key).to eq('primary_key') |
| 174 | + end |
130 | 175 | end |
131 | 176 |
|
132 | | - context 'with snake_case options' do |
133 | | - it 'creates an index without errors' do |
134 | | - uid = SecureRandom.uuid |
| 177 | + describe '#delete_index' do |
| 178 | + context 'when the index exists' do |
| 179 | + it 'deletes the index' do |
| 180 | + client.create_index('existing_index') |
135 | 181 |
|
136 | | - expect do |
137 | | - client.create_index(uid, primary_key: @primary_key) |
138 | | - client.fetch_index(uid) |
139 | | - end.to_not raise_error |
| 182 | + expect(client.delete_index('existing_index')).to be_nil |
| 183 | + expect { client.fetch_index('existing_index') }.to raise_index_not_found_meilisearch_api_error |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + context 'when the index does not exist' do |
| 188 | + it 'raises an index not found error' do |
| 189 | + expect { client.delete_index('index_does_not_exist') }.to raise_index_not_found_meilisearch_api_error |
| 190 | + end |
140 | 191 | end |
141 | 192 | end |
142 | 193 | end |
0 commit comments