File: sql_comments_spec.rb

package info (click to toggle)
ruby-sequel 5.97.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,188 kB
  • sloc: ruby: 123,115; makefile: 3
file content (131 lines) | stat: -rw-r--r-- 4,606 bytes parent folder | download | duplicates (3)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require_relative "spec_helper"

shared_specs = Module.new do
  extend Minitest::Spec::DSL

  it "should not add a comment if one is not set for the dataset" do
    @ds.select_sql.must_equal 'SELECT * FROM t'
    @ds.insert_sql(:a=>1).must_equal 'INSERT INTO t (a) VALUES (1)'
    @ds.delete_sql.must_equal 'DELETE FROM t'
    @ds.update_sql(:a=>1).must_equal 'UPDATE t SET a = 1'
  end

  it "should add a comment if one is set for the dataset" do
    ds = @ds.comment("Some\nComment\r\n Here")
    ds.select_sql.must_equal "SELECT * FROM t -- Some Comment Here\n"
    ds.insert_sql(:a=>1).must_equal "INSERT INTO t (a) VALUES (1) -- Some Comment Here\n"
    ds.delete_sql.must_equal "DELETE FROM t -- Some Comment Here\n"
    ds.update_sql(:a=>1).must_equal "UPDATE t SET a = 1 -- Some Comment Here\n"
  end

  it "should not add a comment multiple times" do
    ds = @ds.comment("Some\nComment\r\n Here")
    ds.sql.must_equal "SELECT * FROM t -- Some Comment Here\n"
    ds.sql.must_equal "SELECT * FROM t -- Some Comment Here\n"
  end

  it "should not add a comment multiple times" do
    ds = @ds.comment("Some\nComment\r\n Here")
    5.times do
      ds.first(:x=>1)
      ds.db.sqls.must_equal ["SELECT * FROM t WHERE (x = 1) LIMIT 1 -- Some Comment Here\n"]
    end
  end

  it "should handle comments used in nested datasets" do
    ds = @ds.comment("Some\nComment\r\n Here")
    ds.where(:id=>ds).select_sql.must_equal "SELECT * FROM t WHERE (id IN (SELECT * FROM t -- Some Comment Here\n)) -- Some Comment Here\n"
  end

  it "should allow overriding comments" do
    @ds.comment("Foo").comment("Some\nComment\r\n Here").select_sql.must_equal "SELECT * FROM t -- Some Comment Here\n"
  end

  it "should allow disabling comments by overridding with nil" do
    @ds.comment("Foo").comment(nil).select_sql.must_equal "SELECT * FROM t"
  end

  it "should handle frozen SQL strings" do
    @ds = Sequel.mock[:t].with_extend{def select_sql; super.freeze; end}.extension(:sql_comments)
    ds = @ds.comment("Some\nComment\r\n Here")
    ds.select_sql.must_equal "SELECT * FROM t -- Some Comment Here\n"
  end
end

describe "sql_comments dataset extension" do
  before do
    @ds = Sequel.mock[:t].extension(:sql_comments)
  end
  
  include shared_specs
end

describe "sql_comments database extension" do
  before do
    @db = Sequel.mock.extension(:sql_comments)
    @ds = @db[:t]
  end
  
  include shared_specs

  it "should support setting comments for all queries executed inside a with_comments block" do
    @db.with_comments(:foo=>'bar', :baz=>'quux') do
      @ds.select_sql.must_equal "SELECT * FROM t -- foo:bar,baz:quux\n"
    end
  end

  it "should work if loading the extension multiple times" do
    @db.with_comments(:foo=>'bar', :baz=>'quux') do
      @db.extension :sql_comments
      @ds.select_sql.must_equal "SELECT * FROM t -- foo:bar,baz:quux\n"
    end
  end

  it "should support nesting with_comments blocks" do
    @db.with_comments(:foo=>'bar') do
      @db.with_comments(:baz=>'quux') do
        @ds.select_sql.must_equal "SELECT * FROM t -- foo:bar,baz:quux\n"
      end
    end
  end

  it "should support nesting with_comments blocks multiple times" do
    @db.with_comments(:foo=>'bar') do
      @db.with_comments(:baz=>'quux') do
        @ds.select_sql.must_equal "SELECT * FROM t -- foo:bar,baz:quux\n"
      end
      @db.with_comments(:x=>'y') do
        @ds.select_sql.must_equal "SELECT * FROM t -- foo:bar,x:y\n"
      end
    end
  end

  it "should support overridding values in nested blocks" do
    @db.with_comments(:foo=>'bar', :baz=>'q') do
      @db.with_comments(:baz=>'quux') do
        @ds.select_sql.must_equal "SELECT * FROM t -- foo:bar,baz:quux\n"
      end
    end
  end

  it "should support removing values in nested using nil" do
    @db.with_comments(:foo=>'bar', :bat=>'q') do
      @db.with_comments(:baz=>'quux', :bat=>nil) do
        @ds.select_sql.must_equal "SELECT * FROM t -- foo:bar,baz:quux\n"
      end
    end
  end

  it "should support combining with dataset-specific comments" do
    @db.with_comments(:foo=>'bar', :baz=>'quux') do
      @ds.comment('specific').select_sql.must_equal "SELECT * FROM t -- foo:bar,baz:quux -- specific \n"
    end
  end

  it "should only use block level comments for main dataset, not for nested datasets" do
    @db.with_comments(:foo=>'bar', :baz=>'quux') do
      ds = @ds.comment("Some\nComment\r\n Here")
      ds.where(:id=>ds).select_sql.must_equal "SELECT * FROM t WHERE (id IN (SELECT * FROM t -- Some Comment Here\n)) -- foo:bar,baz:quux -- Some Comment Here \n"
    end
  end
end