github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/diff/checks_test.go (about)

     1  package diff
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/go-openapi/spec"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_getRef(t *testing.T) {
    13  	type args struct {
    14  		item interface{}
    15  	}
    16  	aRef, _ := spec.NewRef("hello")
    17  	tests := []struct {
    18  		name string
    19  		args args
    20  		want spec.Ref
    21  	}{
    22  		{name: "rando object",
    23  			args: args{item: "bob"},
    24  			want: spec.Ref{},
    25  		},
    26  		{name: "refable",
    27  			args: args{&spec.Refable{Ref: aRef}},
    28  			want: aRef,
    29  		},
    30  		{name: "schema",
    31  			args: args{&spec.Schema{SchemaProps: spec.SchemaProps{Ref: aRef}}},
    32  			want: aRef,
    33  		},
    34  		{name: "schemaProps",
    35  			args: args{&spec.SchemaProps{Ref: aRef}},
    36  			want: aRef,
    37  		},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			if got := getRef(tt.args.item); !reflect.DeepEqual(got, tt.want) {
    42  				t.Errorf("getRef() = %v, want %v", got, tt.want)
    43  			}
    44  		})
    45  	}
    46  }
    47  
    48  // func TestCheckToFromArrayType(t *testing.T) {
    49  // 	type args struct {
    50  // 		diffs []TypeDiff
    51  // 		type1 interface{}
    52  // 		type2 interface{}
    53  // 	}
    54  // 	tests := []struct {
    55  // 		name string
    56  // 		args args
    57  // 		want []TypeDiff
    58  // 	}{
    59  // 		{
    60  // 			name: "to",
    61  // 			args: args{
    62  // 				type1: spec.Int32Property(),
    63  // 				type2: arraySchemaOf("string"),
    64  // 			},
    65  // 			want: []TypeDiff{{Change: ChangedType, FromType: "<integer>", ToType: "<array[string]>"}},
    66  // 		},
    67  // 		{
    68  // 			name: "from",
    69  // 			args: args{
    70  // 				type1: arraySchemaOf("string"),
    71  // 				type2: spec.Int32Property(),
    72  // 			},
    73  // 			want: []TypeDiff{{Change: ChangedType, ToType: "<integer>", FromType: "<array[string]>"}},
    74  // 		},
    75  // 	}
    76  // 	for _, tt := range tests {
    77  // 		t.Run(tt.name, func(t *testing.T) {
    78  // 			if got := CheckToFromArrayType(tt.args.diffs, tt.args.type1, tt.args.type2); !reflect.DeepEqual(got, tt.want) {
    79  // 				t.Errorf("CheckToFromArrayType() = %s, want %s", jsonStr(got), jsonStr(tt.want))
    80  // 			}
    81  // 		})
    82  // 	}
    83  // }
    84  
    85  /*
    86  func arraySchemaOf(typename string) *spec.Schema {
    87  	return &spec.Schema{SchemaProps: spec.SchemaProps{
    88  		Type: spec.StringOrArray{"array"},
    89  		Items: &spec.SchemaOrArray{
    90  			Schema: &spec.Schema{
    91  				SchemaProps: spec.SchemaProps{
    92  					Type: spec.StringOrArray{typename}}}},
    93  	},
    94  	}
    95  }
    96  */
    97  
    98  func TestCheckToFromPrimitiveType(t *testing.T) {
    99  	type args struct {
   100  		diffs []TypeDiff
   101  		type1 interface{}
   102  		type2 interface{}
   103  	}
   104  	tests := []struct {
   105  		name string
   106  		args args
   107  		want []TypeDiff
   108  	}{
   109  		// TODO: Add test cases.
   110  	}
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			if got := CheckToFromPrimitiveType(tt.args.diffs, tt.args.type1, tt.args.type2); !reflect.DeepEqual(got, tt.want) {
   114  				t.Errorf("CheckToFromPrimitiveType() = %v, want %v", got, tt.want)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  func TestCheckRefChange(t *testing.T) {
   121  	type args struct {
   122  		diffs []TypeDiff
   123  		type1 interface{}
   124  		type2 interface{}
   125  	}
   126  	tests := []struct {
   127  		name           string
   128  		args           args
   129  		wantDiffReturn []TypeDiff
   130  	}{
   131  		{
   132  			name: "reftarget",
   133  			args: args{
   134  				type1: spec.RefProperty("#/definitions/FirstObject"),
   135  				type2: spec.RefProperty("#/definitions/SecondObject"),
   136  			},
   137  			wantDiffReturn: []TypeDiff{{Change: RefTargetChanged, FromType: "<FirstObject>", ToType: "<SecondObject>"}},
   138  		},
   139  		{
   140  			name: "toref",
   141  			args: args{
   142  				type1: spec.Int32Property(),
   143  				type2: spec.RefProperty("#/definitions/SecondObject"),
   144  			},
   145  			wantDiffReturn: []TypeDiff{{Change: ChangedType, FromType: "<integer>", ToType: "<SecondObject>"}},
   146  		},
   147  	}
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			if gotDiffReturn := CheckRefChange(tt.args.diffs, tt.args.type1, tt.args.type2); !reflect.DeepEqual(gotDiffReturn, tt.wantDiffReturn) {
   151  				t.Errorf("CheckRefChange() = %s, want %s", jsonStr(gotDiffReturn), jsonStr(tt.wantDiffReturn))
   152  			}
   153  		})
   154  	}
   155  }
   156  
   157  func Test_isRef(t *testing.T) {
   158  	r := spec.RefSchema("#/definitions/Bob")
   159  	p := spec.Int16Property()
   160  
   161  	assert.True(t, isRefType(r))
   162  	assert.False(t, isRefType(p))
   163  
   164  	refb := spec.Refable{Ref: r.Ref}
   165  	assert.True(t, isRefType(refb))
   166  
   167  	ss := spec.SimpleSchema{}
   168  	assert.False(t, isRefType(&ss))
   169  
   170  	ro := time.Timer{}
   171  	assert.False(t, isRefType(ro))
   172  
   173  }
   174  
   175  func Test_compareEnums(t *testing.T) {
   176  	type args struct {
   177  		left  []interface{}
   178  		right []interface{}
   179  	}
   180  	tests := []struct {
   181  		name string
   182  		args args
   183  		want []TypeDiff
   184  	}{
   185  		// TODO: Add test cases.
   186  	}
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			if got := CompareEnums(tt.args.left, tt.args.right); !reflect.DeepEqual(got, tt.want) {
   190  				t.Errorf("compareEnums() = %v, want %v", got, tt.want)
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  func Test_checkNumericTypeChanges(t *testing.T) {
   197  	type args struct {
   198  		diffs []TypeDiff
   199  		type1 *spec.SchemaProps
   200  		type2 *spec.SchemaProps
   201  	}
   202  	tests := []struct {
   203  		name string
   204  		args args
   205  		want []TypeDiff
   206  	}{
   207  		{
   208  			name: "ExclusiveMin",
   209  			args: args{
   210  				type1: &spec.Int32Property().WithMinimum(100, true).SchemaProps,
   211  				type2: &spec.Int32Property().SchemaProps,
   212  			},
   213  			want: []TypeDiff{{Change: WidenedType, Description: "Exclusive Minimum Removed:false->false"}},
   214  		},
   215  	}
   216  	for _, tt := range tests {
   217  		t.Run(tt.name, func(t *testing.T) {
   218  			if got := checkNumericTypeChanges(tt.args.diffs, tt.args.type1, tt.args.type2); !reflect.DeepEqual(got, tt.want) {
   219  				t.Errorf("checkNumericTypeChanges() = %s, want %s", jsonStr(got), jsonStr(tt.want))
   220  			}
   221  		})
   222  	}
   223  }
   224  
   225  func TestCompareFloatValues(t *testing.T) {
   226  	type args struct {
   227  		name   string
   228  		field1 *float64
   229  		field2 *float64
   230  	}
   231  	tests := []struct {
   232  		name string
   233  		args args
   234  		want []TypeDiff
   235  	}{
   236  		{
   237  			name: "both null",
   238  			args: args{name: "bob", field1: nil, field2: nil},
   239  			want: []TypeDiff{},
   240  		},
   241  		{
   242  			name: "greater",
   243  			args: args{name: "bob", field1: floatPointerOf(1.0), field2: floatPointerOf(2.0)},
   244  			want: []TypeDiff{{Change: WidenedType, Description: "bob 1.000000->2.000000"}},
   245  		},
   246  		{
   247  			name: "less",
   248  			args: args{name: "bob", field1: floatPointerOf(2.0), field2: floatPointerOf(1.0)},
   249  			want: []TypeDiff{{Change: NarrowedType, Description: "bob 2.000000->1.000000"}},
   250  		},
   251  		{
   252  			name: "firstNil",
   253  			args: args{name: "bob", field1: nil, field2: floatPointerOf(1.0)},
   254  			want: []TypeDiff{{Change: AddedConstraint, Description: "bob(1.000000)"}},
   255  		},
   256  		{
   257  			name: "secondNil",
   258  			args: args{name: "bob", field1: floatPointerOf(2.0), field2: nil},
   259  			want: []TypeDiff{{Change: DeletedConstraint, Description: "bob(2.000000)"}},
   260  		},
   261  	}
   262  	for _, tt := range tests {
   263  		t.Run(tt.name, func(t *testing.T) {
   264  			if got := CompareFloatValues(tt.args.name, tt.args.field1, tt.args.field2, WidenedType, NarrowedType); !reflect.DeepEqual(got, tt.want) {
   265  				t.Errorf("CheckStringTypeChanges() = %s, want %s", jsonStr(got), jsonStr(tt.want))
   266  			}
   267  		})
   268  	}
   269  }
   270  
   271  func TestCompareIntValues(t *testing.T) {
   272  	type args struct {
   273  		name   string
   274  		field1 *int64
   275  		field2 *int64
   276  	}
   277  	tests := []struct {
   278  		name string
   279  		args args
   280  		want []TypeDiff
   281  	}{
   282  		{
   283  			name: "both null",
   284  			args: args{name: "bob", field1: nil, field2: nil},
   285  			want: []TypeDiff{},
   286  		},
   287  		{
   288  			name: "greater",
   289  			args: args{name: "bob", field1: intPointerOf(1), field2: intPointerOf(2)},
   290  			want: []TypeDiff{{Change: WidenedType, Description: "bob 1->2"}},
   291  		},
   292  		{
   293  			name: "less",
   294  			args: args{name: "bob", field1: intPointerOf(2), field2: intPointerOf(1)},
   295  			want: []TypeDiff{{Change: NarrowedType, Description: "bob 2->1"}},
   296  		},
   297  		{
   298  			name: "firstNil",
   299  			args: args{name: "bob", field1: nil, field2: intPointerOf(1)},
   300  			want: []TypeDiff{{Change: AddedConstraint, Description: "bob(1)"}},
   301  		},
   302  		{
   303  			name: "secondNil",
   304  			args: args{name: "bob", field1: intPointerOf(2), field2: nil},
   305  			want: []TypeDiff{{Change: DeletedConstraint, Description: "bob(2)"}},
   306  		},
   307  	}
   308  	for _, tt := range tests {
   309  		t.Run(tt.name, func(t *testing.T) {
   310  			if got := CompareIntValues(tt.args.name, tt.args.field1, tt.args.field2, WidenedType, NarrowedType); !reflect.DeepEqual(got, tt.want) {
   311  				t.Errorf("CheckStringTypeChanges() = %s, want %s", jsonStr(got), jsonStr(tt.want))
   312  			}
   313  		})
   314  	}
   315  }
   316  
   317  func floatPointerOf(f float64) *float64 {
   318  	return &f
   319  }
   320  
   321  func intPointerOf(f int64) *int64 {
   322  	return &f
   323  }
   324  
   325  func TestCheckToFromRequired(t *testing.T) {
   326  	type args struct {
   327  		required1 bool
   328  		required2 bool
   329  	}
   330  	tests := []struct {
   331  		name      string
   332  		args      args
   333  		wantDiffs []TypeDiff
   334  	}{
   335  		// TODO: Add test cases.
   336  	}
   337  	for _, tt := range tests {
   338  		t.Run(tt.name, func(t *testing.T) {
   339  			if gotDiffs := CheckToFromRequired(tt.args.required1, tt.args.required2); !reflect.DeepEqual(gotDiffs, tt.wantDiffs) {
   340  				t.Errorf("CheckToFromRequired() = %v, want %v", gotDiffs, tt.wantDiffs)
   341  			}
   342  		})
   343  	}
   344  }
   345  
   346  func Test_compareProperties(t *testing.T) {
   347  	type args struct {
   348  		location  DifferenceLocation
   349  		schema1   *spec.Schema
   350  		schema2   *spec.Schema
   351  		getRefFn1 SchemaFromRefFn
   352  		getRefFn2 SchemaFromRefFn
   353  		cmp       CompareSchemaFn
   354  	}
   355  	tests := []struct {
   356  		name string
   357  		args args
   358  		want []SpecDifference
   359  	}{
   360  		// TODO: Add test cases.
   361  	}
   362  	for _, tt := range tests {
   363  		t.Run(tt.name, func(t *testing.T) {
   364  			if got := CompareProperties(tt.args.location, tt.args.schema1, tt.args.schema2, tt.args.getRefFn1, tt.args.getRefFn2, tt.args.cmp); !reflect.DeepEqual(got, tt.want) {
   365  				t.Errorf("compareProperties() = %v, want %v", got, tt.want)
   366  			}
   367  		})
   368  	}
   369  }
   370  
   371  func Test_propertiesFor(t *testing.T) {
   372  	type args struct {
   373  		schema   *spec.Schema
   374  		getRefFn SchemaFromRefFn
   375  	}
   376  	tests := []struct {
   377  		name string
   378  		args args
   379  		want PropertyMap
   380  	}{
   381  		// TODO: Add test cases.
   382  	}
   383  	for _, tt := range tests {
   384  		t.Run(tt.name, func(t *testing.T) {
   385  			if got := propertiesFor(tt.args.schema, tt.args.getRefFn); !reflect.DeepEqual(got, tt.want) {
   386  				t.Errorf("propertiesFor() = %v, want %v", got, tt.want)
   387  			}
   388  		})
   389  	}
   390  }
   391  
   392  func jsonStr(thing interface{}) string {
   393  	bstr, _ := JSONMarshal(thing)
   394  	return string(bstr)
   395  }