github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/courier/swagger/gen/operator_scanner_test.go (about)

     1  package gen
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/artisanhe/tools/codegen/loaderx"
     9  )
    10  
    11  func TestOperatorScanner(t *testing.T) {
    12  	tt := assert.New(t)
    13  
    14  	pkgImportPath, program := loaderx.NewTestProgram(`
    15  	package main
    16  
    17  	import (
    18  		"fmt"
    19  		"net/http"
    20  		"github.com/artisanhe/tools/courier/status_error"
    21  	)
    22  
    23  	type With struct {
    24  		Query string  ^^name:"query" in:"query"^^
    25  	}
    26  
    27  	// swagger:enum
    28  	type Status uint8
    29  
    30  	const (
    31  		STATUS_UNKNOWN Status = iota
    32  		STATUS__A    // A
    33  		STATUS__B    // B
    34  	)
    35  
    36  	// Op
    37  	//go:generate echo
    38  	type Op struct {
    39  		With
    40  		Param string  ^^name:"param" in:"path"^^
    41  		Status Status  ^^name:"status,omitempty" in:"query" validate:"@string{A}"^^
    42  		FormData struct {
    43  			Name string ^^name:"status"^^
    44  		}  ^^in:"formData"^^
    45  	}
    46  
    47  	func (op Op) Output() (resp interface{}, err error) {
    48  		if op.Query == "" {
    49  			return nil, status_error.InvalidStruct
    50  		}
    51  		resp = &Resp{
    52  			Name: "s",
    53  		}
    54  
    55  		call := func() int {
    56  			return 1
    57  		}
    58  		fmt.Println(call())
    59  
    60  		return
    61  	}
    62  
    63  	type Resp struct {
    64  		Name string ^^json:"name"^^
    65  	}
    66  
    67  	func (resp Resp) ContentType() string {
    68  		return "application/json"
    69  	}
    70  
    71  	func (resp Resp) Status() int {
    72  		return http.StatusOK
    73  	}
    74  	`)
    75  
    76  	scanner := NewOperatorScanner(program)
    77  	query := loaderx.NewQuery(program, pkgImportPath)
    78  
    79  	operator := scanner.Operator(query.TypeName("Op"))
    80  
    81  	tt.Equal("Op", operator.Summary)
    82  
    83  	tt.Equal(ToMap(map[string]map[string]interface{}{
    84  		"query": {
    85  			"name":     "query",
    86  			"in":       "query",
    87  			"required": true,
    88  			XField:     "Query",
    89  			"schema": map[string]interface{}{
    90  				"type": "string",
    91  			},
    92  		},
    93  		"status": {
    94  			"name": "status",
    95  			"in":   "query",
    96  			"schema": map[string]interface{}{
    97  				"allOf": []map[string]interface{}{
    98  					{
    99  						"$ref": "#/components/schemas/Status",
   100  					},
   101  					{
   102  						"enum":       []interface{}{"A"},
   103  						XTagValidate: "@string{A}",
   104  					},
   105  				},
   106  			},
   107  			XField: "Status",
   108  		},
   109  		"param": {
   110  			"name":     "param",
   111  			"in":       "path",
   112  			"required": true,
   113  			XField:     "Param",
   114  			"schema": map[string]interface{}{
   115  				"type": "string",
   116  			},
   117  		},
   118  	}), ToMap(operator.NonBodyParameters))
   119  }
   120  
   121  func TestOperatorScannerWithFile(t *testing.T) {
   122  	tt := assert.New(t)
   123  
   124  	pkgImportPath, program := loaderx.NewTestProgram(`
   125  	package main
   126  
   127  	import (
   128  		"github.com/artisanhe/tools/courier/transport_http"
   129  	)
   130  
   131  	// Op
   132  	type Op struct {
   133  	}
   134  
   135  	// @success content-type text/plain
   136  	func (op Op) Output() (resp interface{}, err error) {
   137  		file := transport_http.NewFile("1.txt", "text/plain")
   138  		resp = file
   139  		return
   140  	}
   141  
   142  	`)
   143  
   144  	scanner := NewOperatorScanner(program)
   145  	query := loaderx.NewQuery(program, pkgImportPath)
   146  
   147  	operator := scanner.Operator(query.TypeName("Op"))
   148  
   149  	_, ok := operator.SuccessResponse.WithContent.Content["text/plain"]
   150  	tt.True(ok)
   151  }
   152  
   153  func TestOperatorScannerForWebSocket(t *testing.T) {
   154  	pkgImportPath, program := loaderx.NewTestProgram(`
   155  	package main
   156  
   157  	import (
   158  		"github.com/artisanhe/tools/courier/transport_http"
   159  	)
   160  
   161  	type WS struct {
   162  	}
   163  
   164  	func (ws WS) Output() (resp interface{}, err error) {
   165  		listeners := transport_http.Listeners{}
   166  		listeners.On(Ping{}, func(v interface{}, ws *transport_http.WSClient) error {
   167  			p := v.(*Ping)
   168  			return ws.Send(Pong{
   169  				MS: p.MS + 1,
   170  			})
   171  		})
   172  		resp = listeners
   173  		return
   174  	}
   175  
   176  	type Ping struct {
   177  		MS int ^^json:"ms"^^
   178  	}
   179  
   180  	type Pong struct {
   181  		MS int ^^json:"ms"^^
   182  	}
   183  	`)
   184  
   185  	scanner := NewOperatorScanner(program)
   186  	query := loaderx.NewQuery(program, pkgImportPath)
   187  
   188  	scanner.Operator(query.TypeName("WS"))
   189  }