forked from eatonphil/gosql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit testing for memory.go (eatonphil#14)
* 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
1 parent
8b4f3ad
commit 4dcbe2c
Showing
8 changed files
with
303 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ fmt: | |
|
||
test: | ||
go test -race . | ||
|
||
vet: | ||
go vet . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.