-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: index_callback on update with update action instead of index
- Loading branch information
Showing
15 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
module Esse | ||
module ActiveRecord | ||
VERSION = '0.3.0' | ||
VERSION = '0.3.1' | ||
end | ||
end |
100 changes: 100 additions & 0 deletions
100
spec/esse/active_record/callbacks/indexing_on_update_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Esse::ActiveRecord::Callbacks::IndexingOnUpdate do | ||
describe '.initialize' do | ||
let(:repo) { instance_double(Esse::Repository) } | ||
|
||
it 'sets update_with' do | ||
callback = described_class.new(repo: repo, with: :update) | ||
expect(callback.update_with).to eq(:update) | ||
end | ||
|
||
it 'sets options' do | ||
callback = described_class.new(repo: repo, foo: :bar) | ||
expect(callback.options).to eq(foo: :bar) | ||
end | ||
end | ||
|
||
describe '.call' do | ||
let(:ok_response) { { 'result' => 'indexed' } } | ||
let(:state_class) do | ||
Class.new(State) do | ||
include Esse::ActiveRecord::Model | ||
index_callback 'states:state', on: :update | ||
end | ||
end | ||
let!(:state) { create_record(state_class, name: 'Illinois') } | ||
|
||
before do | ||
clear_active_record_hooks | ||
stub_cluster_info | ||
stub_esse_index(:states) do | ||
repository :state, const: true do | ||
document do |state, **| | ||
{ | ||
_id: state.id, | ||
name: state.name, | ||
} | ||
end | ||
end | ||
end | ||
end | ||
|
||
after do | ||
clean_db | ||
end | ||
|
||
context 'when update_with is :index' do | ||
it 'indexes the update record' do | ||
expect(StatesIndex).to receive(:index).and_call_original | ||
expect(StatesIndex).to esse_receive_request(:index).with( | ||
id: state.id, | ||
index: StatesIndex.index_name, | ||
body: {name: 'IL'}, | ||
).and_return(ok_response) | ||
|
||
state.update!(name: 'IL') | ||
end | ||
end | ||
|
||
context 'when update_with is :update' do | ||
let(:state_class) do | ||
Class.new(State) do | ||
include Esse::ActiveRecord::Model | ||
index_callback 'states:state', on: :update, with: :update | ||
end | ||
end | ||
|
||
it 'updates the update record when it exist' do | ||
expect(StatesIndex).to receive(:update).and_call_original | ||
expect(StatesIndex).to esse_receive_request(:update).with( | ||
id: state.id, | ||
index: StatesIndex.index_name, | ||
body: {doc: { name: 'IL' } }, | ||
).and_return(ok_response) | ||
|
||
state.update!(name: 'IL') | ||
end | ||
|
||
it 'indexes the update record when it does not exist' do | ||
expect(StatesIndex).to receive(:update).and_call_original | ||
expect(StatesIndex).to esse_receive_request(:update).with( | ||
id: state.id, | ||
index: StatesIndex.index_name, | ||
body: {doc: { name: 'IL' } }, | ||
).and_raise_http_status(404, { 'error' => { 'type' => 'not_found' } }) | ||
|
||
expect(StatesIndex).to receive(:index).and_call_original | ||
expect(StatesIndex).to esse_receive_request(:index).with( | ||
id: state.id, | ||
index: StatesIndex.index_name, | ||
body: {name: 'IL'}, | ||
).and_return(ok_response) | ||
|
||
state.update!(name: 'IL') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters