...

Source file src/github.com/go-openapi/validate/result_test.go

Documentation: github.com/go-openapi/validate

     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 validate
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  // Test AddError() uniqueness
    26  func TestResult_AddError(t *testing.T) {
    27  	r := Result{}
    28  	r.AddErrors(errors.New("one error"))
    29  	r.AddErrors(errors.New("another error"))
    30  	r.AddErrors(errors.New("one error"))
    31  	r.AddErrors(errors.New("one error"))
    32  	r.AddErrors(errors.New("one error"))
    33  	r.AddErrors(errors.New("one error"), errors.New("another error"))
    34  
    35  	assert.Len(t, r.Errors, 2)
    36  	assert.Contains(t, r.Errors, errors.New("one error"))
    37  	assert.Contains(t, r.Errors, errors.New("another error"))
    38  }
    39  
    40  func TestResult_AddNilError(t *testing.T) {
    41  	r := Result{}
    42  	r.AddErrors(nil)
    43  	assert.Empty(t, r.Errors)
    44  
    45  	errArray := []error{errors.New("one Error"), nil, errors.New("another error")}
    46  	r.AddErrors(errArray...)
    47  	assert.Len(t, r.Errors, 2)
    48  }
    49  
    50  func TestResult_AddWarnings(t *testing.T) {
    51  	r := Result{}
    52  	r.AddErrors(errors.New("one Error"))
    53  	assert.Len(t, r.Errors, 1)
    54  	assert.Empty(t, r.Warnings)
    55  
    56  	r.AddWarnings(errors.New("one Warning"))
    57  	assert.Len(t, r.Errors, 1)
    58  	assert.Len(t, r.Warnings, 1)
    59  }
    60  
    61  func TestResult_Merge(t *testing.T) {
    62  	r := Result{}
    63  	r.AddErrors(errors.New("one Error"))
    64  	r.AddWarnings(errors.New("one Warning"))
    65  	r.Inc()
    66  	assert.Len(t, r.Errors, 1)
    67  	assert.Len(t, r.Warnings, 1)
    68  	assert.Equal(t, 1, r.MatchCount)
    69  
    70  	// Merge with same
    71  	r2 := Result{}
    72  	r2.AddErrors(errors.New("one Error"))
    73  	r2.AddWarnings(errors.New("one Warning"))
    74  	r2.Inc()
    75  
    76  	r.Merge(&r2)
    77  
    78  	assert.Len(t, r.Errors, 1)
    79  	assert.Len(t, r.Warnings, 1)
    80  	assert.Equal(t, 2, r.MatchCount)
    81  
    82  	// Merge with new
    83  	r3 := Result{}
    84  	r3.AddErrors(errors.New("new Error"))
    85  	r3.AddWarnings(errors.New("new Warning"))
    86  	r3.Inc()
    87  
    88  	r.Merge(&r3)
    89  
    90  	assert.Len(t, r.Errors, 2)
    91  	assert.Len(t, r.Warnings, 2)
    92  	assert.Equal(t, 3, r.MatchCount)
    93  }
    94  
    95  func errorFixture() (Result, Result, Result) {
    96  	r := Result{}
    97  	r.AddErrors(errors.New("one Error"))
    98  	r.AddWarnings(errors.New("one Warning"))
    99  	r.Inc()
   100  
   101  	// same
   102  	r2 := Result{}
   103  	r2.AddErrors(errors.New("one Error"))
   104  	r2.AddWarnings(errors.New("one Warning"))
   105  	r2.Inc()
   106  
   107  	// new
   108  	r3 := Result{}
   109  	r3.AddErrors(errors.New("new Error"))
   110  	r3.AddWarnings(errors.New("new Warning"))
   111  	r3.Inc()
   112  	return r, r2, r3
   113  }
   114  
   115  func TestResult_MergeAsErrors(t *testing.T) {
   116  	r, r2, r3 := errorFixture()
   117  	assert.Len(t, r.Errors, 1)
   118  	assert.Len(t, r.Warnings, 1)
   119  	assert.Equal(t, 1, r.MatchCount)
   120  
   121  	r.MergeAsErrors(&r2, &r3)
   122  
   123  	assert.Len(t, r.Errors, 4) // One Warning added to Errors
   124  	assert.Len(t, r.Warnings, 1)
   125  	assert.Equal(t, 3, r.MatchCount)
   126  }
   127  
   128  func TestResult_MergeAsWarnings(t *testing.T) {
   129  	r, r2, r3 := errorFixture()
   130  	assert.Len(t, r.Errors, 1)
   131  	assert.Len(t, r.Warnings, 1)
   132  	assert.Equal(t, 1, r.MatchCount)
   133  
   134  	r.MergeAsWarnings(&r2, &r3)
   135  
   136  	assert.Len(t, r.Errors, 1) // One Warning added to Errors
   137  	assert.Len(t, r.Warnings, 4)
   138  	assert.Equal(t, 3, r.MatchCount)
   139  }
   140  
   141  func TestResult_IsValid(t *testing.T) {
   142  	r := Result{}
   143  
   144  	assert.True(t, r.IsValid())
   145  	assert.False(t, r.HasErrors())
   146  
   147  	r.AddWarnings(errors.New("one Warning"))
   148  	assert.True(t, r.IsValid())
   149  	assert.False(t, r.HasErrors())
   150  
   151  	r.AddErrors(errors.New("one Error"))
   152  	assert.False(t, r.IsValid())
   153  	assert.True(t, r.HasErrors())
   154  }
   155  
   156  func TestResult_HasWarnings(t *testing.T) {
   157  	r := Result{}
   158  
   159  	assert.False(t, r.HasWarnings())
   160  
   161  	r.AddErrors(errors.New("one Error"))
   162  	assert.False(t, r.HasWarnings())
   163  
   164  	r.AddWarnings(errors.New("one Warning"))
   165  	assert.True(t, r.HasWarnings())
   166  }
   167  
   168  func TestResult_HasErrorsOrWarnings(t *testing.T) {
   169  	r := Result{}
   170  	r2 := Result{}
   171  
   172  	assert.False(t, r.HasErrorsOrWarnings())
   173  
   174  	r.AddErrors(errors.New("one Error"))
   175  	assert.True(t, r.HasErrorsOrWarnings())
   176  
   177  	r2.AddWarnings(errors.New("one Warning"))
   178  	assert.True(t, r2.HasErrorsOrWarnings())
   179  
   180  	r.Merge(&r2)
   181  	assert.True(t, r.HasErrorsOrWarnings())
   182  }
   183  
   184  func TestResult_keepRelevantErrors(t *testing.T) {
   185  	r := Result{}
   186  	r.AddErrors(errors.New("one Error"))
   187  	r.AddErrors(errors.New("IMPORTANT!Another Error"))
   188  	r.AddWarnings(errors.New("one warning"))
   189  	r.AddWarnings(errors.New("IMPORTANT!Another warning"))
   190  	assert.Len(t, r.keepRelevantErrors().Errors, 1)
   191  	assert.Len(t, r.keepRelevantErrors().Warnings, 1)
   192  }
   193  
   194  func TestResult_AsError(t *testing.T) {
   195  	r := Result{}
   196  	require.NoError(t, r.AsError())
   197  	r.AddErrors(errors.New("one Error"))
   198  	r.AddErrors(errors.New("additional Error"))
   199  	res := r.AsError()
   200  	require.Error(t, res)
   201  
   202  	assert.Contains(t, res.Error(), "validation failure list:") // Expected from pkg errors
   203  	assert.Contains(t, res.Error(), "one Error")                // Expected from pkg errors
   204  	assert.Contains(t, res.Error(), "additional Error")         // Expected from pkg errors
   205  }
   206  
   207  // Test methods which suppport a call on a nil instance
   208  func TestResult_NilInstance(t *testing.T) {
   209  	var r *Result
   210  	assert.True(t, r.IsValid())
   211  	assert.False(t, r.HasErrors())
   212  	assert.False(t, r.HasWarnings())
   213  	assert.False(t, r.HasErrorsOrWarnings())
   214  }
   215  

View as plain text