From 1fffe1d6920e4d7c34e3ae0f70ec058c68379813 Mon Sep 17 00:00:00 2001 From: antoinegelloz Date: Thu, 9 Jun 2022 19:19:25 +0200 Subject: [PATCH] simplify controllers tests with generics --- .../controllers/account_controller_test.go | 64 ++++++--------- .../transaction_controller_test.go | 80 +++++++------------ 2 files changed, 53 insertions(+), 91 deletions(-) diff --git a/pkg/api/controllers/account_controller_test.go b/pkg/api/controllers/account_controller_test.go index cda52d946..73fc271d2 100644 --- a/pkg/api/controllers/account_controller_test.go +++ b/pkg/api/controllers/account_controller_test.go @@ -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{ @@ -69,13 +58,12 @@ 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) { @@ -83,11 +71,10 @@ func TestGetAccounts(t *testing.T) { "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) { @@ -95,11 +82,10 @@ func TestGetAccounts(t *testing.T) { "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) { @@ -107,11 +93,10 @@ func TestGetAccounts(t *testing.T) { "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) { @@ -119,11 +104,10 @@ func TestGetAccounts(t *testing.T) { "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) { @@ -140,11 +124,10 @@ 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) { @@ -152,11 +135,10 @@ func TestGetAccounts(t *testing.T) { "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 diff --git a/pkg/api/controllers/transaction_controller_test.go b/pkg/api/controllers/transaction_controller_test.go index 42961d6d7..6272aa019 100644 --- a/pkg/api/controllers/transaction_controller_test.go +++ b/pkg/api/controllers/transaction_controller_test.go @@ -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{ @@ -328,16 +317,15 @@ 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) { @@ -345,11 +333,10 @@ func TestGetTransactions(t *testing.T) { "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) { @@ -357,11 +344,10 @@ func TestGetTransactions(t *testing.T) { "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) { @@ -369,11 +355,10 @@ func TestGetTransactions(t *testing.T) { "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) { @@ -381,12 +366,11 @@ func TestGetTransactions(t *testing.T) { "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) { @@ -394,12 +378,11 @@ func TestGetTransactions(t *testing.T) { "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) { @@ -408,10 +391,9 @@ 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) { @@ -419,10 +401,9 @@ func TestGetTransactions(t *testing.T) { "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) { @@ -430,10 +411,9 @@ func TestGetTransactions(t *testing.T) { "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) {