Merges Album and AlbumDescription
This commit is contained in:
83
album_cover_cache_test.go
Normal file
83
album_cover_cache_test.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
type AlbumCoverCacheSuite struct{}
|
||||
|
||||
var _ = Suite(&AlbumCoverCacheSuite{})
|
||||
|
||||
// a simple HTTPGetter that will never GET anything
|
||||
type errorGetter struct{}
|
||||
|
||||
func (g *errorGetter) Get(URL string) (*http.Response, error) {
|
||||
return nil, fmt.Errorf("I will always have an error")
|
||||
}
|
||||
|
||||
func (s *AlbumCoverCacheSuite) TestCanFetchCache(c *C) {
|
||||
data := []*Album{
|
||||
&Album{
|
||||
ID: 41693,
|
||||
CoverURL: "http://www.bedetheque.com/media/Couvertures/Couv_41693.jpg",
|
||||
},
|
||||
&Album{
|
||||
ID: 1285,
|
||||
CoverURL: "http://www.bedetheque.com/media/Couvertures/OumpahPahLepeauxrouge.jpg",
|
||||
},
|
||||
}
|
||||
|
||||
tmpdir, err := ioutil.TempDir("", "satbdexplorer-tests-cache")
|
||||
c.Assert(err, IsNil)
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
cache, err := NewAlbumCoverCache(tmpdir, 10, 10*time.Second)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
var resData = []bytes.Buffer{}
|
||||
|
||||
for _, a := range data {
|
||||
cover, err := cache.GetCover(a)
|
||||
var buf bytes.Buffer
|
||||
if c.Check(err, IsNil) == true {
|
||||
_, err := io.Copy(&buf, cover)
|
||||
c.Check(err, IsNil)
|
||||
c.Check(cover.Close(), IsNil)
|
||||
}
|
||||
resData = append(resData, buf)
|
||||
}
|
||||
|
||||
cache.getter = &errorGetter{}
|
||||
|
||||
// now we check that we get it again, but from the disk, not
|
||||
// hitting the web
|
||||
for i, a := range data {
|
||||
cover, err := cache.GetCover(a)
|
||||
var buf bytes.Buffer
|
||||
if c.Check(err, IsNil) == true {
|
||||
_, err := io.Copy(&buf, cover)
|
||||
if c.Check(err, IsNil) == true {
|
||||
c.Check(buf.Bytes(), DeepEquals, resData[i].Bytes())
|
||||
}
|
||||
c.Check(cover.Close(), IsNil)
|
||||
}
|
||||
}
|
||||
|
||||
// now if we it the TTL, we will reftech and get error
|
||||
cache.TTL = 0
|
||||
|
||||
for _, a := range data {
|
||||
cover, err := cache.GetCover(a)
|
||||
c.Check(cover, IsNil)
|
||||
c.Check(err, ErrorMatches, "I will always have an error")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user