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

     1  package diff
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/go-openapi/spec"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGetTypeFromSimpleSchema(t *testing.T) {
    12  	s := spec.SimpleSchema{Type: "string"}
    13  	ty, a := getTypeFromSimpleSchema(&s)
    14  	assert.Equal(t, "string", ty)
    15  	assert.False(t, a)
    16  
    17  	arr := spec.SimpleSchema{Type: "array", Items: spec.NewItems().Typed("integer", "int32")}
    18  	ty, a = getTypeFromSimpleSchema(&arr)
    19  	assert.Equal(t, "integer.int32", ty)
    20  	assert.True(t, a)
    21  
    22  }
    23  
    24  func TestIsArray(t *testing.T) {
    25  	arr := spec.SimpleSchema{Type: "array", Items: spec.NewItems().Typed("integer", "int32")}
    26  	assert.True(t, isArray(&arr))
    27  	assert.False(t, isArray(&time.Time{}))
    28  }
    29  
    30  func TestIsPrimitive(t *testing.T) {
    31  	sa := spec.StringOrArray{"string"}
    32  	assert.True(t, isPrimitive(sa))
    33  
    34  	s := spec.Schema{SchemaProps: spec.SchemaProps{Type: sa}}
    35  	assert.True(t, isPrimitive(&s))
    36  	assert.False(t, isPrimitive(&time.Time{}))
    37  
    38  	sc := spec.Schema{}
    39  	assert.False(t, isPrimitive(&sc))
    40  }
    41  
    42  func TestGetSchemaType(t *testing.T) {
    43  	tt, a := getSchemaType(time.Time{})
    44  	assert.False(t, a)
    45  	assert.Equal(t, "unknown", tt)
    46  
    47  	s := spec.SimpleSchema{Type: "string"}
    48  	tt, a = getSchemaType(s)
    49  	assert.False(t, a)
    50  	assert.Equal(t, "string", tt)
    51  
    52  }
    53  
    54  func TestDefinitionFromRef(t *testing.T) {
    55  	refStr := definitionFromRef(spec.MustCreateRef(""))
    56  	assert.True(t, len(refStr) == 0)
    57  }