From cf47f657af55e3c94b357f4c6519f0204aaae9f2 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 2 Oct 2019 12:06:03 -0700 Subject: scaffold for volume server query feature --- weed/query/json/query_json_test.go | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 weed/query/json/query_json_test.go (limited to 'weed/query/json/query_json_test.go') diff --git a/weed/query/json/query_json_test.go b/weed/query/json/query_json_test.go new file mode 100644 index 000000000..7ad837360 --- /dev/null +++ b/weed/query/json/query_json_test.go @@ -0,0 +1,73 @@ +package json + +import ( + "testing" + + "github.com/tidwall/gjson" +) + +func TestGjson(t *testing.T) { + data := ` + { + "quiz": { + "sport": { + "q1": { + "question": "Which one is correct team name in NBA?", + "options": [ + "New York Bulls", + "Los Angeles Kings", + "Golden State Warriros", + "Huston Rocket" + ], + "answer": "Huston Rocket" + } + }, + "maths": { + "q1": { + "question": "5 + 7 = ?", + "options": [ + "10", + "11", + "12", + "13" + ], + "answer": "12" + }, + "q2": { + "question": "12 - 8 = ?", + "options": [ + "1", + "2", + "3", + "4" + ], + "answer": "4" + } + } + } + } + + { + "fruit": "Apple", + "size": "Large", + "quiz": "Red" + } + +` + + projections := []string{"quiz","fruit"} + + gjson.ForEachLine(data, func(line gjson.Result) bool{ + println(line.String()) + println("+++++++++++") + results := gjson.GetMany(line.Raw, projections...) + for _, result := range results { + println(result.Index, result.Type, result.String()) + } + println("-----------") + return true + }) + + + +} -- cgit v1.2.3 From f8d4b7d1c01eecb54edc4169d6b8a0d3c39a730c Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 6 Oct 2019 22:35:05 -0700 Subject: support basic json filtering and selection --- weed/query/json/query_json_test.go | 65 +++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'weed/query/json/query_json_test.go') diff --git a/weed/query/json/query_json_test.go b/weed/query/json/query_json_test.go index 7ad837360..621d4f548 100644 --- a/weed/query/json/query_json_test.go +++ b/weed/query/json/query_json_test.go @@ -58,7 +58,7 @@ func TestGjson(t *testing.T) { projections := []string{"quiz","fruit"} gjson.ForEachLine(data, func(line gjson.Result) bool{ - println(line.String()) + println(line.Raw) println("+++++++++++") results := gjson.GetMany(line.Raw, projections...) for _, result := range results { @@ -71,3 +71,66 @@ func TestGjson(t *testing.T) { } + +func TestJsonQueryRow(t *testing.T) { + + data := ` + { + "fruit": "Bl\"ue", + "size": 6, + "quiz": "green" + } + +` + selections := []string{"fruit", "size"} + + isFiltered, values := QueryJson(data, selections, Query{ + Field: "quiz", + Op: "=", + Value: "green", + }) + + if !isFiltered { + t.Errorf("should have been filtered") + } + + if values == nil { + t.Errorf("values should have been returned") + } + + buf := ToJson(nil, selections, values) + println(string(buf)) + +} + +func TestJsonQueryNumber(t *testing.T) { + + data := ` + { + "fruit": "Bl\"ue", + "size": 6, + "quiz": "green" + } + +` + selections := []string{"fruit", "quiz"} + + isFiltered, values := QueryJson(data, selections, Query{ + Field: "size", + Op: ">=", + Value: "6", + }) + + if !isFiltered { + t.Errorf("should have been filtered") + } + + if values == nil { + t.Errorf("values should have been returned") + } + + buf := ToJson(nil, selections, values) + println(string(buf)) + +} + -- cgit v1.2.3 From 939e4b57a85c56a598fcdf342e2b5d5c9085e552 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 9 Oct 2019 00:03:18 -0700 Subject: go fmt --- weed/query/json/query_json_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'weed/query/json/query_json_test.go') diff --git a/weed/query/json/query_json_test.go b/weed/query/json/query_json_test.go index 621d4f548..1794bb333 100644 --- a/weed/query/json/query_json_test.go +++ b/weed/query/json/query_json_test.go @@ -55,9 +55,9 @@ func TestGjson(t *testing.T) { ` - projections := []string{"quiz","fruit"} + projections := []string{"quiz", "fruit"} - gjson.ForEachLine(data, func(line gjson.Result) bool{ + gjson.ForEachLine(data, func(line gjson.Result) bool { println(line.Raw) println("+++++++++++") results := gjson.GetMany(line.Raw, projections...) @@ -68,8 +68,6 @@ func TestGjson(t *testing.T) { return true }) - - } func TestJsonQueryRow(t *testing.T) { @@ -133,4 +131,3 @@ func TestJsonQueryNumber(t *testing.T) { println(string(buf)) } - -- cgit v1.2.3