cbrfWorker/internal/utils/array_to_map_test.go
2025-01-02 01:10:33 +03:00

70 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils_test
import (
"testing"
"gitea.pena/PenaSide/cbrfWorker/internal/utils"
"github.com/stretchr/testify/assert"
)
func TestArrayToMap(t *testing.T) {
type testModel struct {
ID string
}
t.Run("Успешная конвертация массива в map без коллизий", func(t *testing.T) {
assert.NotPanics(t, func() {
result := utils.ArrayToMap(
[]testModel{{ID: "test"}, {ID: "test2"}},
func(model testModel) string {
return model.ID
},
)
assert.Equal(t,
map[string]testModel{
"test": {ID: "test"},
"test2": {ID: "test2"},
},
result,
)
})
})
t.Run("Успешная конвертация массива в map с коллизиями", func(t *testing.T) {
assert.NotPanics(t, func() {
result := utils.ArrayToMap(
[]testModel{{ID: "test"}, {ID: "test"}},
func(model testModel) string {
return model.ID
},
)
assert.Equal(t,
map[string]testModel{
"test": {ID: "test"},
},
result,
)
})
})
t.Run("Успешная конвертация массива в map с функцией, возаращающая пустую строку", func(t *testing.T) {
assert.NotPanics(t, func() {
result := utils.ArrayToMap(
[]testModel{{ID: "test"}, {ID: "test"}},
func(model testModel) string {
return ""
},
)
assert.Equal(t,
map[string]testModel{
"": {ID: "test"},
},
result,
)
})
})
}