|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Elasticsearch::Persistence::Repository::Response::Results do |
| 4 | + |
| 5 | + before(:all) do |
| 6 | + class MyRepository |
| 7 | + include Elasticsearch::Persistence::Repository |
| 8 | + |
| 9 | + def deserialize(document) |
| 10 | + 'Object' |
| 11 | + end |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + let(:repository) do |
| 16 | + MyRepository.new |
| 17 | + end |
| 18 | + |
| 19 | + after(:all) do |
| 20 | + if defined?(MyRepository) |
| 21 | + Object.send(:remove_const, MyRepository.name) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + let(:response) do |
| 26 | + { "took" => 2, |
| 27 | + "timed_out" => false, |
| 28 | + "_shards" => {"total" => 5, "successful" => 5, "failed" => 0}, |
| 29 | + "hits" => |
| 30 | + { "total" => 2, |
| 31 | + "max_score" => 0.19, |
| 32 | + "hits" => |
| 33 | + [{"_index" => "my_index", |
| 34 | + "_type" => "note", |
| 35 | + "_id" => "1", |
| 36 | + "_score" => 0.19, |
| 37 | + "_source" => {"id" => 1, "title" => "Test 1"}}, |
| 38 | + |
| 39 | + {"_index" => "my_index", |
| 40 | + "_type" => "note", |
| 41 | + "_id" => "2", |
| 42 | + "_score" => 0.19, |
| 43 | + "_source" => {"id" => 2, "title" => "Test 2"}} |
| 44 | + ] |
| 45 | + } |
| 46 | + } |
| 47 | + end |
| 48 | + |
| 49 | + let(:results) do |
| 50 | + described_class.new(repository, response) |
| 51 | + end |
| 52 | + |
| 53 | + describe '#repository' do |
| 54 | + |
| 55 | + it 'should return the repository' do |
| 56 | + expect(results.repository).to be(repository) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + describe '#response' do |
| 61 | + |
| 62 | + it 'returns the response' do |
| 63 | + expect(results.response).to eq(response) |
| 64 | + end |
| 65 | + |
| 66 | + it 'wraps the response in a HashWrapper' do |
| 67 | + expect(results.response._shards.total).to eq(5) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe '#total' do |
| 72 | + |
| 73 | + it 'returns the total' do |
| 74 | + expect(results.total).to eq(2) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + describe '#max_score' do |
| 79 | + |
| 80 | + it 'returns the max score' do |
| 81 | + expect(results.max_score).to eq(0.19) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe '#each' do |
| 86 | + |
| 87 | + it 'delegates the method to the results' do |
| 88 | + expect(results.size).to eq(2) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + describe '#each_with_hit' do |
| 93 | + |
| 94 | + it 'returns each deserialized object with the raw document' do |
| 95 | + expect(results.each_with_hit { |pair| pair[0] = 'Obj'}).to eq(['Obj', 'Obj'].zip(response['hits']['hits'])) |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + describe '#map_with_hit' do |
| 100 | + |
| 101 | + it 'returns the result of the block called on a pair of each raw document and the deserialized object' do |
| 102 | + expect(results.map_with_hit { |pair| pair[0] }).to eq(['Object', 'Object']) |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments