How to use Desc method of got Package

Best Got code snippet using got.Desc

reflection_test.go

Source:reflection_test.go Github

copy

Full Screen

...57 },58 }59 for _, tc := range cases {60 t.Run(tc.name, func(t *testing.T) {61 fd := test.NewFileDescriptor(t, test.File)62 r := NewReflector(&test.MockGrpcreflectClient{FileDescriptor: fd})63 i, err := r.CreateInvocation(tc.serviceName, tc.methodName, []byte(tc.message))64 if got, want := i == nil, tc.invocationIsNil; got != want {65 t.Fatalf("got %t, want %t", got, want)66 }67 if got, want := err == nil, tc.errorIsNil; got != want {68 t.Fatalf("got %v, want %v", got, want)69 }70 })71 }72}73func TestReflectorImpl_ListServices(t *testing.T) {74 fd := test.NewFileDescriptor(t, test.File)75 r := NewReflector(&test.MockGrpcreflectClient{FileDescriptor: fd})76 services, err := r.ListServices()77 if err != nil {78 t.Fatalf("unexpected error: %v", err)79 }80 if got, want := len(services), 1; got != want {81 t.Fatalf("got %d services, want %d", got, want)82 }83}84func TestReflectorImpl_DescribeService(t *testing.T) {85 cases := []struct {86 name string87 serviceName string88 methodsLen int89 descIsNil bool90 errorIsNil bool91 }{92 {93 name: "found",94 serviceName: test.TestService,95 methodsLen: 6,96 descIsNil: false,97 errorIsNil: true,98 },99 {100 name: "not found",101 serviceName: test.NotFoundService,102 methodsLen: 0,103 descIsNil: true,104 errorIsNil: false,105 },106 }107 for _, tc := range cases {108 t.Run(tc.name, func(t *testing.T) {109 r := NewReflector(&test.MockGrpcreflectClient{FileDescriptor: test.NewFileDescriptor(t, test.File)})110 methodDescs, err := r.DescribeService(tc.serviceName)111 if got, want := len(methodDescs), tc.methodsLen; got != want {112 t.Fatalf("got %d methods, want %d", got, want)113 }114 if got, want := err == nil, tc.errorIsNil; got != want {115 t.Fatalf("got %v, want %v", got, want)116 }117 })118 }119}120func TestReflectionClient_ResolveService(t *testing.T) {121 cases := []struct {122 name string123 serviceName string124 descIsNil bool125 error *perrors.ProxyError126 }{127 {128 name: "found",129 serviceName: test.TestService,130 descIsNil: false,131 error: nil,132 },133 {134 name: "not found",135 serviceName: test.NotFoundService,136 descIsNil: true,137 error: &perrors.ProxyError{138 Code: perrors.ServiceNotFound,139 Message: fmt.Sprintf("service %s was not found upstream", "not.found.NoService"),140 },141 },142 }143 for _, tc := range cases {144 t.Run(tc.name, func(t *testing.T) {145 c := newReflectionClient(&test.MockGrpcreflectClient{FileDescriptor: test.NewFileDescriptor(t, test.File)})146 serviceDesc, err := c.resolveService(tc.serviceName)147 if got, want := serviceDesc == nil, tc.descIsNil; got != want {148 t.Fatalf("got %t, want %t", got, want)149 }150 {151 err, ok := err.(*perrors.ProxyError)152 if !ok {153 err = nil154 }155 if got, want := err, tc.error; !reflect.DeepEqual(got, want) {156 t.Fatalf("got %v, want %v", got, want)157 }158 }159 })160 }161}162func TestServiceDescriptor_GetMethods(t *testing.T) {163 fd := test.NewFileDescriptor(t, test.File)164 serviceDesc := ServiceDescriptorFromFileDescriptor(fd, test.TestService)165 if serviceDesc == nil {166 t.Fatalf("service descriptor is nil")167 }168 mds, err := serviceDesc.GetMethods()169 if err != nil {170 t.Fatalf("unexpected error: %v", err)171 }172 want := []string{173 "EmptyCall",174 "UnaryCall",175 "StreamingOutputCall",176 "StreamingInputCall",177 "FullDuplexCall",178 "HalfDuplexCall",179 }180 got := make([]string, len(mds))181 for i, m := range mds {182 got[i] = m.GetName()183 }184 if !reflect.DeepEqual(got, want) {185 t.Fatalf("got %v, want %v", got, want)186 }187}188func TestServiceDescriptor_FindMethodByName(t *testing.T) {189 cases := []struct {190 name string191 methodName string192 descIsNil bool193 error *perrors.ProxyError194 }{195 {196 name: "method found",197 methodName: test.EmptyCall,198 descIsNil: false,199 error: nil,200 },201 {202 name: "method not found",203 methodName: test.NotFoundCall,204 descIsNil: true,205 error: &perrors.ProxyError{206 Code: perrors.MethodNotFound,207 Message: fmt.Sprintf("the method %s was not found", test.NotFoundCall),208 },209 },210 }211 for _, tc := range cases {212 t.Run(tc.name, func(t *testing.T) {213 file := test.NewFileDescriptor(t, test.File)214 serviceDesc := ServiceDescriptorFromFileDescriptor(file, test.TestService)215 if serviceDesc == nil {216 t.Fatalf("service descriptor is nil")217 }218 methodDesc, err := serviceDesc.FindMethodByName(tc.methodName)219 if got, want := methodDesc == nil, tc.descIsNil; got != want {220 t.Fatalf("got %t, want %t", got, want)221 }222 {223 err, ok := err.(*perrors.ProxyError)224 if !ok {225 err = nil226 }227 if got, want := err, tc.error; !reflect.DeepEqual(got, want) {228 t.Fatalf("got %v, want %v", got, want)229 }230 }231 })232 }233}234func TestMethodDescriptor_GetInputType(t *testing.T) {235 cases := []struct {236 name string237 serviceName string238 methodName string239 descIsNil bool240 }{241 {242 name: "input type found",243 serviceName: test.TestService,244 methodName: test.UnaryCall,245 descIsNil: false,246 },247 }248 for _, tc := range cases {249 t.Run(tc.name, func(t *testing.T) {250 file := test.NewFileDescriptor(t, test.File)251 serviceDesc := ServiceDescriptorFromFileDescriptor(file, tc.serviceName)252 methodDesc, err := serviceDesc.FindMethodByName(tc.methodName)253 if err != nil {254 t.Fatalf(err.Error())255 }256 inputMsgDesc := methodDesc.GetInputType()257 if got, want := inputMsgDesc == nil, tc.descIsNil; got != want {258 t.Fatalf("got %t, want %t", got, want)259 }260 })261 }262}263func TestMethodDescriptor_GetOutputType(t *testing.T) {264 cases := []struct {265 name string266 serviceName string267 methodName string268 descIsNil bool269 }{270 {271 name: "output type found",272 serviceName: test.TestService,273 methodName: test.EmptyCall,274 descIsNil: false,275 },276 }277 for _, tc := range cases {278 t.Run(tc.name, func(t *testing.T) {279 file := test.NewFileDescriptor(t, test.File)280 serviceDesc := ServiceDescriptorFromFileDescriptor(file, tc.serviceName)281 methodDesc, err := serviceDesc.FindMethodByName(tc.methodName)282 if err != nil {283 t.Fatalf(err.Error())284 }285 inputMsgDesc := methodDesc.GetOutputType()286 if got, want := inputMsgDesc == nil, tc.descIsNil; got != want {287 t.Fatalf("got %t, want %t", got, want)288 }289 })290 }291}292func TestMethodDescriptor_GetName(t *testing.T) {293 cases := []struct {294 name string295 serviceName string296 methodName string297 descIsNil bool298 }{299 {300 name: "get method name",301 serviceName: test.TestService,302 methodName: test.EmptyCall,303 descIsNil: false,304 },305 }306 for _, tc := range cases {307 t.Run(tc.name, func(t *testing.T) {308 file := test.NewFileDescriptor(t, test.File)309 serviceDesc := ServiceDescriptorFromFileDescriptor(file, tc.serviceName)310 methodDesc, err := serviceDesc.FindMethodByName(tc.methodName)311 if err != nil {312 t.Fatalf(err.Error())313 }314 if got, want := methodDesc.GetName(), tc.methodName; got != want {315 t.Fatalf("got %s, want %s", got, want)316 }317 })318 }319}320func TestMessageDescriptor_NewMessage(t *testing.T) {321 file := test.NewFileDescriptor(t, test.File)322 serviceDesc := ServiceDescriptorFromFileDescriptor(file, test.TestService)323 if serviceDesc == nil {324 t.Fatal("service descriptor is nil")325 }326 methodDesc, err := serviceDesc.FindMethodByName(test.EmptyCall)327 if err != nil {328 t.Fatalf(err.Error())329 }330 inputMsgDesc := methodDesc.GetInputType()331 inputMsg := inputMsgDesc.NewMessage()332 if got, want := inputMsg == nil, false; got != want {333 t.Fatalf("got %t, want %t", got, want)334 }335}336func TestMessageDescriptor_GetFullyQualifiedName(t *testing.T) {337 file := test.NewFileDescriptor(t, test.File)338 serviceDesc := ServiceDescriptorFromFileDescriptor(file, test.TestService)339 if serviceDesc == nil {340 t.Fatal("service descriptor is nil")341 }342 methodDesc, err := serviceDesc.FindMethodByName(test.UnaryCall)343 if err != nil {344 t.Fatalf(err.Error())345 }346 inputMsgDesc := methodDesc.GetInputType()347 name := inputMsgDesc.GetFullyQualifiedName()348 if got, want := name, test.UnaryCallInputMsgName; got != want {349 t.Fatalf("got %s, want %s", got, want)350 }351}352func TestMessage_MarshalJSON(t *testing.T) {353 file := test.NewFileDescriptor(t, test.File)354 cases := []struct {355 name string356 json []byte357 error358 }{359 {360 name: "success",361 json: []byte("{\"body\":\"aGVsbG8=\"}"),362 error: nil,363 },364 }365 for _, tc := range cases {366 t.Run(tc.name, func(t *testing.T) {367 messageDesc := file.FindMessage(test.MessageName)368 if messageDesc == nil {369 t.Fatal("messageImpl descriptor is nil")370 }371 message := messageImpl{372 Message: dynamic.NewMessage(messageDesc),373 }374 message.Message.SetField(message.Message.FindFieldDescriptorByName("body"), []byte("hello"))375 j, err := message.MarshalJSON()376 if got, want := j, tc.json; !reflect.DeepEqual(got, want) {377 t.Fatalf("got %v, want %v", got, want)378 }379 if got, want := err, tc.error; !reflect.DeepEqual(got, want) {380 t.Fatalf("got %v, want %v", got, want)381 }382 })383 }384}385func TestMessage_UnmarshalJSON(t *testing.T) {386 file := test.NewFileDescriptor(t, test.File)387 cases := []struct {388 name string389 json []byte390 error391 }{392 {393 name: "success",394 json: []byte("{\"body\":\"aGVsbG8=\"}"),395 error: nil,396 },397 {398 name: "type mismatch",399 json: []byte("{\"body\":\"hello!\""),400 error: &perrors.ProxyError{401 Code: perrors.MessageTypeMismatch,402 Message: "input JSON does not match messageImpl type",403 },404 },405 }406 for _, tc := range cases {407 t.Run(tc.name, func(t *testing.T) {408 messageDesc := file.FindMessage(test.MessageName)409 if messageDesc == nil {410 t.Fatal("messageImpl descriptor is nil")411 }412 message := messageImpl{413 Message: dynamic.NewMessage(messageDesc),414 }415 err := message.UnmarshalJSON(tc.json)416 expectedMessage := dynamic.NewMessage(messageDesc)417 expectedMessage.SetField(expectedMessage.FindFieldDescriptorByName("body"), []byte("hello!"))418 if got, want := err, tc.error; !reflect.DeepEqual(got, want) {419 t.Fatalf("got %v, want %v", got, want)420 }421 })422 }423}...

Full Screen

Full Screen

value_test.go

Source:value_test.go Github

copy

Full Screen

1package value2import (3 "encoding/json"4 "math"5 "strings"6 "testing"7 "time"8 "github.com/google/uuid"9 "github.com/stretchr/testify/assert"10)11func TestBool(t *testing.T) {12 t.Parallel()13 tests := []struct {14 desc string15 i interface{}16 err bool17 want Bool18 }{19 {20 desc: "value is non-nil and non-bool",21 i: 23,22 err: true,23 },24 {25 desc: "value is nil",26 i: nil,27 want: Bool{},28 },29 {30 desc: "value is false",31 i: false,32 want: Bool{Valid: true},33 },34 {35 desc: "value is true",36 i: true,37 want: Bool{Value: true, Valid: true},38 },39 }40 for _, test := range tests {41 test := test // capture42 t.Run(test.desc, func(t *testing.T) {43 t.Parallel()44 got := Bool{}45 err := got.Unmarshal(test.i)46 if test.err {47 assert.Error(t, err)48 return49 }50 assert.NoError(t, err)51 assert.Equal(t, test.want, got)52 })53 }54}55func TestDateTime(t *testing.T) {56 t.Parallel()57 tests := []struct {58 desc string59 i interface{}60 err bool61 want DateTime62 }{63 {64 desc: "value is non-nil and non-string",65 i: 23,66 err: true,67 },68 {69 desc: "value is non-RFC3339Nano",70 i: "Mon, 02 Jan 2006 15:04:05 -0700",71 err: true,72 },73 {74 desc: "value is nil",75 i: nil,76 want: DateTime{},77 },78 {79 desc: "value is RFC3339Nano",80 i: "2019-08-27T04:14:55.302919Z",81 want: DateTime{82 Value: timeMustParse(time.RFC3339Nano, "2019-08-27T04:14:55.302919Z"),83 Valid: true,84 },85 },86 }87 for _, test := range tests {88 test := test // capture89 t.Run(test.desc, func(t *testing.T) {90 t.Parallel()91 got := DateTime{}92 err := got.Unmarshal(test.i)93 if test.err {94 assert.Error(t, err)95 return96 }97 assert.NoError(t, err)98 assert.Equal(t, test.want, got)99 })100 }101}102func TestDynamic(t *testing.T) {103 t.Parallel()104 tests := []struct {105 desc string106 i interface{}107 err bool108 want Dynamic109 }{110 {111 desc: "value is nil",112 i: nil,113 want: Dynamic{},114 },115 {116 desc: "value is string",117 i: `{"Visualization":null}`,118 want: Dynamic{119 Value: []byte(`{"Visualization":null}`),120 Valid: true,121 },122 },123 {124 desc: "value is []byte",125 i: []byte(`{"Visualization":null}`),126 want: Dynamic{127 Value: []byte(`{"Visualization":null}`),128 Valid: true,129 },130 },131 {132 desc: "value is map[string]interface{}",133 i: map[string]interface{}{"Visualization": nil},134 want: Dynamic{135 Value: []byte(`{"Visualization":null}`),136 Valid: true,137 },138 },139 {140 desc: "value is a []interface{}",141 i: []interface{}{1, "hello", 2.3},142 want: Dynamic{143 Value: []byte(`[1,"hello",2.3]`),144 Valid: true,145 },146 },147 }148 for _, test := range tests {149 test := test // capture150 t.Run(test.desc, func(t *testing.T) {151 t.Parallel()152 got := Dynamic{}153 err := got.Unmarshal(test.i)154 if test.err {155 assert.Error(t, err)156 return157 }158 assert.NoError(t, err)159 assert.EqualValues(t, test.want, got)160 })161 }162}163func TestGUID(t *testing.T) {164 t.Parallel()165 goodUUID := uuid.New()166 tests := []struct {167 desc string168 i interface{}169 err bool170 want GUID171 }{172 {173 desc: "value is non-nil and non-string",174 i: 23,175 err: true,176 },177 {178 desc: "value is a string, but not a UUID",179 i: "hello world",180 err: true,181 },182 {183 desc: "value is nil",184 i: nil,185 want: GUID{},186 },187 {188 desc: "value is a UUID",189 i: goodUUID.String(),190 want: GUID{Value: goodUUID, Valid: true},191 },192 }193 for _, test := range tests {194 test := test // capture195 t.Run(test.desc, func(t *testing.T) {196 t.Parallel()197 got := GUID{}198 err := got.Unmarshal(test.i)199 if test.err {200 assert.Error(t, err)201 return202 }203 assert.NoError(t, err)204 assert.EqualValues(t, test.want, got)205 })206 }207}208func TestInt(t *testing.T) {209 t.Parallel()210 tests := []struct {211 desc string212 i interface{}213 err bool214 want Int215 }{216 {217 desc: "value is non-nil and non-int",218 i: "hello",219 err: true,220 },221 {222 desc: "value is json.Number that is a float",223 i: json.Number("3.2"),224 err: true,225 },226 {227 desc: "value is greater than int32",228 i: math.MaxInt32 + 1,229 err: true,230 },231 {232 desc: "value is nil",233 i: nil,234 want: Int{},235 },236 {237 desc: "value is int",238 i: 2,239 want: Int{Value: 2, Valid: true},240 },241 {242 desc: "value is json.Number",243 i: json.Number("23"),244 want: Int{Value: 23, Valid: true},245 },246 }247 for _, test := range tests {248 test := test // capture249 t.Run(test.desc, func(t *testing.T) {250 t.Parallel()251 got := Int{}252 err := got.Unmarshal(test.i)253 if test.err {254 assert.Error(t, err)255 return256 }257 assert.NoError(t, err)258 assert.EqualValues(t, test.want, got)259 })260 }261}262func TestLong(t *testing.T) {263 t.Parallel()264 tests := []struct {265 desc string266 i interface{}267 err bool268 want Long269 }{270 {271 desc: "value is non-nil and non-int",272 i: "hello",273 err: true,274 },275 {276 desc: "value is json.Number that is a float",277 i: json.Number("3.2"),278 err: true,279 },280 {281 desc: "value is nil",282 i: nil,283 want: Long{},284 },285 {286 desc: "value is int",287 i: 2,288 want: Long{Value: 2, Valid: true},289 },290 {291 desc: "value is json.Number",292 i: json.Number("23"),293 want: Long{Value: 23, Valid: true},294 },295 }296 for _, test := range tests {297 test := test // capture298 t.Run(test.desc, func(t *testing.T) {299 t.Parallel()300 got := Long{}301 err := got.Unmarshal(test.i)302 if test.err {303 assert.Error(t, err)304 return305 }306 assert.NoError(t, err)307 assert.EqualValues(t, test.want, got)308 })309 }310}311func TestReal(t *testing.T) {312 t.Parallel()313 tests := []struct {314 desc string315 i interface{}316 err bool317 want Real318 }{319 {320 desc: "value is non-nil and non-float64",321 i: "hello",322 err: true,323 },324 {325 desc: "value is json.Number that is an int, which will convert to a float64",326 i: json.Number("3"),327 want: Real{Value: 3.0, Valid: true},328 },329 {330 desc: "value is nil",331 i: nil,332 want: Real{},333 },334 {335 desc: "value is float64",336 i: 2.3,337 want: Real{Value: 2.3, Valid: true},338 },339 {340 desc: "value is json.Number",341 i: json.Number("23.2"),342 want: Real{Value: 23.2, Valid: true},343 },344 }345 for _, test := range tests {346 test := test // capture347 t.Run(test.desc, func(t *testing.T) {348 t.Parallel()349 got := Real{}350 err := got.Unmarshal(test.i)351 if test.err {352 assert.Error(t, err)353 return354 }355 assert.NoError(t, err)356 assert.EqualValues(t, test.want, got)357 })358 }359}360func TestString(t *testing.T) {361 t.Parallel()362 tests := []struct {363 desc string364 i interface{}365 err bool366 want String367 }{368 {369 desc: "value is non-nil and non-string",370 i: 23,371 err: true,372 },373 {374 desc: "value is nil",375 i: nil,376 want: String{},377 },378 {379 desc: "value is string",380 i: "hello world",381 want: String{Value: "hello world", Valid: true},382 },383 }384 for _, test := range tests {385 test := test // capture386 t.Run(test.desc, func(t *testing.T) {387 t.Parallel()388 got := String{}389 err := got.Unmarshal(test.i)390 if test.err {391 assert.Error(t, err)392 return393 }394 assert.NoError(t, err)395 assert.EqualValues(t, test.want, got)396 })397 }398}399func TestTimespan(t *testing.T) {400 t.Parallel()401 tests := []struct {402 desc string403 i interface{}404 err bool405 want Timespan406 }{407 {408 desc: "value is non-nil and non-string",409 i: 23,410 err: true,411 },412 {413 desc: "value is nil",414 i: nil,415 want: Timespan{},416 },417 {418 desc: "value is string, but doesn't represent a time",419 i: "hello world",420 err: true,421 },422 {423 desc: "value is string, but doesn't split right",424 i: "00:00",425 err: true,426 },427 {i: "00:00:00", want: Timespan{Valid: true}},428 {i: "00:00:03", want: Timespan{Value: 3 * time.Second, Valid: true}},429 {i: "00:04:03", want: Timespan{Value: 4*time.Minute + 3*time.Second, Valid: true}},430 {i: "02:04:03", want: Timespan{Value: 2*time.Hour + 4*time.Minute + 3*time.Second, Valid: true}},431 {i: "00:00:00.099", want: Timespan{Value: 99 * time.Millisecond, Valid: true}},432 {i: "02:04:03.0123", want: Timespan{Value: 2*time.Hour + 4*time.Minute + 3*time.Second + 12300*time.Microsecond, Valid: true}},433 {i: "01.00:00:00", want: Timespan{Value: 24 * time.Hour, Valid: true}},434 {i: "02.04:05:07", want: Timespan{Value: 2*24*time.Hour + 4*time.Hour + 5*time.Minute + 7*time.Second, Valid: true}},435 {i: "-01.00:00:00", want: Timespan{Value: -24 * time.Hour, Valid: true}},436 {i: "-02.04:05:07", want: Timespan{Value: time.Duration(-1) * (2*24*time.Hour + 4*time.Hour + 5*time.Minute + 7*time.Second), Valid: true}},437 {i: "00.00:00.00:00.000", want: Timespan{Valid: true}},438 {i: "02.04:05:07.789", want: Timespan{Value: 2*24*time.Hour + 4*time.Hour + 5*time.Minute + 7*time.Second + 789*time.Millisecond, Valid: true}},439 {i: "03.00:00:00.111", want: Timespan{Value: 3*24*time.Hour + 111*time.Millisecond, Valid: true}},440 {i: "03.00:00:00.111", want: Timespan{Value: 3*24*time.Hour + 111*time.Millisecond, Valid: true}},441 {i: "364.23:59:59.9999999", want: Timespan{Value: 364*day + 23*time.Hour + 59*time.Minute + 59*time.Second + 9999999*100*time.Nanosecond, Valid: true}},442 }443 for _, test := range tests {444 test := test // capture445 t.Run(test.desc, func(t *testing.T) {446 t.Parallel()447 got := Timespan{}448 err := got.Unmarshal(test.i)449 if test.err {450 assert.Error(t, err)451 return452 }453 assert.NoError(t, err)454 assert.EqualValues(t, test.want, got)455 strGot := got.Marshal()456 if test.i == nil || !got.Valid {457 assert.Equal(t, "00:00:00", strGot)458 return459 }460 assert.EqualValues(t, removeLeadingZeros(test.i.(string)), removeLeadingZeros(strGot))461 })462 }463}464func removeLeadingZeros(s string) string {465 if len(s) == 0 {466 return s467 }468 if string(s[0]) == "-" {469 return string(s[0]) + strings.Trim(s[1:], "0:.")470 }471 return strings.Trim(s, "0:.")472}473func TestDecimal(t *testing.T) {474 t.Parallel()475 tests := []struct {476 desc string477 i interface{}478 err bool479 want Decimal480 }{481 {482 desc: "cannot be a non string",483 i: 3.0,484 err: true,485 },486 {desc: "Conversion of '1',", i: "1", want: Decimal{Value: "1", Valid: true}},487 {desc: "Conversion of '.1',", i: ".1", want: Decimal{Value: ".1", Valid: true}},488 {desc: "Conversion of '1.',", i: "1.", want: Decimal{Value: "1.", Valid: true}},489 {desc: "Conversion of '0.1',", i: "0.1", want: Decimal{Value: "0.1", Valid: true}},490 {desc: "Conversion of '3.07',", i: "3.07", want: Decimal{Value: "3.07", Valid: true}},491 }492 for _, test := range tests {493 test := test // capture494 t.Run(test.desc, func(t *testing.T) {495 t.Parallel()496 got := Decimal{}497 err := got.Unmarshal(test.i)498 if test.err {499 assert.Error(t, err)500 return501 }502 assert.NoError(t, err)503 assert.EqualValues(t, test.want, got)504 })505 }506}507func timeMustParse(layout string, p string) time.Time {508 t, err := time.Parse(layout, p)509 if err != nil {510 panic(err)511 }512 return t513}...

Full Screen

Full Screen

resources_test.go

Source:resources_test.go Github

copy

Full Screen

1package resources2import (3 "context"4 "testing"5 "github.com/kylelemons/godebug/pretty"6 "github.com/Azure/azure-kusto-go/kusto/data/table"7 "github.com/Azure/azure-kusto-go/kusto/data/types"8 "github.com/Azure/azure-kusto-go/kusto/data/value"9)10func TestParse(t *testing.T) {11 t.Parallel()12 tests := []struct {13 desc string14 url string15 err bool16 wantAccount string17 wantObjectType string18 wantObjectName string19 }{20 {21 desc: "account is missing, but has leading dot",22 url: "https://.queue.core.windows.net/objectname",23 err: true,24 },25 {26 desc: "account is missing",27 url: "https://queue.core.windows.net/objectname",28 err: true,29 },30 {31 desc: "invalid object type",32 url: "https://account.invalid.core.windows.net/objectname",33 err: true,34 },35 {36 desc: "invalid domain",37 url: "https://account.blob.core.invalid.net/objectname",38 err: true,39 },40 {41 desc: "no object name provided",42 url: "https://account.invalid.core.windows.net/",43 err: true,44 },45 {46 desc: "bad scheme",47 url: "http://account.table.core.windows.net/objectname",48 err: true,49 },50 {51 desc: "success",52 url: "https://account.table.core.windows.net/objectname",53 wantAccount: "account",54 wantObjectType: "table",55 wantObjectName: "objectname",56 },57 }58 for _, test := range tests {59 got, err := parse(test.url)60 switch {61 case err == nil && test.err:62 t.Errorf("TestParse(%s): got err == nil, want err != nil", test.desc)63 continue64 case err != nil && !test.err:65 t.Errorf("TestParse(%s): got err == %s, want err != nil", test.desc, err)66 continue67 case err != nil:68 continue69 }70 if got.Account() != test.wantAccount {71 t.Errorf("TestParse(%s): URI.Account(): got %s, want %s", test.desc, got.Account(), test.wantAccount)72 }73 if got.ObjectType() != test.wantObjectType {74 t.Errorf("TestParse(%s): URI.ObjectType(): got %s, want %s", test.desc, got.ObjectType(), test.wantObjectType)75 }76 if got.ObjectName() != test.wantObjectName {77 t.Errorf("TestParse(%s): URI.ObjectName(): got %s, want %s", test.desc, got.ObjectName(), test.wantObjectName)78 }79 if got.String() != test.url {80 t.Errorf("TestParse(%s): String(): got %s, want %s", test.desc, got.String(), test.url)81 }82 }83}84func FakeAuthContext(rows []value.Values, setErr bool) *FakeMgmt {85 cols := table.Columns{86 {87 Name: "AuthorizationContext",88 Type: types.String,89 },90 }91 fm := NewFakeMgmt(cols, rows, setErr)92 return fm93}94func TestAuthContext(t *testing.T) {95 t.Parallel()96 tests := []struct {97 desc string98 fakeMgmt *FakeMgmt99 err bool100 want string101 }{102 {103 desc: "Mgmt returns an error",104 fakeMgmt: FakeAuthContext(105 []value.Values{106 {107 value.String{108 Valid: true,109 Value: "authtoken",110 },111 },112 },113 false,114 ).SetMgmtErr(),115 err: true,116 },117 {118 desc: "Returned two rows, only allowed one",119 fakeMgmt: FakeAuthContext(120 []value.Values{121 {122 value.String{123 Valid: true,124 Value: "authtoken",125 },126 },127 {128 value.String{129 Valid: true,130 Value: "authtoken2",131 },132 },133 },134 false,135 ),136 err: true,137 },138 {139 desc: "Success",140 fakeMgmt: FakeAuthContext(141 []value.Values{142 {143 value.String{144 Valid: true,145 Value: "authtoken",146 },147 },148 },149 false,150 ),151 want: "authtoken",152 },153 }154 for _, test := range tests {155 manager := &Manager{client: test.fakeMgmt}156 got, err := manager.AuthContext(context.Background())157 switch {158 case err == nil && test.err:159 t.Errorf("TestAuthContext(%s): got err == nil, want err != nil", test.desc)160 continue161 case err != nil && !test.err:162 t.Errorf("TestAuthContext(%s): got err == %s, want err != nil", test.desc, err)163 continue164 case err != nil:165 continue166 }167 if got != test.want {168 t.Errorf("TestAuthContext(%s): got %s, want %s", test.desc, got, test.want)169 }170 }171}172func mustParse(s string) *URI {173 u, err := parse(s)174 if err != nil {175 panic(err)176 }177 return u178}179func TestResources(t *testing.T) {180 t.Parallel()181 tests := []struct {182 desc string183 fakeMgmt *FakeMgmt184 err bool185 want Ingestion186 }{187 {188 desc: "Mgmt returns an error",189 fakeMgmt: FakeResources(190 []value.Values{},191 false,192 ).SetMgmtErr(),193 err: true,194 },195 {196 desc: "Bad StorageRoot value",197 fakeMgmt: FakeResources(198 []value.Values{199 {200 value.String{201 Valid: true,202 Value: "TempStorage",203 },204 value.String{205 Valid: true,206 Value: "https://.blob.core.windows.net/storageroot",207 },208 },209 },210 false,211 ),212 err: true,213 },214 {215 desc: "Success",216 fakeMgmt: SuccessfulFakeResources(),217 want: Ingestion{218 Queues: []*URI{mustParse("https://account.blob.core.windows.net/storageroot1")},219 Containers: []*URI{mustParse("https://account.blob.core.windows.net/storageroot0")},220 },221 },222 }223 for _, test := range tests {224 manager := &Manager{client: test.fakeMgmt}225 err := manager.fetch(context.Background())226 switch {227 case err == nil && test.err:228 t.Errorf("TestResources(%s): got err == nil, want err != nil", test.desc)229 continue230 case err != nil && !test.err:231 t.Errorf("TestResources(%s): got err == %s, want err != nil", test.desc, err)232 continue233 case err != nil:234 continue235 }236 got, err := manager.Resources()237 if err != nil {238 panic(err)239 }240 if diff := pretty.Compare(test.want, got); diff != "" {241 t.Errorf("TestResources(%s): -want/+got:\n%s", test.desc, diff)242 }243 }244}...

Full Screen

Full Screen

Desc

Using AI Code Generation

copy

Full Screen

1func main() {2}3func main() {4}5func main() {6}7func main() {8}9func main() {10}11func main() {12}13func main() {14}15func main() {16}17func main() {18}19func main() {20}21func main() {22}23func main() {24}25func main() {26}27func main() {28}29func main() {30}31func main()

Full Screen

Full Screen

Desc

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 a.Desc()4}5import (6func main() {7 a.Desc()8}9import (10func main() {11 a.Desc()12}13import (14func main() {15 a.Desc()16}17import (18func main() {19 a.Desc()20}21import (22func main() {23 a.Desc()24}25import (26func main() {27 a.Desc()28}29import (30func main() {31 a.Desc()32}33import (34func main() {35 a.Desc()36}37import (38func main() {39 a.Desc()40}41import (42func main() {43 a.Desc()44}45import (46func main() {47 a.Desc()48}49import (

Full Screen

Full Screen

Desc

Using AI Code Generation

copy

Full Screen

1import "fmt"2type got struct {3}4func (g *got) Desc() {5 fmt.Println(g.name, "is", g.age, "years old")6}7func main() {8 g := &got{"John Snow", 18}9 g.Desc()10}

Full Screen

Full Screen

Desc

Using AI Code Generation

copy

Full Screen

1import "fmt"2type got struct {3}4func (g got) Desc() string {5}6func main() {7g := got{name: "got"}8fmt.Println(g.Desc())9}10import "fmt"11type got struct {12}13func (g *got) Desc() string {14}15func main() {16g := got{name: "got"}17fmt.Println(g.Desc())18}19import "fmt"20type got struct {21}22func (g got) Desc() string {23}24func main() {25g := got{name: "got"}26fmt.Println(g.Desc())27}

Full Screen

Full Screen

Desc

Using AI Code Generation

copy

Full Screen

1got.Desc()2got.Desc()3got.Desc()4got.Desc()5got.Desc()6got.Desc()7got.Desc()8got.Desc()9got.Desc()10got.Desc()11got.Desc()12got.Desc()13got.Desc()14got.Desc()15got.Desc()16got.Desc()17got.Desc()

Full Screen

Full Screen

Desc

Using AI Code Generation

copy

Full Screen

1import ( "fmt"2func main() {3 fmt.Println(math.Desc(3, 5))4}5import ( "fmt"6func main() {7 fmt.Println(math.Sum(3, 5))8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful