Skip to content

Commit

Permalink
added test function
Browse files Browse the repository at this point in the history
  • Loading branch information
apinonformoso committed Jan 28, 2025
1 parent 39c3457 commit c986c62
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
5 changes: 2 additions & 3 deletions partner_interconnect_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ type PartnerInterconnectAttachmentCreateRequest struct {
BGP BGP `json:"bgp,omitempty"`
}

// PartnerInterconnectAttachmentCreateRequest represents a request to create a Partner Interconnect Attachment.
type partnerInterconnectAttachmentRequestBody struct {
// Name is the name of the Partner Interconnect Attachment
Name string `json:"name,omitempty"`
Expand All @@ -62,8 +61,8 @@ type partnerInterconnectAttachmentRequestBody struct {
BGP *BGP `json:"bgp,omitempty"`
}

func (req *PartnerInterconnectAttachmentCreateRequest) buildReq() *PartnerInterconnectAttachmentRequestBody {
request := &PartnerInterconnectAttachmentRequestBody{
func (req *PartnerInterconnectAttachmentCreateRequest) buildReq() *partnerInterconnectAttachmentRequestBody {
request := &partnerInterconnectAttachmentRequestBody{
Name: req.Name,
ConnectionBandwidthInMbps: req.ConnectionBandwidthInMbps,
Region: req.Region,
Expand Down
74 changes: 74 additions & 0 deletions partner_interconnect_attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package godo

import (
"encoding/json"
"io"
"net/http"
"testing"
"time"
Expand All @@ -27,6 +28,17 @@ var vInterconnectTestObj = &PartnerInterconnectAttachment{
CreatedAt: time.Date(2024, 12, 26, 21, 48, 40, 995304079, time.UTC),
}

var vInterconnectNoBGPTestObj = &PartnerInterconnectAttachment{
ID: "880b7f98-f062-404d-b33c-458d545696f6",
Name: "my-new-partner-interconnect",
State: "ACTIVE",
ConnectionBandwidthInMbps: 50,
Region: "NYC",
NaaSProvider: "MEGAPORT",
VPCIDs: []string{"f5a0c5e4-7537-47de-bb8d-46c766f89ffb"},
CreatedAt: time.Date(2024, 12, 26, 21, 48, 40, 995304079, time.UTC),
}

var vInterconnectTestJSON = `
{
"id":"880b7f98-f062-404d-b33c-458d545696f6",
Expand All @@ -46,6 +58,22 @@ var vInterconnectTestJSON = `
}
`

var vInterconnectNoBGPTestJSON = `
{
"id":"880b7f98-f062-404d-b33c-458d545696f6",
"name":"my-new-partner-interconnect",
"state":"ACTIVE",
"connection_bandwidth_in_mbps":50,
"region":"NYC",
"naas_provider":"MEGAPORT",
"vpc_ids":["f5a0c5e4-7537-47de-bb8d-46c766f89ffb"],
"created_at":"2024-12-26T21:48:40.995304079Z"
}
`

const expectedCreateBodyNoBGP = `{"name":"my-new-partner-interconnect","connection_bandwidth_in_mbps":50,"region":"NYC","naas_provider":"MEGAPORT","vpc_ids":["f5a0c5e4-7537-47de-bb8d-46c766f89ffb"]}
`

func TestPartnerInterconnectAttachments_List(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -134,6 +162,52 @@ func TestPartnerInterconnectAttachments_Create(t *testing.T) {
require.Equal(t, want, got)
}

func TestPartnerInterconnectAttachments_CreateNoBGP(t *testing.T) {
setup()
defer teardown()

svc := client.PartnerInterconnectAttachments
path := "/v2/partner_interconnect/attachments"
want := vInterconnectNoBGPTestObj
req := &PartnerInterconnectAttachmentCreateRequest{
Name: "my-new-partner-interconnect",
ConnectionBandwidthInMbps: 50,
Region: "NYC",
NaaSProvider: "MEGAPORT",
VPCIDs: []string{"f5a0c5e4-7537-47de-bb8d-46c766f89ffb"},
}
jsonBlob := `
{
"partner_interconnect_attachment":
` + vInterconnectNoBGPTestJSON + `
}
`

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
defer r.Body.Close()

require.Equal(t, expectedCreateBodyNoBGP, string(body))

c := new(PartnerInterconnectAttachmentCreateRequest)
err = json.Unmarshal(body, c)
if err != nil {
t.Fatal(err)
}

testMethod(t, r, http.MethodPost)
require.Equal(t, c, req)
w.Write([]byte(jsonBlob))
})

got, _, err := svc.Create(ctx, req)
require.NoError(t, err)
require.Equal(t, want, got)
}

func TestPartnerInterconnectAttachments_Get(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit c986c62

Please sign in to comment.