...

Source file src/github.com/go-openapi/validate/post/defaulter_test.go

Documentation: github.com/go-openapi/validate/post

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package post
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/go-openapi/spec"
    25  	"github.com/go-openapi/strfmt"
    26  	"github.com/go-openapi/validate"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  var defaulterFixturesPath = filepath.Join("..", "fixtures", "defaulting")
    32  
    33  func TestDefaulter(t *testing.T) {
    34  	schema, err := defaulterFixture()
    35  	require.NoError(t, err)
    36  
    37  	validator := validate.NewSchemaValidator(schema, nil, "", strfmt.Default)
    38  	x := defaulterFixtureInput()
    39  	t.Logf("Before: %v", x)
    40  
    41  	r := validator.Validate(x)
    42  	assert.False(t, r.HasErrors(), fmt.Sprintf("unexpected validation error: %v", r.AsError()))
    43  
    44  	ApplyDefaults(r)
    45  	t.Logf("After: %v", x)
    46  	var expected interface{}
    47  	err = json.Unmarshal([]byte(`{
    48  		"existing": 100,
    49  		"int": 42,
    50  		"str": "Hello",
    51  		"obj": {"foo": "bar"},
    52  		"nested": {"inner": 7},
    53  		"all": {"foo": 42, "bar": 42},
    54  		"any": {"foo": 42},
    55  		"one": {"bar": 42}
    56  	}`), &expected)
    57  	require.NoError(t, err)
    58  	assert.Equal(t, expected, x)
    59  }
    60  
    61  func TestDefaulterSimple(t *testing.T) {
    62  	schema := spec.Schema{
    63  		SchemaProps: spec.SchemaProps{
    64  			Properties: map[string]spec.Schema{
    65  				"int": {
    66  					SchemaProps: spec.SchemaProps{
    67  						Default: float64(42),
    68  					},
    69  				},
    70  				"str": {
    71  					SchemaProps: spec.SchemaProps{
    72  						Default: "Hello",
    73  					},
    74  				},
    75  			},
    76  		},
    77  	}
    78  	validator := validate.NewSchemaValidator(&schema, nil, "", strfmt.Default)
    79  	x := map[string]interface{}{}
    80  	t.Logf("Before: %v", x)
    81  	r := validator.Validate(x)
    82  	assert.False(t, r.HasErrors(), fmt.Sprintf("unexpected validation error: %v", r.AsError()))
    83  
    84  	ApplyDefaults(r)
    85  	t.Logf("After: %v", x)
    86  	var expected interface{}
    87  	err := json.Unmarshal([]byte(`{
    88  		"int": 42,
    89  		"str": "Hello"
    90  	}`), &expected)
    91  	require.NoError(t, err)
    92  	assert.Equal(t, expected, x)
    93  }
    94  
    95  func BenchmarkDefaulting(b *testing.B) {
    96  	b.ReportAllocs()
    97  
    98  	schema, err := defaulterFixture()
    99  	require.NoError(b, err)
   100  
   101  	for n := 0; n < b.N; n++ {
   102  		validator := validate.NewSchemaValidator(schema, nil, "", strfmt.Default)
   103  		x := defaulterFixtureInput()
   104  		r := validator.Validate(x)
   105  		assert.False(b, r.HasErrors(), fmt.Sprintf("unexpected validation error: %v", r.AsError()))
   106  		ApplyDefaults(r)
   107  	}
   108  }
   109  
   110  func defaulterFixtureInput() map[string]interface{} {
   111  	return map[string]interface{}{
   112  		"existing": float64(100),
   113  		"nested":   map[string]interface{}{},
   114  		"all":      map[string]interface{}{},
   115  		"any":      map[string]interface{}{},
   116  		"one":      map[string]interface{}{},
   117  	}
   118  }
   119  
   120  func defaulterFixture() (*spec.Schema, error) {
   121  	fname := filepath.Join(defaulterFixturesPath, "schema.json")
   122  	b, err := os.ReadFile(fname)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	var schema spec.Schema
   127  	if err := json.Unmarshal(b, &schema); err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	return &schema, spec.ExpandSchema(&schema, nil, nil /*new(noopResCache)*/)
   132  }
   133  

View as plain text