filter enhancements
This commit is contained in:
@@ -1,12 +1,111 @@
|
||||
package list_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/list"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func TestSubtractSliceString(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
base []string
|
||||
subtract []string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
[]string{},
|
||||
[]string{},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]string{},
|
||||
[]string{"1", "2", "3", "4"},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]string{"1", "2", "3", "4"},
|
||||
[]string{},
|
||||
`["1","2","3","4"]`,
|
||||
},
|
||||
{
|
||||
[]string{"1", "2", "3", "4"},
|
||||
[]string{"1", "2", "3", "4"},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]string{"1", "2", "3", "4", "7"},
|
||||
[]string{"2", "4", "5", "6"},
|
||||
`["1","3","7"]`,
|
||||
},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
result := list.SubtractSlice(s.base, s.subtract)
|
||||
|
||||
raw, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatalf("(%d) Failed to serialize: %v", i, err)
|
||||
}
|
||||
|
||||
strResult := string(raw)
|
||||
|
||||
if strResult != s.expected {
|
||||
t.Fatalf("(%d) Expected %v, got %v", i, s.expected, strResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubtractSliceInt(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
base []int
|
||||
subtract []int
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
[]int{},
|
||||
[]int{},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]int{},
|
||||
[]int{1, 2, 3, 4},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]int{1, 2, 3, 4},
|
||||
[]int{},
|
||||
`[1,2,3,4]`,
|
||||
},
|
||||
{
|
||||
[]int{1, 2, 3, 4},
|
||||
[]int{1, 2, 3, 4},
|
||||
`[]`,
|
||||
},
|
||||
{
|
||||
[]int{1, 2, 3, 4, 7},
|
||||
[]int{2, 4, 5, 6},
|
||||
`[1,3,7]`,
|
||||
},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
result := list.SubtractSlice(s.base, s.subtract)
|
||||
|
||||
raw, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatalf("(%d) Failed to serialize: %v", i, err)
|
||||
}
|
||||
|
||||
strResult := string(raw)
|
||||
|
||||
if strResult != s.expected {
|
||||
t.Fatalf("(%d) Expected %v, got %v", i, s.expected, strResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistInSliceString(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
item string
|
||||
|
||||
Reference in New Issue
Block a user