aboutsummaryrefslogtreecommitdiffstats
path: root/spec/attributes/header_spec.rb
blob: 51cba93df31bb2fd761a04c7099321539674fafa (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
require 'spec_helper'

module Arel
  describe "Header" do
    before :all do
      @relation = Model.build do |r|
        r.attribute :id,   Attributes::Integer
        r.attribute :name, Attributes::String
        r.attribute :age,  Attributes::Integer
      end

      @other = Model.build do |r|
        r.attribute :foo, Attributes::String
      end

      @subset = Model.build do |r|
        r.attribute :id, Attributes::Integer
      end
    end

    describe "attribute lookup" do
      it "finds attributes by name" do
        @relation.attributes[:name].should == Attributes::String.new(@relation, :name)
      end

      it "returns nil if no attribute is found" do
        @relation.attributes[:does_not_exist].should be_nil
        @relation[:does_not_exist].should be_nil
      end
    end

    describe "#union" do
      it "keeps all attributes from disjoint headers" do
        (@relation.attributes.union @other.attributes).to_ary.should have(4).items
      end

      it "keeps all attributes from both relations even if they seem like subsets" do
        (@relation.attributes.union @subset.attributes).to_ary.should have(4).items
      end
    end
  end
end