Skip to content

Commit

Permalink
more unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Dec 17, 2024
1 parent 4ec3355 commit 007865b
Show file tree
Hide file tree
Showing 34 changed files with 2,732 additions and 274 deletions.
77 changes: 77 additions & 0 deletions app/shared/display/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,80 @@ func Example_wrappedMsg_json_withError() {
// "error": "an error"
// }
}

func TestOutputFormat_String(t *testing.T) {
tests := []struct {
name string
format OutputFormat
expected string
}{
{
name: "text format",
format: outputFormatText,
expected: "text",
},
{
name: "json format",
format: outputFormatJSON,
expected: "json",
},
{
name: "empty format",
format: "",
expected: "",
},
{
name: "invalid format",
format: "invalid",
expected: "invalid",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.format.string()
assert.Equal(t, tt.expected, result)
})
}
}

func TestOutputFormat_Valid(t *testing.T) {
tests := []struct {
name string
format OutputFormat
expected bool
}{
{
name: "text format",
format: outputFormatText,
expected: true,
},
{
name: "json format",
format: outputFormatJSON,
expected: true,
},
{
name: "empty format",
format: "",
expected: false,
},
{
name: "invalid format",
format: "invalid",
expected: false,
},
{
name: "case sensitive format",
format: "TEXT",
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.format.valid()
assert.Equal(t, tt.expected, result)
})
}
}
2 changes: 1 addition & 1 deletion app/shared/display/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (h RespTxHash) MarshalText() ([]byte, error) {
type RespString string

func (s RespString) MarshalJSON() ([]byte, error) {
return json.Marshal(string(s)) // must convert to string to avoid infinite recursion
return []byte(`"` + string(s) + `"`), nil
}

func (s RespString) MarshalText() ([]byte, error) {
Expand Down
92 changes: 92 additions & 0 deletions app/shared/display/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/kwilteam/kwil-db/core/types"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// NOTE: could do this for all the other tests,
Expand Down Expand Up @@ -233,3 +234,94 @@ func Test_TxHashAndExecResponse(t *testing.T) {
assert.NoError(t, err, "MarshalJSON should not return error")
assert.Equal(t, expectJSON, string(outJSON), "MarshalJSON should return expected json")
}

func TestRespTxQuery_MarshalText(t *testing.T) {
tests := []struct {
name string
input *RespTxQuery
expected string
}{
{
name: "success status",
input: &RespTxQuery{
Msg: &types.TxQueryResponse{
Hash: types.Hash{0x1}, // simple hash for testing
Height: 100,
Result: &types.TxResult{
Code: uint32(types.CodeOk),
Log: "transaction successful",
},
},
},
expected: "Transaction ID: 0100000000000000000000000000000000000000000000000000000000000000\nStatus: success\nHeight: 100\nLog: transaction successful",
},
{
name: "failed status",
input: &RespTxQuery{
Msg: &types.TxQueryResponse{
Hash: types.Hash{0x2}, // different hash
Height: 50,
Result: &types.TxResult{
Code: 1, // non-zero code means failure
Log: "transaction failed",
},
},
},
expected: "Transaction ID: 0200000000000000000000000000000000000000000000000000000000000000\nStatus: failed\nHeight: 50\nLog: transaction failed",
},
{
name: "pending status",
input: &RespTxQuery{
Msg: &types.TxQueryResponse{
Hash: types.Hash{0x3},
Height: -1, // -1 height indicates pending
Result: &types.TxResult{
Code: 0,
Log: "transaction pending",
},
},
},
expected: "Transaction ID: 0300000000000000000000000000000000000000000000000000000000000000\nStatus: pending\nHeight: -1\nLog: transaction pending",
},
{
name: "pending status",
input: &RespTxQuery{
Msg: &types.TxQueryResponse{
Hash: mustUnmarshalHash("ff42fabfe6e73e0c566cb2462cc0bf69de0e050c7a90fa9d5a708ace51243589"),
Height: -1, // -1 height indicates pending
Tx: &types.Transaction{
Body: &types.TransactionBody{
PayloadType: types.PayloadTypeExecute,
Fee: big.NewInt(100),
Nonce: 10,
ChainID: "asdf",
Description: "This is a test transaction",
},
Serialization: types.SignedMsgConcat,
},
Result: &types.TxResult{
Code: 0,
Log: "transaction pending",
},
},
},
expected: "Transaction ID: ff42fabfe6e73e0c566cb2462cc0bf69de0e050c7a90fa9d5a708ace51243589\nStatus: pending\nHeight: -1\nLog: transaction pending",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := tt.input.MarshalText()
require.NoError(t, err)
require.Equal(t, tt.expected, string(result))
})
}
}

func mustUnmarshalHash(s string) types.Hash {
h, err := types.NewHashFromString(s)
if err != nil {
panic(err)
}
return h
}
28 changes: 10 additions & 18 deletions app/shared/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func writeCmd(cmd *cobra.Command, dir string, idx int, write writeFunc, first bo
func createIndexFile(cmd *cobra.Command, dir string, write writeFunc, first bool) error {
dir = dir + "/" + cmd.Name()

header := docusaursHeader{
header := docusaurusHeader{
sidebar_position: 99, // we want these to always be at the bottom of the sidebar
sidebar_label: cmd.Name(),
id: strings.ReplaceAll(cmd.CommandPath(), " ", "-"),
Expand All @@ -67,26 +67,23 @@ func createIndexFile(cmd *cobra.Command, dir string, write writeFunc, first bool
if err != nil {
return err
}
defer file.Close()

file.WriteString(header.String() + "\n\n")

err = doc.GenMarkdownCustom(cmd, file, linkHandler(dir))
if err != nil {
return err
}

return nil
return doc.GenMarkdownCustom(cmd, file, linkHandler(dir))
}

// createCmdFile creates a file for the command, and writes the command's documentation to it.
// it does not call subcommands.
func createCmdFile(cmd *cobra.Command, dir string, idx int, write writeFunc) error {
file, err := write(dir + "/" + cmd.Name() + ".mdx")
file, err := write(filepath.Join(dir, cmd.Name()+".mdx"))
if err != nil {
return err
}
defer file.Close()

header := docusaursHeader{
header := docusaurusHeader{
sidebar_position: idx,
sidebar_label: cmd.Name(),
id: strings.ReplaceAll(cmd.CommandPath(), " ", "-"),
Expand All @@ -97,15 +94,10 @@ func createCmdFile(cmd *cobra.Command, dir string, idx int, write writeFunc) err

file.WriteString(header.String() + "\n\n")

err = doc.GenMarkdownCustom(cmd, file, linkHandler(dir))
if err != nil {
return err
}

return nil
return doc.GenMarkdownCustom(cmd, file, linkHandler(dir))
}

func linkHandler(dir string) func(string) string {
func linkHandler(dir string) func(string) string { // dir string unused -- what is it?
return func(s string) string {
// trying just linking ids??
s = strings.TrimSuffix(s, ".md")
Expand All @@ -120,7 +112,7 @@ func getSlug(cmd *cobra.Command) string {
}

// docusaurusHeader is a page header for a doc page in a docusaurus site.
type docusaursHeader struct {
type docusaurusHeader struct {
sidebar_position int
sidebar_label string
id string
Expand All @@ -130,7 +122,7 @@ type docusaursHeader struct {
}

// String returns the header as a string.
func (d *docusaursHeader) String() string {
func (d *docusaurusHeader) String() string {
return fmt.Sprintf(headerString, d.sidebar_position, d.sidebar_label, d.id, d.title, d.description, d.slug)
}

Expand Down
54 changes: 54 additions & 0 deletions app/shared/version/version_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package version

import (
"testing"

"github.com/kwilteam/kwil-db/app/shared/display"
)

Expand Down Expand Up @@ -53,3 +55,55 @@ func Example_versionInfo_json() {
// "error": ""
// }
}

func Test_versionInfo_marshalJSON(t *testing.T) {
info := &respVersionInfo{
Info: &versionInfo{
Version: "1.0.0",
GitCommit: "abcdef12",
BuildTime: "2023-01-01T12:00:00Z",
APIVersion: "2.0.0",
GoVersion: "1.20",
Os: "linux",
Arch: "amd64",
},
}

data, err := info.MarshalJSON()
if err != nil {
t.Fatal(err)
}
wantJSON := `{"version":"1.0.0","git_commit":"abcdef12","build_time":"2023-01-01T12:00:00Z","api_version":"2.0.0","go_version":"1.20","os":"linux","arch":"amd64"}`
if string(data) != wantJSON {
t.Errorf("got %q, want %q", string(data), wantJSON)
}
}

func Test_versionInfo_marshalText(t *testing.T) {
info := &respVersionInfo{
Info: &versionInfo{
Version: "1.0.0",
GitCommit: "abcdef12",
BuildTime: "2023-01-01T12:00:00Z",
APIVersion: "2.0.0",
GoVersion: "1.20",
Os: "linux",
Arch: "amd64",
},
}

data, err := info.MarshalText()
if err != nil {
t.Fatal(err)
}
wantText := `
Version: 1.0.0
Git commit: abcdef12
Built: 2023-01-01T12:00:00Z
API version: 2.0.0
Go version: 1.20
OS/Arch: linux/amd64`
if string(data) != wantText {
t.Errorf("got %q, want %q", string(data), wantText)
}
}
Loading

0 comments on commit 007865b

Please sign in to comment.