From 177f25ee4494803434157af5ad552ac531fb5a3b Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Wed, 15 Aug 2018 14:15:45 -0500 Subject: [PATCH] Add tests for importing value with column keys into integer fields. Fix a bug in protofuf decoding of pilosa.Row. --- ctl/import_test.go | 27 ++++++++++++++++++++ encoding/proto/proto.go | 1 + http/client_test.go | 55 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/ctl/import_test.go b/ctl/import_test.go index c2f7893f4..cd75a5d0a 100644 --- a/ctl/import_test.go +++ b/ctl/import_test.go @@ -128,6 +128,33 @@ func TestImportCommand_RunKeys(t *testing.T) { } } +// Ensure that integer import with keys runs. +func TestImportCommand_RunValueKeys(t *testing.T) { + buf := bytes.Buffer{} + stdin, stdout, stderr := GetIO(buf) + cm := NewImportCommand(stdin, stdout, stderr) + file, err := ioutil.TempFile("", "import-key.csv") + file.Write([]byte("foo1,2\nfoo3,4\nfoo5,6")) + ctx := context.Background() + if err != nil { + t.Fatal(err) + } + + cmd := test.MustRunCluster(t, 1)[0] + cm.Host = cmd.API.Node().URI.HostPort() + + http.DefaultClient.Do(MustNewHTTPRequest("POST", "http://"+cm.Host+"/index/i", strings.NewReader(`{"options":{"keys": true}}`))) + http.DefaultClient.Do(MustNewHTTPRequest("POST", "http://"+cm.Host+"/index/i/field/f", strings.NewReader(`{"options":{"type": "int", "min": 0, "max": 100}}`))) + + cm.Index = "i" + cm.Field = "f" + cm.Paths = []string{file.Name()} + err = cm.Run(ctx) + if err != nil { + t.Fatalf("Import Run with keys doesn't work: %s", err) + } +} + func TestImportCommand_InvalidFile(t *testing.T) { cmd := test.MustRunCluster(t, 1)[0] diff --git a/encoding/proto/proto.go b/encoding/proto/proto.go index fcdde95f9..298683e92 100644 --- a/encoding/proto/proto.go +++ b/encoding/proto/proto.go @@ -890,6 +890,7 @@ func decodeRow(pr *internal.Row) *pilosa.Row { r := pilosa.NewRow() r.Attrs = decodeAttrs(pr.Attrs) + r.Keys = pr.Keys for _, v := range pr.Columns { r.SetBit(v) } diff --git a/http/client_test.go b/http/client_test.go index 80c4d0425..702860c23 100644 --- a/http/client_test.go +++ b/http/client_test.go @@ -129,11 +129,6 @@ func TestClient_MultiNode(t *testing.T) { Remote: false, } - _, err = client[0].Query(context.Background(), "i", queryRequest) - if err != nil { - t.Fatal(err) - } - result, err := client[0].Query(context.Background(), "i", queryRequest) if err != nil { t.Fatal(err) @@ -376,6 +371,56 @@ func TestClient_ImportKeys(t *testing.T) { } }) }) + + t.Run("IntegerFieldSingleNode", func(t *testing.T) { + cmd := test.MustRunCluster(t, 1)[0] + host := cmd.URL() + holder := cmd.Server.Holder() + hldr := test.Holder{Holder: holder} + + fldName := "f" + + // Load bitmap into cache to ensure cache gets updated. + index := hldr.MustCreateIndexIfNotExists("i", pilosa.IndexOptions{Keys: true}) + field, err := index.CreateFieldIfNotExists(fldName, pilosa.OptFieldTypeInt(-100, 100)) + if err != nil { + t.Fatal(err) + } + + // Send import request. + c := MustNewClient(host, http.GetHTTPClient(nil)) + if err := c.ImportValue(context.Background(), "i", "f", 0, []pilosa.FieldValue{ + {ColumnKey: "col1", Value: -10}, + {ColumnKey: "col2", Value: 20}, + {ColumnKey: "col3", Value: 40}, + }); err != nil { + t.Fatal(err) + } + + // Verify Sum. + sum, cnt, err := field.Sum(nil, fldName) + if err != nil { + t.Fatal(err) + } + if sum != 50 || cnt != 3 { + t.Fatalf("unexpected values: got sum=%v, count=%v; expected sum=50, cnt=3", sum, cnt) + } + + // Verify Range + queryRequest := &pilosa.QueryRequest{ + Query: fmt.Sprintf(`Range(%s>10)`, fldName), + Remote: false, + } + + result, err := c.Query(context.Background(), "i", queryRequest) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(result.Results[0].(*pilosa.Row).Keys, []string{"col2", "col3"}) { + t.Fatalf("unexpected column keys: %s", spew.Sdump(result)) + } + }) } // Ensure client can bulk import value data.