Merge pull request #743 from iwpnd/fix/whereins

fix: whereins defaulting to lower case
This commit is contained in:
Josh Baker 2024-06-03 19:38:33 -07:00 committed by GitHub
commit 16055ff745
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 124 additions and 1 deletions

View File

@ -354,7 +354,7 @@ func (s *Server) parseSearchScanBaseTokens(
valArr[i] = field.ValueOf(valStr) valArr[i] = field.ValueOf(valStr)
} }
t.whereins = append(t.whereins, whereinT{ t.whereins = append(t.whereins, whereinT{
name: strings.ToLower(name), name: name,
valArr: valArr, valArr: valArr,
}) })
continue continue

View File

@ -3,6 +3,8 @@ package server
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/tidwall/tile38/internal/field"
) )
func TestLowerCompare(t *testing.T) { func TestLowerCompare(t *testing.T) {
@ -29,6 +31,118 @@ func TestLowerCompare(t *testing.T) {
} }
} }
func TestParseWhereins(t *testing.T) {
s := &Server{}
type tcase struct {
inputWhereins []whereinT
expWhereins []whereinT
}
fn := func(tc tcase) func(t *testing.T) {
return func(t *testing.T) {
_, tout, err := s.parseSearchScanBaseTokens(
"scan",
searchScanBaseTokens{
whereins: tc.inputWhereins,
},
[]string{"key"},
)
got := tout.whereins
exp := tc.expWhereins
if err != nil {
t.Fatalf("unexpected error while parsing search scan base tokens")
}
if len(got) != len(exp) {
t.Fatalf("expected equal length whereins")
}
for i := range got {
if got[i].name != exp[i].name {
t.Fatalf("expected equal field names")
}
for j := range exp[i].valArr {
if !got[i].match(exp[i].valArr[j]) {
t.Fatalf("expected matching value arrays")
}
}
}
}
}
tests := map[string]tcase{
"upper case": {
inputWhereins: []whereinT{
{
name: "TEST",
valArr: []field.Value{
field.ValueOf("1"),
field.ValueOf("1"),
},
},
},
expWhereins: []whereinT{
{
name: "TEST",
valArr: []field.Value{
field.ValueOf("1"),
field.ValueOf("1"),
},
},
},
},
"lower case": {
inputWhereins: []whereinT{
{
name: "test",
valArr: []field.Value{
field.ValueOf("1"),
field.ValueOf("1"),
},
},
},
expWhereins: []whereinT{
{
name: "test",
valArr: []field.Value{
field.ValueOf("1"),
field.ValueOf("1"),
},
},
},
},
"mixed case": {
inputWhereins: []whereinT{
{
name: "teSt",
valArr: []field.Value{
field.ValueOf("1"),
field.ValueOf("1"),
},
},
},
expWhereins: []whereinT{
{
name: "teSt",
valArr: []field.Value{
field.ValueOf("1"),
field.ValueOf("1"),
},
},
},
},
}
for name, tc := range tests {
t.Run(name, fn(tc))
}
}
// func testParseFloat(t testing.TB, s string, f float64, invalid bool) { // func testParseFloat(t testing.TB, s string, f float64, invalid bool) {
// n, err := parseFloat(s) // n, err := parseFloat(s)
// if err != nil { // if err != nil {

View File

@ -536,6 +536,15 @@ func keys_FIELDS_test(mc *mockServer) error {
Do("SET", "fleet", "truck1", "FIELD", "speed", "2", "POINT", "-112", "33").JSON().OK(), Do("SET", "fleet", "truck1", "FIELD", "speed", "2", "POINT", "-112", "33").JSON().OK(),
Do("GET", "fleet", "truck1", "WITHFIELDS").JSON().Str(`{"ok":true,"object":{"type":"Point","coordinates":[33,-112]},"fields":{"speed":2}}`), Do("GET", "fleet", "truck1", "WITHFIELDS").JSON().Str(`{"ok":true,"object":{"type":"Point","coordinates":[33,-112]},"fields":{"speed":2}}`),
// Do some whereins queries
Do("SET", "whereins", "id1", "FIELD", "test", "2", "POINT", "-112", "33").JSON().OK(),
Do("SET", "whereins", "id2", "FIELD", "TEST", "3", "POINT", "-111", "32").JSON().OK(),
Do("SET", "whereins", "id3", "FIELD", "teSt", "4", "POINT", "-110", "31").JSON().OK(),
Do("SCAN", "whereins", "WHEREIN", "test", "1", "2", "OBJECTS").JSON().Str(`{"ok":true,"fields":["test"],"objects":[{"id":"id1","object":{"type":"Point","coordinates":[33,-112]},"fields":[2]}],"count":1,"cursor":0}`),
Do("SCAN", "whereins", "WHEREIN", "TEST", "1", "3", "OBJECTS").JSON().Str(`{"ok":true,"fields":["TEST"],"objects":[{"id":"id2","object":{"type":"Point","coordinates":[32,-111]},"fields":[3]}],"count":1,"cursor":0}`),
Do("SCAN", "whereins", "WHEREIN", "teSt", "1", "4", "OBJECTS").JSON().Str(`{"ok":true,"fields":["teSt"],"objects":[{"id":"id3","object":{"type":"Point","coordinates":[31,-110]},"fields":[4]}],"count":1,"cursor":0}`),
Do("SCAN", "whereins", "OBJECTS").JSON().Str(`{"ok":true,"fields":["TEST","teSt","test"],"objects":[{"id":"id1","object":{"type":"Point","coordinates":[33,-112]},"fields":[0,0,2]},{"id":"id2","object":{"type":"Point","coordinates":[32,-111]},"fields":[3,0,0]},{"id":"id3","object":{"type":"Point","coordinates":[31,-110]},"fields":[0,4,0]}],"count":3,"cursor":0}`),
// Do some GJSON queries. // Do some GJSON queries.
Do("SET", "fleet", "truck2", "FIELD", "hello", `{"world":"tom"}`, "POINT", "-112", "33").JSON().OK(), Do("SET", "fleet", "truck2", "FIELD", "hello", `{"world":"tom"}`, "POINT", "-112", "33").JSON().OK(),
Do("SCAN", "fleet", "WHERE", "hello", `{"world":"tom"}`, `{"world":"tom"}`, "COUNT").JSON().Str(`{"ok":true,"count":1,"cursor":0}`), Do("SCAN", "fleet", "WHERE", "hello", `{"world":"tom"}`, `{"world":"tom"}`, "COUNT").JSON().Str(`{"ok":true,"count":1,"cursor":0}`),