func TestUnmarshalNext(t *testing.T) { // We only need to check against a few, not all of them. tests := unmarshalingTests[:5] // Create a buffer with many concatenated JSON objects. var b bytes.Buffer for _, tt := range tests { b.WriteString(tt.json) } dec := json.NewDecoder(&b) for _, tt := range tests { // Make a new instance of the type of our expected object. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message) err := tt.unmarshaler.UnmarshalNext(dec, p) if err != nil { t.Errorf("%s: %v", tt.desc, err) continue } // For easier diffs, compare text strings of the protos. exp := proto.MarshalTextString(tt.pb) act := proto.MarshalTextString(p) if string(exp) != string(act) { t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp) } } p := &pb.Simple{} err := new(Unmarshaler).UnmarshalNext(dec, p) if err != io.EOF { t.Errorf("eof: got %v, expected io.EOF", err) } }
func TestToProtoMultiRoot(t *testing.T) { dir, dir2, _ := populateGraph(t) defer os.RemoveAll(dir) defer os.RemoveAll(dir2) query := ` { me(anyof("name", "Michonne Rick Glenn")) { name } } ` gq, _, err := gql.Parse(query) require.NoError(t, err) ctx := context.Background() sg, err := ToSubGraph(ctx, gq) require.NoError(t, err) ch := make(chan error) go ProcessGraph(ctx, sg, nil, ch) err = <-ch require.NoError(t, err) var l Latency pb, err := sg.ToProtocolBuffer(&l) require.NoError(t, err) expectedPb := `attribute: "_root_" children: < attribute: "me" properties: < prop: "name" value: < str_val: "Michonne" > > > children: < attribute: "me" properties: < prop: "name" value: < str_val: "Rick Grimes" > > > children: < attribute: "me" properties: < prop: "name" value: < str_val: "Glenn Rhee" > > > ` require.EqualValues(t, expectedPb, proto.MarshalTextString(pb)) }
func TestUnmarshaling(t *testing.T) { for _, tt := range unmarshalingTests { // Make a new instance of the type of our expected object. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message) err := tt.unmarshaler.Unmarshal(strings.NewReader(tt.json), p) if err != nil { t.Errorf("%s: %v", tt.desc, err) continue } // For easier diffs, compare text strings of the protos. exp := proto.MarshalTextString(tt.pb) act := proto.MarshalTextString(p) if string(exp) != string(act) { t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp) } } }
func TestUnmarshalling(t *testing.T) { for _, tt := range unmarshallingTests { // Make a new instance of the type of our expected object. p := proto.Clone(tt.pb) p.Reset() err := UnmarshalString(tt.json, p) if err != nil { t.Error(err) continue } // For easier diffs, compare text strings of the protos. exp := proto.MarshalTextString(tt.pb) act := proto.MarshalTextString(p) if string(exp) != string(act) { t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp) } } }
func TestToProtoFilterAnd(t *testing.T) { dir, dir2, _ := populateGraph(t) defer os.RemoveAll(dir) defer os.RemoveAll(dir2) // Alright. Now we have everything set up. Let's create the query. query := ` { me(_uid_:0x01) { name gender friend @filter(anyof("name", "Andrea") && anyof("name", "Glenn Rhee")) { name } } } ` gq, _, err := gql.Parse(query) require.NoError(t, err) ctx := context.Background() sg, err := ToSubGraph(ctx, gq) require.NoError(t, err) ch := make(chan error) go ProcessGraph(ctx, sg, nil, ch) err = <-ch require.NoError(t, err) var l Latency pb, err := sg.ToProtocolBuffer(&l) require.NoError(t, err) expectedPb := `attribute: "_root_" children: < attribute: "me" properties: < prop: "name" value: < str_val: "Michonne" > > properties: < prop: "gender" value: < bytes_val: "female" > > > ` require.EqualValues(t, expectedPb, proto.MarshalTextString(pb)) }
func TestSizeMessageProtoText(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedSizeMessage(popr, true) data := github_com_gogo_protobuf_proto.MarshalTextString(p) msg := &SizeMessage{} if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { panic(err) } if !p.Equal(msg) { t.Fatalf("%#v !Proto %#v", msg, p) } }
func TestBarProtoText(t *testing.T) { seed := time.Now().UnixNano() popr := math_rand.New(math_rand.NewSource(seed)) p := NewPopulatedBar(popr, true) data := github_com_gogo_protobuf_proto.MarshalTextString(p) msg := &Bar{} if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { t.Fatalf("seed = %d, err = %v", seed, err) } if !p.Equal(msg) { t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) } }
func TestRepeatedNonNullableExternalStructProtoText(t *testing.T) { seed := time.Now().UnixNano() popr := math_rand.New(math_rand.NewSource(seed)) p := NewPopulatedRepeatedNonNullableExternalStruct(popr, true) dAtA := github_com_gogo_protobuf_proto.MarshalTextString(p) msg := &RepeatedNonNullableExternalStruct{} if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil { t.Fatalf("seed = %d, err = %v", seed, err) } if !p.Equal(msg) { t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) } }
func TestFloatingPointProtoText(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedFloatingPoint(popr, true) data := github_com_gogo_protobuf_proto.MarshalTextString(p) msg := &FloatingPoint{} if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { panic(err) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) } if !p.Equal(msg) { t.Fatalf("%#v !Proto %#v", msg, p) } }
func TestOperation_ExpungeProtoText(t *testing16.T) { popr := math_rand16.New(math_rand16.NewSource(time16.Now().UnixNano())) p := NewPopulatedOperation_Expunge(popr, true) data := github_com_gogo_protobuf_proto9.MarshalTextString(p) msg := &Operation_Expunge{} if err := github_com_gogo_protobuf_proto9.UnmarshalText(data, msg); err != nil { panic(err) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) } if !p.Equal(msg) { t.Fatalf("%#v !Proto %#v", msg, p) } }
func TestClearAllExtensions(t *testing.T) { // unregistered extension desc := &proto.ExtensionDesc{ ExtendedType: (*pb.MyMessage)(nil), ExtensionType: (*bool)(nil), Field: 101010100, Name: "emptyextension", Tag: "varint,0,opt", } m := &pb.MyMessage{} if proto.HasExtension(m, desc) { t.Errorf("proto.HasExtension(%s): got true, want false", proto.MarshalTextString(m)) } if err := proto.SetExtension(m, desc, proto.Bool(true)); err != nil { t.Errorf("proto.SetExtension(m, desc, true): got error %q, want nil", err) } if !proto.HasExtension(m, desc) { t.Errorf("proto.HasExtension(%s): got false, want true", proto.MarshalTextString(m)) } proto.ClearAllExtensions(m) if proto.HasExtension(m, desc) { t.Errorf("proto.HasExtension(%s): got true, want false", proto.MarshalTextString(m)) } }
func TestRebalanceCheckResponseProtoText(t *testing.T) { seed := time.Now().UnixNano() popr := math_rand.New(math_rand.NewSource(seed)) p := NewPopulatedRebalanceCheckResponse(popr, true) data := github_com_gogo_protobuf_proto.MarshalTextString(p) msg := &RebalanceCheckResponse{} if err := github_com_gogo_protobuf_proto.UnmarshalText(data, msg); err != nil { t.Fatalf("seed = %d, err = %v", seed, err) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err) } if !p.Equal(msg) { t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) } }
func TestAllMapsOrderedProtoText(t *testing.T) { seed := time.Now().UnixNano() popr := math_rand.New(math_rand.NewSource(seed)) p := NewPopulatedAllMapsOrdered(popr, true) dAtA := github_com_gogo_protobuf_proto.MarshalTextString(p) msg := &AllMapsOrdered{} if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil { t.Fatalf("seed = %d, err = %v", seed, err) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err) } if !p.Equal(msg) { t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) } }
func TestRepeatedNilText(t *testing.T) { m := &pb.MessageList{ Message: []*pb.MessageList_Message{ nil, { Name: proto.String("Horse"), }, nil, }, } want := `Message <nil> Message { name: "Horse" } Message <nil> ` if s := proto.MarshalTextString(m); s != want { t.Errorf(" got: %s\nwant: %s", s, want) } }
// Test sorting / ordering by dob. func TestToProtoOrderOffsetCount(t *testing.T) { dir, dir2, _ := populateGraph(t) defer os.RemoveAll(dir) defer os.RemoveAll(dir2) query := ` { me(_uid_:0x01) { name gender friend(order: dob, first: 2, offset: 1) { name } } } ` gq, _, err := gql.Parse(query) require.NoError(t, err) ctx := context.Background() sg, err := ToSubGraph(ctx, gq) require.NoError(t, err) ch := make(chan error) go ProcessGraph(ctx, sg, nil, ch) err = <-ch require.NoError(t, err) var l Latency pb, err := sg.ToProtocolBuffer(&l) require.NoError(t, err) expectedPb := `attribute: "_root_" children: < attribute: "me" properties: < prop: "name" value: < str_val: "Michonne" > > properties: < prop: "gender" value: < bytes_val: "female" > > children: < attribute: "friend" properties: < prop: "name" value: < str_val: "Daryl Dixon" > > > children: < attribute: "friend" properties: < prop: "name" value: < str_val: "Glenn Rhee" > > > > ` require.EqualValues(t, expectedPb, proto.MarshalTextString(pb)) }
func TestToProto(t *testing.T) { dir, dir2, _ := populateGraph(t) defer os.RemoveAll(dir) defer os.RemoveAll(dir2) query := ` { debug(_uid_:0x1) { _xid_ name gender alive friend { name } friend { } } } ` gq, _, err := gql.Parse(query) require.NoError(t, err) ctx := context.Background() sg, err := ToSubGraph(ctx, gq) require.NoError(t, err) ch := make(chan error) go ProcessGraph(ctx, sg, nil, ch) err = <-ch require.NoError(t, err) var l Latency gr, err := sg.ToProtocolBuffer(&l) require.NoError(t, err) require.EqualValues(t, `attribute: "_root_" children: < uid: 1 xid: "mich" attribute: "debug" properties: < prop: "name" value: < str_val: "Michonne" > > properties: < prop: "gender" value: < bytes_val: "female" > > properties: < prop: "alive" value: < bool_val: true > > children: < uid: 23 attribute: "friend" properties: < prop: "name" value: < str_val: "Rick Grimes" > > > children: < uid: 24 attribute: "friend" properties: < prop: "name" value: < str_val: "Glenn Rhee" > > > children: < uid: 25 attribute: "friend" properties: < prop: "name" value: < str_val: "Daryl Dixon" > > > children: < uid: 31 attribute: "friend" properties: < prop: "name" value: < str_val: "Andrea" > > > children: < uid: 101 attribute: "friend" > > `, proto.MarshalTextString(gr)) }
func TestFieldAliasProto(t *testing.T) { dir, dir2, _ := populateGraph(t) defer os.RemoveAll(dir) defer os.RemoveAll(dir2) // Alright. Now we have everything set up. Let's create the query. query := ` { me(_uid_:0x01) { MyName:name gender alive Buddies:friend { BudName:name } } } ` gq, _, err := gql.Parse(query) require.NoError(t, err) ctx := context.Background() sg, err := ToSubGraph(ctx, gq) require.NoError(t, err) ch := make(chan error) go ProcessGraph(ctx, sg, nil, ch) err = <-ch require.NoError(t, err) var l Latency pb, err := sg.ToProtocolBuffer(&l) require.NoError(t, err) fmt.Println(proto.MarshalTextString(pb)) expectedPb := `attribute: "_root_" children: < attribute: "me" properties: < prop: "MyName" value: < str_val: "Michonne" > > properties: < prop: "gender" value: < bytes_val: "female" > > properties: < prop: "alive" value: < bool_val: true > > children: < attribute: "Buddies" properties: < prop: "BudName" value: < str_val: "Rick Grimes" > > > children: < attribute: "Buddies" properties: < prop: "BudName" value: < str_val: "Glenn Rhee" > > > children: < attribute: "Buddies" properties: < prop: "BudName" value: < str_val: "Daryl Dixon" > > > children: < attribute: "Buddies" properties: < prop: "BudName" value: < str_val: "Andrea" > > > > ` require.EqualValues(t, expectedPb, proto.MarshalTextString(pb)) }