github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/cache/cache_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package cache 7 8 import ( 9 "fmt" 10 "testing" 11 "time" 12 13 "github.com/gitbundle/modules/setting" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func createTestCache() { 19 conn, _ = newCache(setting.Cache{ 20 Adapter: "memory", 21 TTL: time.Minute, 22 }) 23 } 24 25 func TestNewContext(t *testing.T) { 26 assert.NoError(t, NewContext()) 27 28 setting.CacheService.Cache = setting.Cache{Enabled: true, Adapter: "redis", Conn: "some random string"} 29 con, err := newCache(setting.Cache{ 30 Adapter: "rand", 31 Conn: "false conf", 32 Interval: 100, 33 }) 34 assert.Error(t, err) 35 assert.Nil(t, con) 36 } 37 38 func TestGetCache(t *testing.T) { 39 createTestCache() 40 41 assert.NotNil(t, GetCache()) 42 } 43 44 func TestGetString(t *testing.T) { 45 createTestCache() 46 47 data, err := GetString("key", func() (string, error) { 48 return "", fmt.Errorf("some error") 49 }) 50 assert.Error(t, err) 51 assert.Equal(t, "", data) 52 53 data, err = GetString("key", func() (string, error) { 54 return "", nil 55 }) 56 assert.NoError(t, err) 57 assert.Equal(t, "", data) 58 59 // data, err = GetString("key", func() (string, error) { 60 // return "some data", nil 61 // }) 62 // assert.NoError(t, err) 63 // assert.Equal(t, "", data) 64 // Remove("key") 65 66 data, err = GetString("key", func() (string, error) { 67 return "some data", nil 68 }) 69 assert.NoError(t, err) 70 assert.Equal(t, "some data", data) 71 72 // data, err = GetString("key", func() (string, error) { 73 // return "", fmt.Errorf("some error") 74 // }) 75 // assert.NoError(t, err) 76 // assert.Equal(t, "some data", data) 77 78 // TODO: uncommented code works in IDE but not with go test 79 } 80 81 func TestGetInt(t *testing.T) { 82 createTestCache() 83 84 data, err := GetInt("key", func() (int, error) { 85 return 0, fmt.Errorf("some error") 86 }) 87 assert.Error(t, err) 88 assert.Equal(t, 0, data) 89 90 data, err = GetInt("key", func() (int, error) { 91 return 0, nil 92 }) 93 assert.NoError(t, err) 94 assert.Equal(t, 0, data) 95 96 // data, err = GetInt("key", func() (int, error) { 97 // return 100, nil 98 // }) 99 // assert.NoError(t, err) 100 // assert.Equal(t, 0, data) 101 // Remove("key") 102 103 data, err = GetInt("key", func() (int, error) { 104 return 100, nil 105 }) 106 assert.NoError(t, err) 107 assert.Equal(t, 100, data) 108 109 // data, err = GetInt("key", func() (int, error) { 110 // return 0, fmt.Errorf("some error") 111 // }) 112 // assert.NoError(t, err) 113 // assert.Equal(t, 100, data) 114 115 // TODO: uncommented code works in IDE but not with go test 116 } 117 118 func TestGetInt64(t *testing.T) { 119 createTestCache() 120 121 data, err := GetInt64("key", func() (int64, error) { 122 return 0, fmt.Errorf("some error") 123 }) 124 assert.Error(t, err) 125 assert.EqualValues(t, 0, data) 126 127 data, err = GetInt64("key", func() (int64, error) { 128 return 0, nil 129 }) 130 assert.NoError(t, err) 131 assert.EqualValues(t, 0, data) 132 133 // data, err = GetInt64("key", func() (int64, error) { 134 // return 100, nil 135 // }) 136 // assert.NoError(t, err) 137 // assert.EqualValues(t, 0, data) 138 // Remove("key") 139 140 data, err = GetInt64("key", func() (int64, error) { 141 return 100, nil 142 }) 143 assert.NoError(t, err) 144 assert.EqualValues(t, 100, data) 145 146 // data, err = GetInt64("key", func() (int, error) { 147 // return 0, fmt.Errorf("some error") 148 // }) 149 // assert.NoError(t, err) 150 // assert.EqualValues(t, 100, data) 151 152 // TODO: uncommented code works in IDE but not with go test 153 }