|
| 1 | +// Copyright 2017-present The Hugo Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package cache |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | +) |
| 22 | + |
| 23 | +func TestNewPartitionedLazyCache(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + assert := require.New(t) |
| 27 | + |
| 28 | + p1 := Partition{ |
| 29 | + Key: "p1", |
| 30 | + Load: func() (map[string]interface{}, error) { |
| 31 | + return map[string]interface{}{ |
| 32 | + "p1_1": "p1v1", |
| 33 | + "p1_2": "p1v2", |
| 34 | + "p1_nil": nil, |
| 35 | + }, nil |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + p2 := Partition{ |
| 40 | + Key: "p2", |
| 41 | + Load: func() (map[string]interface{}, error) { |
| 42 | + return map[string]interface{}{ |
| 43 | + "p2_1": "p2v1", |
| 44 | + "p2_2": "p2v2", |
| 45 | + "p2_3": "p2v3", |
| 46 | + }, nil |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + cache := NewPartitionedLazyCache(p1, p2) |
| 51 | + |
| 52 | + v, err := cache.Get("p1", "p1_1") |
| 53 | + assert.NoError(err) |
| 54 | + assert.Equal("p1v1", v) |
| 55 | + |
| 56 | + v, err = cache.Get("p1", "p2_1") |
| 57 | + assert.NoError(err) |
| 58 | + assert.Nil(v) |
| 59 | + |
| 60 | + v, err = cache.Get("p1", "p1_nil") |
| 61 | + assert.NoError(err) |
| 62 | + assert.Nil(v) |
| 63 | + |
| 64 | + v, err = cache.Get("p2", "p2_3") |
| 65 | + assert.NoError(err) |
| 66 | + assert.Equal("p2v3", v) |
| 67 | + |
| 68 | + v, err = cache.Get("doesnotexist", "p1_1") |
| 69 | + assert.NoError(err) |
| 70 | + assert.Nil(v) |
| 71 | + |
| 72 | + v, err = cache.Get("p1", "doesnotexist") |
| 73 | + assert.NoError(err) |
| 74 | + assert.Nil(v) |
| 75 | + |
| 76 | + errorP := Partition{ |
| 77 | + Key: "p3", |
| 78 | + Load: func() (map[string]interface{}, error) { |
| 79 | + return nil, errors.New("Failed") |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + cache = NewPartitionedLazyCache(errorP) |
| 84 | + |
| 85 | + v, err = cache.Get("p1", "doesnotexist") |
| 86 | + assert.NoError(err) |
| 87 | + assert.Nil(v) |
| 88 | + |
| 89 | + _, err = cache.Get("p3", "doesnotexist") |
| 90 | + assert.Error(err) |
| 91 | + |
| 92 | +} |
0 commit comments