chewy 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +26 -4
  3. data/CHANGELOG.md +16 -0
  4. data/Gemfile +3 -1
  5. data/README.md +2 -10
  6. data/chewy.gemspec +0 -1
  7. data/gemfiles/Gemfile.rails-3.2.active_record +6 -0
  8. data/gemfiles/Gemfile.rails-3.2.active_record.kaminari +7 -0
  9. data/gemfiles/Gemfile.rails-3.2.active_record.will_paginate +7 -0
  10. data/gemfiles/Gemfile.rails-4.0.active_record +6 -0
  11. data/gemfiles/Gemfile.rails-4.0.active_record.kaminari +7 -0
  12. data/gemfiles/Gemfile.rails-4.0.active_record.will_paginate +7 -0
  13. data/gemfiles/Gemfile.rails-4.0.mongoid +6 -0
  14. data/gemfiles/Gemfile.rails-4.0.mongoid.kaminari +7 -0
  15. data/gemfiles/Gemfile.rails-4.0.mongoid.will_paginate +7 -0
  16. data/gemfiles/Gemfile.rails-4.1.active_record +6 -0
  17. data/gemfiles/Gemfile.rails-4.1.active_record.kaminari +7 -0
  18. data/gemfiles/Gemfile.rails-4.1.active_record.will_paginate +7 -0
  19. data/gemfiles/Gemfile.rails-4.1.mongoid +6 -0
  20. data/gemfiles/Gemfile.rails-4.1.mongoid.kaminari +7 -0
  21. data/gemfiles/Gemfile.rails-4.1.mongoid.will_paginate +7 -0
  22. data/gemfiles/Gemfile.rails-4.2.active_record +6 -0
  23. data/gemfiles/Gemfile.rails-4.2.active_record.kaminari +7 -0
  24. data/gemfiles/Gemfile.rails-4.2.active_record.will_paginate +7 -0
  25. data/gemfiles/Gemfile.rails-4.2.mongoid +6 -0
  26. data/gemfiles/Gemfile.rails-4.2.mongoid.kaminari +7 -0
  27. data/gemfiles/Gemfile.rails-4.2.mongoid.will_paginate +7 -0
  28. data/lib/chewy.rb +33 -5
  29. data/lib/chewy/config.rb +1 -0
  30. data/lib/chewy/index/search.rb +6 -3
  31. data/lib/chewy/query.rb +74 -1
  32. data/lib/chewy/query/compose.rb +4 -4
  33. data/lib/chewy/query/pagination.rb +5 -4
  34. data/lib/chewy/query/pagination/kaminari.rb +1 -1
  35. data/lib/chewy/query/pagination/will_paginate.rb +27 -0
  36. data/lib/chewy/type.rb +1 -0
  37. data/lib/chewy/type/adapter/active_record.rb +2 -2
  38. data/lib/chewy/type/adapter/mongoid.rb +147 -0
  39. data/lib/chewy/type/adapter/object.rb +1 -1
  40. data/lib/chewy/type/import.rb +1 -0
  41. data/lib/chewy/type/observe.rb +34 -6
  42. data/lib/chewy/version.rb +1 -1
  43. data/spec/chewy/config_spec.rb +17 -17
  44. data/spec/chewy/fields/base_spec.rb +62 -62
  45. data/spec/chewy/fields/root_spec.rb +5 -5
  46. data/spec/chewy/index/actions_spec.rb +127 -127
  47. data/spec/chewy/index/aliases_spec.rb +9 -9
  48. data/spec/chewy/index/search_spec.rb +4 -4
  49. data/spec/chewy/index/settings_spec.rb +33 -33
  50. data/spec/chewy/index_spec.rb +49 -49
  51. data/spec/chewy/query/criteria_spec.rb +173 -161
  52. data/spec/chewy/query/filters_spec.rb +76 -76
  53. data/spec/chewy/query/loading_spec.rb +54 -23
  54. data/spec/chewy/query/nodes/and_spec.rb +4 -4
  55. data/spec/chewy/query/nodes/bool_spec.rb +8 -8
  56. data/spec/chewy/query/nodes/equal_spec.rb +19 -19
  57. data/spec/chewy/query/nodes/exists_spec.rb +6 -6
  58. data/spec/chewy/query/nodes/has_child_spec.rb +25 -25
  59. data/spec/chewy/query/nodes/has_parent_spec.rb +25 -25
  60. data/spec/chewy/query/nodes/match_all_spec.rb +1 -1
  61. data/spec/chewy/query/nodes/missing_spec.rb +4 -4
  62. data/spec/chewy/query/nodes/not_spec.rb +4 -4
  63. data/spec/chewy/query/nodes/or_spec.rb +4 -4
  64. data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
  65. data/spec/chewy/query/nodes/query_spec.rb +2 -2
  66. data/spec/chewy/query/nodes/range_spec.rb +18 -18
  67. data/spec/chewy/query/nodes/raw_spec.rb +1 -1
  68. data/spec/chewy/query/nodes/regexp_spec.rb +18 -18
  69. data/spec/chewy/query/nodes/script_spec.rb +4 -4
  70. data/spec/chewy/query/pagination/kaminari_spec.rb +41 -39
  71. data/spec/chewy/query/pagination/will_paginage_spec.rb +60 -0
  72. data/spec/chewy/query/pagination_spec.rb +8 -7
  73. data/spec/chewy/query_spec.rb +166 -167
  74. data/spec/chewy/rspec/update_index_spec.rb +1 -1
  75. data/spec/chewy/runtime/version_spec.rb +30 -30
  76. data/spec/chewy/runtime_spec.rb +3 -3
  77. data/spec/chewy/type/actions_spec.rb +3 -3
  78. data/spec/chewy/type/adapter/active_record_spec.rb +143 -143
  79. data/spec/chewy/type/adapter/mongoid_spec.rb +219 -0
  80. data/spec/chewy/type/adapter/object_spec.rb +39 -39
  81. data/spec/chewy/type/import_spec.rb +67 -37
  82. data/spec/chewy/type/mapping_spec.rb +12 -12
  83. data/spec/chewy/type/observe_spec.rb +5 -6
  84. data/spec/chewy/type/wrapper_spec.rb +12 -12
  85. data/spec/chewy_spec.rb +26 -28
  86. data/spec/spec_helper.rb +19 -31
  87. data/spec/support/active_record.rb +52 -0
  88. data/spec/support/class_helpers.rb +0 -4
  89. data/spec/support/mongoid.rb +87 -0
  90. metadata +33 -18
  91. data/gemfiles/Gemfile.rails-3.2 +0 -15
  92. data/gemfiles/Gemfile.rails-4.0 +0 -15
@@ -20,19 +20,19 @@ describe Chewy::Type::Mapping do
20
20
  end
21
21
 
22
22
  describe '.field' do
23
- specify { product.root_object.nested.keys.should =~ [:name, :surname, :title, :price] }
24
- specify { product.root_object.nested.values.should satisfy { |v| v.all? { |f| f.is_a? Chewy::Fields::Base } } }
23
+ specify { expect(product.root_object.nested.keys).to match_array([:name, :surname, :title, :price]) }
24
+ specify { expect(product.root_object.nested.values).to satisfy { |v| v.all? { |f| f.is_a? Chewy::Fields::Base } } }
25
25
 
26
- specify { product.root_object.nested[:title].nested.keys.should == [:subfield1] }
27
- specify { product.root_object.nested[:title].nested[:subfield1].should be_a Chewy::Fields::Base }
26
+ specify { expect(product.root_object.nested[:title].nested.keys).to eq([:subfield1]) }
27
+ specify { expect(product.root_object.nested[:title].nested[:subfield1]).to be_a Chewy::Fields::Base }
28
28
 
29
- specify { product.root_object.nested[:price].nested.keys.should == [:subfield2] }
30
- specify { product.root_object.nested[:price].nested[:subfield2].should be_a Chewy::Fields::Base }
29
+ specify { expect(product.root_object.nested[:price].nested.keys).to eq([:subfield2]) }
30
+ specify { expect(product.root_object.nested[:price].nested[:subfield2]).to be_a Chewy::Fields::Base }
31
31
  end
32
32
 
33
33
  describe '.mappings_hash' do
34
- specify { Class.new(Chewy::Type).mappings_hash.should == {} }
35
- specify { product.mappings_hash.should == product.root_object.mappings_hash }
34
+ specify { expect(Class.new(Chewy::Type).mappings_hash).to eq({}) }
35
+ specify { expect(product.mappings_hash).to eq(product.root_object.mappings_hash) }
36
36
 
37
37
  context 'parent-child relationship' do
38
38
  context do
@@ -46,7 +46,7 @@ describe Chewy::Type::Mapping do
46
46
  end
47
47
  end
48
48
 
49
- specify { product.mappings_hash[:product][:_parent].should == { type: 'project' } }
49
+ specify { expect(product.mappings_hash[:product][:_parent]).to eq({ type: 'project' }) }
50
50
  end
51
51
 
52
52
  context do
@@ -60,7 +60,7 @@ describe Chewy::Type::Mapping do
60
60
  end
61
61
  end
62
62
 
63
- specify { product.mappings_hash[:product][:_parent].should == { 'type' => 'project' } }
63
+ specify { expect(product.mappings_hash[:product][:_parent]).to eq({ 'type' => 'project' }) }
64
64
  end
65
65
  end
66
66
  end
@@ -76,7 +76,7 @@ describe Chewy::Type::Mapping do
76
76
  end
77
77
  end
78
78
 
79
- specify { product.root_object.nested[:title].nested.keys.should == [:subfield1] }
80
- specify { product.root_object.nested[:title].nested[:subfield1].should be_a Chewy::Fields::Base }
79
+ specify { expect(product.root_object.nested[:title].nested.keys).to eq([:subfield1]) }
80
+ specify { expect(product.root_object.nested[:title].nested[:subfield1]).to be_a Chewy::Fields::Base }
81
81
  end
82
82
  end
@@ -10,8 +10,6 @@ describe Chewy::Type::Import do
10
10
 
11
11
  let(:backreferenced) { 3.times.map { |i| double(id: i) } }
12
12
 
13
- specify { expect { DummiesIndex.dummy.update_index(backreferenced, urgent: true) }
14
- .to update_index('dummies#dummy', atomic: false).and_reindex(backreferenced) }
15
13
  specify { expect { DummiesIndex.dummy.update_index(backreferenced) }
16
14
  .not_to update_index('dummies#dummy', atomic: false) }
17
15
  specify { expect { DummiesIndex.dummy.update_index([]) }
@@ -20,7 +18,7 @@ describe Chewy::Type::Import do
20
18
  .not_to update_index('dummies#dummy') }
21
19
  end
22
20
 
23
- context 'integration' do
21
+ context 'integration', :orm do
24
22
  before do
25
23
  stub_model(:city) do
26
24
  belongs_to :country
@@ -31,7 +29,7 @@ describe Chewy::Type::Import do
31
29
  stub_model(:country) do
32
30
  has_many :cities
33
31
  update_index('cities#city') { cities }
34
- update_index 'countries#country', :self, urgent: true
32
+ update_index 'countries#country', :self
35
33
  end
36
34
 
37
35
  stub_index(:cities) do
@@ -43,8 +41,8 @@ describe Chewy::Type::Import do
43
41
  end
44
42
  end
45
43
 
46
- let(:city) { City.create!(country: Country.create!) }
47
- let(:country) { Country.create!(cities: 2.times.map { City.create! }) }
44
+ let(:city) { City.create!(id: 1, country: Country.create!(id: 1)) }
45
+ let(:country) { Country.create!(id: 1, cities: 2.times.map { |i| City.create!(id: i) }) }
48
46
 
49
47
  specify { expect { city.save! }.not_to update_index('cities#city', atomic: false) }
50
48
  specify { expect { country.save! }.to update_index('countries#country').and_reindex(country) }
@@ -71,6 +69,7 @@ describe Chewy::Type::Import do
71
69
  end
72
70
 
73
71
  context do
72
+ before { allow(Chewy).to receive_messages(urgent_update: true) }
74
73
  let(:other_country) { Country.create! }
75
74
 
76
75
  specify do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Chewy::Type::Wrapper do
4
4
  before do
5
- stub_model(:city)
5
+ stub_class(:city)
6
6
  stub_index(:cities) do
7
7
  define_type City
8
8
  end
@@ -12,22 +12,22 @@ describe Chewy::Type::Wrapper do
12
12
 
13
13
  subject { city_type.new(name: 'Martin', age: 42) }
14
14
 
15
- it { should respond_to :name }
16
- it { should respond_to :age }
15
+ it { is_expected.to respond_to :name }
16
+ it { is_expected.to respond_to :age }
17
17
  its(:name) { should == 'Martin' }
18
18
  its(:age) { should == 42 }
19
19
 
20
20
  describe '#==' do
21
- specify { city_type.new(id: 42).should == city_type.new(id: 42) }
22
- specify { city_type.new(id: 42, age: 55).should == city_type.new(id: 42, age: 54) }
23
- specify { city_type.new(id: 42).should_not == city_type.new(id: 43) }
24
- specify { city_type.new(id: 42, age: 55).should_not == city_type.new(id: 43, age: 55) }
25
- specify { city_type.new(age: 55).should == city_type.new(age: 55) }
26
- specify { city_type.new(age: 55).should_not == city_type.new(age: 54) }
21
+ specify { expect(city_type.new(id: 42)).to eq(city_type.new(id: 42)) }
22
+ specify { expect(city_type.new(id: 42, age: 55)).to eq(city_type.new(id: 42, age: 54)) }
23
+ specify { expect(city_type.new(id: 42)).not_to eq(city_type.new(id: 43)) }
24
+ specify { expect(city_type.new(id: 42, age: 55)).not_to eq(city_type.new(id: 43, age: 55)) }
25
+ specify { expect(city_type.new(age: 55)).to eq(city_type.new(age: 55)) }
26
+ specify { expect(city_type.new(age: 55)).not_to eq(city_type.new(age: 54)) }
27
27
 
28
- specify { city_type.new(id: '42').should == City.new.tap { |m| m.stub(id: 42) } }
29
- specify { city_type.new(id: 42).should_not == City.new.tap { |m| m.stub(id: 43) } }
28
+ specify { expect(city_type.new(id: '42')).to eq(City.new.tap { |m| allow(m).to receive_messages(id: 42) }) }
29
+ specify { expect(city_type.new(id: 42)).not_to eq(City.new.tap { |m| allow(m).to receive_messages(id: 43) }) }
30
30
 
31
- specify { city_type.new(id: 42).should_not == Class.new }
31
+ specify { expect(city_type.new(id: 42)).not_to eq(Class.new) }
32
32
  end
33
33
  end
@@ -25,11 +25,11 @@ describe Chewy do
25
25
  specify { expect { described_class.derive_type('developers#borogoves') }.to raise_error Chewy::UnderivableType, /DevelopersIndex.*borogoves/ }
26
26
  specify { expect { described_class.derive_type('namespace/autocomplete') }.to raise_error Chewy::UnderivableType, /AutocompleteIndex.*namespace\/autocomplete#type_name/ }
27
27
 
28
- specify { described_class.derive_type(DevelopersIndex.developer).should == DevelopersIndex.developer }
29
- specify { described_class.derive_type('developers').should == DevelopersIndex.developer }
30
- specify { described_class.derive_type('developers#developer').should == DevelopersIndex.developer }
31
- specify { described_class.derive_type('namespace/autocomplete#developer').should == Namespace::AutocompleteIndex.developer }
32
- specify { described_class.derive_type('namespace/autocomplete#company').should == Namespace::AutocompleteIndex.company }
28
+ specify { expect(described_class.derive_type(DevelopersIndex.developer)).to eq(DevelopersIndex.developer) }
29
+ specify { expect(described_class.derive_type('developers')).to eq(DevelopersIndex.developer) }
30
+ specify { expect(described_class.derive_type('developers#developer')).to eq(DevelopersIndex.developer) }
31
+ specify { expect(described_class.derive_type('namespace/autocomplete#developer')).to eq(Namespace::AutocompleteIndex.developer) }
32
+ specify { expect(described_class.derive_type('namespace/autocomplete#company')).to eq(Namespace::AutocompleteIndex.company) }
33
33
  end
34
34
 
35
35
  describe '.create_type' do
@@ -38,55 +38,55 @@ describe Chewy do
38
38
  context 'Symbol' do
39
39
  subject { described_class.create_type(CitiesIndex, :city) }
40
40
 
41
- it { should be_a Class }
42
- it { should be < Chewy::Type }
41
+ it { is_expected.to be_a Class }
42
+ it { is_expected.to be < Chewy::Type }
43
43
  its(:name) { should == 'CitiesIndex::City' }
44
44
  its(:index) { should == CitiesIndex }
45
45
  its(:type_name) { should == 'city' }
46
46
  end
47
47
 
48
- context 'ActiveRecord model' do
49
- before { stub_model(:city) }
48
+ context 'simple model' do
49
+ before { stub_class(:city) }
50
50
  subject { described_class.create_type(CitiesIndex, City) }
51
51
 
52
- it { should be_a Class }
53
- it { should be < Chewy::Type }
52
+ it { is_expected.to be_a Class }
53
+ it { is_expected.to be < Chewy::Type }
54
54
  its(:name) { should == 'CitiesIndex::City' }
55
55
  its(:index) { should == CitiesIndex }
56
56
  its(:type_name) { should == 'city' }
57
57
  end
58
58
 
59
- context 'ActiveRecord scope' do
59
+ context 'model scope', :orm do
60
60
  before { stub_model(:city) }
61
61
  subject { described_class.create_type(CitiesIndex, City.includes(:country)) }
62
62
 
63
- it { should be_a Class }
64
- it { should be < Chewy::Type }
63
+ it { is_expected.to be_a Class }
64
+ it { is_expected.to be < Chewy::Type }
65
65
  its(:name) { should == 'CitiesIndex::City' }
66
66
  its(:index) { should == CitiesIndex }
67
67
  its(:type_name) { should == 'city' }
68
68
  end
69
69
 
70
70
  context 'Namespaced index' do
71
- before { stub_model(:city) }
71
+ before { stub_class(:city) }
72
72
  before { stub_index('namespace/cities') }
73
73
 
74
74
  subject { described_class.create_type(Namespace::CitiesIndex, City) }
75
75
 
76
- it { should be_a Class }
77
- it { should be < Chewy::Type }
76
+ it { is_expected.to be_a Class }
77
+ it { is_expected.to be < Chewy::Type }
78
78
  its(:name) { should == 'Namespace::CitiesIndex::City' }
79
79
  its(:index) { should == Namespace::CitiesIndex }
80
80
  its(:type_name) { should == 'city' }
81
81
  end
82
82
 
83
83
  context 'Namespaced model' do
84
- before { stub_model('namespace/city') }
84
+ before { stub_class('namespace/city') }
85
85
 
86
86
  subject { described_class.create_type(CitiesIndex, Namespace::City) }
87
87
 
88
- it { should be_a Class }
89
- it { should be < Chewy::Type }
88
+ it { is_expected.to be_a Class }
89
+ it { is_expected.to be < Chewy::Type }
90
90
  its(:name) { should == 'CitiesIndex::City' }
91
91
  its(:index) { should == CitiesIndex }
92
92
  its(:type_name) { should == 'city' }
@@ -94,22 +94,20 @@ describe Chewy do
94
94
  end
95
95
 
96
96
  describe '.massacre' do
97
- before do
98
- Chewy.client.indices.delete index: '*'
99
- end
97
+ before { Chewy.massacre }
100
98
 
101
99
  before do
102
- Chewy.configuration = Chewy.configuration.merge(prefix: 'prefix1')
100
+ allow(Chewy).to receive_messages(configuration: Chewy.configuration.merge(prefix: 'prefix1'))
103
101
  stub_index(:admins).create!
104
- Chewy.configuration = Chewy.configuration.merge(prefix: 'prefix2')
102
+ allow(Chewy).to receive_messages(configuration: Chewy.configuration.merge(prefix: 'prefix2'))
105
103
  stub_index(:developers).create!
106
104
  stub_index(:companies).create!
107
105
 
108
106
  Chewy.massacre
109
107
  end
110
108
 
111
- specify { AdminsIndex.exists?.should == true }
112
- specify { DevelopersIndex.exists?.should == false }
113
- specify { CompaniesIndex.exists?.should == false }
109
+ specify { expect(AdminsIndex.exists?).to eq(true) }
110
+ specify { expect(DevelopersIndex.exists?).to eq(false) }
111
+ specify { expect(CompaniesIndex.exists?).to eq(false) }
114
112
  end
115
113
  end
@@ -1,33 +1,22 @@
1
1
  require 'bundler'
2
+
2
3
  Bundler.require
4
+
5
+ begin
6
+ require 'active_record'
7
+ rescue LoadError
8
+ end
9
+
3
10
  require 'rspec/its'
4
11
  require 'rspec/collection_matchers'
5
- require 'active_record'
6
- require 'database_cleaner'
12
+
13
+ Kaminari::Hooks.init if defined?(::Kaminari)
7
14
 
8
15
  require 'support/fail_helpers'
9
16
  require 'support/class_helpers'
10
17
 
11
18
  require 'chewy/rspec'
12
19
 
13
- Kaminari::Hooks.init
14
-
15
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
16
- ActiveRecord::Base.logger = Logger.new('/dev/null')
17
-
18
- ActiveRecord::Schema.define do
19
- create_table :countries do |t|
20
- t.column :name, :string
21
- t.column :rating, :integer
22
- end
23
-
24
- create_table :cities do |t|
25
- t.column :country_id, :integer
26
- t.column :name, :string
27
- t.column :rating, :integer
28
- end
29
- end
30
-
31
20
  Chewy.configuration = {
32
21
  host: 'localhost:9250',
33
22
  wait_for_status: 'green',
@@ -42,17 +31,16 @@ RSpec.configure do |config|
42
31
 
43
32
  config.include FailHelpers
44
33
  config.include ClassHelpers
34
+ end
45
35
 
46
- config.before(:suite) do
47
- DatabaseCleaner.clean_with :truncation
48
- DatabaseCleaner.strategy = :transaction
49
- end
50
-
51
- config.before do
52
- DatabaseCleaner.start
53
- end
54
-
55
- config.after do
56
- DatabaseCleaner.clean
36
+ if defined?(::ActiveRecord)
37
+ require 'support/active_record'
38
+ elsif defined?(::Mongoid)
39
+ require 'support/mongoid'
40
+ else
41
+ RSpec.configure do |config|
42
+ config.filter_run_excluding :orm
43
+ config.filter_run_excluding :mongoid
44
+ config.filter_run_excluding :active_record
57
45
  end
58
46
  end
@@ -0,0 +1,52 @@
1
+ require 'database_cleaner'
2
+
3
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
4
+ ActiveRecord::Base.logger = Logger.new('/dev/null')
5
+
6
+ ActiveRecord::Schema.define do
7
+ create_table :countries do |t|
8
+ t.column :name, :string
9
+ t.column :rating, :integer
10
+ end
11
+
12
+ create_table :cities do |t|
13
+ t.column :country_id, :integer
14
+ t.column :name, :string
15
+ t.column :rating, :integer
16
+ end
17
+ end
18
+
19
+ module ActiveRecordClassHelpers
20
+ extend ActiveSupport::Concern
21
+
22
+ def stub_model name, superclass = nil, &block
23
+ stub_class(name, superclass || ActiveRecord::Base, &block)
24
+ end
25
+
26
+ def active_record?
27
+ true
28
+ end
29
+
30
+ def mongoid?
31
+ false
32
+ end
33
+ end
34
+
35
+ RSpec.configure do |config|
36
+ config.include ActiveRecordClassHelpers
37
+
38
+ config.filter_run_excluding :mongoid
39
+
40
+ config.before(:suite) do
41
+ DatabaseCleaner.clean_with :truncation
42
+ DatabaseCleaner.strategy = :transaction
43
+ end
44
+
45
+ config.before do
46
+ DatabaseCleaner.start
47
+ end
48
+
49
+ config.after do
50
+ DatabaseCleaner.clean
51
+ end
52
+ end
@@ -1,10 +1,6 @@
1
1
  module ClassHelpers
2
2
  extend ActiveSupport::Concern
3
3
 
4
- def stub_model name, superclass = nil, &block
5
- stub_class(name, superclass || ActiveRecord::Base, &block)
6
- end
7
-
8
4
  def stub_index name, superclass = nil, &block
9
5
  stub_class("#{name.to_s.camelize}Index", superclass || Chewy::Index) { index_name = name }
10
6
  .tap { |i| i.class_eval(&block) if block }
@@ -0,0 +1,87 @@
1
+ require 'database_cleaner'
2
+
3
+ CONFIG = {
4
+ sessions: {
5
+ default: {
6
+ uri: 'mongodb://127.0.0.1:27017/chewy_mongoid_test'
7
+ }
8
+ }
9
+ }
10
+
11
+ Mongoid.configure do |config|
12
+ config.load_configuration(CONFIG)
13
+ end
14
+
15
+ Mongoid.logger = Logger.new('/dev/null')
16
+
17
+ module MongoidClassHelpers
18
+ extend ActiveSupport::Concern
19
+
20
+ def stub_model name, superclass = nil, &block
21
+ model = name.to_s.camelize.constantize rescue nil
22
+
23
+ if model
24
+ model.class_eval(&block) if block
25
+ model
26
+ else
27
+ klass = if superclass && superclass.ancestors.include?(Mongoid::Document)
28
+ superclass
29
+ else
30
+ Class.new(*([superclass].compact)) do
31
+ include Mongoid::Document
32
+ store_in collection: name.to_s.tableize
33
+ end
34
+ end
35
+
36
+ stub_class(name, klass, &block)
37
+ end
38
+ end
39
+
40
+ def active_record?
41
+ false
42
+ end
43
+
44
+ def mongoid?
45
+ true
46
+ end
47
+ end
48
+
49
+ RSpec.configure do |config|
50
+ config.include MongoidClassHelpers
51
+
52
+ config.filter_run_excluding :active_record
53
+
54
+ config.before(:suite) do
55
+ DatabaseCleaner.clean_with :truncation
56
+ DatabaseCleaner.strategy = :truncation
57
+ end
58
+
59
+ config.before do
60
+ Object.send(:remove_const, :City) if defined? City
61
+ Object.send(:remove_const, :Country) if defined? Country
62
+
63
+ class Country
64
+ include Mongoid::Document
65
+
66
+ field :name, type: String
67
+ field :rating, type: Integer
68
+
69
+ has_many :cities, order: :id.asc
70
+ end
71
+
72
+ class City
73
+ include Mongoid::Document
74
+
75
+ field :name, type: String
76
+ field :rating, type: Integer
77
+
78
+ belongs_to :country
79
+ end
80
+
81
+ DatabaseCleaner.start
82
+ end
83
+
84
+ config.after do
85
+ DatabaseCleaner.clean
86
+ end
87
+ end