Skip to content

Commit

Permalink
chore: Improve test coverage (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz authored Feb 21, 2023
1 parent 92e4deb commit ca3bc98
Show file tree
Hide file tree
Showing 19 changed files with 819 additions and 326 deletions.
4 changes: 4 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ tasks:
- golangci-lint run --fix {{if eq .VERBOSE "true"}}-v{{end}}

tests:
deps: [postgres]
cmds:
- >
go test {{.TAGS}} {{if eq .VERBOSE "true"}}-v{{end}} -coverpkg {{.PKG}}
-coverprofile coverage.out -covermode atomic {{.PKG}}
env:
NUMARY_STORAGE_DRIVER: "postgres"
NUMARY_STORAGE_POSTGRES_CONN_STRING: "postgresql://ledger:ledger@127.0.0.1/ledger"

tests:local:
cmds:
Expand Down
97 changes: 97 additions & 0 deletions cmd/script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"

"github.com/formancehq/stack/libs/go-libs/api"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/numary/ledger/pkg/api/controllers"
"github.com/numary/ledger/pkg/core"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

func Test_ScriptCommands(t *testing.T) {
ledger := uuid.NewString()
viper.Set("name", ledger)
require.NoError(t, NewStorageInit().Execute())

d1 := []byte(`
send [EUR 1] (
source = @world
destination = @alice
)`)
path := filepath.Join(os.TempDir(), "script")
require.NoError(t, os.WriteFile(path, d1, 0644))

httpServer := httptest.NewServer(http.HandlerFunc(scriptSuccessHandler))
defer func() {
httpServer.CloseClientConnections()
httpServer.Close()
}()

tests := map[string]struct {
args []string
flags map[string]any
want error
}{
"not enough args": {args: []string{path}, flags: map[string]any{}, want: errors.New("accepts 2 arg(s), received 1")},
"success": {args: []string{ledger, path}, flags: map[string]any{serverHttpBindAddressFlag: httpServer.URL[7:]}, want: nil},
"preview": {args: []string{ledger, path}, flags: map[string]any{previewFlag: true}, want: nil},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
for i, f := range tc.flags {
viper.Set(i, f)
}
cmd := NewScriptExec()
cmd.SetArgs(tc.args)
got := cmd.Execute()
if tc.want != nil {
if got == nil {
t.Fatalf("an error is expected, but got nil")
}
diff := cmp.Diff(tc.want.Error(), got.Error())
if diff != "" {
t.Fatalf(diff)
}
}
})
}
}

func scriptSuccessHandler(w http.ResponseWriter, _ *http.Request) {
resp := controllers.ScriptResponse{
ErrorResponse: api.ErrorResponse{},
Transaction: &core.ExpandedTransaction{
Transaction: core.Transaction{
TransactionData: core.TransactionData{
Postings: core.Postings{
{
Source: "world",
Destination: "alice",
Amount: core.NewMonetaryInt(1),
Asset: "EUR",
},
},
Timestamp: time.Now(),
},
},
PreCommitVolumes: nil,
PostCommitVolumes: nil,
},
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
fmt.Printf("ERR:%s\n", err)
}
}
38 changes: 38 additions & 0 deletions cmd/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"testing"

"github.com/google/uuid"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

func Test_StorageCommands(t *testing.T) {
require.NoError(t, NewStorageList().Execute())

viper.Set("name", "")
require.Error(t, NewStorageInit().Execute())

name := uuid.NewString()
viper.Set("name", name)
require.NoError(t, NewStorageInit().Execute())
require.NoError(t, NewStorageInit().Execute())

cmd := NewStorageUpgrade()
cmd.SetArgs([]string{name})
require.NoError(t, cmd.Execute())

cmd = NewStorageDelete()
cmd.SetArgs([]string{name})
require.NoError(t, cmd.Execute())

driver := viper.GetString(storageDriverFlag)
require.NoError(t, NewStorageScan().Execute())

viper.Set(storageDriverFlag, "")
require.ErrorContains(t, NewStorageScan().Execute(),
"Invalid storage driver:")

viper.Set(storageDriverFlag, driver)
}
4 changes: 0 additions & 4 deletions codecov.yml

This file was deleted.

18 changes: 11 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/Shopify/sarama v1.38.1 // indirect
github.com/ThreeDotsLabs/watermill-http v1.1.4 // indirect
Expand Down Expand Up @@ -94,7 +94,7 @@ require (
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
Expand All @@ -106,14 +106,15 @@ require (
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lithammer/shortuuid/v3 v3.0.7 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nats-io/nats.go v1.23.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/gomega v1.24.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
Expand All @@ -122,6 +123,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/segmentio/backo-go v1.0.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down Expand Up @@ -151,11 +153,13 @@ require (
go.uber.org/dig v1.16.1 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect
google.golang.org/grpc v1.52.3 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading

0 comments on commit ca3bc98

Please sign in to comment.