Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a FFTM DB migration command and update unit tests #45

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading