Add mutex to table

This commit is contained in:
2021-06-19 17:55:17 -04:00
parent b66f5ebe67
commit e1568cd759
+14 -10
View File
@@ -1,42 +1,46 @@
package ramdb package ramdb
import ( import (
"sync"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestTable_NewIndex(t *testing.T) { func TestTable_CreateIndex(t *testing.T) {
tests := []struct { tests := []struct {
test string test string
table table table table
onColumn string column string
expectedError error expectedError error
}{ }{
{ {
test: "it should return ErrInvalidIndex if onColumn is empty", test: "it should return ErrInvalidIndex if column is empty",
expectedError: ErrInvalidIndex, expectedError: ErrInvalidIndex,
}, },
{ {
test: "it should return ErrIndexExists if an index exists for onColumn", test: "it should return ErrIndexExists if an index exists for column",
table: table{indexes: map[string]*index{ table: table{
"test_column": &index{}, mutex: &sync.Mutex{},
}}, indexes: map[string]*index{
onColumn: "test_column", "test_column": &index{},
}},
column: "test_column",
expectedError: ErrIndexExists, expectedError: ErrIndexExists,
}, },
{ {
test: "it should create an index successfully", test: "it should create an index successfully",
table: table{ table: table{
mutex: &sync.Mutex{},
indexes: make(map[string]*index), indexes: make(map[string]*index),
}, },
onColumn: "test_column", column: "test_column",
}, },
} }
for _, tc := range tests { for _, tc := range tests {
t.Run(tc.test, func(t *testing.T) { t.Run(tc.test, func(t *testing.T) {
err := tc.table.CreateIndex(tc.onColumn) err := tc.table.CreateIndex(tc.column)
assert.Equal(t, tc.expectedError, err) assert.Equal(t, tc.expectedError, err)
}) })