Skip to content

Commit

Permalink
Remove redundant Ebook model in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ellnix committed Feb 28, 2024
1 parent 50b350f commit fbe9e48
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 67 deletions.
50 changes: 50 additions & 0 deletions spec/integration/active_record/attributes_have_changed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'support/models/color'
require 'support/models/book'

describe 'When record attributes have changed' do
it 'detects attribute changes' do
color = Color.new name: 'dark-blue', short_name: 'blue'

expect(Color.ms_must_reindex?(color)).to be(true)
color.save
expect(Color.ms_must_reindex?(color)).to be(false)

color.hex = 123_456
expect(Color.ms_must_reindex?(color)).to be(false)

color.not_indexed = 'strstr'
expect(Color.ms_must_reindex?(color)).to be(false)
color.name = 'red'
expect(Color.ms_must_reindex?(color)).to be(true)
color.delete
end

it 'detects attribute changes even in a transaction' do
color = Color.new name: 'dark-blue', short_name: 'blue'
color.save
expect(color.instance_variable_get('@ms_must_reindex')).to be_nil
Color.transaction do
color.name = 'red'
color.save
color.not_indexed = 'strstr'
color.save
expect(color.instance_variable_get('@ms_must_reindex')).to be(true)
end
expect(color.instance_variable_get('@ms_must_reindex')).to be_nil
color.delete
end

it 'detects change with ms_dirty? method' do
book = Book.new name: 'My life', author: 'Myself', premium: false, released: true

allow(book).to receive(:ms_dirty?).and_return(true)
expect(Book.ms_must_reindex?(book)).to be(true)

allow(book).to receive(:ms_dirty?).and_return(false)
expect(Book.ms_must_reindex?(book)).to be(false)

allow(book).to receive(:ms_dirty?).and_return(true)
expect(Book.ms_must_reindex?(book)).to be(true)
end
end

44 changes: 0 additions & 44 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,6 @@
end
end

describe 'Attributes change detection' do
it 'detects attribute changes' do
color = Color.new name: 'dark-blue', short_name: 'blue'

expect(Color.ms_must_reindex?(color)).to be(true)
color.save
expect(Color.ms_must_reindex?(color)).to be(false)

color.hex = 123_456
expect(Color.ms_must_reindex?(color)).to be(false)

color.not_indexed = 'strstr'
expect(Color.ms_must_reindex?(color)).to be(false)
color.name = 'red'
expect(Color.ms_must_reindex?(color)).to be(true)
color.delete
end

it 'detects attribute changes even in a transaction' do
color = Color.new name: 'dark-blue', short_name: 'blue'
color.save
expect(color.instance_variable_get('@ms_must_reindex')).to be_nil
Color.transaction do
color.name = 'red'
color.save
color.not_indexed = 'strstr'
color.save
expect(color.instance_variable_get('@ms_must_reindex')).to be(true)
end
expect(color.instance_variable_get('@ms_must_reindex')).to be_nil
color.delete
end

it 'detects change with ms_dirty? method' do
ebook = Ebook.new name: 'My life', author: 'Myself', premium: false, released: true
expect(Ebook.ms_must_reindex?(ebook)).to be(true) # Because it's defined in ms_dirty? method
ebook.current_time = 10
ebook.published_at = 8
expect(Ebook.ms_must_reindex?(ebook)).to be(true)
ebook.published_at = 12
expect(Ebook.ms_must_reindex?(ebook)).to be(false)
end
end

describe 'Namespaced::Model' do
before(:all) do
Namespaced::Model.index.delete_all_documents!
Expand Down
23 changes: 0 additions & 23 deletions spec/support/active_record_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
create_table :mongo_documents do |t|
t.string :name
end
create_table :ebooks do |t|
t.string :name
t.string :author
t.boolean :premium
t.boolean :released
end
create_table :encoded_strings
end

Expand Down Expand Up @@ -53,23 +47,6 @@ def index!
end
end

class Ebook < ActiveRecord::Base
include MeiliSearch::Rails
attr_accessor :current_time, :published_at

meilisearch synchronous: true, index_uid: safe_index_uid('eBooks') do
searchable_attributes [:name]
end

def ms_dirty?
return true if published_at.nil? || current_time.nil?

# Consider dirty if published date is in the past
# This doesn't make so much business sense but it's easy to test.
published_at < current_time
end
end

class EncodedString < ActiveRecord::Base
include MeiliSearch::Rails

Expand Down

0 comments on commit fbe9e48

Please sign in to comment.