github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/events/file_backed_proc_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package events
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
    26  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    27  )
    28  
    29  type SequentialNamer struct {
    30  	idx int
    31  }
    32  
    33  func NewSequentialNamer() *SequentialNamer {
    34  	return &SequentialNamer{}
    35  }
    36  
    37  func (namer *SequentialNamer) Name(bytes []byte) string {
    38  	namer.idx += 1
    39  	name := fmt.Sprintf("%04d%s", namer.idx, evtDataExt)
    40  
    41  	return name
    42  }
    43  
    44  func (namer *SequentialNamer) Check(data []byte, path string) (bool, error) {
    45  	// todo
    46  	return true, nil
    47  }
    48  
    49  func (namer *SequentialNamer) GetIdx() int {
    50  	return namer.idx
    51  }
    52  
    53  func TestFBP(t *testing.T) {
    54  	homeDir := "/home/"
    55  	doltDir := filepath.Join(homeDir, ".tempDolt")
    56  	eventsDataDir := filepath.Join(homeDir, doltDir, eventsDir)
    57  
    58  	version := "1.0.0"
    59  
    60  	tests := []struct {
    61  		name      string
    62  		numEvents int
    63  	}{
    64  		{
    65  			name:      "Save 0 events",
    66  			numEvents: 0,
    67  		},
    68  		{
    69  			name:      "Save 100 events",
    70  			numEvents: 100,
    71  		},
    72  		{
    73  			name:      "Save 1000 events",
    74  			numEvents: 1000,
    75  		},
    76  	}
    77  
    78  	for _, test := range tests {
    79  		t.Run(test.name, func(t *testing.T) {
    80  			// implementation should support dolt filesys api
    81  			fs := filesys.NewInMemFS([]string{eventsDataDir}, nil, eventsDataDir)
    82  
    83  			sn := NewSequentialNamer()
    84  
    85  			fbp := NewFileBackedProc(fs, homeDir, doltDir, sn.Name, sn.Check)
    86  
    87  			ces := make([]*eventsapi.ClientEvent, 0)
    88  
    89  			for i := 0; i < test.numEvents; i++ {
    90  				ce := &eventsapi.ClientEvent{}
    91  				ces = append(ces, ce)
    92  			}
    93  
    94  			assert.Equal(t, len(ces), test.numEvents)
    95  
    96  			err := fbp.WriteEvents(version, ces)
    97  			assert.Equal(t, err, nil)
    98  
    99  			filename := fmt.Sprintf("%04d%s", sn.GetIdx(), evtDataExt)
   100  			path := filepath.Join(eventsDataDir, filename)
   101  
   102  			data, err := fs.ReadFile(path)
   103  			if test.numEvents == 0 {
   104  				// we expect no file to be written if events length is less than 1
   105  				assert.NotNil(t, err)
   106  
   107  			} else {
   108  				// otherwise we should find the file and be able to parse it into a pb Message
   109  				assert.Equal(t, err, nil)
   110  
   111  				req := &eventsapi.LogEventsRequest{}
   112  
   113  				err = proto.Unmarshal(data, req)
   114  
   115  				assert.Equal(t, err, nil)
   116  				assert.Equal(t, len(ces), len(req.Events))
   117  			}
   118  		})
   119  	}
   120  }