|
43 | 43 | end |
44 | 44 |
|
45 | 45 | describe '#create' do |
46 | | - context 'when no options are specified' do |
47 | | - it 'creates creates/indexes a document and returns it' do |
48 | | - stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0])) |
49 | | - .with(body: document, |
50 | | - headers: { |
51 | | - 'X-Typesense-Api-Key' => typesense.configuration.api_key, |
52 | | - 'Content-Type' => 'application/json' |
53 | | - }) |
54 | | - .to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' }) |
| 46 | + it 'creates creates/indexes a document and returns it' do |
| 47 | + stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0])) |
| 48 | + .with(body: document, |
| 49 | + headers: { |
| 50 | + 'X-Typesense-Api-Key' => typesense.configuration.api_key, |
| 51 | + 'Content-Type' => 'application/json' |
| 52 | + }) |
| 53 | + .to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' }) |
55 | 54 |
|
56 | | - result = companies_documents.create(document) |
| 55 | + result = companies_documents.create(document) |
57 | 56 |
|
58 | | - expect(result).to eq(document) |
59 | | - end |
| 57 | + expect(result).to eq(document) |
60 | 58 | end |
| 59 | + end |
61 | 60 |
|
62 | | - context 'when an option is specified' do |
63 | | - it 'creates creates/upserts a document and returns it' do |
64 | | - stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0])) |
65 | | - .with(body: document, |
66 | | - headers: { |
67 | | - 'X-Typesense-Api-Key' => typesense.configuration.api_key, |
68 | | - 'Content-Type' => 'application/json' |
69 | | - }, |
70 | | - query: { |
71 | | - 'upsert' => 'true' |
72 | | - }) |
73 | | - .to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' }) |
| 61 | + describe '#update' do |
| 62 | + it 'updates the document and returns it' do |
| 63 | + stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0])) |
| 64 | + .with(body: document, |
| 65 | + headers: { |
| 66 | + 'X-Typesense-Api-Key' => typesense.configuration.api_key, |
| 67 | + 'Content-Type' => 'application/json' |
| 68 | + }, |
| 69 | + query: { |
| 70 | + 'mode' => 'update' |
| 71 | + }) |
| 72 | + .to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' }) |
74 | 73 |
|
75 | | - result = companies_documents.create(document, upsert: true) |
| 74 | + result = companies_documents.update(document) |
76 | 75 |
|
77 | | - expect(result).to eq(document) |
78 | | - end |
| 76 | + expect(result).to eq(document) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + describe '#upserts' do |
| 81 | + it 'upserts the document and returns it' do |
| 82 | + stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents', typesense.configuration.nodes[0])) |
| 83 | + .with(body: document, |
| 84 | + headers: { |
| 85 | + 'X-Typesense-Api-Key' => typesense.configuration.api_key, |
| 86 | + 'Content-Type' => 'application/json' |
| 87 | + }, |
| 88 | + query: { |
| 89 | + 'mode' => 'upsert' |
| 90 | + }) |
| 91 | + .to_return(status: 200, body: JSON.dump(document), headers: { 'Content-Type': 'application/json' }) |
| 92 | + |
| 93 | + result = companies_documents.upsert(document) |
| 94 | + |
| 95 | + expect(result).to eq(document) |
79 | 96 | end |
80 | 97 | end |
81 | 98 |
|
|
115 | 132 | end |
116 | 133 |
|
117 | 134 | describe '#import' do |
118 | | - context 'when no options are specified' do |
119 | | - it 'imports documents in JSONL format' do |
| 135 | + context 'when an option is specified' do |
| 136 | + it 'passes the option to the API' do |
120 | 137 | stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents/import', typesense.configuration.nodes[0])) |
121 | 138 | .with(body: "#{JSON.dump(document)}\n#{JSON.dump(document)}", |
122 | 139 | headers: { |
123 | 140 | 'X-Typesense-Api-Key' => typesense.configuration.api_key |
| 141 | + }, |
| 142 | + query: { |
| 143 | + 'mode' => 'upsert' |
124 | 144 | }) |
125 | | - .to_return(status: 200, body: '{}', headers: { 'Content-Type': 'application/json' }) |
| 145 | + .to_return(status: 200, body: '{}', headers: { 'Content-Type': 'text/plain' }) |
126 | 146 |
|
127 | | - result = companies_documents.import("#{JSON.dump(document)}\n#{JSON.dump(document)}") |
| 147 | + result = companies_documents.import("#{JSON.dump(document)}\n#{JSON.dump(document)}", mode: :upsert) |
128 | 148 |
|
129 | | - expect(result).to eq({}) |
| 149 | + expect(result).to eq('{}') |
130 | 150 | end |
131 | 151 | end |
| 152 | + context 'when an array of docs is passed' do |
| 153 | + it 'converts it to JSONL and returns an array of results' do |
| 154 | + stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents/import', typesense.configuration.nodes[0])) |
| 155 | + .with(body: "#{JSON.dump(document)}\n#{JSON.dump(document)}", |
| 156 | + headers: { |
| 157 | + 'X-Typesense-Api-Key' => typesense.configuration.api_key |
| 158 | + }) |
| 159 | + .to_return(status: 200, body: "{}\n{}", headers: { 'Content-Type': 'text/plain' }) |
132 | 160 |
|
133 | | - context 'when an option is specified' do |
134 | | - it 'imports documents in JSONL format, with the option' do |
| 161 | + result = companies_documents.import([document, document]) |
| 162 | + |
| 163 | + expect(result).to eq([{}, {}]) |
| 164 | + end |
| 165 | + end |
| 166 | + context 'when a JSONL string is passed' do |
| 167 | + it 'it sends the string as is and returns a string' do |
135 | 168 | stub_request(:post, Typesense::ApiCall.new(typesense.configuration).send(:uri_for, '/collections/companies/documents/import', typesense.configuration.nodes[0])) |
136 | 169 | .with(body: "#{JSON.dump(document)}\n#{JSON.dump(document)}", |
137 | 170 | headers: { |
138 | 171 | 'X-Typesense-Api-Key' => typesense.configuration.api_key |
139 | | - }, |
140 | | - query: { |
141 | | - 'upsert' => 'true' |
142 | 172 | }) |
143 | | - .to_return(status: 200, body: '{}', headers: { 'Content-Type': 'application/json' }) |
| 173 | + .to_return(status: 200, body: "{}\n{}", headers: { 'Content-Type': 'text/plain' }) |
144 | 174 |
|
145 | | - result = companies_documents.import("#{JSON.dump(document)}\n#{JSON.dump(document)}", upsert: true) |
| 175 | + result = companies_documents.import("#{JSON.dump(document)}\n#{JSON.dump(document)}") |
146 | 176 |
|
147 | | - expect(result).to eq({}) |
| 177 | + expect(result).to eq("{}\n{}") |
148 | 178 | end |
149 | 179 | end |
150 | 180 | end |
|
0 commit comments