Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
writing more "real" test cases

Signed-off-by: Avelino <avelinorun@gmail.com>
  • Loading branch information
avelino committed Dec 24, 2021
1 parent 49e5d5c commit 3bc09b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
9 changes: 5 additions & 4 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
// EndpointRules checks if there is a custom caching rule for the endpoint
func EndpointRules(uri string) (cacheEnable bool, time int) {
cacheEnable = false
if config.PrestConf.Cache && len(config.PrestConf.CacheEndpoints) == 0 {
if config.PrestConf.Cache.Enabled && len(config.PrestConf.Cache.Endpoints) == 0 {
cacheEnable = true
}
time = config.PrestConf.CacheTime
for _, endpoint := range config.PrestConf.CacheEndpoints {

time = config.PrestConf.Cache.Time
for _, endpoint := range config.PrestConf.Cache.Endpoints {
if endpoint.Endpoint == uri {
cacheEnable = true
time = endpoint.CacheTime
time = endpoint.Time
return
}
}
Expand Down
31 changes: 28 additions & 3 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ import (
"github.com/prest/prest/config"
)

func init() {
os.Setenv("PREST_CONF", "./testdata/prest.toml")
os.Setenv("PREST_CACHE_ENABLED", "true")
config.Load()
}
func TestEndpointRulesEnable(t *testing.T) {
os.Setenv("PREST_CONF", "./testdata/prest.toml")
os.Setenv("PREST_CACHE", "true")
os.Setenv("PREST_CACHE_ENABLED", "true")
config.Load()
config.PrestConf.Cache.Endpoints = append(config.PrestConf.Cache.Endpoints, config.CacheEndpoint{
Time: 5,
Endpoint: "/prest/public/test",
Enabled: true,
})
cacheEnable, cacheTime := EndpointRules("/prest/public/test")
if !cacheEnable {
t.Errorf("expected cache endpoint rule true, but got %t", cacheEnable)
Expand All @@ -20,11 +30,26 @@ func TestEndpointRulesEnable(t *testing.T) {
}
}

func TestEndpointRulesNotExist(t *testing.T) {
cacheEnable, _ := EndpointRules("/prest/public/test-notexist")
if cacheEnable {
t.Errorf("expected cache endpoint rule false, but got %t", cacheEnable)
}
}

func TestEndpointRulesDisable(t *testing.T) {
os.Setenv("PREST_CACHE", "true")
os.Setenv("PREST_CONF", "./testdata/prest.toml")
os.Setenv("PREST_CACHE_ENABLED", "true")
config.Load()
cacheEnable, _ := EndpointRules("/prest/public/test-disable")
config.PrestConf.Cache.Endpoints = append(config.PrestConf.Cache.Endpoints, config.CacheEndpoint{
Endpoint: "/prest/public/test-disable",
Enabled: false,
})
cacheEnable, cacheTime := EndpointRules("/prest/public/test-diable")
if cacheEnable {
t.Errorf("expected cache endpoint rule false, but got %t", cacheEnable)
}
if cacheTime == 10 {
t.Errorf("expected cache endpoint time is nil, but got %d", cacheTime)
}
}

0 comments on commit 3bc09b5

Please sign in to comment.