Class: TestBlockCache

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/block_cache.rb

Overview

Unit tests for the BlockCache class

This test suite verifies correct behavior of:

  • initialization (with and without initial cache)

  • finding objects by id

  • getting or creating objects via block or proc

  • adding objects (including replacing existing ones)

  • removing objects by id

  • clearing the cache

  • reporting cache size

Instance Method Summary collapse

Instance Method Details

#setupObject



97
98
99
100
101
102
# File 'lib/block_cache.rb', line 97

def setup
  @obj1 = DummyObject.new(1)
  @obj2 = DummyObject.new(2)
  @initial_cache = [@obj1]
  @cache = BlockCache.new(@initial_cache.dup)
end

#test_add_appends_new_objectObject



150
151
152
153
154
155
# File 'lib/block_cache.rb', line 150

def test_add_appends_new_object
  result = @cache.add(@obj2)
  assert_equal @obj2, result
  assert_equal 2, @cache.size
  assert_equal [@obj1, @obj2], @cache.cache
end

#test_add_raises_for_object_without_id_methodObject



165
166
167
168
169
170
# File 'lib/block_cache.rb', line 165

def test_add_raises_for_object_without_id_method
  invalid = Object.new
  assert_raises(ArgumentError) do
    @cache.add(invalid)
  end
end

#test_add_replaces_existing_object_with_same_idObject



157
158
159
160
161
162
163
# File 'lib/block_cache.rb', line 157

def test_add_replaces_existing_object_with_same_id
  replacement = DummyObject.new(1)
  result = @cache.add(replacement)
  assert_equal replacement, result
  assert_equal 1, @cache.size
  assert_equal replacement, @cache.find_by_id(1)
end

#test_clear_empties_the_cacheObject



186
187
188
189
190
# File 'lib/block_cache.rb', line 186

def test_clear_empties_the_cache
  @cache.clear
  assert_equal [], @cache.cache
  assert_equal 0, @cache.size
end

#test_find_by_id_returns_matching_objectObject



113
114
115
# File 'lib/block_cache.rb', line 113

def test_find_by_id_returns_matching_object
  assert_equal @obj1, @cache.find_by_id(1)
end

#test_find_by_id_returns_nil_when_not_foundObject



117
118
119
# File 'lib/block_cache.rb', line 117

def test_find_by_id_returns_nil_when_not_found
  assert_nil @cache.find_by_id(999)
end

#test_get_or_create_creates_with_blockObject



127
128
129
130
131
132
133
# File 'lib/block_cache.rb', line 127

def test_get_or_create_creates_with_block
  new_obj = @cache.get_or_create(3) { |id| DummyObject.new(id) }
  assert_instance_of DummyObject, new_obj
  assert_equal 3, new_obj.id
  assert_includes @cache.cache, new_obj
  assert_equal 2, @cache.size
end

#test_get_or_create_creates_with_procObject



135
136
137
138
139
140
141
142
# File 'lib/block_cache.rb', line 135

def test_get_or_create_creates_with_proc
  factory = proc { |id| DummyObject.new(id * 10) }
  new_obj = @cache.get_or_create(5, factory)
  assert_instance_of DummyObject, new_obj
  assert_equal 50, new_obj.id
  assert_equal new_obj, @cache.find_by_id(50)
  assert_equal 2, @cache.size
end

#test_get_or_create_raises_without_block_or_procObject



144
145
146
147
148
# File 'lib/block_cache.rb', line 144

def test_get_or_create_raises_without_block_or_proc
  assert_raises(ArgumentError) do
    @cache.get_or_create(7)
  end
end

#test_get_or_create_returns_existing_objectObject



121
122
123
124
125
# File 'lib/block_cache.rb', line 121

def test_get_or_create_returns_existing_object
  found = @cache.get_or_create(1) { |id| DummyObject.new(id) }
  assert_equal @obj1, found
  assert_equal 1, @cache.size
end

#test_initialize_defaults_to_empty_arrayObject



104
105
106
107
# File 'lib/block_cache.rb', line 104

def test_initialize_defaults_to_empty_array
  empty_cache = BlockCache.new
  assert_equal [], empty_cache.cache
end

#test_initialize_with_provided_arrayObject



109
110
111
# File 'lib/block_cache.rb', line 109

def test_initialize_with_provided_array
  assert_equal @initial_cache, @cache.cache
end

#test_remove_deletes_object_by_idObject



172
173
174
175
176
177
178
# File 'lib/block_cache.rb', line 172

def test_remove_deletes_object_by_id
  @cache.add(@obj2)
  @cache.remove(1)
  assert_nil @cache.find_by_id(1)
  assert_equal 1, @cache.size
  assert_equal [@obj2], @cache.cache
end

#test_remove_nonexistent_id_leaves_cache_unchangedObject



180
181
182
183
184
# File 'lib/block_cache.rb', line 180

def test_remove_nonexistent_id_leaves_cache_unchanged
  original = @cache.cache.dup
  @cache.remove(999)
  assert_equal original, @cache.cache
end

#test_size_returns_number_of_cached_objectsObject



192
193
194
195
196
# File 'lib/block_cache.rb', line 192

def test_size_returns_number_of_cached_objects
  assert_equal 1, @cache.size
  @cache.add(@obj2)
  assert_equal 2, @cache.size
end