Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for new --ledgerbackend flag
Browse files Browse the repository at this point in the history
urvisavla committed Jul 10, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent fb14114 commit 533fe37
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions services/horizon/cmd/db_test.go
Original file line number Diff line number Diff line change
@@ -3,12 +3,14 @@ package cmd
import (
"testing"

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

horizon "github.com/stellar/go/services/horizon/internal"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ingest"
"github.com/stellar/go/support/db/dbtest"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

func TestDBCommandsTestSuite(t *testing.T) {
@@ -96,3 +98,75 @@ func (s *DBCommandsTestSuite) TestUsesParallelJobSizeWhenSetForBuffered() {
require.NoError(s.T(), RootCmd.Execute())
require.Equal(s.T(), parallelJobSize, uint32(5))
}

func (s *DBCommandsTestSuite) TestDbReingestAndFillGapsCmds() {
tests := []struct {
name string
args []string
ledgerBackend ingest.LedgerBackendType
expectError bool
errorMessage string
}{
{
name: "default ledgerbackend",
args: []string{"1", "100"},
expectError: false,
},
{
name: "captive-core ledgerbackend",
args: []string{"1", "100", "--ledgerbackend", "captive-core"},
expectError: false,
},
{
name: "invalid ledgerbackend",
args: []string{"1", "100", "--ledgerbackend", "unknown"},
expectError: true,
errorMessage: "invalid ledger backend: unknown, must be 'captive-core' or 'datastore'",
},
{
name: "datastore ledgerbackend without config",
args: []string{"1", "100", "--ledgerbackend", "datastore"},
expectError: true,
errorMessage: "datastore config file is required for datastore backend type",
},
{
name: "datastore ledgerbackend missing config file",
args: []string{"1", "100", "--ledgerbackend", "datastore", "--datastore-config", "invalid.config.toml"},
expectError: true,
errorMessage: "failed to load config file",
},
{
name: "datastore ledgerbackend",
args: []string{"1", "100", "--ledgerbackend", "datastore", "--datastore-config", "config.test.toml"},
expectError: false,
},
}

commands := []struct {
cmd []string
name string
}{
{[]string{"db", "reingest", "range"}, "TestDbReingestRangeCmd"},
{[]string{"db", "fill-gaps"}, "TestDbFillGapsCmd"},
}

for _, command := range commands {
for _, tt := range tests {
s.T().Run(tt.name+"_"+command.name, func(t *testing.T) {
args := append(command.cmd, tt.args...)
RootCmd.SetArgs(append([]string{
"--db-url", s.dsn,
"--network", "testnet",
}, args...))

if tt.expectError {
err := RootCmd.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMessage)
} else {
require.NoError(t, RootCmd.Execute())
}
})
}
}
}

0 comments on commit 533fe37

Please sign in to comment.