57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package collections
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
_ "embed"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
//go:embed collection_test.csv
|
|
var testData []byte
|
|
|
|
func TestCollection(t *testing.T) {
|
|
type person struct {
|
|
ID int
|
|
LastName string
|
|
FirstName string
|
|
Email string
|
|
}
|
|
|
|
data := strings.Split(string(testData), "\n")
|
|
|
|
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)
|
|
}
|