1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
require_relative "spec_helper"
Sequel.extension :columns_introspection
describe "columns_introspection extension" do
before do
@db = Sequel.mock.extension(:columns_introspection)
@ds = @db[:a]
@db.sqls
end
it "should not issue a database query if the columns are already loaded" do
@ds.send(:columns=, [:x])
@ds.columns.must_equal [:x]
@db.sqls.length.must_equal 0
end
it "should handle plain symbols without a database query" do
@ds.select(:x).columns.must_equal [:x]
@db.sqls.length.must_equal 0
end
with_symbol_splitting "should handle qualified symbols without a database query" do
@ds.select(:t__x).columns.must_equal [:x]
@db.sqls.length.must_equal 0
end
with_symbol_splitting "should handle aliased symbols without a database query" do
@ds.select(:x___a).columns.must_equal [:a]
@db.sqls.length.must_equal 0
end
with_symbol_splitting "should handle qualified and aliased symbols without a database query" do
@ds.select(:t__x___a).columns.must_equal [:a]
@db.sqls.length.must_equal 0
end
it "should handle SQL::Identifiers " do
@ds.select(Sequel.identifier(:x)).columns.must_equal [:x]
@db.sqls.length.must_equal 0
end
it "should handle SQL::QualifiedIdentifiers" do
@ds.select(Sequel.qualify(:t, :x)).columns.must_equal [:x]
@ds.select(Sequel.identifier(:x).qualify(:t)).columns.must_equal [:x]
@db.sqls.length.must_equal 0
end
it "should handle SQL::AliasedExpressions" do
@ds.select(Sequel.as(:x, :a)).columns.must_equal [:a]
@ds.select(Sequel.as(:x, Sequel.identifier(:a))).columns.must_equal [:a]
@db.sqls.length.must_equal 0
end
it "should handle LiteralStrings in FROM tables by issuing a query" do
@ds.from(Sequel.lit('x')).columns.must_equal []
@db.sqls.must_equal ["SELECT * FROM x LIMIT 0"]
end
it "should handle selecting * from a single subselect with no joins without a database query if the subselect's columns can be handled" do
@ds.select(:x).from_self.columns.must_equal [:x]
@db.sqls.length.must_equal 0
@ds.select(:x).from_self.from_self.columns.must_equal [:x]
@db.sqls.length.must_equal 0
end
it "should handle selecting * from a single table with no joins without a database query if the database has cached schema columns for the table" do
@db.instance_variable_set(:@schemas, "a"=>[[:x, {}]])
@ds.columns.must_equal [:x]
@db.sqls.length.must_equal 0
end
it "should issue a database query for multiple subselects or joins" do
@ds.from(@ds.select(:x), @ds.select(:y)).columns
@db.sqls.length.must_equal 1
@ds.select(:x).from_self.natural_join(:a).columns
@db.sqls.length.must_equal 1
end
it "should issue a database query when common table expressions are used" do
@db.instance_variable_set(:@schemas, "a"=>[[:x, {}]])
@ds.with_extend{def supports_cte?(*) true end}.with(:a, @ds).columns
@db.sqls.length.must_equal 1
end
it "should issue a database query if the wildcard is selected" do
@ds.columns
@db.sqls.length.must_equal 1
end
it "should issue a database query if an unsupported type is used" do
@ds.select(1).columns
@db.sqls.length.must_equal 1
end
end
|