fix(owm): cache also the url used to get the data

The url is used after do generate the hyperlink
This commit is contained in:
lnu 2021-09-24 12:46:32 +02:00 committed by Jan De Dobbeleer
parent 57174b6424
commit 95f776a8ed
2 changed files with 63 additions and 5 deletions

View file

@ -23,8 +23,10 @@ const (
Units Property = "units" Units Property = "units"
// CacheTimeout cache timeout // CacheTimeout cache timeout
CacheTimeout Property = "cache_timeout" CacheTimeout Property = "cache_timeout"
// CacheKey key used when caching the response // CacheKeyResponse key used when caching the response
CacheKey string = "owm_response" CacheKeyResponse string = "owm_response"
// CacheKeyUrl key used when caching the url responsible for the response
CacheKeyURL string = "owm_url"
) )
type weather struct { type weather struct {
@ -70,13 +72,14 @@ func (d *owm) getResult() (*OWMDataResponse, error) {
response := new(OWMDataResponse) response := new(OWMDataResponse)
if cacheTimeout > 0 { if cacheTimeout > 0 {
// check if data stored in cache // check if data stored in cache
val, found := d.env.cache().get(CacheKey) val, found := d.env.cache().get(CacheKeyResponse)
// we got something from te cache // we got something from te cache
if found { if found {
err := json.Unmarshal([]byte(val), response) err := json.Unmarshal([]byte(val), response)
if err != nil { if err != nil {
return nil, err return nil, err
} }
d.url, _ = d.env.cache().get(CacheKeyURL)
return response, nil return response, nil
} }
} }
@ -98,7 +101,8 @@ func (d *owm) getResult() (*OWMDataResponse, error) {
if cacheTimeout > 0 { if cacheTimeout > 0 {
// persist new forecasts in cache // persist new forecasts in cache
d.env.cache().set(CacheKey, string(body), cacheTimeout) d.env.cache().set(CacheKeyResponse, string(body), cacheTimeout)
d.env.cache().set(CacheKeyURL, d.url, cacheTimeout)
} }
return response, nil return response, nil
} }

View file

@ -185,6 +185,33 @@ func TestOWMSegmentIcons(t *testing.T) {
assert.Nil(t, o.setStatus()) assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string(), tc.Case) assert.Equal(t, expectedString, o.string(), tc.Case)
} }
// test with hyperlink enabled
for _, tc := range cases {
env := &MockedEnvironment{}
props := &properties{
values: map[Property]interface{}{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
CacheTimeout: 0,
EnableHyperlink: true,
},
}
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, tc.IconID)
expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", tc.ExpectedIconString)
env.On("doGet", OWMAPIURL).Return([]byte(response), nil)
o := &owm{
props: props,
env: env,
}
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string(), tc.Case)
}
} }
func TestOWMSegmentFromCache(t *testing.T) { func TestOWMSegmentFromCache(t *testing.T) {
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d") response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
@ -204,9 +231,36 @@ func TestOWMSegmentFromCache(t *testing.T) {
env: env, env: env,
} }
cache.On("get", "owm_response").Return(response, true) cache.On("get", "owm_response").Return(response, true)
cache.On("get", "owm_url").Return("http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key", true)
cache.On("set").Return()
env.On("cache", nil).Return(cache)
assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string())
}
func TestOWMSegmentFromCacheWithHyperlink(t *testing.T) {
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
expectedString := fmt.Sprintf("[%s (20°C)](http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", "\ufa98")
env := &MockedEnvironment{}
cache := &MockedCache{}
props := &properties{
values: map[Property]interface{}{
APIKey: "key",
Location: "AMSTERDAM,NL",
Units: "metric",
EnableHyperlink: true,
},
}
o := &owm{
props: props,
env: env,
}
cache.On("get", "owm_response").Return(response, true)
cache.On("get", "owm_url").Return("http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key", true)
cache.On("set").Return() cache.On("set").Return()
env.On("cache", nil).Return(cache) env.On("cache", nil).Return(cache)
// env.On("doGet", "http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key").Return([]byte(response), nil)
assert.Nil(t, o.setStatus()) assert.Nil(t, o.setStatus())
assert.Equal(t, expectedString, o.string()) assert.Equal(t, expectedString, o.string())