github.com/ethersphere/bee/v2@v2.2.0/pkg/file/pipeline/store/store_test.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package store_test 6 7 import ( 8 "bytes" 9 "context" 10 "errors" 11 "testing" 12 13 "github.com/ethersphere/bee/v2/pkg/file/pipeline" 14 mock "github.com/ethersphere/bee/v2/pkg/file/pipeline/mock" 15 "github.com/ethersphere/bee/v2/pkg/file/pipeline/store" 16 "github.com/ethersphere/bee/v2/pkg/storage/inmemchunkstore" 17 "github.com/ethersphere/bee/v2/pkg/swarm" 18 ) 19 20 // TestStoreWriter tests that store writer stores the provided data and calls the next chain writer. 21 func TestStoreWriter(t *testing.T) { 22 t.Parallel() 23 24 mockStore := inmemchunkstore.New() 25 mockChainWriter := mock.NewChainWriter() 26 ctx := context.Background() 27 writer := store.NewStoreWriter(ctx, mockStore, mockChainWriter) 28 29 for _, tc := range []struct { 30 name string 31 ref []byte 32 data []byte 33 expErr error 34 }{ 35 { 36 name: "no data", 37 expErr: store.ErrInvalidData, 38 }, 39 { 40 name: "some data", 41 ref: []byte{0xaa, 0xbb, 0xcc}, 42 data: []byte("hello world"), 43 }, 44 {}, 45 } { 46 args := pipeline.PipeWriteArgs{Ref: tc.ref, Data: tc.data} 47 err := writer.ChainWrite(&args) 48 49 if err != nil && tc.expErr != nil && errors.Is(err, tc.expErr) { 50 return 51 } 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 d, err := mockStore.Get(ctx, swarm.NewAddress(tc.ref)) 57 if err != nil { 58 t.Fatal(err) 59 } 60 if !bytes.Equal(tc.data, d.Data()) { 61 t.Fatal("data mismatch") 62 } 63 if calls := mockChainWriter.ChainWriteCalls(); calls != 1 { 64 t.Errorf("wanted 1 ChainWrite call, got %d", calls) 65 } 66 } 67 } 68 69 // TestSum tests that calling Sum on the store writer results in Sum on the next writer in the chain. 70 func TestSum(t *testing.T) { 71 t.Parallel() 72 73 mockChainWriter := mock.NewChainWriter() 74 ctx := context.Background() 75 writer := store.NewStoreWriter(ctx, nil, mockChainWriter) 76 _, err := writer.Sum() 77 if err != nil { 78 t.Fatal(err) 79 } 80 if calls := mockChainWriter.SumCalls(); calls != 1 { 81 t.Fatalf("wanted 1 Sum call but got %d", calls) 82 } 83 }