implemented category methods

This commit is contained in:
Philipp Häfelfinger 2019-04-06 23:38:39 +02:00
parent 6acd3f8a55
commit 5ca8a92e22
2 changed files with 150 additions and 4 deletions

View File

@ -47,7 +47,7 @@ func (img *ImageMetaData) String() string {
type CategoryProvider interface {
SaveCategory(category CategoryData) error
GetCategoryByPiwigoId(id int) (CategoryData, error)
GetCategoryByPiwigoId(piwigoId int) (CategoryData, error)
GetCategoryByKey(key string) (CategoryData, error)
GetCategoriesToCreate() ([]CategoryData, error)
}
@ -337,8 +337,38 @@ func (d *LocalDataStore) SaveCategory(category CategoryData) error {
return tx.Commit()
}
func (d *LocalDataStore) GetCategoryByPiwigoId(id int) (CategoryData, error) {
panic("implement me")
func (d *LocalDataStore) GetCategoryByPiwigoId(piwigoId int) (CategoryData, error) {
logrus.Tracef("Query category by piwigoid %d", piwigoId)
cat := CategoryData{}
db, err := d.openDatabase()
if err != nil {
return cat, err
}
defer db.Close()
stmt, err := db.Prepare("SELECT categoryId, piwigoId, piwigoParentId, name, key FROM category WHERE piwigoId = ?")
if err != nil {
return cat, err
}
rows, err := stmt.Query(piwigoId)
if err != nil {
return cat, err
}
defer rows.Close()
if rows.Next() {
err = readCategoryFromRow(rows, &cat)
if err != nil {
return cat, err
}
} else {
return cat, ErrorRecordNotFound
}
err = rows.Err()
return cat, err
}
func (d *LocalDataStore) GetCategoryByKey(key string) (CategoryData, error) {
@ -376,7 +406,37 @@ func (d *LocalDataStore) GetCategoryByKey(key string) (CategoryData, error) {
}
func (d *LocalDataStore) GetCategoriesToCreate() ([]CategoryData, error) {
panic("implement me")
logrus.Trace("Query categories to create on piwigo")
db, err := d.openDatabase()
if err != nil {
return nil, err
}
defer db.Close()
stmt, err := db.Prepare("SELECT categoryId, piwigoId, piwigoParentId, name, key FROM category WHERE piwigoId = 0 ORDER BY key")
if err != nil {
return nil, err
}
rows, err := stmt.Query()
if err != nil {
return nil, err
}
defer rows.Close()
var categories []CategoryData
for rows.Next() {
cat := CategoryData{}
err = readCategoryFromRow(rows, &cat)
if err != nil {
return nil, err
}
categories = append(categories, cat)
}
err = rows.Err()
return categories, err
}
func (d *LocalDataStore) openDatabase() (*sql.DB, error) {

View File

@ -306,6 +306,77 @@ func Test_saveCategory_should_store_records(t *testing.T) {
}
}
func Test_saveCategory_should_update_records(t *testing.T) {
if !dbinitOk {
t.Skip("Skipping test as TestDataStoreInitialize failed!")
}
dataStore := setupDatabase(t)
defer cleanupDatabase(t)
category := getExampleCategoryData("2019")
saveCategoryShouldNotFail("addcategory", dataStore, category, t)
category.CategoryId = 1
category.Name = "2019-1"
category.Key = category.Name
category.PiwigoId = 2
category.PiwigoParentId = 3
saveCategoryShouldNotFail("updatecategory", dataStore, category, t)
loadedCategory, err := dataStore.GetCategoryByKey(category.Key)
if err != nil {
t.Fatalf("Could not query category! %s", err)
}
ensureLoadedCategoryIsExpectedCategory(loadedCategory, category, t)
}
func Test_GetCategoryByPiwigoId_should_return_category(t *testing.T) {
if !dbinitOk {
t.Skip("Skipping test as TestDataStoreInitialize failed!")
}
dataStore := setupDatabase(t)
defer cleanupDatabase(t)
category := getExampleCategoryData("2019")
saveCategoryShouldNotFail("getCategoryByPiwigoId", dataStore, category, t)
category.CategoryId = 1
loadedCategory, err := dataStore.GetCategoryByPiwigoId(category.PiwigoId)
if err != nil {
t.Fatalf("Could not query category! %s", err)
}
ensureLoadedCategoryIsExpectedCategory(loadedCategory, category, t)
}
func Test_GetCategoriesToCreate(t *testing.T) {
if !dbinitOk {
t.Skip("Skipping test as TestDataStoreInitialize failed!")
}
dataStore := setupDatabase(t)
defer cleanupDatabase(t)
category := getExampleCategoryData("2019")
category.PiwigoId = 0
saveCategoryShouldNotFail("getCategoriesToCreate", dataStore, category, t)
category.CategoryId = 1
categories, err := dataStore.GetCategoriesToCreate()
if err != nil {
t.Fatalf("Could not query category! %s", err)
}
if len(categories) != 1 {
t.Error("Did not load categories to create correctly!")
}
ensureLoadedCategoryIsExpectedCategory(categories[0], category, t)
}
func saveImageShouldNotFail(action string, dataStore *LocalDataStore, img ImageMetaData, t *testing.T) {
err := dataStore.SaveImageMetadata(img)
if err != nil {
@ -375,3 +446,18 @@ func setupDatabase(t *testing.T) *LocalDataStore {
dbinitOk = true
return dataStore
}
func ensureLoadedCategoryIsExpectedCategory(loaded CategoryData, expected CategoryData, t *testing.T) {
if loaded.Name != expected.Name {
t.Errorf("category update failed. Got: %s - want: %s", loaded.Name, expected.Name)
}
if loaded.Key != expected.Key {
t.Errorf("category update failed. Got: %s - want: %s", loaded.Key, expected.Key)
}
if loaded.PiwigoId != expected.PiwigoId {
t.Errorf("category update failed. Got: %d - want: %d", loaded.PiwigoId, expected.PiwigoId)
}
if loaded.PiwigoParentId != expected.PiwigoParentId {
t.Errorf("category update failed. Got: %d - want: %d", loaded.PiwigoParentId, expected.PiwigoParentId)
}
}