Skip to content

Commit

Permalink
test: add some tests to ensure extensions parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Sep 13, 2022
1 parent 19e884d commit a6622e8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
43 changes: 43 additions & 0 deletions jsonschema/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package jsonschema

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/go-faster/errors"
yaml "github.com/go-faster/yamlx"
"github.com/stretchr/testify/require"

"github.com/ogen-go/ogen/internal/jsonpointer"
"github.com/ogen-go/ogen/internal/location"
)

type components map[string]*RawSchema
Expand Down Expand Up @@ -345,6 +348,46 @@ func TestSchemaReferencedArray(t *testing.T) {
require.Equal(t, expect, out)
}

func TestSchemaExtensions(t *testing.T) {
tests := []struct {
raw string
expect *Schema
expectErr bool
}{
{
`{"type": "string", "x-ogen-name": "foo"}`,
&Schema{
Type: String,
XOgenName: "foo",
},
false,
},
{`{"type": "string", "x-ogen-name": {}}`, nil, true},
}

for i, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
a := require.New(t)

var raw RawSchema
a.NoError(yaml.Unmarshal([]byte(tt.raw), &raw))

out, err := NewParser(Settings{
Filename: "test.yaml",
}).Parse(&raw)
if tt.expectErr {
a.Error(err)
return
}
a.NoError(err)
// Zero locator to simplify comparison.
out.Locator = location.Locator{}
a.Equal(tt.expect, out)
})
}
}

func TestInvalidMultipleOf(t *testing.T) {
values := []int{0, -1, -10}
parser := NewParser(Settings{
Expand Down
43 changes: 43 additions & 0 deletions spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ogen_test

import (
"testing"

yaml "github.com/go-faster/yamlx"
"github.com/stretchr/testify/require"

"github.com/ogen-go/ogen"
)

func encodeDecode[T any](a *require.Assertions, input T) (result T) {
data, err := yaml.Marshal(input)
a.NoError(err)

a.NoError(yaml.Unmarshal(data, &result))
return result
}

func TestExtensionParsing(t *testing.T) {
a := require.New(t)

{
var (
input = `{"url": "/api/v1", "x-ogen-name": "foo"}`
s ogen.Server
)
a.NoError(yaml.Unmarshal([]byte(input), &s))
a.Equal("foo", s.Extensions["x-ogen-name"].Value)
s2 := encodeDecode(a, s)
a.Equal("foo", s2.Extensions["x-ogen-name"].Value)
}

{
var (
input = `{"description": "foo", "x-ogen-extension": "bar"}`
s ogen.Response
)
a.NoError(yaml.Unmarshal([]byte(input), &s))
a.Equal("bar", s.Common.Extensions["x-ogen-extension"].Value)
// FIXME(tdakkota): encodeDecode doesn't work for this type
}
}

0 comments on commit a6622e8

Please sign in to comment.