elastic_queue 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe ElasticQueue::Persistence do
4
+ before :all do
5
+ class Animal < ActiveRecord::Base
6
+ include ElasticQueue::Queueable
7
+ queue_attributes :dangerous, :cute, :birthdate
8
+ not_analyzed_queue_attributes :species, :description, :name
9
+ end
10
+
11
+ class TestAnimalsQueue < ElasticQueue::Base
12
+ models :animal
13
+ end
14
+ end
15
+
16
+ after :all do
17
+ [:Animal, :TestAnimalsQueue].each do |constant|
18
+ Object.send(:remove_const, constant)
19
+ end
20
+ end
21
+
22
+ after :each do
23
+ delete_index('test_animals_queue')
24
+ end
25
+
26
+ describe '#index_exists?' do
27
+ it 'gives false when its index doesnt exist' do
28
+ delete_index('test_animals_queue')
29
+ expect(TestAnimalsQueue.index_exists?).to be false
30
+ end
31
+
32
+ it 'gives true when its index exists' do
33
+ create_index('test_animals_queue')
34
+ expect(TestAnimalsQueue.index_exists?).to be true
35
+ end
36
+ end
37
+
38
+ describe '#reset_index' do
39
+ pending
40
+ end
41
+
42
+ describe '#create_index' do
43
+ it 'sets the default analyzer to use the whitespace tokenizer with the lowercase filter and no stopwords' do
44
+ TestAnimalsQueue.create_index
45
+ expect(`curl -XGET 'localhost:9200/test_animals_queue/_analyze?' -d 'AND OR'`)
46
+ .to eq('{"tokens":[{"token":"and","start_offset":0,"end_offset":3,"type":"word","position":1},{"token":"or","start_offset":4,"end_offset":6,"type":"word","position":2}]}')
47
+
48
+ expect(`curl -XGET 'localhost:9200/test_animals_queue/_analyze?' -d 'under_scored_word'`)
49
+ .to eq('{"tokens":[{"token":"under_scored_word","start_offset":0,"end_offset":17,"type":"word","position":1}]}')
50
+ end
51
+ end
52
+
53
+ describe '#delete_index' do
54
+ pending
55
+ end
56
+
57
+ describe '#bulk_index' do
58
+ before :all do
59
+ ActiveRecord::Base.connection.execute("INSERT INTO animals (name, species)
60
+ VALUES ('pom-pom', 'dog'), ('fluffy', 'dog'), ('muffy', 'cat');")
61
+ end
62
+ it 'creates the index if one doesn\t exist' do
63
+ expect(test_search_client.indices.exists index: 'test_animals_queue').to be false
64
+ TestAnimalsQueue.bulk_index
65
+ expect(test_search_client.indices.exists index: 'test_animals_queue').to be true
66
+ end
67
+
68
+ it 'indexes all existing models' do
69
+ TestAnimalsQueue.bulk_index
70
+ refresh_index('test_animals_queue')
71
+ expect(query_all('test_animals_queue')['hits']['total']).to be 3
72
+ end
73
+
74
+ it 'takes scopes' do
75
+ class Animal < ActiveRecord::Base
76
+ scope :not_fluffy, where("name != 'fluffy'")
77
+ scope :only_dogs, where(species: 'dog')
78
+ end
79
+ TestAnimalsQueue.bulk_index(scopes: { animal: [:not_fluffy, :only_dogs], some_other_model: [:this_wont_get_called] })
80
+ refresh_index('test_animals_queue')
81
+ expect(query_all('test_animals_queue')['hits']['total']).to be 1
82
+ end
83
+ end
84
+
85
+ describe '#add_mappings' do
86
+ pending
87
+ end
88
+
89
+ describe '#index_model' do
90
+ pending
91
+ end
92
+
93
+ describe '#upsert_model' do
94
+ pending
95
+ end
96
+
97
+ describe '#remove_model' do
98
+ pending
99
+ end
100
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe ElasticQueue::Query do
4
+ before :all do
5
+ class Animal < ActiveRecord::Base
6
+ include ElasticQueue::Queueable
7
+ queues :test_animals_queue
8
+ queue_attributes :dangerous, :cute, :birthdate
9
+ not_analyzed_queue_attributes :species, :description, :name
10
+ end
11
+
12
+ class TestAnimalsQueue < ElasticQueue::Base
13
+ models :animal
14
+ end
15
+
16
+ TestAnimalsQueue.create_index
17
+
18
+ animals = []
19
+ (0...55).each { |name| animals << { name: name } }
20
+ Animal.create(animals)
21
+ end
22
+
23
+ after :all do
24
+ Animal.all.each(&:destroy)
25
+ [:Animal, :TestAnimalsQueue].each do |constant|
26
+ Object.send(:remove_const, constant)
27
+ end
28
+ delete_index('test_animals_queue')
29
+ end
30
+
31
+ describe '#paginate' do
32
+ it 'runs the query and returns a WillPaginate::Collection' do
33
+ expect(TestAnimalsQueue.query.paginate).to be_a(WillPaginate::Collection)
34
+ end
35
+
36
+ it 'has pagination defaults of { page: 1, per_page: 30 }' do
37
+ expect(TestAnimalsQueue.query.paginate.length).to eq 30
38
+ expect(TestAnimalsQueue.query.paginate.current_page).to eq 1
39
+ end
40
+
41
+ it 'the returned collection knows how many results there are total' do
42
+ expect(TestAnimalsQueue.query.paginate.total_entries).to eq 55
43
+ end
44
+
45
+ it 'can take per_page as an option' do
46
+ expect(TestAnimalsQueue.query.paginate({ per_page: 2 }).length).to eq 2
47
+ end
48
+
49
+ it 'can take page as an option' do
50
+ first_result_on_page_1 = TestAnimalsQueue.query.paginate({ page: 1 }).first.id
51
+ first_result_on_page_2 = TestAnimalsQueue.query.paginate({ page: 2 }).first.id
52
+ expect(first_result_on_page_1).to_not eq first_result_on_page_2
53
+ end
54
+
55
+ it 'can take both page and per_page as options at the same time' do
56
+ expect(TestAnimalsQueue.query.paginate({ page: 2, per_page: 2 }).length).to eq 2
57
+ expect(TestAnimalsQueue.query.paginate({ page: 2, per_page: 2 }).current_page).to eq 2
58
+ end
59
+ end
60
+
61
+ describe '#all' do
62
+ it 'returns an array' do
63
+ expect(TestAnimalsQueue.query.all).to be_a(Array)
64
+ end
65
+
66
+ it 'returns all results' do
67
+ expect(TestAnimalsQueue.query.all.length).to be 55
68
+ end
69
+ end
70
+
71
+ describe '#count' do
72
+ it 'returns the count of the number of results' do
73
+ expect(TestAnimalsQueue.query.all.count).to be 55
74
+ end
75
+
76
+ it 'does not make elasticsearch fetch all the records' do
77
+ TestAnimalsQueue.search_client.stub(:search) { { hits: { total: 0, hits: [] } } }
78
+ TestAnimalsQueue.search_client.should_receive(:search).with({ index: 'test_animals_queue', body: {}, search_type: 'count' })
79
+ TestAnimalsQueue.query.count
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ElasticQueue::Queueable integration' do
4
+ before :all do
5
+ class Animal < ActiveRecord::Base
6
+ include ElasticQueue::Queueable
7
+ queues :test_animals_queue
8
+ queue_attributes :dangerous, :cute, :birthdate
9
+ not_analyzed_queue_attributes :species, :description, :name
10
+ end
11
+
12
+ class TestAnimalsQueue < ElasticQueue::Base
13
+ models :animal
14
+ end
15
+ end
16
+
17
+ after :all do
18
+ [:Animal, :TestAnimalsQueue].each do |constant|
19
+ Object.send(:remove_const, constant)
20
+ end
21
+ end
22
+
23
+ describe 'callbacks' do
24
+ before :each do
25
+ TestAnimalsQueue.create_index
26
+ @fluffy = Animal.create({ name: 'Fluffy' })
27
+ end
28
+
29
+ after :each do
30
+ Animal.all.each(&:destroy)
31
+ delete_index('test_animals_queue')
32
+ end
33
+
34
+ it 'adds newly created models to the queue' do
35
+ # model was created in before block
36
+ expect(query_all('test_animals_queue')['hits']['hits'].first['_source']['name']).to eq 'Fluffy'
37
+ end
38
+
39
+ it 'deletes deleted models from the queue' do
40
+ Animal.find(@fluffy.id).destroy
41
+ refresh_index('test_animals_queue')
42
+ expect(query_all('test_animals_queue')['hits']['total']).to be 0
43
+ end
44
+
45
+ it 'updates a changed model in the queue' do
46
+ @fluffy.update_attributes(name: 'Muffy')
47
+ refresh_index('test_animals_queue')
48
+ expect(query_all('test_animals_queue')['hits']['hits'].first['_source']['name']).to eq 'Muffy'
49
+ end
50
+ end
51
+
52
+ # TODO: Turn back on later
53
+ # describe '#add_queue' do
54
+ # after :each do
55
+ # [:SomeModel, :SomeOtherModel, :SomeQueue, :SomeOtherQueue].each do |constant|
56
+ # Object.send(:remove_const, constant) if Object.constants.include? constant
57
+ # end
58
+ # end
59
+ # it 'adds a queue to its list of queues' do
60
+ # class SomeModel < ActiveRecord::Base
61
+ # include ElasticQueue::Queueable
62
+ # end
63
+ # class SomeQueue < ElasticQueue::Base
64
+ # end
65
+ # SomeModel.add_queue(:some_queue)
66
+ # expect(SomeModel.queues.sort).to eq [:some_queue]
67
+ # end
68
+ #
69
+ # it 'only adds each queue once' do
70
+ # class SomeOtherModel < ActiveRecord::Base
71
+ # include ElasticQueue::Queueable
72
+ # end
73
+ # class SomeQueue < ElasticQueue::Base
74
+ # end
75
+ # class SomeOtherQueue < ElasticQueue::Base
76
+ # end
77
+ # SomeOtherModel.add_queue(:some_queue)
78
+ # SomeOtherModel.add_queue(:some_other_queue)
79
+ # SomeOtherModel.add_queue(:some_queue)
80
+ # expect(SomeOtherModel.queues.sort).to eq [:some_other_queue, :some_queue]
81
+ # end
82
+ # end
83
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ElasticQueue::Sorts integration' do
4
+ before :all do
5
+ class Animal < ActiveRecord::Base
6
+ include ElasticQueue::Queueable
7
+ queues :test_animals_queue
8
+ queue_attributes :dangerous, :cute, :birthdate
9
+ not_analyzed_queue_attributes :species, :description, :name
10
+ end
11
+
12
+ class TestAnimalsQueue < ElasticQueue::Base
13
+ models :animal
14
+ end
15
+
16
+ TestAnimalsQueue.create_index
17
+ end
18
+
19
+ after :all do
20
+ Animal.all.each(&:destroy)
21
+ [:Animal, :TestAnimalsQueue].each do |constant|
22
+ Object.send(:remove_const, constant)
23
+ end
24
+ delete_index('test_animals_queue')
25
+ end
26
+
27
+ describe 'ElasticQueue::Query#sort' do
28
+ after :each do
29
+ Animal.all.each(&:destroy)
30
+ end
31
+
32
+ it 'sorts ascending and descending on one value' do
33
+ Animal.create({ name: 'a', birthdate: Date.today.at_midnight - 1.year })
34
+ Animal.create({ name: 'b', birthdate: Date.today.at_midnight - 2.years })
35
+ Animal.create({ name: 'c', birthdate: Date.today.at_midnight - 3.years })
36
+ expect(TestAnimalsQueue.query.sort(birthdate: 'asc').all.map(&:name)).to eq ['c', 'b', 'a']
37
+ expect(TestAnimalsQueue.query.sort(birthdate: 'desc').all.map(&:name)).to eq ['a', 'b', 'c']
38
+ end
39
+
40
+ it 'sorts ascending and descending on two values' do
41
+ Animal.create({ name: 'a', birthdate: Date.today.at_midnight - 1.year })
42
+ Animal.create({ name: 'b', birthdate: Date.today.at_midnight - 1.years })
43
+ Animal.create({ name: 'c', birthdate: Date.today.at_midnight - 3.years })
44
+ expect(TestAnimalsQueue.query.sort(birthdate: 'asc').sort(name: 'asc').all.map(&:name)).to eq ['c', 'a', 'b']
45
+ expect(TestAnimalsQueue.query.sort(birthdate: 'asc').sort(name: 'desc').all.map(&:name)).to eq ['c', 'b', 'a']
46
+ end
47
+
48
+ it 'doesn\'t fail to sort because of stopwords' do
49
+ Animal.create({ name: 'and' })
50
+ Animal.create({ name: 'or' })
51
+ Animal.create({ name: 'if' })
52
+ expect(TestAnimalsQueue.query.sort(name: 'asc').all.map(&:name)).to eq ['and', 'if', 'or']
53
+ end
54
+
55
+ it 'doesn\'t error if you try to sort on a nonexistent value' do
56
+ Animal.create({ name: 'a'})
57
+ expect { TestAnimalsQueue.query.sort(fake_value: 'yes').all }.to_not raise_error
58
+ end
59
+ end
60
+ end
data/spec/spec_helper.rb CHANGED
@@ -5,20 +5,13 @@ require 'rspec/autorun'
5
5
  require 'factory_girl'
6
6
  require File.expand_path('../../lib/elastic_queue', __FILE__)
7
7
  require 'support/elastic_queue_helper'
8
+ require 'support/database_helper'
8
9
 
9
- # Requires supporting ruby files with custom matchers and macros, etc,
10
- # in spec/support/ and its subdirectories.
11
- # Dir['spec/support/**/*.rb'].each { |f| require f }
12
- # require_relative '../../lib/elastic_queue'
10
+ DatabaseHelper.new({ :adapter => 'sqlite3', :database => './spec/eq_test.db' }).initialize_db
13
11
 
14
12
  RSpec.configure do |config|
15
- # Run specs in random order to surface order dependencies. If you find an
16
- # order dependency and want to debug it, you can fix the order by providing
17
- # the seed, which is printed after each run.
18
- # --seed 1234
19
13
  config.order = 'random'
20
14
  config.color_enabled = true
21
-
22
15
  config.include FactoryGirl::Syntax::Methods
23
16
  config.include(ElasticQueueHelper)
24
17
  end
@@ -0,0 +1,37 @@
1
+ require 'active_record'
2
+ require 'sqlite3'
3
+
4
+ class DatabaseHelper
5
+
6
+ def initialize(opts)
7
+ @opts = opts
8
+ end
9
+
10
+ def initialize_db
11
+ ActiveRecord::Base.establish_connection(@opts)
12
+ drop_tables
13
+ initialize_tables
14
+ end
15
+
16
+ def initialize_tables
17
+ ActiveRecord::Migration.class_eval do
18
+ create_table :animals do |t|
19
+ t.string :name
20
+ t.string :species
21
+ t.datetime :birthdate
22
+ t.boolean :dangerous
23
+ t.boolean :cute
24
+ t.text :description
25
+ end
26
+ end
27
+ end
28
+
29
+ def drop_tables
30
+ if ActiveRecord::Base.connection.table_exists? 'animals'
31
+ ActiveRecord::Migration.class_eval do
32
+ drop_table :animals
33
+ end
34
+ end
35
+ end
36
+
37
+ end
@@ -1,18 +1,30 @@
1
1
  module ElasticQueueHelper
2
+ ElasticQueue::OPTIONS = { elasticsearch_hosts: [{ host: 'localhost', port: 9200, protocol: 'http' }] }
2
3
 
3
4
  TEST_SEARCH_CLIENT = Elasticsearch::Client.new
4
-
5
+
5
6
  def test_search_client
6
7
  TEST_SEARCH_CLIENT
7
8
  end
8
9
 
9
- def refresh_index
10
+ def create_index(index_name)
11
+ delete_index(index_name)
12
+ test_search_client.indices.create index: index_name
13
+ end
14
+
15
+ def delete_index(index_name)
16
+ if test_search_client.indices.exists index: index_name
17
+ test_search_client.indices.delete index: index_name
18
+ end
19
+ end
20
+
21
+ def refresh_index(index_name)
10
22
  # forces the index to refresh itself so the search doesn't happen before the models are done being added to the index
11
- TEST_SEARCH_CLIENT.indices.refresh index: 'test_queue'
23
+ test_search_client.indices.refresh index: index_name
12
24
  end
13
25
 
14
- def query_all
26
+ def query_all(index_name)
15
27
  query = { 'query' => {'match_all' => {} } }.to_json
16
- TEST_SEARCH_CLIENT.search index: 'test_queue', body: query, size: 500
28
+ test_search_client.search index: index_name, body: query, size: 500
17
29
  end
18
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruth Thompson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-02 00:00:00.000000000 Z
12
+ date: 2014-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '3.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.0'
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: elasticsearch
30
44
  requirement: !ruby/object:Gem::Requirement
@@ -123,6 +137,20 @@ dependencies:
123
137
  - - "~>"
124
138
  - !ruby/object:Gem::Version
125
139
  version: '10.0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: debugger
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '1.6'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: '1.6'
126
154
  description: A library for storing and filtering documents on elastic search with
127
155
  a queue paradigm for retrieval.
128
156
  email:
@@ -153,9 +181,14 @@ files:
153
181
  - lib/elastic_queue/sorts.rb
154
182
  - lib/elastic_queue/version.rb
155
183
  - spec/factories.rb
156
- - spec/lib/elastic_queue_functional_spec.rb
157
- - spec/lib/elastic_queue_unit_spec.rb
184
+ - spec/lib/integration/elastic_queue_base_spec.rb
185
+ - spec/lib/integration/elastic_queue_filters_spec.rb
186
+ - spec/lib/integration/elastic_queue_persistence_spec.rb
187
+ - spec/lib/integration/elastic_queue_query_spec.rb
188
+ - spec/lib/integration/elastic_queue_queuable_spec.rb
189
+ - spec/lib/integration/elastic_queue_sorts_spec.rb
158
190
  - spec/spec_helper.rb
191
+ - spec/support/database_helper.rb
159
192
  - spec/support/elastic_queue_helper.rb
160
193
  homepage: https://github.com/RuthThompson/elastic_queue
161
194
  licenses:
@@ -183,7 +216,12 @@ specification_version: 4
183
216
  summary: A queueing system built on top of elasticsearch.
184
217
  test_files:
185
218
  - spec/factories.rb
186
- - spec/lib/elastic_queue_functional_spec.rb
187
- - spec/lib/elastic_queue_unit_spec.rb
219
+ - spec/lib/integration/elastic_queue_base_spec.rb
220
+ - spec/lib/integration/elastic_queue_filters_spec.rb
221
+ - spec/lib/integration/elastic_queue_persistence_spec.rb
222
+ - spec/lib/integration/elastic_queue_query_spec.rb
223
+ - spec/lib/integration/elastic_queue_queuable_spec.rb
224
+ - spec/lib/integration/elastic_queue_sorts_spec.rb
188
225
  - spec/spec_helper.rb
226
+ - spec/support/database_helper.rb
189
227
  - spec/support/elastic_queue_helper.rb