From 46f5d2d224f111d5a494444aea02a4e8b4a7e4c7 Mon Sep 17 00:00:00 2001 From: Melissa Baker Date: Tue, 21 May 2019 12:15:56 -0700 Subject: [PATCH 1/2] Fixed nearby json field results showing wrong data issue #453 --- internal/server/scanner.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/server/scanner.go b/internal/server/scanner.go index e3cae7b1..05247614 100644 --- a/internal/server/scanner.go +++ b/internal/server/scanner.go @@ -355,13 +355,10 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool { sw.mu.Lock() defer sw.mu.Unlock() } - var fieldVals []float64 var ok bool keepGoing := true - if opts.skipTesting { - fieldVals = sw.fvals - } else { - ok, keepGoing, fieldVals = sw.testObject(opts.id, opts.o, opts.fields, opts.ignoreGlobMatch) + if !opts.skipTesting { + ok, keepGoing, _ = sw.testObject(opts.id, opts.o, opts.fields, opts.ignoreGlobMatch) if !ok { return keepGoing } @@ -403,11 +400,15 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool { } else if len(sw.farr) > 0 { jsfields = `,"fields":[` - for i, field := range fieldVals { + for i := range sw.farr { if i > 0 { - jsfields += "," + jsfields += `,` + } + if len(opts.fields) > i { + jsfields += strconv.FormatFloat(opts.fields[i], 'f', -1, 64) + } else { + jsfields += "0" } - jsfields += strconv.FormatFloat(field, 'f', -1, 64) } jsfields += `]` } From 4ba237b10ec55a443d868add2da36b73ecb4220d Mon Sep 17 00:00:00 2001 From: Melissa Baker Date: Tue, 28 May 2019 11:44:39 -0700 Subject: [PATCH 2/2] Added test for correct json fields output --- tests/keys_search_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/keys_search_test.go b/tests/keys_search_test.go index 2552f387..f78dbed6 100644 --- a/tests/keys_search_test.go +++ b/tests/keys_search_test.go @@ -18,6 +18,7 @@ func subTestSearch(t *testing.T, mc *mockServer) { runStep(t, mc, "SCAN_CURSOR", keys_SCAN_CURSOR_test) runStep(t, mc, "SEARCH_CURSOR", keys_SEARCH_CURSOR_test) runStep(t, mc, "MATCH", keys_MATCH_test) + runStep(t, mc, "FIELDS", keys_FIELDS_search_test) } func keys_KNN_test(mc *mockServer) error { @@ -355,6 +356,44 @@ func keys_MATCH_test(mc *mockServer) error { }) } +func keys_FIELDS_search_test(mc *mockServer) error { + return mc.DoBatch([][]interface{}{ + {"SET", "mykey", "1", "FIELD", "field1", 10, "FIELD", "field2", 11 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2791,33.5220]}`}, {"OK"}, + {"SET", "mykey", "2", "FIELD", "field1", 20, "FIELD", "field2", 10 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2793,33.5222]}`}, {"OK"}, + {"SET", "mykey", "3", "FIELD", "field1", 30, "FIELD", "field2", 13 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2795,33.5224]}`}, {"OK"}, + {"SET", "mykey", "4", "FIELD", "field1", 40, "FIELD", "field2", 14 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2797,33.5226]}`}, {"OK"}, + {"SET", "mykey", "5" /* field1 undefined */, "FIELD", "field2", 15, "FIELD", "field3", 28, "OBJECT", `{"type":"Point","coordinates":[-112.2799,33.5228]}`}, {"OK"}, + {"SET", "mykey", "6" /* field1 & field2 undefined */, "FIELD", "field3", 29, "OBJECT", `{"type":"Point","coordinates":[-112.2801,33.5230]}`}, {"OK"}, + {"SET", "mykey", "7" /* field1, field2, & field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2803,33.5232]}`}, {"OK"}, + {"OUTPUT", "json"}, {`{"ok":true}`}, + {"NEARBY", "mykey", "WHERE", "field2", 11, "+inf", "POINT", 33.462, -112.268, 60000}, { + `{"ok":true,"fields":["field1","field2","field3"],"objects":[` + + `{"id":"1","object":{"type":"Point","coordinates":[-112.2791,33.522]},"fields":[10,11,0]},` + + `{"id":"3","object":{"type":"Point","coordinates":[-112.2795,33.5224]},"fields":[30,13,0]},` + + `{"id":"4","object":{"type":"Point","coordinates":[-112.2797,33.5226]},"fields":[40,14,0]},` + + `{"id":"5","object":{"type":"Point","coordinates":[-112.2799,33.5228]},"fields":[0,15,28]}` + + `],"count":4,"cursor":0}`}, + {"NEARBY", "mykey", "WHERE", "field2", 0, 2, "POINT", 33.462, -112.268, 60000}, { + `{"ok":true,"fields":["field1","field2","field3"],"objects":[` + + `{"id":"6","object":{"type":"Point","coordinates":[-112.2801,33.523]},"fields":[0,0,29]},` + + `{"id":"7","object":{"type":"Point","coordinates":[-112.2803,33.5232]},"fields":[0,0,0]}` + + `],"count":2,"cursor":0}`}, + + {"WITHIN", "mykey", "WHERE", "field2", 11, "+inf", "CIRCLE", 33.462, -112.268, 60000}, { + `{"ok":true,"fields":["field1","field2","field3"],"objects":[` + + `{"id":"1","object":{"type":"Point","coordinates":[-112.2791,33.522]},"fields":[10,11,0]},` + + `{"id":"3","object":{"type":"Point","coordinates":[-112.2795,33.5224]},"fields":[30,13,0]},` + + `{"id":"4","object":{"type":"Point","coordinates":[-112.2797,33.5226]},"fields":[40,14,0]},` + + `{"id":"5","object":{"type":"Point","coordinates":[-112.2799,33.5228]},"fields":[0,15,28]}` + + `],"count":4,"cursor":0}`}, + {"WITHIN", "mykey", "WHERE", "field2", 0, 2, "CIRCLE", 33.462, -112.268, 60000}, { + `{"ok":true,"fields":["field1","field2","field3"],"objects":[` + + `{"id":"6","object":{"type":"Point","coordinates":[-112.2801,33.523]},"fields":[0,0,29]},` + + `{"id":"7","object":{"type":"Point","coordinates":[-112.2803,33.5232]},"fields":[0,0,0]}` + + `],"count":2,"cursor":0}`}, + }) +} + // match sorts the response and compares to the expected input func match(expectIn string) func(org, v interface{}) (resp, expect interface{}) { return func(v, org interface{}) (resp, expect interface{}) {