summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/active_record_spec.rb
blob: 2baef33fe5046ca968f4ed7eb78d2b0cb31f0cd8 (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
#!/usr/bin/env rspec
require 'spec_helper'

require 'puppet/rails'
require 'puppet/indirector/active_record'

describe Puppet::Indirector::ActiveRecord do
  before do
    Puppet::Rails.stubs(:init)

    Puppet::Indirector::Terminus.stubs(:register_terminus_class)
    @model = mock 'model'
    @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
    Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)

    module Testing; end
    @active_record_class = class Testing::MyActiveRecord < Puppet::Indirector::ActiveRecord
      self
    end

    @ar_model = mock 'ar_model'

    @active_record_class.use_ar_model @ar_model
    @terminus = @active_record_class.new

    @name = "me"
    @instance = stub 'instance', :name => @name

    @request = stub 'request', :key => @name, :instance => @instance
  end

  it "should allow declaration of an ActiveRecord model to use" do
    @active_record_class.use_ar_model "foo"
    @active_record_class.ar_model.should == "foo"
  end

  describe "when initializing" do
    it "should init Rails" do
      Puppet::Rails.expects(:init)
      @active_record_class.new
    end
  end

  describe "when finding an instance" do
    it "should use the ActiveRecord model to find the instance" do
      @ar_model.expects(:find_by_name).with(@name)

      @terminus.find(@request)
    end

    it "should return nil if no instance is found" do
      @ar_model.expects(:find_by_name).with(@name).returns nil
      @terminus.find(@request).should be_nil
    end

    it "should convert the instance to a Puppet object if it is found" do
      instance = mock 'rails_instance'
      instance.expects(:to_puppet).returns "mypuppet"

      @ar_model.expects(:find_by_name).with(@name).returns instance
      @terminus.find(@request).should == "mypuppet"
    end
  end

  describe "when saving an instance" do
    it "should use the ActiveRecord model to convert the instance into a Rails object and then save that rails object" do
      rails_object = mock 'rails_object'
      @ar_model.expects(:from_puppet).with(@instance).returns rails_object

      rails_object.expects(:save)

      @terminus.save(@request)
    end
  end
end