WIP: refactored tests

This commit is contained in:
Philipp Häfelfinger 2019-04-04 09:07:49 +02:00
parent 41bf858523
commit 550230195a

View File

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