aboutsummaryrefslogtreecommitdiffstats
path: root/spec/engines/sql/unit/relations/having_spec.rb
blob: 931e8d0a06806ea72b17f4a97478bd85c1ed27e6 (plain) (blame)
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
require 'spec_helper'

module Arel
  describe Having do
    before do
      @relation = Table.new(:developers)
    end

    describe '#to_sql' do
      describe 'when given a predicate' do
        it "manufactures sql with where clause conditions" do
          sql = @relation.group(@relation[:department]).having("MIN(salary) > 1000").to_sql

          adapter_is :mysql do
            sql.should be_like(%Q{
              SELECT `developers`.`id`, `developers`.`name`, `developers`.`salary`, `developers`.`department`, `developers`.`created_at`
              FROM `developers`
              GROUP BY `developers`.`department`
              HAVING MIN(salary) > 1000
            })
          end

          adapter_is :oracle do
            sql.should be_like(%Q{
              SELECT "DEVELOPERS"."ID", "DEVELOPERS"."NAME", "DEVELOPERS"."SALARY", "DEVELOPERS"."DEPARTMENT", "DEVELOPERS"."CREATED_AT"
              FROM "DEVELOPERS"
              GROUP BY "DEVELOPERS"."DEPARTMENT"
              HAVING MIN(salary) > 1000
            })
          end

          adapter_is_not :mysql, :oracle do
            sql.should be_like(%Q{
              SELECT "developers"."id", "developers"."name", "developers"."salary", "developers"."department", "developers"."created_at"
              FROM "developers"
              GROUP BY "developers"."department"
              HAVING MIN(salary) > 1000
            })
          end
        end
      end

      describe 'when given two predicates' do
        it "manufactures sql with where clause conditions joined by AND" do
          sql = @relation.group(@relation[:department]).having("MIN(salary) > 1000", "MAX(salary) < 10000").to_sql

          adapter_is :mysql do
            sql.should be_like(%Q{
              SELECT `developers`.`id`, `developers`.`name`, `developers`.`salary`, `developers`.`department`, `developers`.`created_at`
              FROM `developers`
              GROUP BY `developers`.`department`
              HAVING MIN(salary) > 1000 AND MAX(salary) < 10000
            })
          end

          adapter_is :oracle do
            sql.should be_like(%Q{
              SELECT "DEVELOPERS"."ID", "DEVELOPERS"."NAME", "DEVELOPERS"."SALARY", "DEVELOPERS"."DEPARTMENT", "DEVELOPERS"."CREATED_AT"
              FROM "DEVELOPERS"
              GROUP BY "DEVELOPERS"."DEPARTMENT"
              HAVING MIN(salary) > 1000 AND MAX(salary) < 10000
            })
          end

          adapter_is_not :mysql, :oracle do
            sql.should be_like(%Q{
              SELECT "developers"."id", "developers"."name", "developers"."salary", "developers"."department", "developers"."created_at"
              FROM "developers"
              GROUP BY "developers"."department"
              HAVING MIN(salary) > 1000 AND MAX(salary) < 10000
            })
          end
        end
      end
    end
  end
end