File: error_test.go

package info (click to toggle)
golang-github-meilisearch-meilisearch-go 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,084 kB
  • sloc: makefile: 9
file content (83 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package meilisearch

import (
	"fmt"
	"io"
	"net/http"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestError_VersionErrorHintMessage(t *testing.T) {
	type args struct {
		request     *internalRequest
		mockedError error
	}
	tests := []struct {
		name string
		args args
	}{
		{
			name: "VersionErrorHintMessageGetDocument",
			args: args{
				request: &internalRequest{
					functionName: "GetDocuments",
				},
				mockedError: &Error{
					Endpoint:         "endpointForGetDocuments",
					Method:           http.MethodPost,
					Function:         "GetDocuments",
					RequestToString:  "empty request",
					ResponseToString: "empty response",
					MeilisearchApiError: meilisearchApiError{
						Message: "empty Meilisearch message",
					},
					StatusCode: 1,
					rawMessage: "empty message",
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := VersionErrorHintMessage(tt.args.mockedError, tt.args.request)
			require.Error(t, err)
			fmt.Println(err)
			require.Equal(t, tt.args.mockedError.Error()+". Hint: It might not be working because you're not up to date with the Meilisearch version that "+tt.args.request.functionName+" call requires", err.Error())
		})
	}
}

type failEncoder struct{}

func (f *failEncoder) Encode(r io.Reader) (io.ReadCloser, error) {
	return nil, nil
}
func (f *failEncoder) Decode(_ []byte, v interface{}) error {
	return fmt.Errorf("decode failed")
}

func TestError_ErrorBody_WithEncoder(t *testing.T) {

	err := &Error{
		encoder: &mockEncoder{},
	}
	body := []byte(`{"message":"should not be used"}`)
	err.ErrorBody(body)
	require.Equal(t, "mocked message", err.MeilisearchApiError.Message)
	require.Equal(t, "mocked code", err.MeilisearchApiError.Code)
	require.Equal(t, "mocked type", err.MeilisearchApiError.Type)
	require.Equal(t, "mocked link", err.MeilisearchApiError.Link)

	err2 := &Error{
		encoder: &failEncoder{},
	}
	body2 := []byte(`{"message":"should not be used"}`)
	err2.ErrorBody(body2)
	// Should not set MeilisearchApiError fields
	require.Empty(t, err2.MeilisearchApiError.Message)
	require.Empty(t, err2.MeilisearchApiError.Code)
	require.Empty(t, err2.MeilisearchApiError.Type)
	require.Empty(t, err2.MeilisearchApiError.Link)
}