From 1aa1a26a64fbfe40dae95a186bf94d9123de3f05 Mon Sep 17 00:00:00 2001 From: Keenan Nemetz Date: Wed, 6 Dec 2023 09:21:19 -0800 Subject: [PATCH] add index test --- client/descriptions.go | 2 ++ db/index.go | 9 ++++++++- db/indexed_docs_test.go | 2 ++ request/graphql/schema/types/scalars.go | 6 +++--- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/client/descriptions.go b/client/descriptions.go index 6ef29cebaa..96f68108b4 100644 --- a/client/descriptions.go +++ b/client/descriptions.go @@ -146,6 +146,8 @@ func (f FieldKind) String() string { return "[String]" case FieldKind_STRING_ARRAY: return "[String!]" + case FieldKind_BLOB: + return "Blob" default: return fmt.Sprint(uint8(f)) } diff --git a/db/index.go b/db/index.go index 4bd62f6ae3..5d43bddb21 100644 --- a/db/index.go +++ b/db/index.go @@ -18,6 +18,7 @@ import ( "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/datastore" "github.com/sourcenetwork/defradb/errors" + "github.com/sourcenetwork/defradb/request/graphql/schema/types" ) // CollectionIndex is an interface for collection indexes @@ -52,7 +53,13 @@ func getValidateIndexFieldFunc(kind client.FieldKind) func(any) bool { case client.FieldKind_BOOL: return canConvertIndexFieldValue[bool] case client.FieldKind_BLOB: - return canConvertIndexFieldValue[string] + return func(val any) bool { + blobStrVal, ok := val.(string) + if !ok { + return false + } + return types.BlobPattern.MatchString(blobStrVal) + } case client.FieldKind_DATETIME: return func(val any) bool { timeStrVal, ok := val.(string) diff --git a/db/indexed_docs_test.go b/db/indexed_docs_test.go index f1f8d6270f..56d7026e1e 100644 --- a/db/indexed_docs_test.go +++ b/db/indexed_docs_test.go @@ -421,6 +421,7 @@ func TestNonUnique_StoringIndexedFieldValueOfDifferentTypes(t *testing.T) { {Name: "invalid bool", FieldKind: client.FieldKind_BOOL, FieldVal: "invalid", ShouldFail: true}, {Name: "invalid datetime", FieldKind: client.FieldKind_DATETIME, FieldVal: nowStr[1:], ShouldFail: true}, {Name: "invalid datetime type", FieldKind: client.FieldKind_DATETIME, FieldVal: 1, ShouldFail: true}, + {Name: "invalid blob type", FieldKind: client.FieldKind_BLOB, FieldVal: "invalid", ShouldFail: true}, {Name: "valid int", FieldKind: client.FieldKind_INT, FieldVal: 12}, {Name: "valid float", FieldKind: client.FieldKind_FLOAT, FieldVal: 36.654}, @@ -428,6 +429,7 @@ func TestNonUnique_StoringIndexedFieldValueOfDifferentTypes(t *testing.T) { {Name: "valid bool false", FieldKind: client.FieldKind_BOOL, FieldVal: false}, {Name: "valid datetime string", FieldKind: client.FieldKind_DATETIME, FieldVal: nowStr}, {Name: "valid empty string", FieldKind: client.FieldKind_STRING, FieldVal: ""}, + {Name: "valid blob type", FieldKind: client.FieldKind_BLOB, FieldVal: "00ff"}, } for i, tc := range testCase { diff --git a/request/graphql/schema/types/scalars.go b/request/graphql/schema/types/scalars.go index 49d5055199..c1a1bc4d3b 100644 --- a/request/graphql/schema/types/scalars.go +++ b/request/graphql/schema/types/scalars.go @@ -18,8 +18,8 @@ import ( "github.com/sourcenetwork/graphql-go/language/ast" ) -// blobPattern is a regex for validating blob hex strings -var blobPattern = regexp.MustCompile("^[0-9a-fA-F]+$") +// BlobPattern is a regex for validating blob hex strings +var BlobPattern = regexp.MustCompile("^[0-9a-fA-F]+$") // coerceBlob converts the given value into a valid hex string. // If the value cannot be converted nil is returned. @@ -32,7 +32,7 @@ func coerceBlob(value any) any { return coerceBlob(*value) case string: - if !blobPattern.MatchString(value) { + if !BlobPattern.MatchString(value) { return nil } return value