MCPcopy
hub / github.com/redis/go-redis / TestVectorScoreAttribSliceCmd_readReply

Function TestVectorScoreAttribSliceCmd_readReply

vectorset_commands_test.go:429–485  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

427}
428
429func TestVectorScoreAttribSliceCmd_readReply(t *testing.T) {
430 t.Run("resp3FlatArray", func(t *testing.T) {
431 // *6 -> two (name, score, attrib) triplets; second attrib is nil
432 reply := "*6\r\n" +
433 "$2\r\na1\r\n,1.5\r\n$4\r\nattr\r\n" +
434 "$2\r\na2\r\n,2.5\r\n_\r\n"
435 cmd := NewVectorScoreAttribSliceCmd(context.Background())
436 rd := proto.NewReader(strings.NewReader(reply))
437 if err := cmd.readReply(rd); err != nil {
438 t.Fatal(err)
439 }
440
441 if len(cmd.val) != 2 {
442 t.Fatalf("len(val) = %d, want 2", len(cmd.val))
443 }
444
445 if cmd.val[0].Name != "a1" || cmd.val[0].Score != 1.5 || cmd.val[0].Attribs == nil || *cmd.val[0].Attribs != "attr" {
446 t.Errorf("first row = %+v, want {a1 1.5 %q}", cmd.val[0], "attr")
447 }
448
449 if cmd.val[1].Name != "a2" || cmd.val[1].Score != 2.5 || cmd.val[1].Attribs != nil {
450 t.Errorf("second row = %+v, want {a2 2.5 nil}", cmd.val[1])
451 }
452 })
453
454 t.Run("resp3Map", func(t *testing.T) {
455 // %2 -> two map entries; value each is *2 [score, attrib]
456 reply := "%2\r\n" +
457 "$2\r\nb1\r\n*2\r\n,0.5\r\n$1\r\na\r\n" +
458 "$2\r\nb2\r\n*2\r\n,1\r\n_\r\n"
459 cmd := NewVectorScoreAttribSliceCmd(context.Background())
460 rd := proto.NewReader(strings.NewReader(reply))
461 if err := cmd.readReply(rd); err != nil {
462 t.Fatal(err)
463 }
464
465 if len(cmd.val) != 2 {
466 t.Fatalf("len(val) = %d, want 2", len(cmd.val))
467 }
468
469 if cmd.val[0].Name != "b1" || cmd.val[0].Score != 0.5 || cmd.val[0].Attribs == nil || *cmd.val[0].Attribs != "a" {
470 t.Errorf("first row = %+v, want {b1 0.5 %q}", cmd.val[0], "a")
471 }
472
473 if cmd.val[1].Name != "b2" || cmd.val[1].Score != 1 || cmd.val[1].Attribs != nil {
474 t.Errorf("second row = %+v, want {b2 1 nil}", cmd.val[1])
475 }
476 })
477
478 t.Run("flatArrayLenNotMultipleOf3", func(t *testing.T) {
479 cmd := NewVectorScoreAttribSliceCmd(context.Background())
480 rd := proto.NewReader(strings.NewReader("*1\r\n$1\r\nx\r\n"))
481 if err := cmd.readReply(rd); err == nil {
482 t.Fatal("expected error for array length not divisible by 3")
483 }
484 })
485}
486

Callers

nothing calls this directly

Calls 4

readReplyMethod · 0.95
NewReaderFunction · 0.92
RunMethod · 0.45

Tested by

no test coverage detected