File: query_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 (103 lines) | stat: -rw-r--r-- 2,539 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
require_relative "spec_helper"

describe "Database#query" do
  before do
    @db = Sequel.mock.extension(:query)
  end

  it "should delegate to Dataset#query if block is provided" do
    @d = @db.query {select :x; from :y}
    @d.must_be_kind_of(Sequel::Dataset)
    @d.sql.must_equal "SELECT x FROM y"
  end
end

describe "Dataset#query" do
  before do
    @d = Sequel.mock.dataset.extension(:query)
  end
  
  it "should allow cloning without arguments" do
    q = @d.query {clone}
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT *"
  end
  
  it "should support #from" do
    q = @d.query {from :xxx}
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT * FROM xxx"
  end
  
  it "should support #select" do
    q = @d.query do
      select :a, Sequel[:b].as(:mongo)
      from :yyy
    end
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT a, b AS mongo FROM yyy"
  end
  
  it "should support #where" do
    q = @d.query do
      from :zzz
      where{x + 2 > Sequel.expr(:y) + 3}
    end
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT * FROM zzz WHERE ((x + 2) > (y + 3))"

    q = @d.from(:zzz).query do
      where{(x > 1) & (Sequel.expr(:y) > 2)}
    end
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT * FROM zzz WHERE ((x > 1) AND (y > 2))"

    q = @d.from(:zzz).query do
      where :x => 33
    end
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT * FROM zzz WHERE (x = 33)"
  end
  
  it "should support #group_by and #having" do
    q = @d.query do
      from :abc
      group_by :id
      having{x >= 2}
    end
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT * FROM abc GROUP BY id HAVING (x >= 2)"
  end
  
  it "should support #order, #order_by" do
    q = @d.query do
      from :xyz
      order_by :stamp
    end
    q.class.must_equal @d.class
    q.sql.must_equal "SELECT * FROM xyz ORDER BY stamp"
  end

  it "should support blocks that end in nil" do
    condition = false
    q = @d.query do
      from :xyz
      order_by :stamp if condition
    end
    q.sql.must_equal "SELECT * FROM xyz"
  end
  
  it "should raise on non-chainable method calls" do
    proc {@d.query {row_proc}}.must_raise(Sequel::Error)
    proc {@d.query {all}}.must_raise(Sequel::Error)
  end
  
  if RUBY_VERSION >= '2.7'
    it "should handle keywords when delegating" do
      eval '@d = @d.with_extend{def foo(name: (raise)) clone(:name=>name) end}'
      @d.query do
        foo(name: '1')
      end.opts[:name].must_equal '1'
    end
  end
end