Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

easyjson unmarshaling test #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.DEFAULT_GOAL := help

test:
go test -v ./tests/...
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/wildberries-ru/go-transport-generator

go 1.14

require (
github.com/aws/aws-sdk-go v1.35.28
github.com/buaazp/fasthttprouter v0.1.1
github.com/bxcodec/faker/v3 v3.5.0
github.com/fatih/structtag v1.2.0 // indirect
github.com/go-kit/kit v0.10.0
github.com/mailru/easyjson v0.7.6
github.com/pkg/errors v0.9.1
github.com/prometheus/common v0.7.0
github.com/stretchr/testify v1.6.1
github.com/valyala/fasthttp v1.17.0
github.com/vetcher/go-astra v1.2.0
golang.org/x/mod v0.3.0
gopkg.in/yaml.v2 v2.3.0
)
404 changes: 404 additions & 0 deletions go.sum

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions tests/unmarshalling/httpclient/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Package httpclient ...
// CODE GENERATED AUTOMATICALLY
// DO NOT EDIT
package httpclient

import (
"net/url"

"github.com/pkg/errors"
"github.com/valyala/fasthttp"
)

const (
httpMethodTestEasyJson = "GET"
uriPathClientTestEasyJson = "/api/testeasyjson"
)

type errorProcessor interface {
Decode(r *fasthttp.Response) error
}

// New ...
func New(
serverURL string,
maxConns int,
errorProcessor errorProcessor,
options map[interface{}]Option,
) (client Service, err error) {
parsedServerURL, err := url.Parse(serverURL)
if err != nil {
err = errors.Wrap(err, "failed to parse server url")
return
}
transportTestEasyJson := NewTestEasyJsonTransport(
errorProcessor,
parsedServerURL.Scheme+"://"+parsedServerURL.Host+uriPathClientTestEasyJson,
httpMethodTestEasyJson,
)

client = NewClient(
&fasthttp.HostClient{
Addr: parsedServerURL.Host,
MaxConns: maxConns,
},
transportTestEasyJson,
options,
)
return
}
71 changes: 71 additions & 0 deletions tests/unmarshalling/httpclient/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Package httpclient ...
// CODE GENERATED AUTOMATICALLY
// DO NOT EDIT
package httpclient

import (
"fmt"
"math/rand"
"net/url"
"reflect"
"testing"
"time"

"github.com/valyala/fasthttp"
)

type testErrorProcessor struct{}

func TestNew(t *testing.T) {
serverURL := fmt.Sprintf("https://%v.com", time.Now().UnixNano())
parsedServerURL, _ := url.Parse(serverURL)
maxConns := rand.Int()
opts := map[interface{}]Option{}

transportTestEasyJson := NewTestEasyJsonTransport(
&testErrorProcessor{},
parsedServerURL.Scheme+"://"+parsedServerURL.Host+uriPathClientTestEasyJson,
httpMethodTestEasyJson,
)

cl := client{
&fasthttp.HostClient{
Addr: parsedServerURL.Host,
MaxConns: maxConns,
},
transportTestEasyJson,
opts,
}

type args struct {
serverURL string
maxConns int
errorProcessor errorProcessor
options map[interface{}]Option
}
tests := []struct {
name string
args args
wantClient Service
wantErr bool
}{
{"test new builder", args{serverURL, maxConns, &testErrorProcessor{}, opts}, &cl, false},
{"test new builder incorrect URL", args{" http:example%20.com", maxConns, &testErrorProcessor{}, opts}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotClient, err := New(tt.args.serverURL, tt.args.maxConns, tt.args.errorProcessor, tt.args.options)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotClient, tt.wantClient) {
t.Errorf("New() = %v, want %v", gotClient, tt.wantClient)
}
})
}
}

func (ep *testErrorProcessor) Decode(r *fasthttp.Response) error {
return nil
}
66 changes: 66 additions & 0 deletions tests/unmarshalling/httpclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package httpclient ...
// CODE GENERATED AUTOMATICALLY
// DO NOT EDIT
package httpclient

import (
"context"

"github.com/valyala/fasthttp"
)

// Options ...
var (
TestEasyJson = option{}
)

type option struct{}

// Option ...
type Option interface {
Prepare(ctx context.Context, r *fasthttp.Request)
}

// Service ...
type Service interface {
TestEasyJson(ctx context.Context, param1 int) (field1 string, field2 string, err error)
}

type client struct {
cli *fasthttp.HostClient
transportTestEasyJson TestEasyJsonTransport
options map[interface{}]Option
}

// TestEasyJson ...
func (s *client) TestEasyJson(ctx context.Context, param1 int) (field1 string, field2 string, err error) {
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(res)
}()
if opt, ok := s.options[TestEasyJson]; ok {
opt.Prepare(ctx, req)
}
if err = s.transportTestEasyJson.EncodeRequest(ctx, req, param1); err != nil {
return
}
err = s.cli.Do(req, res)
if err != nil {
return
}
return s.transportTestEasyJson.DecodeResponse(ctx, res)
}

// NewClient the client creator
func NewClient(
cli *fasthttp.HostClient,
transportTestEasyJson TestEasyJsonTransport,
options map[interface{}]Option,
) Service {
return &client{
cli: cli,
transportTestEasyJson: transportTestEasyJson,
options: options,
}
}
163 changes: 163 additions & 0 deletions tests/unmarshalling/httpclient/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Package httpclient ...
// CODE GENERATED AUTOMATICALLY
// DO NOT EDIT
package httpclient

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"

"github.com/bxcodec/faker/v3"
"github.com/valyala/fasthttp"
)

func Test_client_TestEasyJson(t *testing.T) {

var param1 int
_ = faker.FakeData(&param1)

var field1 string
_ = faker.FakeData(&field1)

var field2 string
_ = faker.FakeData(&field2)

maxConns := rand.Int() + 1
opts := map[interface{}]Option{}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

result := struct {
Field1 string `json:"field1"`

Field2 string `json:"field2"`
}{

Field1: field1,

Field2: field2,
}

b, _ := json.Marshal(result)
w.Write(b)
}))
defer ts.Close()

parsedServerURL, _ := url.Parse(ts.URL)

hostClient := &fasthttp.HostClient{
Addr: parsedServerURL.Host,
MaxConns: maxConns,
}

transportTestEasyJson := NewTestEasyJsonTransport(
&testErrorProcessor{},
parsedServerURL.Scheme+"://"+parsedServerURL.Host+uriPathClientTestEasyJson,
httpMethodTestEasyJson,
)

type fields struct {
cli *fasthttp.HostClient
transportTestEasyJson TestEasyJsonTransport
options map[interface{}]Option
}
type args struct {
ctx context.Context
param1 int
}
tests := []struct {
name string
fields fields
args args

wantField1 string

wantField2 string

wantErr bool
}{
{
"test TestEasyJson",
fields{hostClient, transportTestEasyJson, opts},
args{context.Background(), param1},
field1,
field2,

false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &client{
cli: tt.fields.cli,
transportTestEasyJson: tt.fields.transportTestEasyJson,
options: tt.fields.options,
}
gotField1, gotField2, err := s.TestEasyJson(tt.args.ctx, tt.args.param1)
if (err != nil) != tt.wantErr {
t.Errorf("client.TestEasyJson() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !reflect.DeepEqual(gotField1, tt.wantField1) {
t.Errorf("client.field1() = %v, want %v", gotField1, tt.wantField1)
}

if !reflect.DeepEqual(gotField2, tt.wantField2) {
t.Errorf("client.field2() = %v, want %v", gotField2, tt.wantField2)
}

})
}
}

func TestNewClient(t *testing.T) {
serverURL := fmt.Sprintf("https://%v.com", time.Now().UnixNano())
parsedServerURL, _ := url.Parse(serverURL)
hostClient := &fasthttp.HostClient{
Addr: parsedServerURL.Host,
MaxConns: rand.Int(),
}
opts := map[interface{}]Option{}

transportTestEasyJson := NewTestEasyJsonTransport(
&testErrorProcessor{},
parsedServerURL.Scheme+"://"+parsedServerURL.Host+uriPathClientTestEasyJson,
httpMethodTestEasyJson,
)

cl := &client{
hostClient,
transportTestEasyJson,
opts,
}

type args struct {
cli *fasthttp.HostClient

transportTestEasyJson TestEasyJsonTransport

options map[interface{}]Option
}
tests := []struct {
name string
args args
want Service
}{
{"test new client", args{hostClient, transportTestEasyJson, opts}, cl},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewClient(tt.args.cli, tt.args.transportTestEasyJson, tt.args.options); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewClient() = %v, want %v", got, tt.want)
}
})
}
}
Loading