merged cont.
[opensuse:yast-rest-service.git] / webservice / vendor / plugins / static_record_cache / test / acts_as_static_record_test.rb
1 require File.dirname(__FILE__) + '/test_helper'
2
3 class StaticActiveRecordContextTest < TestCaseSuperClass
4   self.fixtures :carriers, :phone_numbers
5
6
7   def setup
8     super
9     StaticCarrierWithKey.clear_static_record_cache
10     StaticCarrier.clear_static_record_cache
11     StaticCarrierWithNonColumnKey.clear_static_record_cache
12   end
13   
14   def test_should_load_all_records
15     StaticCarrier.find :all
16     assert_equal Carrier.count, cache_instance(StaticCarrier)[:primary_key].size
17   end
18
19   def test_should_load_all_records_with_key
20     StaticCarrierWithKey.find :all
21     assert_equal Carrier.count, cache_instance(StaticCarrierWithKey)[:primary_key].size
22     assert_equal Carrier.count, cache_instance(StaticCarrierWithKey)[:key].size
23
24     #cached on key
25     cache_instance(StaticCarrierWithKey)[:key].each {|cache_key, cache_item|
26       assert(cache_key, cache_item.name)
27     }
28   end
29
30   def test_should_load_all_records_with_non_column_key
31     StaticCarrierWithNonColumnKey.find :all
32     assert_equal Carrier.count, cache_instance(StaticCarrierWithNonColumnKey)[:primary_key].size
33     assert_equal Carrier.count, cache_instance(StaticCarrierWithNonColumnKey)[:key].size
34
35     #cached on key
36     cache_instance(StaticCarrierWithNonColumnKey)[:key].each {|cache_key, cache_item|
37       assert(cache_key, cache_item.non_column)
38     }
39   end
40
41   def test_should_not_load_cache_with_conditions
42     StaticCarrier.find :all, :conditions => 'id is not null'
43     assert_nil cache_instance(StaticCarrier)
44   end
45
46   def test_should_load_all_when_accessing_one_record
47     phone = phone_numbers(:phone_number_1)
48     phone.static_carrier
49     phone.reload
50     assert_queries(0){
51      phone.static_carrier
52     }
53     assert_equal Carrier.count, cache_instance(StaticCarrier)[:primary_key].size
54     assert_equal 0, cache_instance(StaticCarrier)[:key].size
55   end
56
57   def test_should_define_method_for_non_column_key
58     assert StaticCarrierWithNonColumnKey.respond_to?(:find_by_non_column)
59
60     record = StaticCarrierWithNonColumnKey.find_by_non_column('NONCOLUMN: 1')
61     assert(1, record.to_param)
62   end
63
64
65   def test_finders_for_column_key
66
67     assert_queries(1) {
68       record = StaticCarrierWithKey.find_by_name_and_id('Verizon', 1)
69       assert(record)
70       assert_equal('Verizon', record.name)
71       assert_equal("1", record.to_param)
72
73       record = StaticCarrierWithKey.find_by_name_and_id('Verizon', 2)
74       assert_nil(record)
75     }
76   end
77
78   def test_finders_for_primary_key_id
79
80     StaticCarrierWithKey.find :all
81     assert_queries(0) {
82       record = StaticCarrierWithKey.find_by_id(1)
83       assert(record)
84       assert_equal('Verizon', record.name)
85       assert_equal("1", record.to_param)
86     }
87     
88     assert_queries(1) {
89       record = StaticCarrierWithKey.find_by_id(2, :conditions => 'id = 1')
90       assert_nil(record)
91     }
92   end
93
94   def test_finders_for_column_key_base
95
96     StaticCarrierWithKey.find :all
97     assert_queries(0) {
98       record = StaticCarrierWithKey.find_by_name('Verizon')
99       assert(record)
100       assert_equal('Verizon', record.name)
101       assert_equal("1", record.to_param)
102     }
103     assert_queries(1) {
104       record = StaticCarrierWithKey.find_by_name('Verizondd', :conditions => 'id is not null')
105       assert_nil(record)
106     }
107   end
108
109
110   def test_finders_for_column_key_with_conditions
111     #load up the cache
112     StaticCarrierWithKey.find :all
113     
114     assert_queries(1) {
115       #This performs one query because the conditions are not nil
116       record = StaticCarrierWithKey.find_by_name_and_id('Verizon', 1, :conditions => 'id is not null')
117       assert(record)
118       assert_equal('Verizon', record.name)
119       assert_equal("1", record.to_param)
120
121       #This should use the cache
122       record = StaticCarrierWithKey.find_by_name_and_id('Verizon', 1)
123       assert(record)
124       assert_equal('Verizon', record.name)
125       assert_equal("1", record.to_param)
126     }
127     
128     #This should use the newly defined method and run it since there are conditions
129     assert_queries(1) {
130       #This performs one query because the conditions are not nil
131       record = StaticCarrierWithKey.find_by_name_and_id('Verizon', 1, :conditions => 'id is not null')
132       assert(record)
133       assert_equal('Verizon', record.name)
134       assert_equal("1", record.to_param)
135     }
136   end
137
138
139   protected
140
141   #calling static_record_cache invokes the cache
142   def cache_instance(klass)
143     klass.instance_variable_get('@static_record_cache')
144   end
145
146 end