NameL3ss

elastic rspec

Jul 25th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. RSpec.shared_examples 'index documents for' do |model_name|
  2. include BeetrackAuth::Testing
  3. let!(:account) { create_resource(:account) }
  4. let!(:permission) { create(:permission, account_id: account.id) }
  5. let!(:resource_class) { model_name.downcase.to_sym }
  6.  
  7. before do
  8. @model = model_name.classify.constantize
  9. @worker = Elasticsearch::IndexDocumentWorker
  10. @client = @model.__elasticsearch__.client
  11. @index_name = @model.index_name
  12. end
  13.  
  14. context "for #{model_name.pluralize}" do
  15. context 'create record callback' do
  16. let!(:resource) { create(resource_class, account_id: account.id) }
  17.  
  18. before(:each) do
  19. Sidekiq::Queues.clear_all
  20. @model.__elasticsearch__.create_index!(force: true)
  21. @model.__elasticsearch__.refresh_index!
  22. @params = { action: 'create', model: @model.name, id: resource.id }
  23. end
  24.  
  25. after(:each) do
  26. @model.__elasticsearch__.delete_index!
  27. end
  28.  
  29. it 'should enqueue a job when worker is called' do
  30. expect do
  31. @worker.perform_async(@params)
  32. end.to change(@worker.jobs, :size).by(1)
  33. end
  34.  
  35. it 'should save document in elasticsearch' do
  36. expect(@model.search("id:#{resource.id}").records.length).to eq 0
  37. Sidekiq::Testing.inline! do
  38. @worker.perform_async(@params)
  39. @worker.drain
  40. end
  41. @model.__elasticsearch__.refresh_index!
  42. expect(@model.search("id:#{resource.id}").records.length).to eq 1
  43. end
  44.  
  45. it 'document should match the resource data' do
  46. Sidekiq::Testing.inline! do
  47. @worker.perform_async(@params)
  48. @worker.drain
  49. end
  50. @model.__elasticsearch__.refresh_index!
  51. expected_document = resource.as_indexed_json
  52. indexed_document = @client.get({ index: @index_name, id: resource.id })['_source']
  53. expect(indexed_document).to eq expected_document
  54. end
  55. end
  56.  
  57. context 'Update record callback' do
  58. let!(:resource) { create(resource_class, account_id: account.id) }
  59.  
  60. before(:each) do
  61. Sidekiq::Queues.clear_all
  62. @model.__elasticsearch__.create_index!(force: true)
  63. @params = { action: 'update', model: @model.name, id: resource.id }
  64.  
  65. # Create the document in elasticsearch
  66. Sidekiq::Testing.inline! do
  67. create_params = { action: 'create', model: @model.name, id: resource.id }
  68. @worker.perform_async(create_params)
  69. @worker.drain
  70. end
  71.  
  72. @model.__elasticsearch__.refresh_index!
  73. end
  74.  
  75. after(:each) do
  76. @model.__elasticsearch__.delete_index!
  77. end
  78.  
  79. it 'should enqueue a job when worker is called' do
  80. expect do
  81. @worker.perform_async(@params)
  82. end.to change(@worker.jobs, :size).by(1)
  83. end
  84.  
  85. it 'should not create a new document in elasticsearch' do
  86. expect(@model.search("id:#{resource.id}").records.length).to eq 1
  87. resource.touch
  88. Sidekiq::Testing.inline! do
  89. @worker.perform_async(@params)
  90. @worker.drain
  91. end
  92. @model.__elasticsearch__.refresh_index!
  93. expect(@model.search("id:#{resource.id}").records.length).to eq 1
  94. end
  95.  
  96. it 'document should change on update' do
  97. original_document = @client.get({ index: @index_name, id: resource.id })['_source']
  98. resource.touch
  99. Sidekiq::Testing.inline! do
  100. @worker.perform_async(@params)
  101. @worker.drain
  102. end
  103. @model.__elasticsearch__.refresh_index!
  104. updated_document = @client.get({ index: @index_name, id: resource.id })['_source']
  105. expect(updated_document).not_to eq original_document
  106. end
  107.  
  108. it 'should change its value to the updated one' do
  109. resource.touch
  110. Sidekiq::Testing.inline! do
  111. @worker.perform_async(@params)
  112. @worker.drain
  113. end
  114. @model.__elasticsearch__.refresh_index!
  115. expected_document = resource.as_indexed_json
  116. indexed_document = @client.get({ index: @index_name, id: resource.id })['_source']
  117. expect(indexed_document).to eq expected_document
  118. expect(indexed_document['updated_at']).to eq expected_document['updated_at']
  119. end
  120.  
  121. it 'should support logical deletion' do
  122. resource.soft_delete
  123. Sidekiq::Testing.inline! do
  124. @worker.perform_async(@params)
  125. @worker.drain
  126. end
  127. @model.__elasticsearch__.refresh_index!
  128. expected_document = resource.as_indexed_json
  129. indexed_document = @client.get({ index: @index_name, id: resource.id })['_source']
  130. expect(indexed_document).to eq expected_document
  131. expect(indexed_document['active']).to eq false
  132. end
  133. end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment