Skip to content

Commit

Permalink
test: add new mocks for custom list repository
Browse files Browse the repository at this point in the history
  • Loading branch information
balzdur committed Dec 18, 2024
1 parent 5a00d48 commit 2228cc9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
24 changes: 23 additions & 1 deletion mocks/custom_list_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (cl *CustomListRepository) GetCustomListById(ctx context.Context, exec repo
}

func (cl *CustomListRepository) GetCustomListValues(ctx context.Context, exec repositories.Executor,
getCustomList models.GetCustomListValuesInput,
getCustomList models.GetCustomListValuesInput, forUpdate ...bool,
) ([]models.CustomListValue, error) {
args := cl.Called(exec, getCustomList)
return args.Get(0).([]models.CustomListValue), args.Error(1)
Expand Down Expand Up @@ -65,6 +65,17 @@ func (cl *CustomListRepository) AddCustomListValue(
return args.Error(0)
}

func (cl *CustomListRepository) BatchInsertCustomListValues(
ctx context.Context,
exec repositories.Executor,
customListId string,
customListValues []models.BatchInsertCustomListValue,
userId *models.UserId,
) error {
args := cl.Called(ctx, exec, customListId, customListValues, userId)
return args.Error(0)
}

func (cl *CustomListRepository) DeleteCustomListValue(
ctx context.Context,
exec repositories.Executor,
Expand All @@ -74,3 +85,14 @@ func (cl *CustomListRepository) DeleteCustomListValue(
args := cl.Called(ctx, exec, deleteCustomListValue, userId)
return args.Error(0)
}

func (cl *CustomListRepository) BatchDeleteCustomListValues(
ctx context.Context,
exec repositories.Executor,
customListId string,
deleteCustomListValueIds []string,
userId *models.UserId,
) error {
args := cl.Called(ctx, exec, customListId, deleteCustomListValueIds, userId)
return args.Error(0)
}
22 changes: 13 additions & 9 deletions usecases/custom_list_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,33 @@ func (usecase *CustomListUseCase) AddCustomListValue(ctx context.Context,
}

func (usecase *CustomListUseCase) ReadCustomListValuesToCSV(ctx context.Context, customListID string, w io.Writer) (string, error) {
customList, err := usecase.GetCustomListById(ctx, customListID)
exec := usecase.executorFactory.NewExecutor()
customList, err := usecase.CustomListRepository.GetCustomListById(ctx, exec, customListID)
if err != nil {
return "", err
}
customListValues, err := usecase.CustomListRepository.GetCustomListValues(ctx,
usecase.executorFactory.NewExecutor(), models.GetCustomListValuesInput{Id: customListID})
if err != nil {
if err := usecase.enforceSecurity.ReadCustomList(customList); err != nil {
return "", err
}

csvWriter := csv.NewWriter(w)

// Write header
if err := csvWriter.Write(customListValuesCSVHeader); err != nil {
customListValues, err := usecase.CustomListRepository.GetCustomListValues(ctx, exec, models.GetCustomListValuesInput{
Id: customListID,
})
if err != nil {
return "", err
}

// Write values
csvWriter := csv.NewWriter(w)
for _, customListValue := range customListValues {
if err := csvWriter.Write([]string{customListValue.Value}); err != nil {
return "", err
}
}
csvWriter.Flush()
if err = csvWriter.Error(); err != nil {
return "", err
}

return customList.Name, nil
}

Expand Down

0 comments on commit 2228cc9

Please sign in to comment.