Skip to content

Commit

Permalink
Merge pull request #45 from hyperledger/patch/ut
Browse files Browse the repository at this point in the history
Add a FFTM DB migration command and update unit tests
  • Loading branch information
alex-semenyuk authored May 2, 2024
2 parents 01eb164 + 5c05575 commit 2f9f33a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 68 deletions.
8 changes: 8 additions & 0 deletions cmd/tezosconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func init() {
rootCmd.AddCommand(versionCommand())
rootCmd.AddCommand(configCommand())
rootCmd.AddCommand(fftmcmd.ClientCommand())
migrateCmd := fftmcmd.MigrateCommand(func() error {
InitConfig()
err := config.ReadConfig("tezosconnect", cfgFile)
config.SetupLogging(context.Background())
return err
})
migrateCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file")
rootCmd.AddCommand(migrateCmd)
}

func Execute() error {
Expand Down
10 changes: 10 additions & 0 deletions cmd/tezosconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ func TestRun(t *testing.T) {
})
}
}

func TestRunMigrationsBadConfig(t *testing.T) {
rootCmd.SetArgs([]string{
"migrate", "leveldb2postgres", "-f", "../test/fail-start.tezosconnect.yaml",
})
defer rootCmd.SetArgs([]string{})

err := Execute()
assert.Regexp(t, "FF21049", err)
}
104 changes: 38 additions & 66 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -1,77 +1,49 @@
package cmd

import (
"strconv"
"runtime/debug"
"testing"
"time"

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

func TestVersionCommand(t *testing.T) {
testCases := []struct {
name string
buildDate string
buildCommit string
buildVersionOverride string
output string
shortened bool
errMsg string
}{
{
name: "error",
buildDate: time.Now().String(),
buildCommit: "243413535",
buildVersionOverride: "0.0.1",
output: "unknown",
errMsg: "FF23016: Invalid output type: unknown",
},
{
name: "yaml output",
buildDate: time.Now().String(),
buildCommit: "243413535",
buildVersionOverride: "0.0.1",
output: "yaml",
},
{
name: "json output",
buildDate: time.Now().String(),
buildCommit: "243413535",
buildVersionOverride: "0.0.1",
output: "json",
},
{
name: "shortened",
buildDate: time.Now().String(),
buildCommit: "243413535",
buildVersionOverride: "0.0.1",
shortened: true,
},
{
name: "version is empty",
buildDate: time.Now().String(),
buildCommit: "243413535",
buildVersionOverride: "",
output: "json",
},
}
func TestVersionCmdDefault(t *testing.T) {
rootCmd.SetArgs([]string{"version"})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.NoError(t, err)
}

func TestVersionCmdYAML(t *testing.T) {
rootCmd.SetArgs([]string{"version", "-o", "yaml"})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.NoError(t, err)
}

func TestVersionCmdJSON(t *testing.T) {
rootCmd.SetArgs([]string{"version", "-o", "json"})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.NoError(t, err)
}

func TestVersionCmdInvalidType(t *testing.T) {
rootCmd.SetArgs([]string{"version", "-o", "wrong"})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.Regexp(t, "FF23016", err)
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
command := versionCommand()
BuildVersionOverride = tc.buildVersionOverride
BuildCommit = tc.buildCommit
BuildDate = tc.buildDate
command.Flags().Set("output", tc.output)
command.Flags().Set("short", strconv.FormatBool(tc.shortened))
err := command.RunE(command, []string{"arg1"})
func TestVersionCmdShorthand(t *testing.T) {
rootCmd.SetArgs([]string{"version", "-s"})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.NoError(t, err)
}

if tc.errMsg != "" {
assert.Error(t, err)
assert.Equal(t, tc.errMsg, err.Error())
} else {
assert.NoError(t, err)
}
})
}
func TestSetBuildInfoWithBI(t *testing.T) {
info := &Info{}
setBuildInfo(info, &debug.BuildInfo{Main: debug.Module{Version: "12345"}}, true)
assert.Equal(t, "12345", info.Version)
}
3 changes: 3 additions & 0 deletions test/fail-start.tezosconnect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
persistence:
leveldb:
path: "../test/ldb"
2 changes: 1 addition & 1 deletion test/firefly.tezosconnect-without-connector.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
persistence:
leveldb:
path: ./.leveldb
path: "../test/ldb"
2 changes: 1 addition & 1 deletion test/firefly.tezosconnect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ connector:
rpc: https://ghostnet.example.com
persistence:
leveldb:
path: ./.leveldb
path: "../test/ldb"

0 comments on commit 2f9f33a

Please sign in to comment.