Skip to content

Commit

Permalink
Separate tests into integration and unit
Browse files Browse the repository at this point in the history
  • Loading branch information
paulnsorensen committed Dec 15, 2013
1 parent ae70c5b commit c2181d0
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 120 deletions.
3 changes: 1 addition & 2 deletions lib/lifesaver/notification/indexing_graph.rb
Expand Up @@ -5,7 +5,6 @@ def initialize
@queue = Lifesaver::Notification::TraversalQueue.new
@loader = Lifesaver::Notification::EagerLoader.new
@models_to_index = []
@done = false
end

def initialize_models(serialized_models)
Expand Down Expand Up @@ -35,7 +34,7 @@ def generate

private

attr_accessor :queue, :loader, :models_to_index, :done
attr_accessor :queue, :loader, :models_to_index

def loader_full?
!loader.empty?
Expand Down
71 changes: 71 additions & 0 deletions spec/integration/lifesaver_spec.rb
@@ -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
118 changes: 0 additions & 118 deletions spec/lifesaver_spec.rb

This file was deleted.

3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -6,13 +6,16 @@

require 'support/active_record'
require 'support/test_models'
require 'support/tire_helper'

Resque.inline = true
Tire::Model::Search.index_prefix 'lifesaver_test'

Model = Struct.new(:id)

RSpec.configure do |config|
config.include TireHelper

config.expect_with :rspec do |c|
c.syntax = :expect
end
Expand Down
37 changes: 37 additions & 0 deletions spec/support/tire_helper.rb
@@ -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.

0 comments on commit c2181d0

Please sign in to comment.