added tests for getParentId
fixed some small issues in getParentId
This commit is contained in:
parent
7a2633d221
commit
fa54c63712
@ -145,17 +145,20 @@ func createMissingCategories(piwigoApi piwigo.PiwigoCategoryApi, db datastore.Ca
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getParentId(category datastore.CategoryData, db datastore.CategoryProvider) (int, error) {
|
func getParentId(category datastore.CategoryData, db datastore.CategoryProvider) (int, error) {
|
||||||
|
if category.Key == "" || category.Key == "." {
|
||||||
|
msg := fmt.Sprintf("Category with id %d has a invalid value in the keyfield!", category.CategoryId)
|
||||||
|
logrus.Warnf(msg)
|
||||||
|
return 0, errors.New(msg)
|
||||||
|
}
|
||||||
|
|
||||||
parentKey := filepath.Dir(category.Key)
|
parentKey := filepath.Dir(category.Key)
|
||||||
if category.Name == parentKey {
|
if category.Name == parentKey || parentKey == "." || parentKey == "" {
|
||||||
logrus.Debugf("The category %s is a root category, there is no parent", category.Name)
|
logrus.Debugf("The category %s is a root category, there is no parent", category.Name)
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Debugf("Looking up parent with key %s", parentKey)
|
logrus.Debugf("Looking up parent with key %s", parentKey)
|
||||||
parentCategory, err := db.GetCategoryByKey(parentKey)
|
parentCategory, err := db.GetCategoryByKey(parentKey)
|
||||||
if err == datastore.ErrorRecordNotFound {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@ -74,8 +74,106 @@ func Test_updatePiwigoCategoriesFromServer_updates_a_category(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_getParentId_returns_0_for_root_nodes(t *testing.T) {
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
category := crateDbRootCategory()
|
||||||
|
|
||||||
|
dbmock := NewMockCategoryProvider(mockCtrl)
|
||||||
|
dbmock.EXPECT().GetCategoryByKey(gomock.Any()).Times(0)
|
||||||
|
|
||||||
|
parentId, err := getParentId(category, dbmock)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if parentId != 0 {
|
||||||
|
t.Errorf("Found parent id %d but expected 0", parentId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_getParentId_returns_error_if_parentkey_is_not_found(t *testing.T) {
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
category := createDbSubCategory()
|
||||||
|
|
||||||
|
dbmock := NewMockCategoryProvider(mockCtrl)
|
||||||
|
dbmock.EXPECT().GetCategoryByKey(gomock.Any()).Return(datastore.CategoryData{}, datastore.ErrorRecordNotFound).Times(1)
|
||||||
|
|
||||||
|
parentId, err := getParentId(category, dbmock)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("There should an error be returned if category key value is not valid!")
|
||||||
|
}
|
||||||
|
if parentId != 0 {
|
||||||
|
t.Errorf("Found parent id %d but expected 0", parentId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_getParentId_returns_error_if_key_invalid(t *testing.T) {
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
category := crateDbRootCategory()
|
||||||
|
category.Key = "."
|
||||||
|
|
||||||
|
dbmock := NewMockCategoryProvider(mockCtrl)
|
||||||
|
dbmock.EXPECT().GetCategoryByKey(gomock.Any()).Times(0)
|
||||||
|
|
||||||
|
parentId, err := getParentId(category, dbmock)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("There should an error be returned if category key value is not valid!")
|
||||||
|
}
|
||||||
|
if parentId != 0 {
|
||||||
|
t.Errorf("Found parent id %d but expected 0", parentId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_getParentId_finds_the_exptected_parent_id(t *testing.T) {
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
parentCategory := crateDbRootCategory()
|
||||||
|
category := createDbSubCategory()
|
||||||
|
|
||||||
|
dbmock := NewMockCategoryProvider(mockCtrl)
|
||||||
|
dbmock.EXPECT().GetCategoryByKey("2019").Return(parentCategory, nil).Times(1)
|
||||||
|
|
||||||
|
parentId, err := getParentId(category, dbmock)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if parentId != 1 {
|
||||||
|
t.Errorf("Found parent id %d but expected 1", parentId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func crateDbRootCategory() datastore.CategoryData {
|
||||||
|
parentCategory := datastore.CategoryData{
|
||||||
|
PiwigoId: 1,
|
||||||
|
PiwigoParentId: 0,
|
||||||
|
CategoryId: 1,
|
||||||
|
Key: "2019",
|
||||||
|
Name: "2019",
|
||||||
|
}
|
||||||
|
return parentCategory
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDbSubCategory() datastore.CategoryData {
|
||||||
|
category := datastore.CategoryData{
|
||||||
|
PiwigoId: 2,
|
||||||
|
PiwigoParentId: 0,
|
||||||
|
CategoryId: 2,
|
||||||
|
Key: "2019/testalbumb",
|
||||||
|
Name: "testalbumb",
|
||||||
|
}
|
||||||
|
return category
|
||||||
|
}
|
||||||
|
|
||||||
func createDbCategoriesFrom(categories map[string]*piwigo.PiwigoCategory) []datastore.CategoryData {
|
func createDbCategoriesFrom(categories map[string]*piwigo.PiwigoCategory) []datastore.CategoryData {
|
||||||
dbCategories := []datastore.CategoryData{}
|
var dbCategories []datastore.CategoryData
|
||||||
for _, cat := range categories {
|
for _, cat := range categories {
|
||||||
dbCat := datastore.CategoryData{
|
dbCat := datastore.CategoryData{
|
||||||
PiwigoId: cat.Id,
|
PiwigoId: cat.Id,
|
||||||
|
Loading…
Reference in New Issue
Block a user