Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RSpec.shared_examples 'index documents for' do |model_name|
- include BeetrackAuth::Testing
- let!(:account) { create_resource(:account) }
- let!(:permission) { create(:permission, account_id: account.id) }
- let!(:resource_class) { model_name.downcase.to_sym }
- before do
- @model = model_name.classify.constantize
- @worker = Elasticsearch::IndexDocumentWorker
- @client = @model.__elasticsearch__.client
- @index_name = @model.index_name
- end
- context "for #{model_name.pluralize}" do
- context 'create record callback' do
- let!(:resource) { create(resource_class, account_id: account.id) }
- before(:each) do
- Sidekiq::Queues.clear_all
- @model.__elasticsearch__.create_index!(force: true)
- @model.__elasticsearch__.refresh_index!
- @params = { action: 'create', model: @model.name, id: resource.id }
- end
- after(:each) do
- @model.__elasticsearch__.delete_index!
- end
- it 'should enqueue a job when worker is called' do
- expect do
- @worker.perform_async(@params)
- end.to change(@worker.jobs, :size).by(1)
- end
- it 'should save document in elasticsearch' do
- expect(@model.search("id:#{resource.id}").records.length).to eq 0
- Sidekiq::Testing.inline! do
- @worker.perform_async(@params)
- @worker.drain
- end
- @model.__elasticsearch__.refresh_index!
- expect(@model.search("id:#{resource.id}").records.length).to eq 1
- end
- it 'document should match the resource data' do
- Sidekiq::Testing.inline! do
- @worker.perform_async(@params)
- @worker.drain
- end
- @model.__elasticsearch__.refresh_index!
- expected_document = resource.as_indexed_json
- indexed_document = @client.get({ index: @index_name, id: resource.id })['_source']
- expect(indexed_document).to eq expected_document
- end
- end
- context 'Update record callback' do
- let!(:resource) { create(resource_class, account_id: account.id) }
- before(:each) do
- Sidekiq::Queues.clear_all
- @model.__elasticsearch__.create_index!(force: true)
- @params = { action: 'update', model: @model.name, id: resource.id }
- # Create the document in elasticsearch
- Sidekiq::Testing.inline! do
- create_params = { action: 'create', model: @model.name, id: resource.id }
- @worker.perform_async(create_params)
- @worker.drain
- end
- @model.__elasticsearch__.refresh_index!
- end
- after(:each) do
- @model.__elasticsearch__.delete_index!
- end
- it 'should enqueue a job when worker is called' do
- expect do
- @worker.perform_async(@params)
- end.to change(@worker.jobs, :size).by(1)
- end
- it 'should not create a new document in elasticsearch' do
- expect(@model.search("id:#{resource.id}").records.length).to eq 1
- resource.touch
- Sidekiq::Testing.inline! do
- @worker.perform_async(@params)
- @worker.drain
- end
- @model.__elasticsearch__.refresh_index!
- expect(@model.search("id:#{resource.id}").records.length).to eq 1
- end
- it 'document should change on update' do
- original_document = @client.get({ index: @index_name, id: resource.id })['_source']
- resource.touch
- Sidekiq::Testing.inline! do
- @worker.perform_async(@params)
- @worker.drain
- end
- @model.__elasticsearch__.refresh_index!
- updated_document = @client.get({ index: @index_name, id: resource.id })['_source']
- expect(updated_document).not_to eq original_document
- end
- it 'should change its value to the updated one' do
- resource.touch
- Sidekiq::Testing.inline! do
- @worker.perform_async(@params)
- @worker.drain
- end
- @model.__elasticsearch__.refresh_index!
- expected_document = resource.as_indexed_json
- indexed_document = @client.get({ index: @index_name, id: resource.id })['_source']
- expect(indexed_document).to eq expected_document
- expect(indexed_document['updated_at']).to eq expected_document['updated_at']
- end
- it 'should support logical deletion' do
- resource.soft_delete
- Sidekiq::Testing.inline! do
- @worker.perform_async(@params)
- @worker.drain
- end
- @model.__elasticsearch__.refresh_index!
- expected_document = resource.as_indexed_json
- indexed_document = @client.get({ index: @index_name, id: resource.id })['_source']
- expect(indexed_document).to eq expected_document
- expect(indexed_document['active']).to eq false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment