File: sql_comments_spec.rb

package info (click to toggle)
ruby-sequel 5.41.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,548 kB
  • sloc: ruby: 104,241; makefile: 3
file content (41 lines) | stat: -rw-r--r-- 1,696 bytes parent folder | download
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
require_relative "spec_helper"

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

  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 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