Skip to content

Commit

Permalink
feat: bufferize api result until transaction commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Sep 13, 2022
1 parent 100f63c commit 5f95a9d
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 144 deletions.
32 changes: 16 additions & 16 deletions pkg/api/controllers/script_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/numary/ledger/pkg/core"
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
)

Expand Down Expand Up @@ -86,13 +86,13 @@ func TestPostScript(t *testing.T) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
rsp := internal.PostScript(t, api, tc.script, url.Values{})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
require.Equal(t, http.StatusOK, rsp.Result().StatusCode)

res := controllers.ScriptResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &res))
require.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &res))

res.Transaction = nil
assert.EqualValues(t, tc.expectedResponse, res)
require.EqualValues(t, tc.expectedResponse, res)
return nil
},
})
Expand Down Expand Up @@ -120,13 +120,13 @@ func TestPostScriptPreview(t *testing.T) {
Plain: script,
}, values)

assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
require.Equal(t, http.StatusOK, rsp.Result().StatusCode)
res := controllers.ScriptResponse{}
internal.Decode(t, rsp.Body, &res)

cursor, err := store.GetTransactions(ctx, *ledger.NewTransactionsQuery())
assert.NoError(t, err)
assert.Len(t, cursor.Data, 0)
require.NoError(t, err)
require.Len(t, cursor.Data, 0)
})

t.Run("false", func(t *testing.T) {
Expand All @@ -137,13 +137,13 @@ func TestPostScriptPreview(t *testing.T) {
Plain: script,
}, values)

assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
require.Equal(t, http.StatusOK, rsp.Result().StatusCode)
res := controllers.ScriptResponse{}
internal.Decode(t, rsp.Body, &res)

cursor, err := store.GetTransactions(ctx, *ledger.NewTransactionsQuery())
assert.NoError(t, err)
assert.Len(t, cursor.Data, 1)
require.NoError(t, err)
require.Len(t, cursor.Data, 1)
})

return nil
Expand All @@ -166,17 +166,17 @@ func TestPostScriptWithReference(t *testing.T) {
)`,
Reference: reference,
}, url.Values{})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
require.Equal(t, http.StatusOK, rsp.Result().StatusCode)

res := controllers.ScriptResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &res))
assert.Equal(t, reference, res.Transaction.Reference)
require.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &res))
require.Equal(t, reference, res.Transaction.Reference)

store := internal.GetLedgerStore(t, driver, ctx)
cursor, err := store.GetTransactions(ctx, *ledger.NewTransactionsQuery())
assert.NoError(t, err)
assert.Len(t, cursor.Data, 1)
assert.Equal(t, reference, cursor.Data[0].Reference)
require.NoError(t, err)
require.Len(t, cursor.Data, 1)
require.Equal(t, reference, cursor.Data[0].Reference)

return nil
},
Expand Down
Loading

0 comments on commit 5f95a9d

Please sign in to comment.