Skip to content

Commit

Permalink
test: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Nov 26, 2024
1 parent 18a5347 commit 9003e80
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 18 deletions.
1 change: 1 addition & 0 deletions internal/api/bulking/bulker.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (b *Bulker) processElement(ctx context.Context, ctrl ledgercontroller.Contr
return nil, 0, err
}

// todo(next api version): no reason to return only the transaction...
return createTransactionResult.Transaction, log.ID, nil
case ActionAddMetadata:
req := data.Data.(AddMetadataRequest)
Expand Down
24 changes: 23 additions & 1 deletion internal/api/bulking/bulker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestBulk(t *testing.T) {
}},
},
{
name: "delete metadata",
name: "delete metadata on transaction",
bulk: []BulkElement{{
Action: ActionDeleteMetadata,
Data: DeleteMetadataRequest{
Expand All @@ -177,6 +177,28 @@ func TestBulk(t *testing.T) {
},
expectResults: []BulkElementResult{{}},
},
{
name: "delete metadata on account",
bulk: []BulkElement{{
Action: ActionDeleteMetadata,
Data: DeleteMetadataRequest{
TargetID: json.RawMessage(`"world"`),
TargetType: "ACCOUNT",
Key: "foo",
},
}},
expectations: func(mockLedger *LedgerController) {
mockLedger.EXPECT().
DeleteAccountMetadata(gomock.Any(), ledgercontroller.Parameters[ledgercontroller.DeleteAccountMetadata]{
Input: ledgercontroller.DeleteAccountMetadata{
Address: "world",
Key: "foo",
},
}).
Return(&ledger.Log{}, nil)
},
expectResults: []BulkElementResult{{}},
},
{
name: "error in the middle",
bulk: []BulkElement{{
Expand Down
6 changes: 5 additions & 1 deletion internal/api/bulking/text_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func ParseTextStream(scanner *bufio.Scanner) (*BulkElement, error) {
case text == "":
case strings.HasPrefix(text, "//script"):
bulkElement := BulkElement{}
text = strings.TrimSuffix(text, "//script")
bulkElement.Action = ActionCreateTransaction
text = strings.TrimPrefix(text, "//script")
text = strings.TrimSpace(text)

if len(text) > 0 {
Expand All @@ -28,6 +29,9 @@ func ParseTextStream(scanner *bufio.Scanner) (*BulkElement, error) {
parts2 := strings.Split(part, "=")
switch {
case parts2[0] == "ik":
if bulkElement.IdempotencyKey != "" {
return nil, errors.New("invalid header, idempotency key already set")
}
bulkElement.IdempotencyKey = parts2[1]
default:
return nil, errors.New("invalid header, key '" + parts2[0] + "' not recognized")
Expand Down
126 changes: 110 additions & 16 deletions internal/api/bulking/text_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bulking
import (
"bufio"
"bytes"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/stretchr/testify/require"
"testing"
)
Expand All @@ -11,39 +12,80 @@ func TestParseStream(t *testing.T) {
t.Parallel()

type testCase struct {
name string
stream string
expectedError bool
expectedCount int
name string
stream string
expectedError bool
expectedElements []BulkElement
}

for _, testCase := range []testCase{
{
name: "nominal",
expectedCount: 1,
name: "nominal",
expectedElements: []BulkElement{
{
Action: ActionCreateTransaction,
Data: TransactionRequest{
Script: ledgercontroller.ScriptV1{
Script: ledgercontroller.Script{
Plain: `send [USD 100] (
source = @world
destination = @alice
)`,
},
},
},
},
},
stream: `
//script
send [USD 100] (
source = @world
destination = @alice
}
)
//end`,
},
{
name: "multiple scripts",
expectedCount: 2,
name: "multiple scripts",
expectedElements: []BulkElement{
{
Action: ActionCreateTransaction,
Data: TransactionRequest{
Script: ledgercontroller.ScriptV1{
Script: ledgercontroller.Script{
Plain: `send [USD 100] (
source = @world
destination = @alice
)`,
},
},
},
},
{
Action: ActionCreateTransaction,
Data: TransactionRequest{
Script: ledgercontroller.ScriptV1{
Script: ledgercontroller.Script{
Plain: `send [USD 100] (
source = @world
destination = @bob
)`,
},
},
},
},
},
stream: `
//script
send [USD 100] (
source = @world
destination = @alice
}
)
//end
//script
send [USD 100] (
source = @world
destination = @bob
}
)
//end`,
},
{
Expand All @@ -52,18 +94,69 @@ send [USD 100] (
send [USD 100] (
source = @world
destination = @alice
}`,
)`,
expectedError: true,
},
{
name: "no ending tag",
expectedCount: 1,
name: "no ending tag",
expectedElements: []BulkElement{
{
Action: ActionCreateTransaction,
Data: TransactionRequest{
Script: ledgercontroller.ScriptV1{
Script: ledgercontroller.Script{
Plain: `send [USD 100] (
source = @world
destination = @alice
)`,
},
},
},
},
},
stream: `
//script
send [USD 100] (
source = @world
destination = @alice
}`,
)`,
},
{
name: "script with ik",
expectedElements: []BulkElement{
{
Action: ActionCreateTransaction,
IdempotencyKey: "foo",
Data: TransactionRequest{
Script: ledgercontroller.ScriptV1{
Script: ledgercontroller.Script{
Plain: `send [USD 100] (
source = @world
destination = @alice
)`,
},
},
},
},
},
stream: `
//script ik=foo
send [USD 100] (
source = @world
destination = @alice
)
//end`,
},
{
name: "script with ik specified twice",
expectedError: true,
stream: `
//script ik=foo,ik=bar
send [USD 100] (
source = @world
destination = @alice
)
//end`,
},
} {
t.Run(testCase.name, func(t *testing.T) {
Expand All @@ -75,10 +168,11 @@ send [USD 100] (
return
} else {
scanner := bufio.NewScanner(bytes.NewBufferString(testCase.stream))
for range testCase.expectedCount {
for _, element := range testCase.expectedElements {
ret, err := ParseTextStream(scanner)
require.NoError(t, err)
require.NotNil(t, ret)
require.Equal(t, element, *ret)
}
}
})
Expand Down

0 comments on commit 9003e80

Please sign in to comment.