From a47f02b02d4293261a491dd9bd54fcc3061e42df Mon Sep 17 00:00:00 2001 From: Benjamin Ramser Date: Mon, 27 May 2024 22:54:55 +0200 Subject: [PATCH] fix: whereins defaulting to lower case --- internal/server/token.go | 2 +- internal/server/token_test.go | 114 ++++++++++++++++++++++++++++++++++ tests/keys_test.go | 9 +++ 3 files changed, 124 insertions(+), 1 deletion(-) diff --git a/internal/server/token.go b/internal/server/token.go index 2b3ed628e..e6d58b168 100644 --- a/internal/server/token.go +++ b/internal/server/token.go @@ -354,7 +354,7 @@ func (s *Server) parseSearchScanBaseTokens( valArr[i] = field.ValueOf(valStr) } t.whereins = append(t.whereins, whereinT{ - name: strings.ToLower(name), + name: name, valArr: valArr, }) continue diff --git a/internal/server/token_test.go b/internal/server/token_test.go index 759b1abf1..aad6fbe8d 100644 --- a/internal/server/token_test.go +++ b/internal/server/token_test.go @@ -3,6 +3,8 @@ package server import ( "strings" "testing" + + "github.com/tidwall/tile38/internal/field" ) 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) { // n, err := parseFloat(s) // if err != nil { diff --git a/tests/keys_test.go b/tests/keys_test.go index 8063ad4b7..e310d6289 100644 --- a/tests/keys_test.go +++ b/tests/keys_test.go @@ -514,6 +514,15 @@ func keys_FIELDS_test(mc *mockServer) error { 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 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("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}`),