blob: f4741c53093465deae00734d1e58fd9ebe11e460 (
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 ruby
require 'spec_helper'
require 'puppet/util/rails/cache_accumulator'
describe Puppet::Util::CacheAccumulator do
before :each do
@test_class = Class.new do
attr_accessor :name
include Puppet::Util::CacheAccumulator
accumulates :name
def initialize(n)
self.name = n
end
end
end
it 'should delegate to underlying find_or_create_by_* method and accumulate results' do
@test_class.expects(:find_or_create_by_name).with('foo').returns(@test_class.new('foo')).once
obj = @test_class.accumulate_by_name('foo')
obj.name.should == 'foo'
@test_class.accumulate_by_name('foo').should == obj
end
it 'should delegate bulk lookups to find with appropriate arguments and returning result count' do
@test_class.expects(:find).with(
:all,
:conditions => {:name => ['a', 'b', 'c']}
).returns(['a','b','c'].collect {|n| @test_class.new(n)}).once
@test_class.accumulate_by_name('a', 'b', 'c').should == 3
end
it 'should only need find_or_create_by_name lookup for missing bulk entries' do
@test_class.expects(:find).with(
:all,
:conditions => {:name => ['a', 'b']}
).returns([ @test_class.new('a') ]).once
@test_class.expects(:find_or_create_by_name).with('b').returns(@test_class.new('b')).once
@test_class.expects(:find_or_create_by_name).with('a').never
@test_class.accumulate_by_name('a','b').should == 1
@test_class.accumulate_by_name('a').name.should == 'a'
@test_class.accumulate_by_name('b').name.should == 'b'
end
it 'should keep consumer classes separate' do
@alt_class = Class.new do
attr_accessor :name
include Puppet::Util::CacheAccumulator
accumulates :name
def initialize(n)
self.name = n
end
end
name = 'foo'
@test_class.expects(:find_or_create_by_name).with(name).returns(@test_class.new(name)).once
@alt_class.expects(:find_or_create_by_name).with(name).returns(@alt_class.new(name)).once
[@test_class, @alt_class].each do |klass|
klass.accumulate_by_name(name).name.should == name
klass.accumulate_by_name(name).class.should == klass
end
end
it 'should clear accumulated cache with reset_*_accumulator' do
# figure out how to test this appropriately...
end
end
|