github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/store/file/blob_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/store/file/blob_test.go
    14  
    15  package file
    16  
    17  import (
    18  	"bytes"
    19  	"io/ioutil"
    20  	"testing"
    21  
    22  	"github.com/tickoalcantara12/micro/v3/service/store"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestBlobStore(t *testing.T) {
    27  	blob, err := NewBlobStore()
    28  	assert.NotNilf(t, blob, "Blob should not be nil")
    29  	assert.Nilf(t, err, "Error should be nil")
    30  
    31  	t.Run("ReadMissingKey", func(t *testing.T) {
    32  		res, err := blob.Read("")
    33  		assert.Equal(t, store.ErrMissingKey, err, "Error should be missing key")
    34  		assert.Nil(t, res, "Result should be nil")
    35  	})
    36  
    37  	t.Run("ReadNotFound", func(t *testing.T) {
    38  		res, err := blob.Read("foo")
    39  		assert.Equal(t, store.ErrNotFound, err, "Error should be not found")
    40  		assert.Nil(t, res, "Result should be nil")
    41  	})
    42  
    43  	t.Run("WriteMissingKey", func(t *testing.T) {
    44  		buf := bytes.NewBuffer([]byte("HelloWorld"))
    45  		err := blob.Write("", buf)
    46  		assert.Equal(t, store.ErrMissingKey, err, "Error should be missing key")
    47  	})
    48  
    49  	t.Run("WriteValid", func(t *testing.T) {
    50  		buf := bytes.NewBuffer([]byte("world"))
    51  		err := blob.Write("hello", buf)
    52  		assert.Nilf(t, err, "Error should be nil")
    53  	})
    54  
    55  	t.Run("ReadValid", func(t *testing.T) {
    56  		val, err := blob.Read("hello")
    57  		bytes, _ := ioutil.ReadAll(val)
    58  		assert.Nilf(t, err, "Error should be nil")
    59  		assert.Equal(t, string(bytes), "world", "Value should be world")
    60  	})
    61  
    62  	t.Run("ReadIncorrectNamespace", func(t *testing.T) {
    63  		val, err := blob.Read("hello", store.BlobNamespace("bar"))
    64  		assert.Equal(t, store.ErrNotFound, err, "Error should be not found")
    65  		assert.Nil(t, val, "Value should be nil")
    66  	})
    67  
    68  	t.Run("ReadCorrectNamespace", func(t *testing.T) {
    69  		val, err := blob.Read("hello", store.BlobNamespace("micro"))
    70  		bytes, _ := ioutil.ReadAll(val)
    71  		assert.Nil(t, err, "Error should be nil")
    72  		assert.Equal(t, string(bytes), "world", "Value should be world")
    73  	})
    74  
    75  	t.Run("DeleteIncorrectNamespace", func(t *testing.T) {
    76  		err := blob.Delete("hello", store.BlobNamespace("bar"))
    77  		assert.Nil(t, err, "Error should be nil")
    78  	})
    79  
    80  	t.Run("DeleteCorrectNamespaceIncorrectKey", func(t *testing.T) {
    81  		err := blob.Delete("world", store.BlobNamespace("micro"))
    82  		assert.Nil(t, err, "Error should be nil")
    83  	})
    84  
    85  	t.Run("DeleteCorrectNamespace", func(t *testing.T) {
    86  		err := blob.Delete("hello", store.BlobNamespace("micro"))
    87  		assert.Nil(t, err, "Error should be nil")
    88  	})
    89  
    90  	t.Run("ReadDeletedKey", func(t *testing.T) {
    91  		res, err := blob.Read("hello", store.BlobNamespace("micro"))
    92  		assert.Equal(t, store.ErrNotFound, err, "Error should be not found")
    93  		assert.Nil(t, res, "Result should be nil")
    94  	})
    95  }