diff --git a/internal/pkg/category/category_test.go b/internal/pkg/category/category_test.go index e9b9cb7..d8bb70f 100644 --- a/internal/pkg/category/category_test.go +++ b/internal/pkg/category/category_test.go @@ -19,19 +19,17 @@ func Test_SynchronizePiwigoCategories_adds_new_categories(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() - piwigoCategory := createTestPiwigoCategory(1) - - piwigoCategories := make(map[string]*piwigo.PiwigoCategory) - piwigoCategories[piwigoCategory.Key] = &piwigoCategory - numberOfCategories := len(piwigoCategories) - - category := createTestCategoryData(1) + piwigoCategories := createTwoServerCategories() + dbCategories := createDbCategoriesFrom(piwigoCategories) dbmock := NewMockCategoryProvider(mockCtrl) - dbmock.EXPECT().SaveCategory(category).Times(numberOfCategories) + + for _, cat := range dbCategories { + dbmock.EXPECT().SaveCategory(cat).Times(1) + } piwigoMock := NewMockPiwigoCategoryApi(mockCtrl) - piwigoMock.EXPECT().GetAllCategories().Return(piwigoCategories, nil).Times(len(piwigoCategories)) + piwigoMock.EXPECT().GetAllCategories().Return(piwigoCategories, nil).Times(1) err := SynchronizePiwigoCategories(piwigoMock, dbmock) if err != nil { @@ -39,23 +37,36 @@ func Test_SynchronizePiwigoCategories_adds_new_categories(t *testing.T) { } } -func createTestPiwigoCategory(piwigoId int) piwigo.PiwigoCategory { - cat := piwigo.PiwigoCategory{ - Id: piwigoId, +func createDbCategoriesFrom(categories map[string]*piwigo.PiwigoCategory) []datastore.CategoryData { + dbCategories := []datastore.CategoryData{} + for _, cat := range categories { + dbCat := datastore.CategoryData{ + PiwigoId: cat.Id, + PiwigoParentId: cat.ParentId, + Key: cat.Key, + Name: cat.Name, + CategoryId: 0, + } + dbCategories = append(dbCategories, dbCat) + } + return dbCategories +} + +func createTwoServerCategories() map[string]*piwigo.PiwigoCategory { + piwigoCategory1 := piwigo.PiwigoCategory{ + Id: 1, Name: "2019", Key: "2019", ParentId: 0, } - return cat -} - -func createTestCategoryData(piwigoId int) datastore.CategoryData { - cat := datastore.CategoryData{ - CategoryId: 0, - PiwigoId: piwigoId, - PiwigoParentId: 0, - Name: "2019", - Key: "2019", + piwigoCategory2 := piwigo.PiwigoCategory{ + Id: 2, + Name: "SubCategory", + Key: "2019/SubCategory", + ParentId: 1, } - return cat + piwigoCategories := make(map[string]*piwigo.PiwigoCategory) + piwigoCategories[piwigoCategory1.Key] = &piwigoCategory1 + piwigoCategories[piwigoCategory2.Key] = &piwigoCategory2 + return piwigoCategories }