collections/collection_test.go

57 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-07-26 22:13:21 +02:00
package collections
import (
"strconv"
"strings"
"testing"
2024-05-21 23:05:21 +02:00
_ "embed"
2023-07-26 22:13:21 +02:00
"github.com/stretchr/testify/require"
)
//go:embed collection_test.csv
2024-05-21 23:05:21 +02:00
var testData []byte
2023-07-26 22:13:21 +02:00
func TestCollection(t *testing.T) {
type person struct {
ID int
LastName string
FirstName string
Email string
}
2024-05-21 23:05:21 +02:00
data := strings.Split(string(testData), "\n")
2023-07-26 22:13:21 +02:00
lines := OfSlice(data).Skip(1).Filter(func(s string) bool {
return strings.Contains(s, ",")
})
firstGloryWithComMail := MapCollection(
lines,
func(line string) person {
elements := strings.Split(line, ",")
id, _ := strconv.Atoi(elements[0])
return person{
ID: id,
LastName: elements[1],
FirstName: elements[2],
Email: elements[3],
}
},
).Filter(func(p person) bool {
return strings.HasSuffix(p.Email, ".com")
}).Filter(func(p person) bool {
return p.FirstName == "Glory"
}).Sorted(func(p1, p2 person) bool {
return p1.ID < p2.ID
}).FindFirst().Get()
require.EqualValues(t, person{
ID: 62,
LastName: "Joss",
FirstName: "Glory",
Email: "gjoss1p@taobao.com",
}, firstGloryWithComMail)
}