From ba2bff080f8bdb62e5876f6bdab7c0ab8e141804 Mon Sep 17 00:00:00 2001 From: Bailin He Date: Tue, 17 Oct 2023 21:53:52 +0000 Subject: [PATCH] address review suggestions --- pkg/jsonschema/unique_constraint.go | 15 +++----- pkg/jsonschema/unique_constraint_test.go | 46 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/pkg/jsonschema/unique_constraint.go b/pkg/jsonschema/unique_constraint.go index cac6d124..ba8f4bb2 100644 --- a/pkg/jsonschema/unique_constraint.go +++ b/pkg/jsonschema/unique_constraint.go @@ -64,17 +64,16 @@ func (s *UniqueConstraintSchema) Validate(_ jsonschema.ValidationContext, v inte qms = append(qms, qm.Where("id != ?", *s.ResourceID)) } - for k, value := range mappedValue { - // Convert the value to string - v := fmt.Sprint(value) - + for k, v := range mappedValue { fieldType, exists := s.UniqueFieldTypesMap[k] if !exists { continue } if fieldType == "string" { - v = fmt.Sprintf(`"%s"`, v) + if vStr, ok := v.(string); ok { + v = fmt.Sprintf(`"%s"`, vStr) + } } qms = append(qms, qm.Where(`resource->? = ?`, k, v)) @@ -192,11 +191,7 @@ func (uc *UniqueConstraintCompiler) compileUniqueConstraint( // Checks if the provided field type is valid for unique constraints func isValidType(fieldType string) bool { - _, ok := map[string]bool{ - "string": true, "number": true, "integer": true, "boolean": true, - }[fieldType] - - return fieldType == "string" || fieldType == "number" || fieldType == "integer" || fieldType == "boolean" + return fieldType == "string" || fieldType == "number" || fieldType == "integer" || fieldType == "boolean" } // helper function to assert string slice type diff --git a/pkg/jsonschema/unique_constraint_test.go b/pkg/jsonschema/unique_constraint_test.go index d81cee4e..a6dcb797 100644 --- a/pkg/jsonschema/unique_constraint_test.go +++ b/pkg/jsonschema/unique_constraint_test.go @@ -3,6 +3,7 @@ package jsonschema import ( "context" "database/sql" + "reflect" "testing" "github.com/cockroachdb/cockroach-go/v2/testserver" @@ -266,6 +267,51 @@ func (s *UniqueConstrainTestSuite) TestValidate() { } } +func TestAssertStringSlice(t *testing.T) { + tests := []struct { + name string + input interface{} + expected []string + expectedErr string + }{ + { + name: "valid string slice", + input: []interface{}{"foo", "bar", "baz"}, + expected: []string{"foo", "bar", "baz"}, + expectedErr: "", + }, + { + name: "invalid type", + input: "not a slice", + expected: nil, + expectedErr: "to string array", + }, + { + name: "invalid element type", + input: []interface{}{"foo", 42, "baz"}, + expected: nil, + expectedErr: "to string", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual, err := assertStringSlice(tt.input) + + if tt.expectedErr == "" { + assert.Nil(t, err) + if !reflect.DeepEqual(actual, tt.expected) { + t.Errorf("assertStringSlice() = %v, expected %v", actual, tt.expected) + } + } else { + assert.NotNil(t, err) + assert.Contains(t, err.Error(), tt.expectedErr) + return + } + }) + } +} + func TestUniqueConstraintSuite(t *testing.T) { suite.Run(t, new(UniqueConstrainTestSuite)) }