aboutsummaryrefslogtreecommitdiffstats
path: root/spec/arel/unit/relations/array_spec.rb
blob: d1c65c60a99b2e275b09c25cdb76617be5a5379e (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
79
80
81
82
83
84
85
86
87
require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')

module Arel
  describe Array do
    before do
      @relation = Array.new([
        [1, 'duck' ],
        [2, 'duck' ],
        [3, 'goose']
      ], [:id, :name])
    end

    describe '#attributes' do
      it 'manufactures attributes corresponding to the names given on construction' do
        @relation.attributes.should == [
          Attribute.new(@relation, :id),
          Attribute.new(@relation, :name)
        ]
      end
    end

    describe '#call' do
      it "manufactures an array of hashes of attributes to values" do
        @relation.call.should == [
          { @relation[:id] => 1, @relation[:name] => 'duck'  },
          { @relation[:id] => 2, @relation[:name] => 'duck'  },
          { @relation[:id] => 3, @relation[:name] => 'goose' }
        ]
      end
      
      describe 'where' do
        it 'filters the relation with the provided predicate' do
          @relation.where(@relation[:id].lt(3)).call.should == [
            { @relation[:id] => 1, @relation[:name] => 'duck' },
            { @relation[:id] => 2, @relation[:name] => 'duck' }
          ]
        end
      end
      
      describe 'group' do
        it 'sorts the relation with the provided ordering' do
        end
      end
      
      describe 'order' do
        it 'sorts the relation with the provided ordering' do
          @relation.order(@relation[:id].desc).call.should == [
            { @relation[:id] => 3, @relation[:name] => 'goose' },
            { @relation[:id] => 2, @relation[:name] => 'duck'  },
            { @relation[:id] => 1, @relation[:name] => 'duck'  }
          ]
        end
      end
      
      describe 'project' do
        it 'projects' do
          @relation.project(@relation[:id]).call.should == [
            { @relation[:id] => 1 },
            { @relation[:id] => 2 },
            { @relation[:id] => 3 }
          ]
        end
      end
      
      describe 'skip' do
        it 'slices' do
          @relation.skip(1).call.should == [
            { @relation[:id] => 2, @relation[:name] => 'duck'  },
            { @relation[:id] => 3, @relation[:name] => 'goose' }
          ]
        end
      end
      
      describe 'take' do
        it 'dices' do
          @relation.take(2).call.should == [
            { @relation[:id] => 1, @relation[:name] => 'duck' },
            { @relation[:id] => 2, @relation[:name] => 'duck' }
          ]
        end
      end
      
      describe 'join' do
      end
    end
  end
end