github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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/stretchr/testify/assert"
    23  	"google.golang.org/protobuf/proto"
    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  	filename := filepath.Base(path)
    46  	ext := filepath.Ext(filename)
    47  
    48  	return ext == evtDataExt, nil
    49  }
    50  
    51  func (namer *SequentialNamer) GetIdx() int {
    52  	return namer.idx
    53  }
    54  
    55  func TestFBP(t *testing.T) {
    56  	homeDir := "/home/"
    57  	doltDir := filepath.Join(homeDir, ".tempDolt")
    58  	eventsDataDir := filepath.Join(homeDir, doltDir, eventsDir)
    59  
    60  	version := "1.0.0"
    61  
    62  	tests := []struct {
    63  		name      string
    64  		numEvents int
    65  	}{
    66  		{
    67  			name:      "Save 0 events",
    68  			numEvents: 0,
    69  		},
    70  		{
    71  			name:      "Save 100 events",
    72  			numEvents: 100,
    73  		},
    74  		{
    75  			name:      "Save 1000 events",
    76  			numEvents: 1000,
    77  		},
    78  	}
    79  
    80  	for _, test := range tests {
    81  		t.Run(test.name, func(t *testing.T) {
    82  			// implementation should support dolt filesys api
    83  			fs := filesys.NewInMemFS([]string{eventsDataDir}, nil, eventsDataDir)
    84  
    85  			sn := NewSequentialNamer()
    86  
    87  			fbp := NewFileBackedProc(fs, homeDir, doltDir, sn.Name, sn.Check)
    88  
    89  			ces := make([]*eventsapi.ClientEvent, 0)
    90  
    91  			for i := 0; i < test.numEvents; i++ {
    92  				ce := &eventsapi.ClientEvent{}
    93  				ces = append(ces, ce)
    94  			}
    95  
    96  			assert.Equal(t, len(ces), test.numEvents)
    97  
    98  			err := fbp.WriteEvents(version, ces)
    99  			assert.Equal(t, err, nil)
   100  
   101  			filename := fmt.Sprintf("%04d%s", sn.GetIdx(), evtDataExt)
   102  			path := filepath.Join(eventsDataDir, filename)
   103  
   104  			data, err := fs.ReadFile(path)
   105  			if test.numEvents == 0 {
   106  				// we expect no file to be written if events length is less than 1
   107  				assert.NotNil(t, err)
   108  
   109  			} else {
   110  				// otherwise we should find the file and be able to parse it into a pb Message
   111  				assert.Equal(t, err, nil)
   112  
   113  				req := &eventsapi.LogEventsRequest{}
   114  
   115  				err = proto.Unmarshal(data, req)
   116  
   117  				assert.Equal(t, err, nil)
   118  				assert.Equal(t, len(ces), len(req.Events))
   119  			}
   120  		})
   121  	}
   122  }