-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antoine Gelloz
authored
Feb 21, 2023
1 parent
92e4deb
commit ca3bc98
Showing
19 changed files
with
819 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.