test

package
v3.10.55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ExtToTypes map file extension to content type
	ExtToTypes = map[string][]string{
		"json":  {"application/json", "application/grpc+json"},
		"yaml":  {"application/yaml", "application/yml", "text/yaml", "text/yml"},
		"yml":   {"application/yaml", "application/yml", "text/yaml", "text/yml"},
		"proto": {"application/grpc", "application/grpc+proto", "application/proto"},
	}
	// DefaultExts specifies default file extensions to load data
	DefaultExts = []string{"csv", "json", "yaml", "yml", "proto"}
	// Codecs map to detect codec for test file or request content type
	Codecs map[string]codec.Codec

	// ResponseCompareFunc used to compare actual response with test case data
	ResponseCompareFunc = func(expectRsp []byte, testRsp interface{}, expectCodec codec.Codec, testCodec codec.Codec) error {
		var err error

		expectMap := make(map[string]interface{})
		if err = expectCodec.Unmarshal(expectRsp, &expectMap); err != nil {
			return fmt.Errorf("failed to unmarshal err: %w", err)
		}

		testMap := make(map[string]interface{})
		switch v := testRsp.(type) {
		case *codec.Frame:
			if err = testCodec.Unmarshal(v.Data, &testMap); err != nil {
				return fmt.Errorf("failed to unmarshal err: %w", err)
			}
		case *errors.Error:
			if err = expectCodec.Unmarshal([]byte(v.Error()), &testMap); err != nil {
				return fmt.Errorf("failed to unmarshal err: %w", err)
			}
		case error:
			st, ok := status.FromError(v)
			if !ok {
				return v
			}
			me := errors.Parse(st.Message())
			if me.Code != 0 {
				if err = expectCodec.Unmarshal([]byte(me.Error()), &testMap); err != nil {
					return fmt.Errorf("failed to unmarshal err: %w", err)
				}
				break
			}
			for _, se := range st.Details() {
				switch ne := se.(type) {
				case proto.Message:
					buf, err := testCodec.Marshal(ne)
					if err != nil {
						return fmt.Errorf("failed to marshal err: %w", err)
					}
					if err = testCodec.Unmarshal(buf, &testMap); err != nil {
						return fmt.Errorf("failed to unmarshal err: %w", err)
					}
				default:
					return st.Err()
				}
			}
		case interface{ GRPCStatus() *status.Status }:
			st := v.GRPCStatus()
			me := errors.Parse(st.Message())
			if me.Code != 0 {
				if err = expectCodec.Unmarshal([]byte(me.Error()), &testMap); err != nil {
					return fmt.Errorf("failed to unmarshal err: %w", err)
				}
				break
			}
		case *status.Status:
			me := errors.Parse(v.Message())
			if me.Code != 0 {
				if err = expectCodec.Unmarshal([]byte(me.Error()), &testMap); err != nil {
					return fmt.Errorf("failed to unmarshal err: %w", err)
				}
				break
			}
			for _, se := range v.Details() {
				switch ne := se.(type) {
				case proto.Message:
					buf, err := testCodec.Marshal(ne)
					if err != nil {
						return fmt.Errorf("failed to marshal err: %w", err)
					}
					if err = testCodec.Unmarshal(buf, &testMap); err != nil {
						return fmt.Errorf("failed to unmarshal err: %w", err)
					}
				default:
					return v.Err()
				}
			}
		}

		if !reflect.DeepEqual(expectMap, testMap) {
			return fmt.Errorf("test: %s != rsp: %s", expectMap, testMap)
		}

		return nil
	}
)
View Source
var ErrUnknownContentType = fmt.Errorf("unknown content type")

Functions

func CSVColumnParser

func CSVColumnParser(s string) []byte

func FromCSVString

func FromCSVString(columns []*sqlmock.Column, rows *sqlmock.Rows, s string) *sqlmock.Rows

func NewRequestFromFile

func NewRequestFromFile(c client.Client, reqfile string) (client.Request, error)

func NewResponseFromFile

func NewResponseFromFile(rspfile string) (*codec.Frame, error)

func Run

func Run(ctx context.Context, c client.Client, m sqlmock.Sqlmock, dir string, exts []string) error

func SQLFromBytes

func SQLFromBytes(m sqlmock.Sqlmock, buf []byte) error

func SQLFromFile

func SQLFromFile(m sqlmock.Sqlmock, name string) error

func SQLFromReader

func SQLFromReader(m sqlmock.Sqlmock, r io.Reader) error

func SQLFromString

func SQLFromString(m sqlmock.Sqlmock, buf string) error

Types

type Case

type Case struct {
	// contains filtered or unexported fields
}

func GetCases

func GetCases(dir string, exts []string) ([]Case, error)

type Extension

type Extension struct {
	Ext []string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL