From 40e1e55215abf7764228babf6ae1649826542808 Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Thu, 21 Jan 2016 19:34:35 +0100 Subject: [PATCH] Adds tests for AlbumDatabase --- album_database.go | 2 +- album_database_test.go | 53 ++++++++++++++++++++++++++++++++ album_description_getter_test.go | 1 + data_test.go | 6 +++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 album_database_test.go diff --git a/album_database.go b/album_database.go index af178dd..dd1ec8b 100644 --- a/album_database.go +++ b/album_database.go @@ -22,6 +22,6 @@ func (db *AlbumDatabase) Get(AlbumID) (*Album, error) { return nil, notYetImplemented() } -func (db *AlbumDatabase) ByPurchaseDate() ([]*Album, error) { +func (db *AlbumDatabase) ByPurchaseDate() ([]AlbumID, error) { return nil, notYetImplemented() } diff --git a/album_database_test.go b/album_database_test.go new file mode 100644 index 0000000..ef0bea4 --- /dev/null +++ b/album_database_test.go @@ -0,0 +1,53 @@ +package main + +import ( + "path/filepath" + + . "gopkg.in/check.v1" +) + +type AlbumDatabaseSuite struct { + db *AlbumDatabase +} + +var _ = Suite(&AlbumDatabaseSuite{}) + +func (s *AlbumDatabaseSuite) SetUpSuite(c *C) { + var err error + s.db, err = OpenAlbumDatabase(filepath.Join(c.MkDir(), "satdb.bar.satellite/db")) + c.Assert(err, IsNil) + + for _, a := range albumsDataTest { + c.Assert(s.db.AddOrUpdate(&a), IsNil) + } +} + +func (s *AlbumDatabaseSuite) TestCanDelete(c *C) { + defer func() { + //we can always add it twice + c.Assert(s.db.AddOrUpdate(&(albumsDataTest[0])), IsNil) + }() + + c.Assert(s.db.Delete(albumsDataTest[0].ID), IsNil) +} + +func (s *AlbumDatabaseSuite) TestCanGet(c *C) { + + for _, a := range albumsDataTest { + fromDb, err := s.db.Get(a.ID) + if c.Check(err, IsNil) == true { + c.Check(*fromDb, DeepEquals, a) + } + } +} + +func (s *AlbumDatabaseSuite) TestCanSort(c *C) { + // here + data := []AlbumID{} + sorted, err := s.db.ByPurchaseDate() + c.Assert(err, IsNil) + c.Assert(len(sorted), Equals, len(data)) + for i, a := range sorted { + c.Check(a, Equals, data[i]) + } +} diff --git a/album_description_getter_test.go b/album_description_getter_test.go index 2ff2297..4f83153 100644 --- a/album_description_getter_test.go +++ b/album_description_getter_test.go @@ -26,6 +26,7 @@ func (s *AlbumDescriptionGetterSuite) TestGet(c *C) { for _, a := range albumsDataTest { aStripped := StripNonCsvField(a) if c.Check(s.g.Get(&aStripped), IsNil) == true { + //we skip the fetch date, for sure it will always expire aStripped.FetchDate = a.FetchDate c.Check(aStripped, DeepEquals, a) } diff --git a/data_test.go b/data_test.go index 739dce5..ed7c250 100644 --- a/data_test.go +++ b/data_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "log" "os" "path/filepath" "regexp" @@ -83,6 +84,7 @@ func GetCsvData() *bytes.Buffer { } func init() { + start := time.Now() albumsPath := filepath.Join("testdata", "albums.json") f, err := os.Open(albumsPath) if err != nil { @@ -95,10 +97,12 @@ func init() { panic(fmt.Sprintf("Could not parse '%s': %s", albumsPath, err)) } + log.Printf("loaded test data in %s", time.Since(start)) + start = time.Now() dv := diskv.New(diskv.Options{ BasePath: filepath.Join("testdata", "web-cache"), CacheSizeMax: 100 * 1024 * 1024, // 100MB }) testGetter = httpcache.NewTransport(diskcache.NewWithDiskv(dv)).Client() - + log.Printf("loaded httpcache in %s", time.Since(start)) }