|
28 | 28 | expect(method_call.size).to be(top_hits.size) |
29 | 29 | end |
30 | 30 |
|
| 31 | + context "when no 'sort' has been given" do |
| 32 | + it "has its 'sort' set to nil" do |
| 33 | + expect(method_call.sort).to be_nil |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context "when a 'sort' has been given" do |
| 38 | + let(:sort) { { timestamp: :desc } } |
| 39 | + let(:constructor_params) { super().merge(sort: sort) } |
| 40 | + |
| 41 | + it "has its 'sort' set to the same value" do |
| 42 | + expect(method_call.sort).to eq(sort) |
| 43 | + end |
| 44 | + |
| 45 | + it 'is not the same hash' do |
| 46 | + expect(method_call.sort).not_to be(top_hits.sort) |
| 47 | + end |
| 48 | + end |
| 49 | + |
31 | 50 | context 'when the original object has nested aggregations' do |
32 | 51 | let(:cloned_aggregations) { instance_double(JayAPI::Elasticsearch::QueryBuilder::Aggregations) } |
33 | 52 |
|
|
50 | 69 | describe '#to_h' do |
51 | 70 | subject(:method_call) { top_hits.to_h } |
52 | 71 |
|
53 | | - let(:expected_hash) do |
54 | | - { |
55 | | - 'an_aggregation_sample' => { |
56 | | - top_hits: { size: 1 } |
| 72 | + it_behaves_like 'JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation#to_h' |
| 73 | + |
| 74 | + context "when no 'sort' has been given" do |
| 75 | + let(:expected_hash) do |
| 76 | + { |
| 77 | + 'an_aggregation_sample' => { |
| 78 | + top_hits: { size: 1 } |
| 79 | + } |
57 | 80 | } |
58 | | - } |
| 81 | + end |
| 82 | + |
| 83 | + it 'returns the expected Hash (does not include the :sort key)' do |
| 84 | + expect(method_call).to eq(expected_hash) |
| 85 | + end |
59 | 86 | end |
60 | 87 |
|
61 | | - it_behaves_like 'JayAPI::Elasticsearch::QueryBuilder::Aggregations::Aggregation#to_h' |
| 88 | + context "when a 'sort' has been given" do |
| 89 | + let(:sort) { { timestamp: :desc } } |
| 90 | + let(:constructor_params) { super().merge(sort: sort) } |
| 91 | + |
| 92 | + let(:expected_hash) do |
| 93 | + { |
| 94 | + 'an_aggregation_sample' => { |
| 95 | + top_hits: { |
| 96 | + size: 1, |
| 97 | + sort: { timestamp: :desc } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + end |
62 | 102 |
|
63 | | - it 'returns the expected Hash' do |
64 | | - expect(method_call).to eq(expected_hash) |
| 103 | + it 'returns the expected Hash (includes the expected :sort key)' do |
| 104 | + expect(method_call).to eq(expected_hash) |
| 105 | + end |
| 106 | + |
| 107 | + it "does not return a reference to the internal 'sort' Hash" do |
| 108 | + sort_hash = method_call.dig('an_aggregation_sample', :top_hits, :sort) |
| 109 | + expect(sort_hash).not_to be(top_hits.sort) |
| 110 | + expect(sort_hash).to eq(sort) |
| 111 | + end |
65 | 112 | end |
66 | 113 | end |
67 | 114 | end |
0 commit comments