2020-09-28 11:23:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
require "rails_helper"
|
2020-09-28 11:23:53 -04:00
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
describe "fix query ids rake task" do
|
2020-09-28 11:23:53 -04:00
|
|
|
before do
|
2023-02-23 12:10:46 -05:00
|
|
|
Rake::Task.clear
|
2020-09-28 11:23:53 -04:00
|
|
|
Discourse::Application.load_tasks
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
let(:query_name) { "Awesome query" }
|
2020-09-28 11:23:53 -04:00
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "fixes the ID of the query if they share the same name" do
|
2020-09-28 11:23:53 -04:00
|
|
|
original_query_id = 4
|
|
|
|
create_plugin_store_row(query_name, original_query_id)
|
|
|
|
create_query(query_name)
|
|
|
|
|
|
|
|
run_task
|
|
|
|
|
|
|
|
expect(find(query_name).id).to eq(original_query_id)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "only fixes queries with unique name" do
|
2020-09-28 11:23:53 -04:00
|
|
|
original_query_id = 4
|
|
|
|
create_plugin_store_row(query_name, original_query_id)
|
|
|
|
create_query(query_name)
|
|
|
|
create_query(query_name)
|
|
|
|
|
|
|
|
run_task
|
|
|
|
|
|
|
|
expect(find(query_name).id).not_to eq(original_query_id)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "skips queries that already have the same ID" do
|
2020-09-28 11:23:53 -04:00
|
|
|
db_query = create_query(query_name)
|
|
|
|
last_updated_at = db_query.updated_at
|
|
|
|
create_plugin_store_row(query_name, db_query.id)
|
|
|
|
|
|
|
|
run_task
|
|
|
|
|
|
|
|
expect(find(query_name).updated_at).to eq_time(last_updated_at)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "keeps queries the rest of the queries" do
|
2020-09-28 11:23:53 -04:00
|
|
|
original_query_id = 4
|
2022-12-29 07:31:29 -05:00
|
|
|
different_query_name = "Another query"
|
2020-09-28 11:23:53 -04:00
|
|
|
create_plugin_store_row(query_name, original_query_id)
|
|
|
|
create_query(query_name)
|
|
|
|
create_query(different_query_name)
|
|
|
|
|
|
|
|
run_task
|
|
|
|
|
|
|
|
expect(find(different_query_name)).not_to be_nil
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "works even if they are additional conflicts" do
|
|
|
|
different_query_name = "Another query"
|
2020-09-28 11:23:53 -04:00
|
|
|
additional_conflict = create_query(different_query_name)
|
|
|
|
create_query(query_name)
|
|
|
|
create_plugin_store_row(query_name, additional_conflict.id)
|
|
|
|
|
|
|
|
run_task
|
|
|
|
|
|
|
|
expect(find(different_query_name).id).not_to eq(additional_conflict.id)
|
|
|
|
expect(find(query_name).id).to eq(additional_conflict.id)
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
describe "query groups" do
|
2020-09-28 11:23:53 -04:00
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
it "fixes the query group's query_id" do
|
|
|
|
original_query_id = 4
|
|
|
|
create_query(query_name, [group.id])
|
|
|
|
create_plugin_store_row(query_name, original_query_id, [group.id])
|
|
|
|
|
|
|
|
run_task
|
|
|
|
|
|
|
|
expect(find_query_group(original_query_id)).not_to be_nil
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "works with additional conflicts" do
|
|
|
|
different_query_name = "Another query"
|
2020-09-28 11:23:53 -04:00
|
|
|
additional_conflict = create_query(different_query_name, [group.id])
|
|
|
|
create_query(query_name, [group.id])
|
|
|
|
create_plugin_store_row(query_name, additional_conflict.id, [group.id])
|
|
|
|
|
|
|
|
run_task
|
|
|
|
|
|
|
|
conflict = find(different_query_name).query_groups.first
|
|
|
|
fixed = find_query_group(additional_conflict.id)
|
|
|
|
|
|
|
|
expect(conflict.query_id).not_to eq(additional_conflict.id)
|
|
|
|
expect(fixed.query_id).to eq(additional_conflict.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_query_group(id)
|
2023-03-22 17:29:08 -04:00
|
|
|
DiscourseDataExplorer::QueryGroup.find_by(query_id: id)
|
2020-09-28 11:23:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
it "changes the serial sequence for future queries" do
|
2020-09-28 11:23:53 -04:00
|
|
|
original_query_id = 4
|
|
|
|
create_plugin_store_row(query_name, original_query_id)
|
|
|
|
create_query(query_name)
|
|
|
|
|
|
|
|
run_task
|
|
|
|
post_fix_query = create_query(query_name)
|
|
|
|
|
|
|
|
expect(post_fix_query.id).to eq(original_query_id + 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_task
|
2022-12-29 07:31:29 -05:00
|
|
|
Rake::Task["data_explorer:fix_query_ids"].invoke
|
2020-09-28 11:23:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_plugin_store_row(name, id, group_ids = [])
|
|
|
|
key = "q:#{id}"
|
|
|
|
|
|
|
|
PluginStore.set(
|
2023-03-22 17:29:08 -04:00
|
|
|
DiscourseDataExplorer::PLUGIN_NAME,
|
2020-09-28 11:23:53 -04:00
|
|
|
key,
|
2022-12-29 07:31:29 -05:00
|
|
|
attributes(name).merge(group_ids: group_ids, id: id),
|
2020-09-28 11:23:53 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_query(name, group_ids = [])
|
2023-03-22 17:29:08 -04:00
|
|
|
DiscourseDataExplorer::Query
|
2022-12-29 07:31:29 -05:00
|
|
|
.create!(attributes(name))
|
|
|
|
.tap { |query| group_ids.each { |group_id| query.query_groups.create!(group_id: group_id) } }
|
2020-09-28 11:23:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def attributes(name)
|
|
|
|
{
|
2023-03-22 17:29:08 -04:00
|
|
|
id:
|
|
|
|
DiscourseDataExplorer::Query.count == 0 ? 5 : DiscourseDataExplorer::Query.maximum(:id) + 1,
|
2020-09-28 11:23:53 -04:00
|
|
|
name: name,
|
2022-12-29 07:31:29 -05:00
|
|
|
description: "A Query",
|
2020-09-28 11:23:53 -04:00
|
|
|
sql: "SELECT 1",
|
|
|
|
created_at: 3.hours.ago,
|
|
|
|
last_run_at: 1.hour.ago,
|
2022-12-29 07:31:29 -05:00
|
|
|
hidden: false,
|
2020-09-28 11:23:53 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(name)
|
2023-03-22 17:29:08 -04:00
|
|
|
DiscourseDataExplorer::Query.find_by(name: name)
|
2020-09-28 11:23:53 -04:00
|
|
|
end
|
|
|
|
end
|