Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate tests into integration and unit
- Loading branch information
1 parent
ae70c5b
commit c2181d0
Showing
14 changed files
with
112 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'spec_helper' | ||
|
||
describe Lifesaver do | ||
before do | ||
Lifesaver.suppress_indexing | ||
end | ||
let(:affiliate) { Affiliate.create(name: 'Prosper Forebearer') } | ||
let(:author) do | ||
Author.create(name: 'Theo Epstein', affiliate_id: affiliate.id) | ||
end | ||
let(:post) do | ||
Post.create( | ||
title: 'Lifesavers are my favorite candy', | ||
content: 'Lorem ipsum', | ||
tags: %w(candy stuff opinions) | ||
) | ||
end | ||
let(:comment) do | ||
Comment.create( | ||
post: post, | ||
text: 'We love this!' | ||
) | ||
end | ||
before do | ||
Authorship.create(post: post, author: author) | ||
author.reload | ||
post.reload | ||
|
||
setup_indexes([author, post]) | ||
|
||
Lifesaver.unsuppress_indexing | ||
end | ||
|
||
it 'should traverse the provided graph' do | ||
input = [{ 'class_name' => 'Author', 'id' => 1 }] | ||
indexing_graph = Lifesaver::Notification::IndexingGraph.new | ||
indexing_graph.initialize_models(input) | ||
models = indexing_graph.generate | ||
output = [author, post] | ||
expect(models).to eql(output) | ||
end | ||
|
||
it 'should reindex on destroy' do | ||
author.destroy | ||
sleep(1.seconds) | ||
expect(Author.search(query: 'Theo Epstein').count).to eql(0) | ||
end | ||
|
||
it 'should reindex on update' do | ||
author.name = 'Harry Carry' | ||
author.save! | ||
sleep(1.seconds) # need to wait for elasticsearch to update | ||
expect(Author.search(query: 'Harry Carry').count).to eql(1) | ||
end | ||
|
||
it 'should update distant related indexes' do | ||
post.tags << 'werd' | ||
post.save! | ||
sleep(1.seconds) | ||
expect(Author.search(query: 'werd').count).to eql(1) | ||
end | ||
|
||
it "should update related indexes if saved model doesn't have index" do | ||
comment.text = 'We hate this!' | ||
comment.save! | ||
sleep(1.seconds) | ||
result = Post.search(query: 'Lifesavers').to_a.first | ||
comment_text = result.comments.first.text | ||
expect(comment_text).to eql('We hate this!') | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module TireHelper | ||
def setup_indexes(models) | ||
@class_buckets = {} | ||
bucketize_models(models) | ||
refresh_indexes | ||
end | ||
|
||
private | ||
|
||
attr_accessor :class_buckets | ||
|
||
def bucketize_models(models) | ||
models.each do |model| | ||
add_model_to_bucket(model) | ||
end | ||
end | ||
|
||
def add_model_to_bucket(model) | ||
key = model.class.name | ||
class_buckets[key] ||= [] | ||
class_buckets[key] << model | ||
end | ||
|
||
def refresh_indexes | ||
class_buckets.each do |class_name, models| | ||
refresh_index(class_name, models) | ||
end | ||
end | ||
|
||
def refresh_index(class_name, models) | ||
klass = class_name.constantize | ||
klass.tire.index.delete | ||
klass.tire.create_elasticsearch_index | ||
models.each { |model| model.tire.update_index } | ||
klass.tire.index.refresh | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.