...

Source file src/github.com/go-openapi/analysis/mixin_test.go

Documentation: github.com/go-openapi/analysis

     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 analysis
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/go-openapi/analysis/internal/antest"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  const (
    25  	widgetFile     = "fixtures/widget-crud.yml"
    26  	fooFile        = "fixtures/foo-crud.yml"
    27  	barFile        = "fixtures/bar-crud.yml"
    28  	noPathsFile    = "fixtures/no-paths.yml"
    29  	emptyPathsFile = "fixtures/empty-paths.json"
    30  	securityFile   = "fixtures/securitydef.yml"
    31  	otherMixin     = "fixtures/other-mixin.yml"
    32  	emptyProps     = "fixtures/empty-props.yml"
    33  	swaggerProps   = "fixtures/swagger-props.yml"
    34  )
    35  
    36  func TestMixin_All(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	primary := antest.LoadOrFail(t, widgetFile)
    40  	mixin1 := antest.LoadOrFail(t, fooFile)
    41  	mixin2 := antest.LoadOrFail(t, barFile)
    42  	mixin3 := antest.LoadOrFail(t, noPathsFile)
    43  	mixin4 := antest.LoadOrFail(t, securityFile)
    44  	mixin5 := antest.LoadOrFail(t, otherMixin)
    45  
    46  	collisions := Mixin(primary, mixin1, mixin2, mixin3, mixin4, mixin5)
    47  
    48  	require.Lenf(t, collisions, 19, "TestMixin: Expected 19 collisions, got %v\n%v", len(collisions), collisions)
    49  	require.Lenf(t, primary.Paths.Paths, 7, "TestMixin: Expected 7 paths in merged, got %v\n", len(primary.Paths.Paths))
    50  	require.Lenf(t, primary.Definitions, 8, "TestMixin: Expected 8 definitions in merged, got %v\n", len(primary.Definitions))
    51  	require.Lenf(t, primary.Parameters, 4, "TestMixin: Expected 4 top level parameters in merged, got %v\n", len(primary.Parameters))
    52  	require.Lenf(t, primary.Responses, 2, "TestMixin: Expected 2 top level responses in merged, got %v\n", len(primary.Responses))
    53  	require.Lenf(t, primary.SecurityDefinitions, 5, "TestMixin: Expected 5 top level SecurityDefinitions in merged, got %v\n", len(primary.SecurityDefinitions))
    54  	require.Lenf(t, primary.Security, 3, "TestMixin: Expected 3 top level Security requirements in merged, got %v\n", len(primary.Security))
    55  	require.Lenf(t, primary.Tags, 3, "TestMixin: Expected 3 top level tags merged, got %v\n", len(primary.Security))
    56  	require.Lenf(t, primary.Schemes, 2, "TestMixin: Expected 2 top level schemes merged, got %v\n", len(primary.Security))
    57  	require.Lenf(t, primary.Consumes, 2, "TestMixin: Expected 2 top level Consumers merged, got %v\n", len(primary.Security))
    58  	require.Lenf(t, primary.Produces, 2, "TestMixin: Expected 2 top level Producers merged, got %v\n", len(primary.Security))
    59  }
    60  
    61  func TestMixin_EmptyPath(t *testing.T) {
    62  	t.Parallel()
    63  
    64  	// test that adding paths to a primary with no paths works (was NPE)
    65  	primary := antest.LoadOrFail(t, widgetFile)
    66  	emptyPaths := antest.LoadOrFail(t, emptyPathsFile)
    67  
    68  	collisions := Mixin(emptyPaths, primary)
    69  
    70  	require.Emptyf(t, collisions, "TestMixin: Expected 0 collisions, got %v\n%v", len(collisions), collisions)
    71  }
    72  
    73  func TestMixin_FromNilPath(t *testing.T) {
    74  	t.Parallel()
    75  
    76  	primary := antest.LoadOrFail(t, otherMixin)
    77  	mixin1 := antest.LoadOrFail(t, widgetFile)
    78  
    79  	collisions := Mixin(primary, mixin1)
    80  
    81  	require.Lenf(t, collisions, 1, "TestMixin: Expected 1 collisions, got %v\n%v", len(collisions), collisions)
    82  	require.Lenf(t, primary.Paths.Paths, 3, "TestMixin: Expected 3 paths in merged, got %v\n", len(primary.Paths.Paths))
    83  }
    84  
    85  func TestMixin_SwaggerProps(t *testing.T) {
    86  	t.Parallel()
    87  
    88  	primary := antest.LoadOrFail(t, emptyProps)
    89  	mixin := antest.LoadOrFail(t, swaggerProps)
    90  
    91  	collisions := Mixin(primary, mixin)
    92  
    93  	require.Lenf(t, collisions, 1, "TestMixin: Expected 1 collisions, got %v\n%v", len(collisions), collisions)
    94  }
    95  

View as plain text