Skip to content

Commit

Permalink
Add unit testing for memory.go (eatonphil#14)
Browse files Browse the repository at this point in the history
* Add memory_test.go

* memory_test.go: remove unused code

* memory_test.go changes

* Add internal parser state, use defined error variables, use assert.Nil

* Move to fully defined test and results, add test for * in the middle of the select columns

Co-authored-by: Phil Eaton <[email protected]>
  • Loading branch information
krischerven and Phil Eaton authored Apr 19, 2020
1 parent 8b4f3ad commit 4dcbe2c
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 103 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ fmt:

test:
go test -race .

vet:
go vet .
25 changes: 20 additions & 5 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ const (
BoolType
)

func (c ColumnType) String() string {
switch c {
case TextType:
return "TextType"
case IntType:
return "IntType"
case BoolType:
return "BoolType"
default:
return "Error"
}
}

type Cell interface {
AsText() string
AsInt() int32
AsBool() bool
}

type Results struct {
Columns []struct {
Type ColumnType
Name string
}
Rows [][]Cell
Columns []ResultColumn
Rows [][]Cell
}

type ResultColumn struct {
Type ColumnType
Name string
}

type Backend interface {
Expand Down
3 changes: 2 additions & 1 deletion cmd/libraryexample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
func main() {
mb := gosql.NewMemoryBackend()

ast, err := gosql.Parse("CREATE TABLE users (id INT, name TEXT); INSERT INTO users VALUES (1, 'Admin'); SELECT id, name FROM users")
parser := gosql.Parser{}
ast, err := parser.Parse("CREATE TABLE users (id INT, name TEXT); INSERT INTO users VALUES (1, 'Admin'); SELECT id, name FROM users")
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ repl:
break
}

ast, err := gosql.Parse(line)
parser := gosql.Parser{}
ast, err := parser.Parse(line)
if err != nil {
fmt.Println("Error while parsing:", err)
continue repl
Expand Down
5 changes: 1 addition & 4 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ func (mb *MemoryBackend) Select(slct *SelectStatement) (*Results, error) {
}

results := [][]Cell{}
columns := []struct {
Type ColumnType
Name string
}{}
columns := []ResultColumn{}

if slct.from == nil {
t = &table{}
Expand Down
173 changes: 173 additions & 0 deletions memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package gosql

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

var mb *MemoryBackend

func TestSelect(t *testing.T) {
mb = NewMemoryBackend()

parser := Parser{HelpMessagesDisabled: true}
ast, err := parser.Parse("SELECT * FROM test")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
_, err = mb.Select(ast.Statements[0].SelectStatement)
assert.Equal(t, err, ErrTableDoesNotExist)

ast, err = parser.Parse("CREATE TABLE test(x INT, y INT, z BOOLEAN);")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
err = mb.CreateTable(ast.Statements[0].CreateTableStatement)
assert.Nil(t, err)

ast, err = parser.Parse("INSERT INTO test VALUES(100, 200, true)")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
err = mb.Insert(ast.Statements[0].InsertStatement)
assert.Nil(t, err)

value100 := literalToMemoryCell(&token{"100", numericKind, location{}})
value200 := literalToMemoryCell(&token{"200", numericKind, location{}})
xCol := ResultColumn{IntType, "x"}
yCol := ResultColumn{IntType, "y"}
zCol := ResultColumn{BoolType, "z"}

tests := []struct{
query string
results Results
}{
{
"SELECT * FROM test",
Results{
[]ResultColumn{xCol, yCol, zCol},
[][]Cell{{value100, value200, trueMemoryCell}},
},
},
{
"SELECT x FROM test",
Results{
[]ResultColumn{xCol},
[][]Cell{{value100}},
},
},
{
"SELECT x, y FROM test",
Results{
[]ResultColumn{xCol, yCol},
[][]Cell{{value100, value200}},
},
},
{
"SELECT x, y, z FROM test",
Results{
[]ResultColumn{xCol, yCol, zCol},
[][]Cell{{value100, value200, trueMemoryCell}},
},
},
{
"SELECT *, x FROM test",
Results{
[]ResultColumn{xCol, yCol, zCol, xCol},
[][]Cell{{value100, value200, trueMemoryCell, value100}},
},
},
{
"SELECT *, x, y FROM test",
Results{
[]ResultColumn{xCol, yCol, zCol, xCol, yCol},
[][]Cell{{value100, value200, trueMemoryCell, value100, value200}},
},
},
{
"SELECT *, x, y, z FROM test",
Results{
[]ResultColumn{xCol, yCol, zCol, xCol, yCol, zCol},
[][]Cell{{value100, value200, trueMemoryCell, value100, value200, trueMemoryCell}},
},
},
{
"SELECT x, *, z FROM test",
Results{
[]ResultColumn{xCol, xCol, yCol, zCol, zCol},
[][]Cell{{value100, value100, value200, trueMemoryCell, trueMemoryCell}},
},
},
}

for _, test := range tests {
fmt.Println("(Memory) Testing:", test.query)
ast, err = parser.Parse(test.query)
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)

var res *Results
res, err = mb.Select(ast.Statements[0].SelectStatement)
assert.Nil(t, err)
assert.Equal(t, *res, test.results)
}
}

func TestInsert(t *testing.T) {
fmt.Println("(Memory) Testing: INSERT INTO test VALUES(100, 200, 300)")
mb = NewMemoryBackend()

parser := Parser{HelpMessagesDisabled: true}
ast, err := parser.Parse("INSERT INTO test VALUES(100, 200, 300)")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
err = mb.Insert(ast.Statements[0].InsertStatement)
assert.Equal(t, err, ErrTableDoesNotExist)

ast, err = parser.Parse("CREATE TABLE test(x INT, y INT, z INT);")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
err = mb.CreateTable(ast.Statements[0].CreateTableStatement)
assert.Nil(t, err)

ast, err = parser.Parse("INSERT INTO test VALUES(100, 200, 300)")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
err = mb.Insert(ast.Statements[0].InsertStatement)
assert.Nil(t, err)
}

func TestCreateTable(t *testing.T) {
fmt.Println("(Memory) Testing: CREATE TABLE test(x INT, y INT, z INT)")
mb = NewMemoryBackend()

parser := Parser{HelpMessagesDisabled: true}
ast, err := parser.Parse("CREATE TABLE test(x INT, y INT, z INT)")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)

err = mb.CreateTable(ast.Statements[0].CreateTableStatement)
assert.Nil(t, err)
}

func TestDropTable(t *testing.T) {
fmt.Println("(Memory) Testing: DROP TABLE test")
mb = NewMemoryBackend()

parser := Parser{HelpMessagesDisabled: true}
ast, err := parser.Parse("DROP TABLE test;")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
err = mb.DropTable(ast.Statements[0].DropTableStatement)
assert.Equal(t, err, ErrTableDoesNotExist)

ast, err = parser.Parse("CREATE TABLE test(x INT, y INT, z INT);")
assert.Nil(t, err)
err = mb.CreateTable(ast.Statements[0].CreateTableStatement)
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)

ast, err = parser.Parse("DROP TABLE test;")
assert.Nil(t, err)
assert.NotEqual(t, ast, nil)
err = mb.DropTable(ast.Statements[0].DropTableStatement)
assert.Nil(t, err)
}
Loading

0 comments on commit 4dcbe2c

Please sign in to comment.