github.com/weaviate/weaviate@v1.24.6/usecases/replica/transport_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package replica
    13  
    14  import (
    15  	"encoding/json"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/pkg/errors"
    20  	"github.com/stretchr/testify/assert"
    21  	"golang.org/x/net/context"
    22  )
    23  
    24  func TestReplicationErrorTimeout(t *testing.T) {
    25  	ctx := context.Background()
    26  	ctx, cancel := context.WithDeadline(ctx, time.Now())
    27  	defer cancel()
    28  	err := &Error{Err: ctx.Err()}
    29  	assert.True(t, err.Timeout())
    30  	err = err.Clone()
    31  	assert.ErrorIs(t, err, context.DeadlineExceeded)
    32  }
    33  
    34  func TestReplicationErrorMarshal(t *testing.T) {
    35  	rawErr := Error{Code: StatusClassNotFound, Msg: "Article", Err: errors.New("error cannot be marshalled")}
    36  	bytes, err := json.Marshal(&rawErr)
    37  	assert.Nil(t, err)
    38  	got := NewError(0, "")
    39  	assert.Nil(t, json.Unmarshal(bytes, got))
    40  	want := &Error{Code: StatusClassNotFound, Msg: "Article"}
    41  	assert.Equal(t, want, got)
    42  }
    43  
    44  func TestReplicationErrorStatus(t *testing.T) {
    45  	tests := []struct {
    46  		code StatusCode
    47  		desc string
    48  	}{
    49  		{-1, ""},
    50  		{StatusOK, "ok"},
    51  		{StatusClassNotFound, "class not found"},
    52  		{StatusShardNotFound, "shard not found"},
    53  		{StatusNotFound, "not found"},
    54  		{StatusAlreadyExisted, "already existed"},
    55  		{StatusConflict, "conflict"},
    56  		{StatusPreconditionFailed, "precondition failed"},
    57  		{StatusReadOnly, "read only"},
    58  	}
    59  	for _, test := range tests {
    60  		got := statusText(test.code)
    61  		if got != test.desc {
    62  			t.Errorf("statusText(%d) want %v got %v", test.code, test.desc, got)
    63  		}
    64  	}
    65  }