Skip to content

Commit

Permalink
simplify controllers tests with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinegelloz committed Jun 9, 2022
1 parent b88103c commit 1fffe1d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 91 deletions.
64 changes: 23 additions & 41 deletions pkg/api/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ import (
"go.uber.org/fx"
)

type GetAccountsCursor struct {
PageSize int `json:"page_size,omitempty"`
Previous string `json:"previous,omitempty"`
Next string `json:"next,omitempty"`
Data []core.Account `json:"data"`
}

type getAccountsResponse struct {
Cursor *GetAccountsCursor `json:"cursor,omitempty"`
}

func TestGetAccounts(t *testing.T) {
internal.RunTest(t, fx.Invoke(func(lc fx.Lifecycle, api *api.API) {
lc.Append(fx.Hook{
Expand Down Expand Up @@ -69,61 +58,56 @@ func TestGetAccounts(t *testing.T) {
t.Run("all", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 3 accounts: world, bob, alice
assert.Len(t, resp.Cursor.Data, 3)
assert.Equal(t, resp.Cursor.Data[0].Address, "world")
assert.Equal(t, resp.Cursor.Data[1].Address, "bob")
assert.Equal(t, resp.Cursor.Data[2].Address, "alice")
assert.Len(t, cursor.Data, 3)
assert.Equal(t, cursor.Data[0].Address, "world")
assert.Equal(t, cursor.Data[1].Address, "bob")
assert.Equal(t, cursor.Data[2].Address, "alice")
})

t.Run("meta roles", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[roles]": []string{"admin"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta accountId", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[accountId]": []string{"3"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta enabled", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[enabled]": []string{"true"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta nested", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[a.nested.key]": []string{"hello"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta unknown", func(t *testing.T) {
Expand All @@ -140,23 +124,21 @@ func TestGetAccounts(t *testing.T) {
"after": []string{"bob"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: alice
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "alice")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "alice")
})

t.Run("address", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"address": []string{"b.b"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

return nil
Expand Down
80 changes: 30 additions & 50 deletions pkg/api/controllers/transaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,6 @@ func TestNotFoundTransaction(t *testing.T) {
}))
}

type GetTransactionsCursor struct {
PageSize int `json:"page_size,omitempty"`
Previous string `json:"previous,omitempty"`
Next string `json:"next,omitempty"`
Data []core.Transaction `json:"data"`
}

type getTransactionsResponse struct {
Cursor *GetTransactionsCursor `json:"cursor,omitempty"`
}

func TestGetTransactions(t *testing.T) {
internal.RunTest(t, fx.Invoke(func(lc fx.Lifecycle, api *api.API, driver storage.Driver) {
lc.Append(fx.Hook{
Expand Down Expand Up @@ -328,78 +317,72 @@ func TestGetTransactions(t *testing.T) {
t.Run("all", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// all transactions
assert.Len(t, resp.Cursor.Data, 3)
assert.Equal(t, resp.Cursor.Data[0].ID, uint64(2))
assert.Equal(t, resp.Cursor.Data[1].ID, uint64(1))
assert.Equal(t, resp.Cursor.Data[2].ID, uint64(0))
assert.Len(t, cursor.Data, 3)
assert.Equal(t, cursor.Data[0].ID, uint64(2))
assert.Equal(t, cursor.Data[1].ID, uint64(1))
assert.Equal(t, cursor.Data[2].ID, uint64(0))

tx1Timestamp = resp.Cursor.Data[1].Timestamp
tx2Timestamp = resp.Cursor.Data[0].Timestamp
tx1Timestamp = cursor.Data[1].Timestamp
tx2Timestamp = cursor.Data[0].Timestamp
})

t.Run("after", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{
"after": []string{"1"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// 1 transaction: txid 0
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].ID, uint64(0))
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].ID, uint64(0))
})

t.Run("reference", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{
"reference": []string{"ref:001"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// 1 transaction: txid 0
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].ID, uint64(0))
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].ID, uint64(0))
})

t.Run("destination", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{
"destination": []string{"central_bank1"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// 1 transaction: txid 0
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].ID, uint64(0))
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].ID, uint64(0))
})

t.Run("source", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{
"source": []string{"world"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// 2 transactions: txid 0 and txid 1
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
assert.Len(t, resp.Cursor.Data, 2)
assert.Equal(t, resp.Cursor.Data[0].ID, uint64(1))
assert.Equal(t, resp.Cursor.Data[1].ID, uint64(0))
assert.Len(t, cursor.Data, 2)
assert.Equal(t, cursor.Data[0].ID, uint64(1))
assert.Equal(t, cursor.Data[1].ID, uint64(0))
})

t.Run("account", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{
"account": []string{"world"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// 2 transactions: txid 0 and txid 1
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
assert.Len(t, resp.Cursor.Data, 2)
assert.Equal(t, resp.Cursor.Data[0].ID, uint64(1))
assert.Equal(t, resp.Cursor.Data[1].ID, uint64(0))
assert.Len(t, cursor.Data, 2)
assert.Equal(t, cursor.Data[0].ID, uint64(1))
assert.Equal(t, cursor.Data[1].ID, uint64(0))
})

t.Run("time range", func(t *testing.T) {
Expand All @@ -408,32 +391,29 @@ func TestGetTransactions(t *testing.T) {
"end_time": []string{tx2Timestamp},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// 1 transaction: txid 1
assert.Len(t, resp.Cursor.Data, 1)
assert.Len(t, cursor.Data, 1)
})

t.Run("only start time", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{
"start_time": []string{time.Now().Add(time.Second).Format(time.RFC3339)},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// no transaction
assert.Len(t, resp.Cursor.Data, 0)
assert.Len(t, cursor.Data, 0)
})

t.Run("only end time", func(t *testing.T) {
rsp = internal.GetTransactions(api, url.Values{
"end_time": []string{time.Now().Add(time.Second).Format(time.RFC3339)},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getTransactionsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Transaction](t, rsp.Body)
// all transactions
assert.Len(t, resp.Cursor.Data, 3)
assert.Len(t, cursor.Data, 3)
})

t.Run("invalid start time", func(t *testing.T) {
Expand Down

0 comments on commit 1fffe1d

Please sign in to comment.