github.com/wrgl/wrgl@v0.14.0/pkg/factory/rows.go (about)

     1  package factory
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/wrgl/wrgl/pkg/objects"
    11  	"github.com/wrgl/wrgl/pkg/slice"
    12  	"github.com/wrgl/wrgl/pkg/testutils"
    13  )
    14  
    15  func GetRows(t *testing.T, db objects.Store, tbl *objects.Table) [][]string {
    16  	var rows [][]string
    17  	var bb []byte
    18  	var err error
    19  	var blk [][]string
    20  	for _, sum := range tbl.Blocks {
    21  		blk, bb, err = objects.GetBlock(db, bb, sum)
    22  		require.NoError(t, err)
    23  		rows = append(rows, blk...)
    24  	}
    25  	return rows
    26  }
    27  
    28  func IndexRows(pk []uint32, rows [][]string) map[string][]string {
    29  	m := map[string][]string{}
    30  	for _, row := range rows {
    31  		m[strings.Join(slice.IndicesToValues(row, pk), "-")] = row
    32  	}
    33  	return m
    34  }
    35  
    36  func AssertDuplicatedRowsRemoved(t *testing.T, db objects.Store, newTbl, oldTbl *objects.Table) {
    37  	t.Helper()
    38  	assert.Equal(t, newTbl.Columns, oldTbl.Columns)
    39  	assert.Equal(t, newTbl.PK, oldTbl.PK)
    40  	assert.Less(t, newTbl.RowsCount, oldTbl.RowsCount)
    41  	newRows := GetRows(t, db, newTbl)
    42  	oldRows := GetRows(t, db, oldTbl)
    43  	assert.Less(t, len(newRows), len(oldRows))
    44  	assert.Equal(t, IndexRows(newTbl.PK, newRows), IndexRows(oldTbl.PK, oldRows))
    45  }
    46  
    47  func CreateRows(t *testing.T, ncols, nrows, ndup int) [][]string {
    48  	t.Helper()
    49  	cols := strings.Split(testutils.LowerAlphaBytes[:ncols], "")
    50  	rows := make([][]string, 0, nrows+1)
    51  	rows = append(rows, cols)
    52  	for i := 0; i < nrows; i++ {
    53  		rows = append(rows, append(
    54  			[]string{strconv.Itoa(i + 1)},
    55  			strings.Split(testutils.BrokenRandomAlphaNumericString(ncols-1), "")...,
    56  		))
    57  	}
    58  	for j := 0; j < ndup; j++ {
    59  		copy(rows[nrows-ndup+j+1], rows[nrows-ndup+j-1+1])
    60  	}
    61  	return rows
    62  }