|
1 | 1 | require 'test_helper' |
2 | 2 |
|
3 | 3 | class CollectionTest < ActiveSupport::TestCase |
| 4 | + teardown do |
| 5 | + DummyMaterial.clear_index! |
| 6 | + end |
| 7 | + |
| 8 | + class DummyMaterial < ::Material |
| 9 | + def self.index |
| 10 | + (@index ||= Hash.new).values.flatten.uniq |
| 11 | + end |
| 12 | + |
| 13 | + def self.add_to_index(m) |
| 14 | + index |
| 15 | + @index[m.id] = m.reload.collections.to_a |
| 16 | + end |
| 17 | + |
| 18 | + def self.clear_index! |
| 19 | + @index = Hash.new |
| 20 | + end |
| 21 | + |
| 22 | + def solr_index |
| 23 | + self.class.add_to_index(self) |
| 24 | + end |
| 25 | + end |
4 | 26 |
|
5 | 27 | test 'visibility scope' do |
6 | 28 | assert_not_includes Collection.visible_by(nil), collections(:secret_collection) |
@@ -92,4 +114,48 @@ class CollectionTest < ActiveSupport::TestCase |
92 | 114 | assert collection.destroy |
93 | 115 | end |
94 | 116 | end |
| 117 | + |
| 118 | + test 'index collection resources when collection created or destroyed' do |
| 119 | + user = users(:regular_user) |
| 120 | + material = materials(:good_material).becomes(DummyMaterial) |
| 121 | + with_settings(solr_enabled: true) do |
| 122 | + collection = user.collections.create!(title: 'test 123', materials: [material]) |
| 123 | + |
| 124 | + assert collection |
| 125 | + assert_includes DummyMaterial.index, collection |
| 126 | + assert_equal 1, DummyMaterial.index.length |
| 127 | + |
| 128 | + assert collection.destroy |
| 129 | + assert_equal 0, DummyMaterial.index.length |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + test 'index collection resources when collection renamed' do |
| 134 | + user = users(:regular_user) |
| 135 | + material = materials(:good_material).becomes(DummyMaterial) |
| 136 | + collection = user.collections.create!(title: 'test 123', materials: [material]) |
| 137 | + assert collection |
| 138 | + |
| 139 | + with_settings(solr_enabled: true) do |
| 140 | + assert_equal 0, DummyMaterial.index.length |
| 141 | + |
| 142 | + collection.update!(title: 'Hello world') |
| 143 | + assert_includes DummyMaterial.index, collection |
| 144 | + assert_equal 1, DummyMaterial.index.length |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + test 'do not index collection resources when collection updated without renaming' do |
| 149 | + user = users(:regular_user) |
| 150 | + material = materials(:good_material).becomes(DummyMaterial) |
| 151 | + collection = user.collections.create!(title: 'test 123', materials: [material]) |
| 152 | + assert collection |
| 153 | + |
| 154 | + with_settings(solr_enabled: true) do |
| 155 | + assert_equal 0, DummyMaterial.index.length |
| 156 | + |
| 157 | + collection.update!(description: 'hello') |
| 158 | + assert_equal 0, DummyMaterial.index.length |
| 159 | + end |
| 160 | + end |
95 | 161 | end |
0 commit comments