github.com/weaviate/weaviate@v1.24.6/usecases/objects/head_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 objects
    13  
    14  import (
    15  	"context"
    16  	"testing"
    17  
    18  	"github.com/go-openapi/strfmt"
    19  	"github.com/pkg/errors"
    20  	"github.com/weaviate/weaviate/entities/schema"
    21  )
    22  
    23  func Test_HeadObject(t *testing.T) {
    24  	t.Parallel()
    25  	var (
    26  		cls    = "MyClass"
    27  		id     = strfmt.UUID("5a1cd361-1e0d-42ae-bd52-ee09cb5f31cc")
    28  		m      = newFakeGetManager(schema.Schema{})
    29  		errAny = errors.New("any")
    30  	)
    31  
    32  	tests := []struct {
    33  		class     string
    34  		mockedOk  bool
    35  		mockedErr error
    36  		authErr   error
    37  		lockErr   error
    38  		wantOK    bool
    39  		wantCode  int
    40  	}{
    41  		{
    42  			mockedOk: true,
    43  			wantOK:   true,
    44  		},
    45  		{
    46  			class:    cls,
    47  			mockedOk: true,
    48  			wantOK:   true,
    49  		},
    50  		{
    51  			class:    cls,
    52  			mockedOk: false,
    53  			wantOK:   false,
    54  		},
    55  		{
    56  			class:     cls,
    57  			mockedOk:  false,
    58  			mockedErr: errAny,
    59  			wantOK:    false,
    60  			wantCode:  StatusInternalServerError,
    61  		},
    62  		{
    63  			class:    cls,
    64  			authErr:  errAny,
    65  			wantOK:   false,
    66  			wantCode: StatusForbidden,
    67  		},
    68  		{
    69  			class:    cls,
    70  			lockErr:  errAny,
    71  			wantOK:   false,
    72  			wantCode: StatusInternalServerError,
    73  		},
    74  	}
    75  	for i, tc := range tests {
    76  		m.authorizer.Err = tc.authErr
    77  		m.locks.Err = tc.lockErr
    78  		if tc.authErr == nil && tc.lockErr == nil {
    79  			m.repo.On("Exists", tc.class, id).Return(tc.mockedOk, tc.mockedErr).Once()
    80  		}
    81  		ok, err := m.Manager.HeadObject(context.Background(), nil, tc.class, id, nil, "")
    82  		code := 0
    83  		if err != nil {
    84  			code = err.Code
    85  		}
    86  		if tc.wantOK != ok || tc.wantCode != code {
    87  			t.Errorf("case %d expected:(%v, %v) got:(%v, %v)", i+1, tc.wantOK, tc.wantCode, ok, code)
    88  		}
    89  	}
    90  }