File: sql_comments_plugin_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 (98 lines) | stat: -rw-r--r-- 4,198 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
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
require_relative "spec_helper"

describe "sql_comments plugin " do
  before do
    @db = Sequel.mock(:fetch=>{:id=>1, :name=>'a'})
    @c = Class.new(Sequel::Model(@db[:t]))
    @c.columns :id, :name
    def @c.to_s; 'C' end
    @c.many_to_one :c, :class=>@c, :key=>:id
    @c.one_to_many :cs, :class=>@c, :key=>:id
    @o = @c.new(:name=>'a'){|o| o.id = 1}
    @c.plugin :sql_comments
    @ds = @c.dataset
    @db.sqls
  end
  
  it "should give temporary name to name model-specific dataset module" do
    def @c.name; "Foo" end
    @c.dataset_module{where :a, :a}
    @c.sql_comments_dataset_methods :a
    @c.dataset.class.ancestors[1].name.must_equal "Foo::@_sql_comments_dataset_module"
    @c.a.all
    @db.sqls.must_equal ["SELECT * FROM t WHERE a -- model:C,method_type:dataset,method:all\n"]
  end if RUBY_VERSION >= '3.3'

  it "should include SQL comments for default class methods that issue queries" do
    @c.with_pk!(1)
    @db.sqls.must_equal ["SELECT * FROM t WHERE (id = 1) LIMIT 1 -- model:C,method_type:class,method:with_pk\n"]
  end
  
  it "should include SQL comments for default instance methods that issue queries" do
    @o.update(:name=>'b')
    @db.sqls.must_equal ["INSERT INTO t (name, id) VALUES ('b', 1) -- model:C,method_type:instance,method:update\n",
      "SELECT * FROM t WHERE (id = 1) LIMIT 1 -- model:C,method_type:instance,method:update\n"]
  end
  
  it "should include SQL comments for default dataset methods that issue queries" do
    @c.all
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:dataset,method:all\n"]
  end
  
  it "should add comments for instance methods if :model is not already one of the comments" do
    @db.with_comments(:foo=>'bar'){@o.update(:name=>'b')}
    @db.sqls.must_equal ["INSERT INTO t (name, id) VALUES ('b', 1) -- foo:bar,model:C,method_type:instance,method:update\n",
      "SELECT * FROM t WHERE (id = 1) LIMIT 1 -- foo:bar,model:C,method_type:instance,method:update\n"]
  end
  
  it "should add comments for dataset methods if :model is not already one of the comments" do
    @db.with_comments(:foo=>'bar'){@c.all}
    @db.sqls.must_equal ["SELECT * FROM t -- foo:bar,model:C,method_type:dataset,method:all\n"]
  end
  
  it "should include SQL comments for association load queries" do
    @o.c
    @db.sqls.must_equal ["SELECT * FROM t WHERE (id = 1) LIMIT 1 -- model:C,method_type:association_load,association:c\n"]
  end
  
  it "should include SQL comments for association load queries even after finalizing associations " do
    @c.finalize_associations
    @c.freeze
    @o.cs
    @db.sqls.must_equal ["SELECT * FROM t WHERE (t.id = 1) -- model:C,method_type:association_load,association:cs\n"]
  end
  
  it "should include SQL comments for eager association loads issue queries" do
    @c.eager(:c).all
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:dataset,method:all\n",
      "SELECT * FROM t WHERE (t.id IN (1)) -- model:C,method_type:association_eager_load,association:c\n"]
  end
  
  it "should support adding comments for custom class methods" do
    @c.extend(Module.new{def c; all; end; def d; all; end})
    @c.sql_comments_class_methods :c, :d
    @c.c
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:class,method:c\n"]
    @c.d
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:class,method:d\n"]
  end

  it "should support adding comments for custom instance methods" do
    @c.send(:include, Module.new{def c; model.all; end; def d; model.all; end})
    @c.sql_comments_instance_methods :c, :d
    @o.c
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:instance,method:c\n"]
    @o.d
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:instance,method:d\n"]
  end

  it "should support adding comments for custom dataset methods" do
    @c.dataset_module(Module.new{def c; all; end; def d; all; end})
    @c.sql_comments_dataset_methods :c
    @c.dataset.c
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:dataset,method:c\n"]
    @c.sql_comments_dataset_methods :d
    @c.dataset.d
    @db.sqls.must_equal ["SELECT * FROM t -- model:C,method_type:dataset,method:d\n"]
  end
end