Best Got code snippet using got.ToJSON
sync_test.go
Source:sync_test.go
1// Copyright (c) 2020, Amazon.com, Inc.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14// Package internal ...15package internal16import (17 "encoding/json"18 "log"19 "reflect"20 "testing"21 "github.com/awslabs/ssosync/internal/aws"22 admin "google.golang.org/api/admin/directory/v1"23)24// toJSON return a json pretty of the stc25func toJSON(stc interface{}) []byte {26 JSON, err := json.MarshalIndent(stc, "", " ")27 if err != nil {28 log.Fatalf(err.Error())29 }30 return JSON31}32func Test_getGroupOperations(t *testing.T) {33 type args struct {34 awsGroups []*aws.Group35 googleGroups []*admin.Group36 }37 tests := []struct {38 name string39 args args40 wantAdd []*aws.Group41 wantDelete []*aws.Group42 wantEquals []*aws.Group43 }{44 {45 name: "equal groups google and aws",46 args: args{47 awsGroups: []*aws.Group{48 aws.NewGroup("Group-1"),49 aws.NewGroup("Group-2"),50 },51 googleGroups: []*admin.Group{52 {Name: "Group-1"},53 {Name: "Group-2"},54 },55 },56 wantAdd: nil,57 wantDelete: nil,58 wantEquals: []*aws.Group{59 aws.NewGroup("Group-1"),60 aws.NewGroup("Group-2"),61 },62 },63 {64 name: "add two new aws groups",65 args: args{66 awsGroups: nil,67 googleGroups: []*admin.Group{68 {Name: "Group-1"},69 {Name: "Group-2"},70 },71 },72 wantAdd: []*aws.Group{73 aws.NewGroup("Group-1"),74 aws.NewGroup("Group-2"),75 },76 wantDelete: nil,77 wantEquals: nil,78 },79 {80 name: "delete two aws groups",81 args: args{82 awsGroups: []*aws.Group{83 aws.NewGroup("Group-1"),84 aws.NewGroup("Group-2"),85 }, googleGroups: nil,86 },87 wantAdd: nil,88 wantDelete: []*aws.Group{89 aws.NewGroup("Group-1"),90 aws.NewGroup("Group-2"),91 },92 wantEquals: nil,93 },94 {95 name: "add one, delete one and one equal",96 args: args{97 awsGroups: []*aws.Group{98 aws.NewGroup("Group-2"),99 aws.NewGroup("Group-3"),100 },101 googleGroups: []*admin.Group{102 {Name: "Group-1"},103 {Name: "Group-2"},104 },105 },106 wantAdd: []*aws.Group{107 aws.NewGroup("Group-1"),108 },109 wantDelete: []*aws.Group{110 aws.NewGroup("Group-3"),111 },112 wantEquals: []*aws.Group{113 aws.NewGroup("Group-2"),114 },115 },116 }117 for _, tt := range tests {118 t.Run(tt.name, func(t *testing.T) {119 gotAdd, gotDelete, gotEquals := getGroupOperations(tt.args.awsGroups, tt.args.googleGroups)120 if !reflect.DeepEqual(gotAdd, tt.wantAdd) {121 t.Errorf("getGroupOperations() gotAdd = %s, want %s", toJSON(gotAdd), toJSON(tt.wantAdd))122 }123 if !reflect.DeepEqual(gotDelete, tt.wantDelete) {124 t.Errorf("getGroupOperations() gotDelete = %s, want %s", toJSON(gotDelete), toJSON(tt.wantDelete))125 }126 if !reflect.DeepEqual(gotEquals, tt.wantEquals) {127 t.Errorf("getGroupOperations() gotEquals = %s, want %s", toJSON(gotEquals), toJSON(tt.wantEquals))128 }129 })130 }131}132func Test_getUserOperations(t *testing.T) {133 type args struct {134 awsUsers []*aws.User135 googleUsers []*admin.User136 }137 tests := []struct {138 name string139 args args140 wantAdd []*aws.User141 wantDelete []*aws.User142 wantUpdate []*aws.User143 wantEquals []*aws.User144 }{145 {146 name: "equal user google and aws",147 args: args{148 awsUsers: []*aws.User{149 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),150 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),151 },152 googleUsers: []*admin.User{153 {Name: &admin.UserName{154 GivenName: "name-1",155 FamilyName: "lastname-1",156 },157 Suspended: false,158 PrimaryEmail: "user-1@email.com",159 },160 {Name: &admin.UserName{161 GivenName: "name-2",162 FamilyName: "lastname-2",163 },164 Suspended: false,165 PrimaryEmail: "user-2@email.com",166 },167 },168 },169 wantAdd: nil,170 wantDelete: nil,171 wantUpdate: nil,172 wantEquals: []*aws.User{173 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),174 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),175 },176 },177 {178 name: "add two new aws users",179 args: args{180 awsUsers: nil,181 googleUsers: []*admin.User{182 {Name: &admin.UserName{183 GivenName: "name-1",184 FamilyName: "lastname-1",185 },186 Suspended: false,187 PrimaryEmail: "user-1@email.com",188 },189 {Name: &admin.UserName{190 GivenName: "name-2",191 FamilyName: "lastname-2",192 },193 Suspended: false,194 PrimaryEmail: "user-2@email.com",195 },196 },197 },198 wantAdd: []*aws.User{199 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),200 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),201 },202 wantDelete: nil,203 wantUpdate: nil,204 wantEquals: nil,205 },206 {207 name: "delete two aws users",208 args: args{209 awsUsers: []*aws.User{210 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),211 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),212 },213 googleUsers: nil,214 },215 wantAdd: nil,216 wantDelete: []*aws.User{217 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),218 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),219 },220 wantUpdate: nil,221 wantEquals: nil,222 },223 {224 name: "add on, delete one, update one and one equal",225 args: args{226 awsUsers: []*aws.User{227 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),228 aws.NewUser("name-3", "lastname-3", "user-3@email.com", true),229 aws.NewUser("name-4", "lastname-4", "user-4@email.com", true),230 },231 googleUsers: []*admin.User{232 {233 Name: &admin.UserName{234 GivenName: "name-1",235 FamilyName: "lastname-1",236 },237 Suspended: false,238 PrimaryEmail: "user-1@email.com",239 },240 {241 Name: &admin.UserName{242 GivenName: "name-2",243 FamilyName: "lastname-2",244 },245 Suspended: false,246 PrimaryEmail: "user-2@email.com",247 },248 {249 Name: &admin.UserName{250 GivenName: "name-4",251 FamilyName: "lastname-4",252 },253 Suspended: true,254 PrimaryEmail: "user-4@email.com",255 },256 },257 },258 wantAdd: []*aws.User{259 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),260 },261 wantDelete: []*aws.User{262 aws.NewUser("name-3", "lastname-3", "user-3@email.com", true),263 },264 wantUpdate: []*aws.User{265 aws.NewUser("name-4", "lastname-4", "user-4@email.com", false),266 },267 wantEquals: []*aws.User{268 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),269 },270 },271 }272 for _, tt := range tests {273 t.Run(tt.name, func(t *testing.T) {274 gotAdd, gotDelete, gotUpdate, gotEquals := getUserOperations(tt.args.awsUsers, tt.args.googleUsers)275 if !reflect.DeepEqual(gotAdd, tt.wantAdd) {276 t.Errorf("getUserOperations() gotAdd = %s, want %s", toJSON(gotAdd), toJSON(tt.wantAdd))277 }278 if !reflect.DeepEqual(gotDelete, tt.wantDelete) {279 t.Errorf("getUserOperations() gotDelete = %s, want %s", toJSON(gotDelete), toJSON(tt.wantDelete))280 }281 if !reflect.DeepEqual(gotUpdate, tt.wantUpdate) {282 t.Errorf("getUserOperations() gotUpdate = %s, want %s", toJSON(gotUpdate), toJSON(tt.wantUpdate))283 }284 if !reflect.DeepEqual(gotEquals, tt.wantEquals) {285 t.Errorf("getUserOperations() gotEquals = %s, want %s", toJSON(gotEquals), toJSON(tt.wantEquals))286 }287 })288 }289}290func Test_getGroupUsersOperations(t *testing.T) {291 type args struct {292 gGroupsUsers map[string][]*admin.User293 awsGroupsUsers map[string][]*aws.User294 }295 tests := []struct {296 name string297 args args298 wantDelete map[string][]*aws.User299 wantEquals map[string][]*aws.User300 }{301 {302 name: "one add, one delete, one equal",303 args: args{304 gGroupsUsers: map[string][]*admin.User{305 "group-1": {306 {307 Name: &admin.UserName{308 GivenName: "name-1",309 FamilyName: "lastname-1",310 },311 Suspended: false,312 PrimaryEmail: "user-1@email.com",313 },314 },315 },316 awsGroupsUsers: map[string][]*aws.User{317 "group-1": {318 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),319 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),320 },321 },322 },323 wantDelete: map[string][]*aws.User{324 "group-1": {325 aws.NewUser("name-2", "lastname-2", "user-2@email.com", true),326 },327 },328 wantEquals: map[string][]*aws.User{329 "group-1": {330 aws.NewUser("name-1", "lastname-1", "user-1@email.com", true),331 },332 },333 },334 }335 for _, tt := range tests {336 t.Run(tt.name, func(t *testing.T) {337 gotDelete, gotEquals := getGroupUsersOperations(tt.args.gGroupsUsers, tt.args.awsGroupsUsers)338 if !reflect.DeepEqual(gotDelete, tt.wantDelete) {339 t.Errorf("getGroupUsersOperations() gotDelete = %s, want %s", toJSON(gotDelete), toJSON(tt.wantDelete))340 }341 if !reflect.DeepEqual(gotEquals, tt.wantEquals) {342 t.Errorf("getGroupUsersOperations() gotEquals = %s, want %s", toJSON(gotEquals), toJSON(tt.wantEquals))343 }344 })345 }346}...
json_row_test.go
Source:json_row_test.go
1package hake_test2import (3 "bytes"4 "testing"5 "cloud.google.com/go/spanner"6 . "github.com/gcpug/hake"7 "github.com/xeipuuv/gojsonschema"8)9func TestJSONRow_MarshalJSON(t *testing.T) {10 cases := []struct {11 name string12 row *spanner.Row13 want string14 }{15 {"null", row(t, nil), toJSON(t, nil)},16 {"empty", row(t, R{}), toJSON(t, R{})},17 {"single", row(t, R{"col1", 100}), toJSON(t, R{"col1", "100"})},18 {"multiple", row(t, R{"col1", 100, "col2", 10.5}), toJSON(t, R{"col1", "100", "col2", 10.5})},19 }20 for _, tt := range cases {21 tt := tt22 t.Run(tt.name, func(t *testing.T) {23 got := toJSON(t, (*JSONRow)(tt.row))24 if got != tt.want {25 t.Errorf("want %s but got %s", tt.want, got)26 }27 })28 }29}30func TestRows(t *testing.T) {31 cases := []struct {32 name string33 rows []*spanner.Row34 want string35 }{36 {"null", rows(t, nil), toJSON(t, nil)},37 {"empties", rows(t, []R{{}, {}}), toJSON(t, []R{{}, {}})},38 {"singles", rows(t, []R{{"col1", 100}, {"col1", 200}}), toJSON(t, []R{{"col1", "100"}, {"col1", "200"}})},39 }40 for _, tt := range cases {41 tt := tt42 t.Run(tt.name, func(t *testing.T) {43 got := toJSON(t, JSONRows(tt.rows))44 if got != tt.want {45 t.Errorf("want %s but got %s", tt.want, got)46 }47 })48 }49}50type noopFormatCheker struct{}51func (noopFormatCheker) IsFormat(_ interface{}) bool {52 return true53}54func init() {55 gojsonschema.FormatCheckers.Add("datetime", noopFormatCheker{})56 gojsonschema.FormatCheckers.Add("textarea", noopFormatCheker{})57}58func TestJSONRow_JSONSchema(t *testing.T) {59 type T struct {60 N int61 S string62 }63 type NT struct {64 T T65 }66 cases := []struct {67 name string68 row *spanner.Row69 isErr bool70 }{71 {"int", row(t, R{"col1", 100}), false},72 {"int string", row(t, R{"col1", 100, "col2", "string"}), false},73 {"nested struct", row(t, R{"col1", 100, "col2", T{N: 100, S: ""}}), false},74 {"timestamp", row(t, R{"col1", 100, "col2", timestamp(t, "2002-10-02T10:00:00Z")}), false},75 {"bytes", row(t, R{"col1", []byte("test")}), false},76 }77 for _, tt := range cases {78 tt := tt79 t.Run(tt.name, func(t *testing.T) {80 var got bytes.Buffer81 err := (*JSONRow)(tt.row).JSONSchema(&got)82 switch {83 case tt.isErr && err == nil:84 t.Errorf("expected error does not occur")85 case !tt.isErr && err != nil:86 t.Errorf("unexpected error %v", err)87 }88 l := gojsonschema.NewStringLoader(got.String())89 s, err := gojsonschema.NewSchema(l)90 if err != nil {91 t.Fatalf("unexpected error %v", err)92 }93 rowJSON := toJSON(t, (*JSONRow)(tt.row))94 r, err := s.Validate(gojsonschema.NewStringLoader(rowJSON))95 if err != nil {96 t.Fatalf("unexpected error %v", err)97 }98 if !r.Valid() {99 t.Errorf("invalid JSON Schema: %s", got.String())100 }101 })102 }103}104func TestJSONRows_JSONSchema(t *testing.T) {105 type T struct {106 N int107 S string108 }109 type NT struct {110 T T111 }112 cases := []struct {113 name string114 rows []*spanner.Row115 isErr bool116 }{117 {"empty", rows(t, []R{}), false},118 {"int", rows(t, []R{{"col1", 100}}), false},119 {"int int", rows(t, []R{{"col1", 100}, {"col1", 200}}), false},120 {"int string", rows(t, []R{{"col1", 100, "col2", "string"}}), false},121 {"nested struct", rows(t, []R{{"col1", 100, "col2", T{N: 100, S: ""}}}), false},122 {"timestamp", rows(t, []R{{"col1", 100, "col2", timestamp(t, "2002-10-02T10:00:00Z")}}), false},123 {"bytes", rows(t, []R{{"col1", []byte("test")}}), false},124 }125 for _, tt := range cases {126 tt := tt127 t.Run(tt.name, func(t *testing.T) {128 var got bytes.Buffer129 err := JSONRows(tt.rows).JSONSchema(&got)130 switch {131 case tt.isErr && err == nil:132 t.Errorf("expected error does not occur")133 case !tt.isErr && err != nil:134 t.Errorf("unexpected error %v", err)135 }136 l := gojsonschema.NewStringLoader(got.String())137 s, err := gojsonschema.NewSchema(l)138 if err != nil {139 t.Fatalf("unexpected error %v", err)140 }141 rowJSON := toJSON(t, JSONRows(tt.rows))142 r, err := s.Validate(gojsonschema.NewStringLoader(rowJSON))143 if err != nil {144 t.Fatalf("unexpected error %v", err)145 }146 if !r.Valid() {147 t.Errorf("invalid JSON Schema: %s", got.String())148 }149 })150 }151}...
json_column_test.go
Source:json_column_test.go
1package hake_test2import (3 "testing"4 "cloud.google.com/go/spanner"5 . "github.com/gcpug/hake"6 structpb "github.com/golang/protobuf/ptypes/struct"7 gspanner "google.golang.org/genproto/googleapis/spanner/v1"8)9func TestColumn_MarshalJSON(t *testing.T) {10 type T struct {11 N interface{}12 S string13 }14 type NT struct {15 T T16 }17 floatWithString := func(s string) *spanner.GenericColumnValue {18 return &spanner.GenericColumnValue{Type: &gspanner.Type{Code: gspanner.TypeCode_FLOAT64}, Value: &structpb.Value{Kind: &structpb.Value_StringValue{StringValue: s}}}19 }20 cases := []struct {21 name string22 col *spanner.GenericColumnValue23 want string24 }{25 {"null", column(t, nil), toJSON(t, nil)},26 {"int", column(t, 100), toJSON(t, "100")},27 {"big int", column(t, 72057596404714278), toJSON(t, "72057596404714278")},28 {"float", column(t, 10.5), toJSON(t, 10.5)},29 {"float with NaN", floatWithString("NaN"), toJSON(t, nil)},30 {"float with Infinity", floatWithString("Infinity"), toJSON(t, nil)},31 {"float with -Infinity", floatWithString("-Infinity"), toJSON(t, nil)},32 {"string", column(t, "test"), toJSON(t, "test")},33 {"bool", column(t, true), toJSON(t, true)},34 {"struct", column(t, T{N: 100, S: "test"}), toJSON(t, T{N: "100", S: "test"})},35 {"nested struct", column(t, NT{T{N: 100, S: "test"}}), toJSON(t, NT{T{N: "100", S: "test"}})},36 {"timestamp", column(t, timestamp(t, "2002-10-02T10:00:00Z")), toJSON(t, "2002-10-02T10:00:00Z")},37 {"bytes", column(t, []byte("test")), toJSON(t, []byte("test"))},38 }39 for _, tt := range cases {40 tt := tt41 t.Run(tt.name, func(t *testing.T) {42 got := toJSON(t, (*JSONColumn)(tt.col))43 if got != tt.want {44 t.Errorf("want %s but got %s", tt.want, got)45 }46 })47 }48}...
ToJSON
Using AI Code Generation
1import "fmt"2import "encoding/json"3type Person struct {4}5func (p *Person) ToJSON() ([]byte, error) {6return json.Marshal(p)7}8func main() {9p := &Person{Name: "Bob", Age: 20}10b, err := p.ToJSON()11if err != nil {12fmt.Println(err)13}14fmt.Println(string(b))15}16{"Name":"Bob","Age":20}
ToJSON
Using AI Code Generation
1import (2type Person struct {3}4func main() {5 p1 := Person{"James", 20}6 bs, _ := json.Marshal(p1)7 fmt.Println(string(bs))8}9{"Name":"James","Age":20}10import (11type Person struct {12}13func main() {14 bs := []byte(`{"Name":"James","Age":20}`)15 json.Unmarshal(bs, &p1)16 fmt.Println(p1.Name)17}18import (19type Person struct {20}21func main() {22 p1 := Person{"James", 20}23 bs, _ := json.Marshal(p1)24 fmt.Println(string(bs))25 json.Unmarshal(bs, &p2)26 fmt.Println(p2)27}28{"Name":"James","Age":20}29{James 20}30import (31type Person struct {32}33func main() {34 p1 := Person{"James", "Bond", 20}35 bs, _ := json.Marshal(p1)36 fmt.Println(string(bs))37 json.Unmarshal(bs, &p2)38 fmt.Println(p2)39}40{"First":"James","wisdom score":20}41{James 0 20}42import (43type Person struct {
ToJSON
Using AI Code Generation
1func main() {2 got.ToJSON()3}4func main() {5 got.ToJSON()6}7func main() {8 got.ToJSON()9}10func main() {11 got.ToJSON()12}13func main() {14 got.ToJSON()15}16func main() {17 got.ToJSON()18}19func main() {20 got.ToJSON()21}22func main() {
ToJSON
Using AI Code Generation
1import (2func main() {3 j, err := got.ToJSON()4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(string(j))8}9{"Name":"Jon","Age":18,"Born":"Winterfell","Dead":false}10import (11func main() {12 err := got.FromJSON([]byte(`{"Name":"Jon","Age":18,"Born":"Winterfell","Dead":false}`))13 if err != nil {14 fmt.Println(err)15 }16 fmt.Println(got)17}18{Jon 18 Winterfell false}
ToJSON
Using AI Code Generation
1import (2func main() {3 got.ToJSON()4 fmt.Println("Hello")5}6{"Name":"Gopher","Age":2}
ToJSON
Using AI Code Generation
1import (2func main() {3 g := got.ToJSON("Hello World")4 fmt.Println(g)5}6import (7func main() {8 m := map[string]string{9 }10 g := got.ToJSON(m)11 fmt.Println(g)12}13{"Age":"23","Name":"John"}14import (15type Person struct {16}17func main() {18 p := Person{19 }20 g := got.ToJSON(p)21 fmt.Println(g)22}23{"Age":23,"Name":"John"}24import (25func main() {26 s := []string{"John", "Paul", "George", "Ringo"}27 g := got.ToJSON(s)28 fmt.Println(g)29}30import (31func main() {32 g := got.ToJSON(&i)33 fmt.Println(g)34}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!