Skip to content

Commit 78461ae

Browse files
Luis Silvaluisfvieirasilva
authored andcommitted
change(test): make tests faster by using internal/shell and keeping same db connection over entire suite
1 parent 656b385 commit 78461ae

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

test/root_command_exec_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func (s *RootCommandExecSuite) SetupSuite() {
2626
s.tc.DropAllTables()
2727
}
2828

29+
func (s *RootCommandExecSuite) TearDownSuite() {
30+
s.tc.Close()
31+
}
32+
2933
func (s *RootCommandExecSuite) TearDownTest() {
3034
s.tc.DropAllTables()
3135
}

test/root_command_flags_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestRootCommandFlags_WhenAllFlagsAreProvided_ExpectSQLStatementsExecutedWit
1515
dbPath := c.TempDir() + `\test.sqlite`
1616
rootCmd := cmd.NewRootCmd()
1717

18-
_, _, err := utils.Execute(t, rootCmd, "--exec", "CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT);", dbPath)
18+
_, _, err := utils.ExecuteCobraCommand(t, rootCmd, "--exec", "CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT);", dbPath)
1919

2020
c.Assert(err, qt.IsNil)
2121
}
@@ -25,16 +25,18 @@ func TestRootCommandFlags_WhenDbIsMissing_ExpectErrorReturned(t *testing.T) {
2525

2626
rootCmd := cmd.NewRootCmd()
2727

28-
_, _, err := utils.Execute(t, rootCmd, "--exec", "CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT);")
28+
_, _, err := utils.ExecuteCobraCommand(t, rootCmd, "--exec", "CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT);")
2929

3030
c.Assert(err.Error(), qt.Equals, `accepts 1 arg(s), received 0`)
3131
}
3232

3333
func TestRootCommandFlags_GivenEmptyStatements_ExpectErrorReturned(t *testing.T) {
34-
dbPath := t.TempDir() + `\test.sqlite`
35-
tc := utils.NewTestContext(t, dbPath)
34+
c := qt.New(t)
35+
36+
dbPath := c.TempDir() + `\test.sqlite`
37+
rootCmd := cmd.NewRootCmd()
3638

37-
_, _, err := tc.Execute("")
39+
_, _, err := utils.ExecuteCobraCommand(t, rootCmd, "--exec", "", dbPath)
3840

39-
tc.Assert(err.Error(), qt.IsNotNil)
41+
c.Assert(err.Error(), qt.IsNotNil)
4042
}

test/root_command_shell_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func (s *RootCommandShellSuite) SetupSuite() {
2525
s.tc.DropAllTables()
2626
}
2727

28+
func (s *RootCommandShellSuite) TearDownSuite() {
29+
s.tc.Close()
30+
}
31+
2832
func (s *RootCommandShellSuite) TearDownTest() {
2933
s.tc.DropAllTables()
3034
}

test/utils/db_test_context.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,75 @@
11
package utils
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
67
"strings"
78
"testing"
89

910
qt "github.com/frankban/quicktest"
1011

11-
"github.com/libsql/libsql-shell-go/internal/cmd"
12+
"github.com/libsql/libsql-shell-go/internal/db"
13+
"github.com/libsql/libsql-shell-go/internal/shell"
14+
"github.com/libsql/libsql-shell-go/pkg/shell/enums"
1215
)
1316

1417
type DbTestContext struct {
1518
*testing.T
1619
*qt.C
1720

1821
dbPath string
22+
23+
db *db.Db
1924
}
2025

2126
func NewTestContext(t *testing.T, dbPath string) *DbTestContext {
22-
return &DbTestContext{T: t, C: qt.New(t), dbPath: dbPath}
27+
db, err := db.NewDb(dbPath)
28+
if err != nil {
29+
t.Fatalf("Fail to create new db")
30+
}
31+
32+
return &DbTestContext{T: t, C: qt.New(t), dbPath: dbPath, db: db}
33+
}
34+
35+
func (tc *DbTestContext) Close() {
36+
tc.db.Close()
2337
}
2438

2539
func (tc *DbTestContext) Execute(statements string) (string, string, error) {
26-
rootCmd := cmd.NewRootCmd()
27-
return Execute(tc.T, rootCmd, "--exec", statements, tc.dbPath)
40+
bufOut, bufErr, config := tc.createShellConfig("")
41+
shellInstance, err := shell.NewShell(config, tc.db)
42+
if err != nil {
43+
tc.T.Fatalf("Fail to create new shell")
44+
}
45+
46+
executionError := shellInstance.ExecuteCommandOrStatements(statements)
47+
return strings.TrimSpace(bufOut.String()), strings.TrimSpace(bufErr.String()), executionError
2848
}
2949

3050
func (tc *DbTestContext) ExecuteShell(commands []string) (outS string, errS string, err error) {
31-
rootCmd := cmd.NewRootCmd()
32-
return ExecuteWithInitialInput(tc.T, rootCmd, strings.Join(commands, "\n"), tc.dbPath, "--quiet")
51+
bufOut, bufErr, config := tc.createShellConfig(strings.Join(commands, "\n"))
52+
shellInstance, err := shell.NewShell(config, tc.db)
53+
if err != nil {
54+
tc.T.Fatalf("Fail to create new shell")
55+
}
56+
57+
executionError := shellInstance.Run()
58+
return strings.TrimSpace(bufOut.String()), strings.TrimSpace(bufErr.String()), executionError
59+
}
60+
61+
func (tc *DbTestContext) createShellConfig(initialInput string) (bufOut *bytes.Buffer, bufErr *bytes.Buffer, config shell.ShellConfig) {
62+
bufOut = new(bytes.Buffer)
63+
bufErr = new(bytes.Buffer)
64+
bufIn := new(bytes.Buffer)
65+
66+
_, err := bufIn.Write([]byte(initialInput))
67+
if err != nil {
68+
tc.T.Fatalf("Fail to write inside initial buffer")
69+
}
70+
71+
config = shell.ShellConfig{InF: bufIn, OutF: bufOut, ErrF: bufErr, HistoryMode: enums.SingleHistory, QuietMode: true}
72+
return
3373
}
3474

3575
func (tc *DbTestContext) CreateEmptySimpleTable(tableName string) {

test/utils/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12-
func Execute(t *testing.T, c *cobra.Command, args ...string) (string, string, error) {
13-
return ExecuteWithInitialInput(t, c, "", args...)
12+
func ExecuteCobraCommand(t *testing.T, c *cobra.Command, args ...string) (string, string, error) {
13+
return ExecuteCobraCommandWithInitialInput(t, c, "", args...)
1414
}
1515

16-
func ExecuteWithInitialInput(t *testing.T, c *cobra.Command, initialInput string, args ...string) (string, string, error) {
16+
func ExecuteCobraCommandWithInitialInput(t *testing.T, c *cobra.Command, initialInput string, args ...string) (string, string, error) {
1717
t.Helper()
1818

1919
bufOut := new(bytes.Buffer)

0 commit comments

Comments
 (0)