Retrofitting with tests

This commit is contained in:
Deluan 2016-02-29 10:04:30 -05:00
parent ec9398f2bd
commit b0bd0a63a7
8 changed files with 2057 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
lastupdate.tmp
gosonic
iTunes*.xml
iTunes.xml
iTunesFull.xml
gosonic.index
static/Jamstash
devDb

View File

@ -24,4 +24,5 @@ httpPort = 8081
enableAdmin = false
user=deluan
password=wordpass
dbPath = /tmp/testDb
dbPath = /tmp/testDb
musicFolder=./tests/iTunes.xml

View File

@ -1 +1 @@
-short
#-short

View File

@ -14,6 +14,7 @@ type BaseRepository struct {
table string
}
// TODO Use annotations to specify fields to be used
func (r *BaseRepository) NewId(fields ...string) string {
s := fmt.Sprintf("%s\\%s", strings.ToUpper(r.table), strings.Join(fields, ""))
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
@ -24,14 +25,7 @@ func (r *BaseRepository) CountAll() (int, error) {
return len(ids), err
}
func (r *BaseRepository) saveOrUpdate(id string, rec interface{}) error {
return r.saveEntity(id, rec)
}
func (r *BaseRepository) Dump() {
}
func (r *BaseRepository) saveEntity(id string, entity interface{}) error {
func (r *BaseRepository) saveOrUpdate(id string, entity interface{}) error {
recordPrefix := fmt.Sprintf("%s:%s:", r.table, id)
allKey := r.table + "s:all"

View File

@ -0,0 +1,89 @@
package repositories
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/deluan/gosonic/tests"
"fmt"
)
type TestEntity struct {
Id string
Name string
}
func shouldBeEqual(actualStruct interface{}, expectedStruct ...interface{}) string {
actual := fmt.Sprintf("%#v", actualStruct)
expected := fmt.Sprintf("%#v", expectedStruct[0])
return ShouldEqual(actual, expected)
}
func TestIntegrationBaseRepository(t *testing.T) {
tests.Init(t, true)
Convey("Subject: saveOrUpdate", t, func() {
Convey("Given an empty DB", func() {
dropDb()
repo := &BaseRepository{table: "test"}
Convey("When I save a new entity", func() {
entity := &TestEntity{"123", "My Name"}
err := repo.saveOrUpdate("123", entity)
Convey("Then the method shouldn't return any errors", func() {
So(err, ShouldBeNil)
})
Convey("Then the number of entities should be 1", func() {
count, _ := repo.CountAll()
So(count, ShouldEqual, 1)
})
Convey("And this entity should be equal to the the saved one", func() {
actualEntity := &TestEntity{}
repo.loadEntity("123", actualEntity)
So(actualEntity, shouldBeEqual, entity)
})
})
})
Convey("Given a table with one entity", func() {
dropDb()
repo := &BaseRepository{table: "test"}
entity := &TestEntity{"111", "One Name"}
repo.saveOrUpdate(entity.Id, entity)
Convey("When I save an entity with a different Id", func() {
newEntity := &TestEntity{"222", "Another Name"}
repo.saveOrUpdate(newEntity.Id, newEntity)
Convey("Then the number of entities should be 2", func() {
count, _ := repo.CountAll()
So(count, ShouldEqual, 2)
})
})
Convey("When I save an entity with the same Id", func() {
newEntity := &TestEntity{"111", "New Name"}
repo.saveOrUpdate(newEntity.Id, newEntity)
Convey("Then the number of entities should be 1", func() {
count, _ := repo.CountAll()
So(count, ShouldEqual, 1)
})
Convey("And the entity should be updated", func() {
actualEntity := &TestEntity{}
repo.loadEntity("111", actualEntity)
So(actualEntity.Name, ShouldEqual, newEntity.Name)
})
})
})
})
}

View File

@ -0,0 +1,44 @@
package repositories
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/deluan/gosonic/tests"
)
func TestUnitBaseRepository(t *testing.T) {
tests.Init(t, false)
Convey("Subject: NewId", t, func() {
repo := &BaseRepository{table: "test_table"}
Convey("When I call NewId with a name", func() {
Id := repo.NewId("a name")
Convey("Then it should return a new Id", func() {
So(Id, ShouldNotBeEmpty)
})
})
Convey("When I call NewId with the same name twice", func() {
FirstId := repo.NewId("a name")
SecondId := repo.NewId("a name")
Convey("Then it should return the same Id each time", func() {
So(FirstId, ShouldEqual, SecondId)
})
})
Convey("When I call NewId with different names", func() {
FirstId := repo.NewId("first name")
SecondId := repo.NewId("second name")
Convey("Then it should return different Ids", func() {
So(FirstId, ShouldNotEqual, SecondId)
})
})
})
}

View File

@ -8,6 +8,7 @@ import (
)
var (
_ledisInstance *ledis.Ledis
_dbInstance *ledis.DB
once sync.Once
)
@ -21,7 +22,14 @@ func db() *ledis.DB {
if err != nil {
panic(err)
}
_ledisInstance = l
_dbInstance = instance
})
return _dbInstance
}
func dropDb() {
db()
_ledisInstance.FlushAll()
}

1909
tests/fixtures/itunes-library.xml vendored Normal file

File diff suppressed because it is too large Load Diff