DEV: Clean up some specs code (#161)

This commit is contained in:
Jarek Radosz 2022-02-01 21:38:24 +01:00 committed by GitHub
parent 4813615488
commit bac43572cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 30 deletions

View File

@ -3,7 +3,6 @@
require 'rails_helper' require 'rails_helper'
describe Guardian do describe Guardian do
before { SiteSetting.data_explorer_enabled = true } before { SiteSetting.data_explorer_enabled = true }
def make_query(group_ids = []) def make_query(group_ids = [])
@ -18,10 +17,9 @@ describe Guardian do
let(:user) { build(:user) } let(:user) { build(:user) }
let(:admin) { build(:admin) } let(:admin) { build(:admin) }
fab!(:group) { Fabricate(:group) }
describe "#user_is_a_member_of_group?" do describe "#user_is_a_member_of_group?" do
let(:group) { Fabricate(:group) }
it "is true when the user is an admin" do it "is true when the user is an admin" do
expect(Guardian.new(admin).user_is_a_member_of_group?(group)).to eq(true) expect(Guardian.new(admin).user_is_a_member_of_group?(group)).to eq(true)
end end
@ -38,8 +36,6 @@ describe Guardian do
end end
describe "#group_and_user_can_access_query?" do describe "#group_and_user_can_access_query?" do
let(:group) { Fabricate(:group) }
it "is true if the user is an admin" do it "is true if the user is an admin" do
expect(Guardian.new(admin).group_and_user_can_access_query?(group, make_query)).to eq(true) expect(Guardian.new(admin).group_and_user_can_access_query?(group, make_query)).to eq(true)
end end

View File

@ -3,10 +3,6 @@
require 'rails_helper' require 'rails_helper'
describe 'Data Explorer rake tasks' do describe 'Data Explorer rake tasks' do
before :all do
$stdout = File.open(File::NULL, 'w')
end
before do before do
Rake::Task.clear Rake::Task.clear
Discourse::Application.load_tasks Discourse::Application.load_tasks
@ -30,7 +26,7 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 1, name: 'A') make_query('SELECT 1 as value', id: 1, name: 'A')
make_query('SELECT 1 as value', id: 2, name: 'B') make_query('SELECT 1 as value', id: 2, name: 'B')
# rake data_explorer[1] => hide query with ID 1 # rake data_explorer[1] => hide query with ID 1
Rake::Task['data_explorer'].invoke(1) silence_stdout { Rake::Task['data_explorer'].invoke(1) }
# Soft deletion: PluginStoreRow should not be modified # Soft deletion: PluginStoreRow should not be modified
expect(DataExplorer::Query.all.length).to eq(2) expect(DataExplorer::Query.all.length).to eq(2)
@ -47,7 +43,7 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 3, name: 'C') make_query('SELECT 1 as value', id: 3, name: 'C')
make_query('SELECT 1 as value', id: 4, name: 'D') make_query('SELECT 1 as value', id: 4, name: 'D')
# rake data_explorer[1,2,4] => hide queries with IDs 1, 2 and 4 # rake data_explorer[1,2,4] => hide queries with IDs 1, 2 and 4
Rake::Task['data_explorer'].invoke(1, 2, 4) silence_stdout { Rake::Task['data_explorer'].invoke(1, 2, 4) }
# Soft deletion: PluginStoreRow should not be modified # Soft deletion: PluginStoreRow should not be modified
expect(DataExplorer::Query.all.length).to eq(4) expect(DataExplorer::Query.all.length).to eq(4)
@ -65,9 +61,9 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 1, name: 'A') make_query('SELECT 1 as value', id: 1, name: 'A')
make_query('SELECT 1 as value', id: 2, name: 'B') make_query('SELECT 1 as value', id: 2, name: 'B')
# rake data_explorer[3] => try to hide query with ID 3 # rake data_explorer[3] => try to hide query with ID 3
Rake::Task['data_explorer'].invoke(3) silence_stdout { Rake::Task['data_explorer'].invoke(3) }
# rake data_explorer[3,4,5] => try to hide queries with IDs 3, 4 and 5 # rake data_explorer[3,4,5] => try to hide queries with IDs 3, 4 and 5
Rake::Task['data_explorer'].invoke(3, 4, 5) silence_stdout { Rake::Task['data_explorer'].invoke(3, 4, 5) }
# Array of hidden queries should be empty # Array of hidden queries should be empty
expect(hidden_queries.length).to eq(0) expect(hidden_queries.length).to eq(0)
@ -81,7 +77,7 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true) make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true)
make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true) make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true)
# rake data_explorer:unhide_query[1] => unhide query with ID 1 # rake data_explorer:unhide_query[1] => unhide query with ID 1
Rake::Task['data_explorer:unhide_query'].invoke(1) silence_stdout { Rake::Task['data_explorer:unhide_query'].invoke(1) }
# Soft deletion: PluginStoreRow should not be modified # Soft deletion: PluginStoreRow should not be modified
expect(DataExplorer::Query.all.length).to eq(2) expect(DataExplorer::Query.all.length).to eq(2)
@ -98,7 +94,7 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 3, name: 'C', hidden: true) make_query('SELECT 1 as value', id: 3, name: 'C', hidden: true)
make_query('SELECT 1 as value', id: 4, name: 'D', hidden: true) make_query('SELECT 1 as value', id: 4, name: 'D', hidden: true)
# rake data_explorer:unhide_query[1,2,4] => unhide queries with IDs 1, 2 and 4 # rake data_explorer:unhide_query[1,2,4] => unhide queries with IDs 1, 2 and 4
Rake::Task['data_explorer:unhide_query'].invoke(1, 2, 4) silence_stdout { Rake::Task['data_explorer:unhide_query'].invoke(1, 2, 4) }
# Soft deletion: PluginStoreRow should not be modified # Soft deletion: PluginStoreRow should not be modified
expect(DataExplorer::Query.all.length).to eq(4) expect(DataExplorer::Query.all.length).to eq(4)
@ -114,9 +110,9 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true) make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true)
make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true) make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true)
# rake data_explorer:unhide_query[3] => try to unhide query with ID 3 # rake data_explorer:unhide_query[3] => try to unhide query with ID 3
Rake::Task['data_explorer:unhide_query'].invoke(3) silence_stdout { Rake::Task['data_explorer:unhide_query'].invoke(3) }
# rake data_explorer:unhide_query[3,4,5] => try to unhide queries with IDs 3, 4 and 5 # rake data_explorer:unhide_query[3,4,5] => try to unhide queries with IDs 3, 4 and 5
Rake::Task['data_explorer:unhide_query'].invoke(3, 4, 5) silence_stdout { Rake::Task['data_explorer:unhide_query'].invoke(3, 4, 5) }
# Array of hidden queries shouldn't change # Array of hidden queries shouldn't change
expect(hidden_queries.length).to eq(2) expect(hidden_queries.length).to eq(2)
@ -130,7 +126,7 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true) make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true)
make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true) make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true)
# rake data_explorer:hard_delete[1] => hard delete query with ID 1 # rake data_explorer:hard_delete[1] => hard delete query with ID 1
Rake::Task['data_explorer:hard_delete'].invoke(1) silence_stdout { Rake::Task['data_explorer:hard_delete'].invoke(1) }
# Hard deletion: query list should be shorter by 1 # Hard deletion: query list should be shorter by 1
expect(DataExplorer::Query.all.length).to eq(1) expect(DataExplorer::Query.all.length).to eq(1)
@ -147,7 +143,7 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 3, name: 'C', hidden: true) make_query('SELECT 1 as value', id: 3, name: 'C', hidden: true)
make_query('SELECT 1 as value', id: 4, name: 'D', hidden: true) make_query('SELECT 1 as value', id: 4, name: 'D', hidden: true)
# rake data_explorer:hard_delete[1,2,4] => hard delete queries with IDs 1, 2 and 4 # rake data_explorer:hard_delete[1,2,4] => hard delete queries with IDs 1, 2 and 4
Rake::Task['data_explorer:hard_delete'].invoke(1, 2, 4) silence_stdout { Rake::Task['data_explorer:hard_delete'].invoke(1, 2, 4) }
# Hard deletion: query list should be shorter by 3 # Hard deletion: query list should be shorter by 3
expect(DataExplorer::Query.all.length).to eq(1) expect(DataExplorer::Query.all.length).to eq(1)
@ -163,9 +159,9 @@ describe 'Data Explorer rake tasks' do
make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true) make_query('SELECT 1 as value', id: 1, name: 'A', hidden: true)
make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true) make_query('SELECT 1 as value', id: 2, name: 'B', hidden: true)
# rake data_explorer:hard_delete[3] => try to hard delete query with ID 3 # rake data_explorer:hard_delete[3] => try to hard delete query with ID 3
Rake::Task['data_explorer:hard_delete'].invoke(3) silence_stdout { Rake::Task['data_explorer:hard_delete'].invoke(3) }
# rake data_explorer:hard_delete[3,4,5] => try to hard delete queries with IDs 3, 4 and 5 # rake data_explorer:hard_delete[3,4,5] => try to hard delete queries with IDs 3, 4 and 5
Rake::Task['data_explorer:hard_delete'].invoke(3, 4, 5) silence_stdout { Rake::Task['data_explorer:hard_delete'].invoke(3, 4, 5) }
# Array of hidden queries shouldn't change # Array of hidden queries shouldn't change
expect(hidden_queries.length).to eq(2) expect(hidden_queries.length).to eq(2)
@ -177,7 +173,7 @@ describe 'Data Explorer rake tasks' do
DataExplorer::Query.destroy_all DataExplorer::Query.destroy_all
make_query('SELECT 1 as value', id: 1, name: 'A') make_query('SELECT 1 as value', id: 1, name: 'A')
# rake data_explorer:hard_delete[1] => try to hard delete query with ID 1 # rake data_explorer:hard_delete[1] => try to hard delete query with ID 1
Rake::Task['data_explorer:hard_delete'].invoke(1) silence_stdout { Rake::Task['data_explorer:hard_delete'].invoke(1) }
# List of queries shouldn't change # List of queries shouldn't change
expect(DataExplorer::Query.all.length).to eq(1) expect(DataExplorer::Query.all.length).to eq(1)

View File

@ -3,9 +3,9 @@
require 'rails_helper' require 'rails_helper'
describe "Data explorer group serializer additions" do describe "Data explorer group serializer additions" do
let(:group_user) { Fabricate(:user) } fab!(:group_user) { Fabricate(:user) }
let(:other_user) { Fabricate(:user) } fab!(:other_user) { Fabricate(:user) }
let(:group) { Fabricate(:group) } fab!(:group) { Fabricate(:group) }
let!(:query) { DataExplorer::Query.create!(name: "My query", sql: "") } let!(:query) { DataExplorer::Query.create!(name: "My query", sql: "") }
before do before do

View File

@ -4,7 +4,7 @@ require 'rails_helper'
describe DataExplorer::QueryController do describe DataExplorer::QueryController do
def response_json def response_json
MultiJson.load(response.body) response.parsed_body
end end
before do before do
@ -58,10 +58,6 @@ describe DataExplorer::QueryController do
end end
describe "#index" do describe "#index" do
before do
require_dependency File.expand_path('../../../lib/queries.rb', __FILE__)
end
it "behaves nicely with no user created queries" do it "behaves nicely with no user created queries" do
DataExplorer::Query.destroy_all DataExplorer::Query.destroy_all
get "/admin/plugins/explorer/queries.json" get "/admin/plugins/explorer/queries.json"