Skip to content

Commit da30909

Browse files
committed
Add tests
1 parent 1b1a203 commit da30909

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

spec/integration_spec.rb

+45-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
end
5757
create_table :namespaced_models do |t|
5858
t.string :name
59+
t.integer :another_private_value
5960
end
6061
create_table :uniq_users, :id => false do |t|
6162
t.string :name
@@ -181,6 +182,10 @@ def hex_changed?
181182
def will_save_change_to_short_name?
182183
false
183184
end
185+
186+
def will_save_change_to__tags?
187+
false
188+
end
184189
end
185190

186191
class DisabledBoolean < ActiveRecord::Base
@@ -216,20 +221,16 @@ def self.table_name_prefix
216221
class Namespaced::Model < ActiveRecord::Base
217222
include AlgoliaSearch
218223

219-
algoliasearch do
224+
algoliasearch :synchronous => true do
220225
attribute :customAttr do
221226
40 + another_private_value
222227
end
223228
attribute :myid do
224229
id
225230
end
231+
searchableAttributes ['customAttr']
226232
tags ['static_tag1', 'static_tag2']
227233
end
228-
229-
private
230-
def another_private_value
231-
2
232-
end
233234
end
234235

235236
class UniqUser < ActiveRecord::Base
@@ -583,6 +584,23 @@ class SerializedObject < ActiveRecord::Base
583584
color.delete
584585
end
585586

587+
it "should detect attribute changes even in a transaction" do
588+
color = Color.new :name => "dark-blue", :short_name => "blue"
589+
color.save
590+
591+
color.instance_variable_get("@algolia_must_reindex").should == nil
592+
Color.transaction do
593+
color.name = "red"
594+
color.save
595+
color.not_indexed = "strstr"
596+
color.save
597+
color.instance_variable_get("@algolia_must_reindex").should == true
598+
end
599+
color.instance_variable_get("@algolia_must_reindex").should == nil
600+
601+
color.delete
602+
end
603+
586604
it "should detect change with algolia_dirty? method" do
587605
ebook = Ebook.new :name => "My life", :author => "Myself", :premium => false, :released => true
588606

@@ -614,16 +632,36 @@ class SerializedObject < ActiveRecord::Base
614632
end
615633

616634
describe 'Namespaced::Model' do
635+
before(:all) do
636+
Namespaced::Model.index.clear_index!
637+
end
638+
617639
it "should have an index name without :: hierarchy" do
618640
Namespaced::Model.index_name.should == "Namespaced_Model"
619641
end
620642

621643
it "should use the block to determine attribute's value" do
622-
m = Namespaced::Model.new
644+
m = Namespaced::Model.new(another_private_value: 2)
623645
attributes = Namespaced::Model.algoliasearch_settings.get_attributes(m)
624646
attributes['customAttr'].should == 42
625647
attributes['myid'].should == m.id
626648
end
649+
650+
it "should always update when there is no custom _changed? function" do
651+
m = Namespaced::Model.create!(another_private_value: 2)
652+
results = Namespaced::Model.search(42)
653+
expect(results.size).to eq(1)
654+
expect(results[0].id).to eq(m.id)
655+
656+
m.update!(another_private_value: 5)
657+
658+
results = Namespaced::Model.search(42)
659+
expect(results.size).to eq(0)
660+
661+
results = Namespaced::Model.search(45)
662+
expect(results.size).to eq(1)
663+
expect(results[0].id).to eq(m.id)
664+
end
627665
end
628666

629667
describe 'UniqUsers' do

0 commit comments

Comments
 (0)