github.com/weaviate/weaviate@v1.24.6/adapters/handlers/graphql/common/json_number_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package common 13 14 import ( 15 "encoding/json" 16 "fmt" 17 "testing" 18 19 "github.com/stretchr/testify/assert" 20 "github.com/tailor-inc/graphql" 21 ) 22 23 type testCase struct { 24 input interface{} 25 expectedOutput float64 26 } 27 28 func TestJSONNumberResolver(t *testing.T) { 29 tests := []testCase{ 30 { 31 input: json.Number("10"), 32 expectedOutput: 10.0, 33 }, 34 { 35 input: int(10), 36 expectedOutput: 10.0, 37 }, 38 { 39 input: int64(10), 40 expectedOutput: 10.0, 41 }, 42 { 43 input: float64(10), 44 expectedOutput: 10.0, 45 }, 46 } 47 48 for _, test := range tests { 49 name := fmt.Sprintf("%#v -> %#v", test.input, test.expectedOutput) 50 t.Run(name, func(t *testing.T) { 51 result, err := JSONNumberResolver(resolveParams(test.input)) 52 assert.Nil(t, err, "should not error") 53 assert.Equal(t, test.expectedOutput, result) 54 }) 55 } 56 } 57 58 func resolveParams(input interface{}) graphql.ResolveParams { 59 return graphql.ResolveParams{ 60 Source: map[string]interface{}{ 61 "myField": input, 62 }, 63 Info: graphql.ResolveInfo{FieldName: "myField"}, 64 } 65 } 66 67 func TestNumberFieldNotPresent(t *testing.T) { 68 // shouldn't return anything, but also not error. This can otherwise lead to 69 // odd behavior when no entries are present, yet we asked for int props and 70 // type, see https://github.com/weaviate/weaviate/issues/775 71 params := graphql.ResolveParams{ 72 Source: map[string]interface{}{}, 73 Info: graphql.ResolveInfo{FieldName: "myField"}, 74 } 75 76 result, err := JSONNumberResolver(params) 77 assert.Nil(t, err) 78 assert.Equal(t, nil, result) 79 }