|  | 
| 315 | 315 |     end | 
| 316 | 316 |   end | 
| 317 | 317 | 
 | 
|  | 318 | +  describe "#bulk" do | 
|  | 319 | +    it "imports objects to the specified indices" do | 
|  | 320 | +      connection = SearchFlip::Connection.new | 
|  | 321 | + | 
|  | 322 | +      bulk = proc do | 
|  | 323 | +        connection.bulk do |indexer| | 
|  | 324 | +          indexer.index 1, { id: 1 }, _index: ProductIndex.index_name, ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 325 | +          indexer.index 2, { id: 2 }, _index: ProductIndex.index_name, ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 326 | +          indexer.index 1, { id: 1 }, _index: CommentIndex.index_name, ** connection.version.to_i < 8 ? { _type: CommentIndex.type_name } : {} | 
|  | 327 | +        end | 
|  | 328 | +      end | 
|  | 329 | + | 
|  | 330 | +      expect(&bulk).to(change { CommentIndex.total_count }.by(1).and(change { CommentIndex.total_count }.by(1))) | 
|  | 331 | +    end | 
|  | 332 | + | 
|  | 333 | +    it "raises when no index is given" do | 
|  | 334 | +      connection = SearchFlip::Connection.new | 
|  | 335 | + | 
|  | 336 | +      bulk = proc do | 
|  | 337 | +        connection.bulk do |indexer| | 
|  | 338 | +          indexer.index 1, id: 1 | 
|  | 339 | +        end | 
|  | 340 | +      end | 
|  | 341 | + | 
|  | 342 | +      expect(&bulk).to raise_error(SearchFlip::ResponseError) | 
|  | 343 | +    end | 
|  | 344 | + | 
|  | 345 | +    it "respects options" do | 
|  | 346 | +      connection = SearchFlip::Connection.new | 
|  | 347 | + | 
|  | 348 | +      connection.bulk do |indexer| | 
|  | 349 | +        indexer.index 1, { id: 1 }, _index: ProductIndex.index_name, ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 350 | +        indexer.index 2, { id: 2 }, _index: ProductIndex.index_name, ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 351 | +      end | 
|  | 352 | + | 
|  | 353 | +      bulk = proc do | 
|  | 354 | +        connection.bulk do |indexer| | 
|  | 355 | +          indexer.index 1, { id: 1 }, _index: ProductIndex.index_name, version: 1, version_type: "external", ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 356 | +          indexer.index 2, { id: 2 }, _index: ProductIndex.index_name, version: 1, version_type: "external", ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 357 | +        end | 
|  | 358 | +      end | 
|  | 359 | + | 
|  | 360 | +      expect(&bulk).to raise_error(SearchFlip::Bulk::Error) | 
|  | 361 | + | 
|  | 362 | +      bulk = proc do | 
|  | 363 | +        connection.bulk ignore_errors: [409] do |indexer| | 
|  | 364 | +          indexer.index 1, { id: 1 }, _index: ProductIndex.index_name, version: 1, version_type: "external", ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 365 | +          indexer.index 2, { id: 2 }, _index: ProductIndex.index_name, version: 1, version_type: "external", ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 366 | +        end | 
|  | 367 | +      end | 
|  | 368 | + | 
|  | 369 | +      expect(&bulk).not_to(change { ProductIndex.total_count }) | 
|  | 370 | +    end | 
|  | 371 | + | 
|  | 372 | +    it "passes default options" do | 
|  | 373 | +      allow(SearchFlip::Bulk).to receive(:new) | 
|  | 374 | + | 
|  | 375 | +      connection = SearchFlip::Connection.new | 
|  | 376 | + | 
|  | 377 | +      connection.bulk do |indexer| | 
|  | 378 | +        indexer.index 1, { id: 1 }, _index: ProductIndex.index_name, ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 379 | +      end | 
|  | 380 | + | 
|  | 381 | +      expect(SearchFlip::Bulk).to have_received(:new).with( | 
|  | 382 | +        anything, | 
|  | 383 | +        http_client: connection.http_client, | 
|  | 384 | +        bulk_limit: connection.bulk_limit, | 
|  | 385 | +        bulk_max_mb: connection.bulk_max_mb | 
|  | 386 | +      ) | 
|  | 387 | +    end | 
|  | 388 | + | 
|  | 389 | +    it "passes custom options" do | 
|  | 390 | +      allow(SearchFlip::Bulk).to receive(:new) | 
|  | 391 | + | 
|  | 392 | +      connection = SearchFlip::Connection.new | 
|  | 393 | + | 
|  | 394 | +      options = { | 
|  | 395 | +        bulk_limit: "bulk limit", | 
|  | 396 | +        bulk_max_mb: "bulk max mb", | 
|  | 397 | +        http_client: "http client" | 
|  | 398 | +      } | 
|  | 399 | + | 
|  | 400 | +      connection.bulk(options) do |indexer| | 
|  | 401 | +        indexer.index 1, { id: 1 }, _index: ProductIndex.index_name, ** connection.version.to_i < 8 ? { _type: ProductIndex.type_name } : {} | 
|  | 402 | +      end | 
|  | 403 | + | 
|  | 404 | +      expect(SearchFlip::Bulk).to have_received(:new).with(anything, options) | 
|  | 405 | +    end | 
|  | 406 | +  end | 
|  | 407 | + | 
| 318 | 408 |   describe "#index_url" do | 
| 319 | 409 |     it "returns the index url for the specified index" do | 
| 320 | 410 |       connection = SearchFlip::Connection.new(base_url: "base_url") | 
|  | 
0 commit comments