summaryrefslogtreecommitdiffstats
path: root/spec/unit/rails/resource_spec.rb
blob: 9791f49cb75cb13dfd5e6b7a08f027fed2394cdc (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env ruby

require 'spec_helper'
require 'puppet/rails'

describe "Puppet::Rails::Resource", :if => Puppet.features.rails? do
  def column(name, type)
    ActiveRecord::ConnectionAdapters::Column.new(name, nil, type, false)
  end

  before do
    require 'puppet/rails/resource'

    # Stub this so we don't need access to the DB.
    Puppet::Rails::Resource.stubs(:columns).returns([column("title", "string"), column("restype", "string"), column("exported", "boolean")])
  end

  describe "when creating initial resource arguments" do
    it "should set the restype to the resource's type" do
      Puppet::Rails::Resource.rails_resource_initial_args(Puppet::Resource.new(:file, "/file"))[:restype].should == "File"
    end

    it "should set the title to the resource's title" do
      Puppet::Rails::Resource.rails_resource_initial_args(Puppet::Resource.new(:file, "/file"))[:title].should == "/file"
    end

    it "should set the line to the resource's line if one is available" do
      resource = Puppet::Resource.new(:file, "/file")
      resource.line = 50

      Puppet::Rails::Resource.rails_resource_initial_args(resource)[:line].should == 50
    end

    it "should set 'exported' to true of the resource is exported" do
      resource = Puppet::Resource.new(:file, "/file")
      resource.exported = true

      Puppet::Rails::Resource.rails_resource_initial_args(resource)[:exported].should be_true
    end

    it "should set 'exported' to false of the resource is not exported" do
      resource = Puppet::Resource.new(:file, "/file")
      resource.exported = false

      Puppet::Rails::Resource.rails_resource_initial_args(resource)[:exported].should be_false

      resource = Puppet::Resource.new(:file, "/file")
      resource.exported = nil

      Puppet::Rails::Resource.rails_resource_initial_args(resource)[:exported].should be_false
    end
  end

  describe "when merging in a parser resource" do
    before do
      @parser = mock 'parser resource'

      @resource = Puppet::Rails::Resource.new
      [:merge_attributes, :merge_parameters, :merge_tags, :save].each { |m| @resource.stubs(m) }
    end

    it "should merge the attributes" do
      @resource.expects(:merge_attributes).with(@parser)

      @resource.merge_parser_resource(@parser)
    end

    it "should merge the parameters" do
      @resource.expects(:merge_parameters).with(@parser)

      @resource.merge_parser_resource(@parser)
    end

    it "should merge the tags" do
      @resource.expects(:merge_tags).with(@parser)

      @resource.merge_parser_resource(@parser)
    end

    it "should save itself" do
      @resource.expects(:save)

      @resource.merge_parser_resource(@parser)
    end
  end

  describe "merge_parameters" do
    it "should replace values that have changed" do
      @resource = Puppet::Rails::Resource.new
      @resource.params_list = [{"name" => "replace", "value" => 1, "id" => 100 }]

      Puppet::Rails::ParamValue.expects(:delete).with([100])
      param_values = stub "param_values"
      param_values.expects(:build).with({:value=>nil, :param_name=>nil, :line=>{"replace"=>2}})
      @resource.stubs(:param_values).returns(param_values)

      Puppet::Rails::ParamName.stubs(:accumulate_by_name)

      merge_resource = stub "merge_resource"
      merge_resource.expects(:line).returns({ "replace" => 2 })
      merge_resource.stubs(:each).yields([["replace", 2]])

      @resource.merge_parameters(merge_resource)
    end
  end

  describe "#to_resource" do
    it "should instantiate a Puppet::Parser::Resource" do
      scope = stub "scope", :source => nil, :environment => nil, :namespaces => nil

      @resource = Puppet::Rails::Resource.new
      @resource.stubs(:attributes).returns({
        "restype" => 'notify',
        "title"   => 'hello'
      })
      @resource.stubs(:param_names).returns([])

      @resource.to_resource(scope).should be_a(Puppet::Parser::Resource)

    end
  end
end