Merge pull request 'fix: added some unit tests for vm' (#8) from dev into main
Reviewed-on: #8
This commit was merged in pull request #8.
This commit is contained in:
@@ -37,6 +37,10 @@ func Run() {
|
||||
fmt.Println("Syntax error, could not parse statement")
|
||||
case PrepareUnrecognised:
|
||||
fmt.Printf("Unrecognised keyword at the start of '%s'.\n", inputBuffer.Buffer)
|
||||
case PrepareTooManyArgs:
|
||||
fmt.Printf("Too many arguments in the insert statement '%v'\n", inputBuffer.Buffer)
|
||||
case PrepareValueTooLong:
|
||||
fmt.Printf("Values for either username or email are too long\n")
|
||||
}
|
||||
|
||||
switch statement.ExecuteStatement(table) {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package repl
|
||||
|
||||
// TODO
|
||||
|
||||
// in trying to implement unit test for Run we would kinda endup implementing a test which would be somewhat similar to
|
||||
// e2e test which we already have in place under ../tests directory
|
||||
// PrintConsole simply prints godb > to the console, so not sure if we would need a test for that
|
||||
// Skipping unit test for this, while marking it as a TODO
|
||||
+40
-10
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
. "godb/internals/buffer"
|
||||
@@ -21,6 +23,8 @@ type PrepareResults int
|
||||
const (
|
||||
PrepareSuccess PrepareResults = iota
|
||||
PrepareUnrecognised
|
||||
PrepareTooManyArgs
|
||||
PrepareValueTooLong
|
||||
PrepareSyntaxError
|
||||
)
|
||||
|
||||
@@ -151,19 +155,45 @@ func DoMetaCommands(inputBuffer *InputBuffer) MetaCommandResults {
|
||||
return metacommand
|
||||
}
|
||||
|
||||
func (statement *Statement) perpareInsert(inputBuffer *InputBuffer) PrepareResults {
|
||||
inputs := strings.Split(inputBuffer.Buffer, " ")[1:]
|
||||
if len(inputs) < 3 {
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
if len(inputs) > 3 {
|
||||
return PrepareTooManyArgs
|
||||
}
|
||||
if slices.Contains(inputs, "null") || slices.Contains(inputs, "NULL") || slices.Contains(inputs, "Null") || slices.Contains(inputs, " ") {
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
|
||||
tid, err := strconv.ParseUint(inputs[0], 10, 0)
|
||||
if err != nil {
|
||||
// fmt.Printf("supplied ID: %v isn't acceptable, please correct and retry\n", inputs[0])
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
id := uint(tid)
|
||||
if len(inputs[1]) > usernameSize {
|
||||
// fmt.Printf("%v is too long for username\n", inputs[1])
|
||||
return PrepareValueTooLong
|
||||
}
|
||||
if len(inputs[2]) > emailSize {
|
||||
// fmt.Printf("%v is too long for email\n", inputs[2])
|
||||
return PrepareValueTooLong
|
||||
}
|
||||
|
||||
statement.row.id = id
|
||||
statement.row.username = inputs[1]
|
||||
statement.row.email = inputs[2]
|
||||
|
||||
statement.stype = StatementInsert
|
||||
return PrepareSuccess
|
||||
}
|
||||
|
||||
func (statement *Statement) PrepareStatements(inputBuffer *InputBuffer) PrepareResults {
|
||||
statement.stype = StatementUnrecognized
|
||||
if strings.HasPrefix(inputBuffer.Buffer, "insert") {
|
||||
argsAssigned, err := fmt.Sscanf(inputBuffer.Buffer, "insert %d %s %s", &statement.row.id, &statement.row.username, &statement.row.email)
|
||||
if err != nil {
|
||||
fmt.Println("error parsing insert statement")
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
if err != nil || argsAssigned > 3 {
|
||||
return PrepareSyntaxError
|
||||
}
|
||||
statement.stype = StatementInsert
|
||||
return PrepareSuccess
|
||||
return statement.perpareInsert(inputBuffer)
|
||||
}
|
||||
if inputBuffer.Buffer == "select" {
|
||||
statement.stype = StatementSelect
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"godb/internals/buffer"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var inputBuffer = buffer.NewInputBuffer()
|
||||
|
||||
var statement = NewStatement()
|
||||
|
||||
func TestVM(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
prepareStatusExpected PrepareResults
|
||||
statementResultExpected statementType
|
||||
}{
|
||||
{
|
||||
name: "Testing Select Statement",
|
||||
input: "select",
|
||||
prepareStatusExpected: PrepareSuccess,
|
||||
statementResultExpected: StatementSelect,
|
||||
},
|
||||
{
|
||||
name: "Testing Insert Statement",
|
||||
input: "insert 0 hello hello.com",
|
||||
prepareStatusExpected: PrepareSuccess,
|
||||
statementResultExpected: StatementInsert,
|
||||
},
|
||||
{
|
||||
name: "Testing Unrecognized Statement",
|
||||
input: "something",
|
||||
prepareStatusExpected: PrepareUnrecognised,
|
||||
statementResultExpected: StatementUnrecognized,
|
||||
},
|
||||
{
|
||||
name: "Testing Syntax Errors",
|
||||
input: "insert 0 hello",
|
||||
prepareStatusExpected: PrepareSyntaxError,
|
||||
statementResultExpected: StatementUnrecognized,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
inputBuffer.Buffer = test.input
|
||||
result := statement.PrepareStatements(inputBuffer)
|
||||
if result != test.prepareStatusExpected && statement.stype == test.statementResultExpected {
|
||||
t.Errorf("Expected prepareResult and stype to be: (%v, %v), got: (%v,%v)\n", test.prepareStatusExpected, test.statementResultExpected, result, statement.stype)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+54
-17
@@ -35,15 +35,21 @@ func runner(commands []string, t *testing.T) []string {
|
||||
go func() {
|
||||
defer stdin.Close()
|
||||
for _, c := range commands {
|
||||
_, _ = io.WriteString(stdin, c+"\n")
|
||||
_, err = io.WriteString(stdin, c+"\n")
|
||||
if err != nil {
|
||||
t.Errorf("Failed to execute %v because of %q\n", c, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
reader := bufio.NewReader(stdout)
|
||||
for {
|
||||
// fmt.Printf("reading command outputs...\n")
|
||||
line, err := reader.ReadString('\n')
|
||||
if line != "" {
|
||||
formattedline := fmt.Sprintf("%v", strings.Trim(strings.ReplaceAll(line, "\x00", ""), "\n"))
|
||||
// fmt.Printf("formatting and appending result: %v\n", formattedline)
|
||||
results = append(results, formattedline)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -60,26 +66,57 @@ func runner(commands []string, t *testing.T) []string {
|
||||
return results
|
||||
}
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
commands := []string{
|
||||
"insert 1 user1 person1@example.com",
|
||||
"select",
|
||||
".exit",
|
||||
func TestE2E(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
commands []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "Test Main Loop",
|
||||
commands: []string{"insert 1 user1 person1@example.com", "select", ".exit"},
|
||||
expected: []string{"godb > Executed.", "godb > {1 user1 person1@example.com}", "Executed.", "godb > bye!"},
|
||||
},
|
||||
{
|
||||
name: "Test Maximum Length For Values",
|
||||
commands: []string{"insert 0 " + strings.Repeat("n", 32) + " " + strings.Repeat("n", 255), ".exit"},
|
||||
expected: []string{"godb > Executed.", "godb > bye!"},
|
||||
},
|
||||
{
|
||||
name: "Test Over Maximum Length For Values",
|
||||
commands: []string{"insert 0 " + strings.Repeat("n", 42) + " " + strings.Repeat("n", 275), ".exit"},
|
||||
expected: []string{"godb > Values for either username or email are too long", "godb > bye!"},
|
||||
},
|
||||
}
|
||||
|
||||
expected := []string{
|
||||
"godb > Executed.",
|
||||
"godb > {1 user1 person1@example.com}",
|
||||
"Executed.",
|
||||
"godb > bye!",
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
results := runner(test.commands, t)
|
||||
for i := range results {
|
||||
results[i] = strings.ReplaceAll(results[i], "\n", "")
|
||||
}
|
||||
|
||||
if !slices.Equal(test.expected, results) {
|
||||
t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", test.expected, results)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTableFull(t *testing.T) {
|
||||
maxRows := 1400
|
||||
commands := make([]string, 0)
|
||||
|
||||
for i := range maxRows {
|
||||
c := fmt.Sprintf("insert %d user%d person%d@example.com", i, i, i)
|
||||
commands = append(commands, c)
|
||||
}
|
||||
commands = append(commands, ".exit")
|
||||
|
||||
results := runner(commands, t)
|
||||
|
||||
for i := range results {
|
||||
results[i] = strings.ReplaceAll(results[i], "\n", "")
|
||||
}
|
||||
|
||||
if !slices.Equal(expected, results) {
|
||||
t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", expected, results)
|
||||
if results[1300] != "godb > Error: Table is full" {
|
||||
t.Errorf("Our Tables max capacity is 1300 rows, but we're able to insert %v\n", len(results))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user