|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe "Arel::Visitors::OracleCommon#visit_Arel_Nodes_HomogeneousIn" do |
| 4 | + before(:all) do |
| 5 | + ActiveRecord::Base.establish_connection(CONNECTION_PARAMS) |
| 6 | + end |
| 7 | + |
| 8 | + before(:each) do |
| 9 | + @visitor = Arel::Visitors::Oracle12.new(ActiveRecord::Base.connection) |
| 10 | + type_caster = Class.new { def type_for_attribute(_name) = ActiveRecord::Type::Value.new }.new |
| 11 | + @table = Arel::Table.new(:users, type_caster: type_caster) |
| 12 | + end |
| 13 | + |
| 14 | + def compile(node, visitor: @visitor) |
| 15 | + visitor.accept(node, Arel::Collectors::SQLString.new).value |
| 16 | + end |
| 17 | + |
| 18 | + it "marks the collector as not preparable" do |
| 19 | + node = Arel::Nodes::HomogeneousIn.new([1, 2, 3], @table[:id], :in) |
| 20 | + collector = Arel::Collectors::SQLString.new |
| 21 | + collector.preparable = true |
| 22 | + @visitor.accept(node, collector) |
| 23 | + expect(collector.preparable).to be(false) |
| 24 | + end |
| 25 | + |
| 26 | + it "renders :in as a literal IN list" do |
| 27 | + node = Arel::Nodes::HomogeneousIn.new([1, 2, 3], @table[:id], :in) |
| 28 | + expect(compile(node)).to match(/"USERS"\."ID"\s+IN\s+\(1,2,3\)/) |
| 29 | + end |
| 30 | + |
| 31 | + it "renders :notin as a literal NOT IN list" do |
| 32 | + node = Arel::Nodes::HomogeneousIn.new([1, 2, 3], @table[:id], :notin) |
| 33 | + expect(compile(node)).to match(/"USERS"\."ID"\s+NOT IN\s+\(1,2,3\)/) |
| 34 | + end |
| 35 | + |
| 36 | + it "falls back to NULL when :in has an empty values array" do |
| 37 | + node = Arel::Nodes::HomogeneousIn.new([], @table[:id], :in) |
| 38 | + expect(compile(node)).to match(/"USERS"\."ID"\s+IN\s+\(NULL\)/) |
| 39 | + end |
| 40 | + |
| 41 | + it "falls back to NULL when :notin has an empty values array" do |
| 42 | + node = Arel::Nodes::HomogeneousIn.new([], @table[:id], :notin) |
| 43 | + expect(compile(node)).to match(/"USERS"\."ID"\s+NOT IN\s+\(NULL\)/) |
| 44 | + end |
| 45 | + |
| 46 | + it "chunks :in into multiple IN groups joined by OR when values exceed in_clause_length" do |
| 47 | + node = Arel::Nodes::HomogeneousIn.new((1..1001).to_a, @table[:id], :in) |
| 48 | + sql = compile(node) |
| 49 | + expect(sql.scan(/"USERS"\."ID" IN \(/).size).to eq(2) |
| 50 | + expect(sql).to include(" OR ") |
| 51 | + expect(sql).to start_with("(") |
| 52 | + expect(sql).to end_with(")") |
| 53 | + expect(sql).to include("IN (1001)") |
| 54 | + end |
| 55 | + |
| 56 | + it "chunks :notin into multiple NOT IN groups joined by AND when values exceed in_clause_length" do |
| 57 | + node = Arel::Nodes::HomogeneousIn.new((1..1001).to_a, @table[:id], :notin) |
| 58 | + sql = compile(node) |
| 59 | + expect(sql.scan(/"USERS"\."ID" NOT IN \(/).size).to eq(2) |
| 60 | + expect(sql).to include(" AND ") |
| 61 | + expect(sql).to start_with("(") |
| 62 | + expect(sql).to end_with(")") |
| 63 | + expect(sql).to include("NOT IN (1001)") |
| 64 | + end |
| 65 | + |
| 66 | + it "chunks :in the same way via Arel::Visitors::Oracle" do |
| 67 | + oracle = Arel::Visitors::Oracle.new(ActiveRecord::Base.connection) |
| 68 | + node = Arel::Nodes::HomogeneousIn.new((1..1001).to_a, @table[:id], :in) |
| 69 | + sql = compile(node, visitor: oracle) |
| 70 | + expect(sql.scan(/"USERS"\."ID" IN \(/).size).to eq(2) |
| 71 | + expect(sql).to include(" OR ") |
| 72 | + end |
| 73 | + |
| 74 | + it "chunks :notin the same way via Arel::Visitors::Oracle" do |
| 75 | + oracle = Arel::Visitors::Oracle.new(ActiveRecord::Base.connection) |
| 76 | + node = Arel::Nodes::HomogeneousIn.new((1..1001).to_a, @table[:id], :notin) |
| 77 | + sql = compile(node, visitor: oracle) |
| 78 | + expect(sql.scan(/"USERS"\."ID" NOT IN \(/).size).to eq(2) |
| 79 | + expect(sql).to include(" AND ") |
| 80 | + end |
| 81 | +end |
0 commit comments