2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-15 23:46:57 +08:00
|
|
|
require "rails_helper"
|
|
|
|
|
|
|
|
describe Searchable do
|
|
|
|
context "has search data" do
|
|
|
|
before do
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("create temporary table searchable_records(id SERIAL primary key)")
|
|
|
|
DB.exec("create temporary table searchable_record_search_data(searchable_record_id int primary key, search_data tsvector, raw_data text, locale text)")
|
2017-08-15 23:46:57 +08:00
|
|
|
|
|
|
|
class SearchableRecord < ActiveRecord::Base
|
|
|
|
include Searchable
|
|
|
|
end
|
|
|
|
|
|
|
|
class SearchableRecordSearchData < ActiveRecord::Base
|
|
|
|
self.primary_key = 'searchable_record_id'
|
|
|
|
belongs_to :test_item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("drop table searchable_records")
|
|
|
|
DB.exec("drop table searchable_record_search_data")
|
2017-08-15 23:46:57 +08:00
|
|
|
|
2019-04-30 14:58:18 +08:00
|
|
|
# this weakref in the descendant tracker should clean up the two tests
|
|
|
|
# if this becomes an issue we can revisit (watch out for erratic tests)
|
|
|
|
Object.send(:remove_const, :SearchableRecord)
|
|
|
|
Object.send(:remove_const, :SearchableRecordSearchData)
|
2017-08-15 23:46:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:item) { SearchableRecord.create! }
|
|
|
|
|
|
|
|
it 'can build the data' do
|
|
|
|
expect(item.build_searchable_record_search_data).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can save the data' do
|
|
|
|
item.build_searchable_record_search_data(
|
|
|
|
search_data: '',
|
|
|
|
raw_data: 'a',
|
|
|
|
locale: 'en')
|
|
|
|
item.save
|
|
|
|
|
|
|
|
loaded = SearchableRecord.find(item.id)
|
|
|
|
expect(loaded.searchable_record_search_data.raw_data).to eq 'a'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroy the search data when the item is deprived' do
|
|
|
|
item.build_searchable_record_search_data(
|
|
|
|
search_data: '',
|
|
|
|
raw_data: 'a',
|
|
|
|
locale: 'en')
|
|
|
|
item.save
|
|
|
|
item_id = item.id
|
|
|
|
item.destroy
|
|
|
|
expect(SearchableRecordSearchData.find_by(searchable_record_id: item_id)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|