Skip to content

Commit

Permalink
add index test
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Dec 6, 2023
1 parent 389ffef commit 1aa1a26
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
9 changes: 8 additions & 1 deletion db/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions db/indexed_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,15 @@ 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},
{Name: "valid bool true", FieldKind: client.FieldKind_BOOL, FieldVal: true},
{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 {
Expand Down
6 changes: 3 additions & 3 deletions request/graphql/schema/types/scalars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 1aa1a26

Please sign in to comment.