mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Fix: unlinked topic search model (#5044)
This commit is contained in:
47
spec/components/concern/has_search_data_spec.rb
Normal file
47
spec/components/concern/has_search_data_spec.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe HasSearchData do
|
||||
context "belongs to its model" do
|
||||
before do
|
||||
Topic.exec_sql("create temporary table model_items(id SERIAL primary key)")
|
||||
Topic.exec_sql("create temporary table model_item_search_data(model_item_id int primary key, search_data tsvector, raw_data text, locale text)")
|
||||
|
||||
class ModelItem < ActiveRecord::Base
|
||||
has_one :model_item_search_data, dependent: :destroy
|
||||
end
|
||||
|
||||
class ModelItemSearchData < ActiveRecord::Base
|
||||
include HasSearchData
|
||||
end
|
||||
end
|
||||
|
||||
after do
|
||||
Topic.exec_sql("drop table model_items")
|
||||
Topic.exec_sql("drop table model_item_search_data")
|
||||
|
||||
# import is making my life hard, we need to nuke this out of orbit
|
||||
des = ActiveSupport::DescendantsTracker.class_variable_get :@@direct_descendants
|
||||
des[ActiveRecord::Base].delete(ModelItem)
|
||||
des[ActiveRecord::Base].delete(ModelItemSearchData)
|
||||
end
|
||||
|
||||
let(:item) do
|
||||
item = ModelItem.create!
|
||||
item.create_model_item_search_data!(
|
||||
model_item_id: item.id,
|
||||
search_data: 'a',
|
||||
raw_data: 'a',
|
||||
locale: 'en')
|
||||
item
|
||||
end
|
||||
|
||||
it 'sets its primary key into asscoiated model' do
|
||||
expect(ModelItemSearchData.primary_key).to eq 'model_item_id'
|
||||
end
|
||||
|
||||
it 'can access the model' do
|
||||
record_id = item.id
|
||||
expect(ModelItemSearchData.find_by(model_item_id: record_id).model_item_id).to eq record_id
|
||||
end
|
||||
end
|
||||
end
|
||||
57
spec/components/concern/searchable_spec.rb
Normal file
57
spec/components/concern/searchable_spec.rb
Normal file
@@ -0,0 +1,57 @@
|
||||
require "rails_helper"
|
||||
|
||||
describe Searchable do
|
||||
context "has search data" do
|
||||
before do
|
||||
Topic.exec_sql("create temporary table searchable_records(id SERIAL primary key)")
|
||||
Topic.exec_sql("create temporary table searchable_record_search_data(searchable_record_id int primary key, search_data tsvector, raw_data text, locale text)")
|
||||
|
||||
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
|
||||
Topic.exec_sql("drop table searchable_records")
|
||||
Topic.exec_sql("drop table searchable_record_search_data")
|
||||
|
||||
# import is making my life hard, we need to nuke this out of orbit
|
||||
des = ActiveSupport::DescendantsTracker.class_variable_get :@@direct_descendants
|
||||
des[ActiveRecord::Base].delete(SearchableRecord)
|
||||
des[ActiveRecord::Base].delete(SearchableRecordSearchData)
|
||||
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
|
||||
Reference in New Issue
Block a user