Skip to content

Commit

Permalink
Add proper assertions for list function
Browse files Browse the repository at this point in the history
  • Loading branch information
omarmosid committed Dec 28, 2023
1 parent 0e64929 commit 211aa11
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
28 changes: 14 additions & 14 deletions zaraz.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,21 @@ type UpdateZarazWorkflowParams = string

type PublishZarazConfigParams = string

type ZarazHistoryRow struct {
ID int64 `json:"id,omitempty"`
UserId string `json:"usedId,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt *time.Time `json:"createdAt,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
type ZarazHistoryRecord struct {
ID int64 `json:"id,omitempty"`
UserId string `json:"userId,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}

type GetZarazConfigHistoryListResponse struct {
Result []ZarazHistoryRow `json:"result"`
type ZarazConfigHistoryListResponse struct {
Result []ZarazHistoryRecord `json:"result"`
Response
ResultInfo `json:"result_info"`
}

type GetZarazConfigHistoryListParams struct {
type ListZarazConfigHistoryParams struct {
ResultInfo
}

Expand Down Expand Up @@ -324,7 +324,7 @@ func (api *API) PublishZarazConfig(ctx context.Context, rc *ResourceContainer, p
return response, nil
}

func (api *API) GetZarazConfigHistoryList(ctx context.Context, rc *ResourceContainer, params GetZarazConfigHistoryListParams) ([]ZarazHistoryRow, *ResultInfo, error) {
func (api *API) ListZarazConfigHistory(ctx context.Context, rc *ResourceContainer, params ListZarazConfigHistoryParams) ([]ZarazHistoryRecord, *ResultInfo, error) {
if rc.Identifier == "" {
return nil, nil, ErrMissingZoneID
}
Expand All @@ -342,19 +342,19 @@ func (api *API) GetZarazConfigHistoryList(ctx context.Context, rc *ResourceConta
params.Page = 1
}

var records []ZarazHistoryRow
var records []ZarazHistoryRecord
var lastResultInfo ResultInfo

for {
uri := buildURI(fmt.Sprintf("/zones/%s/settings/zaraz/v2/history", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []ZarazHistoryRow{}, &ResultInfo{}, err
return []ZarazHistoryRecord{}, &ResultInfo{}, err
}
var listResponse GetZarazConfigHistoryListResponse
var listResponse ZarazConfigHistoryListResponse
err = json.Unmarshal(res, &listResponse)
if err != nil {
return []ZarazHistoryRow{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
return []ZarazHistoryRecord{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
records = append(records, listResponse.Result...)
lastResultInfo = listResponse.ResultInfo
Expand Down
65 changes: 53 additions & 12 deletions zaraz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -547,23 +548,63 @@ func TestGetZarazConfigHistoryList(t *testing.T) {

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"count": 1,
"data": [
{
"createdAt": "2023-12-19T19:24:42.779683Z",
"description": "Moving to Preview & Publish workflow",
"id": 1005135,
"updatedAt": "2023-12-19T19:24:42.779683Z",
"userId": "9ceddf6f117afe04c64716c83468d3a4"
}
]
}`)
"result": [
{
"createdAt": "2023-01-01T05:20:00Z",
"description": "test 1",
"id": 1005736,
"updatedAt": "2023-01-01T05:20:00Z",
"userId": "9ceddf6f117afe04c64716c83468d3a4"
},
{
"createdAt": "2023-01-01T05:20:00Z",
"description": "test 2",
"id": 1005735,
"updatedAt": "2023-01-01T05:20:00Z",
"userId": "9ceddf6f117afe04c64716c83468d3a4"
}
],
"success": true,
"errors": [],
"messages": [],
"result_info": {
"page": 1,
"per_page": 2,
"count": 2,
"total_count": 8
}
}`)
}

mux.HandleFunc("/zones/"+testZoneID+"/settings/zaraz/v2/history", handler)
createdAt, _ := time.Parse(time.RFC3339, "2023-01-01T05:20:00Z")
updatedAt, _ := time.Parse(time.RFC3339, "2023-01-01T05:20:00Z")
want := []ZarazHistoryRecord{
{
CreatedAt: createdAt,
Description: "test 1",
ID: 1005736,
UpdatedAt: updatedAt,
UserId: "9ceddf6f117afe04c64716c83468d3a4",
},
{
CreatedAt: createdAt,
Description: "test 2",
ID: 1005735,
UpdatedAt: updatedAt,
UserId: "9ceddf6f117afe04c64716c83468d3a4",
},
}

actual, _, err := client.ListZarazConfigHistory(context.Background(), ZoneIdentifier(testZoneID), ListZarazConfigHistoryParams{
ResultInfo: ResultInfo{
PerPage: 2,
},
})

_, _, err := client.GetZarazConfigHistoryList(context.Background(), ZoneIdentifier(testZoneID), GetZarazConfigHistoryListParams{})
require.NoError(t, err)

assert.Equal(t, want, actual)
}

func TestGetDefaultZarazConfig(t *testing.T) {
Expand Down

0 comments on commit 211aa11

Please sign in to comment.