Skip to content

Commit

Permalink
metafields tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kmigielek committed Jan 24, 2024
1 parent 624cea3 commit 02fa1b0
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 152 deletions.
152 changes: 0 additions & 152 deletions test/api_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package test

import (
"context"
"fmt"
"math/rand"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -92,152 +89,3 @@ func (s *APISuite) SetupTest() {
zip: "02120",
}
}

func (s *APITestSuite) createCustomer(ctx context.Context) models.Customer {
person := s.fkr.Person()

resp, err := s.client.CustomersController().CreateCustomer(ctx, &models.CreateCustomerRequest{
Customer: models.CreateCustomer{
FirstName: person.FirstName(),
LastName: person.LastName(),
Email: person.Contact().Email,
Organization: strPtr(s.fkr.Company().Name()),
Reference: strPtr(fmt.Sprintf("%d", s.fkr.RandomNumber(10))),
Address: strPtr(person.Faker.Address().StreetAddress()),
City: strPtr(person.Faker.Address().City()),
State: strPtr(person.Faker.Address().State()),
Zip: strPtr(person.Faker.Address().PostCode()),
Country: strPtr(person.Faker.Address().Country()),
Phone: strPtr(person.Faker.Address().Country()),
},
})

s.NoErrorf(err, "create customer err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create customer status code")

return resp.Data.Customer
}

func (s *APITestSuite) createProductFamily(ctx context.Context) models.ProductFamily {
resp, err := s.client.ProductFamiliesController().CreateProductFamily(ctx, &models.CreateProductFamilyRequest{
ProductFamily: models.CreateProductFamily{
Name: strPtr(s.fkr.Company().Name()),
},
})

s.NoErrorf(err, "create product family err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create product family status code")

return *resp.Data.ProductFamily
}

func (s *APITestSuite) createProduct(ctx context.Context, productFamilyID int) models.Product {
resp, err := s.client.ProductsController().CreateProduct(ctx, productFamilyID, &models.CreateOrUpdateProductRequest{
Product: models.CreateOrUpdateProduct{
Name: "Test",
Description: "Testable product",
PriceInCents: 50,
Interval: 1,
IntervalUnit: models.IntervalUnit_MONTH,
},
})

s.NoErrorf(err, "create product err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create product status code")

return resp.Data.Product
}

func (s *APITestSuite) createCoupon(ctx context.Context, productFamilyID int) models.Coupon {
coupon := &models.Coupon{
Name: strPtr("100\\% off first month of usage"),
Code: strPtr("100OFF"),
Description: strPtr("100\\% off one-time"),
Percentage: models.NewOptional[string](strPtr("100")),
AllowNegativeBalance: boolPtr(false),
Recurring: boolPtr(false),
EndDate: models.NewOptional[string](strPtr(newDate())),
ProductFamilyId: &productFamilyID,
Stackable: boolPtr(false),
ExcludeMidPeriodAllocations: boolPtr(true),
ApplyOnCancelAtEndOfPeriod: boolPtr(true),
}

resp, err := s.client.CouponsController().CreateCoupon(ctx, productFamilyID, &models.CreateOrUpdateCoupon{
Coupon: interfacePtr(coupon),
})

s.NoErrorf(err, "create coupon err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create coupon err")

return *resp.Data.Coupon
}

type MeteredComponent struct {
Name string `json:"name"`
UnitName string `json:"unit_name"`
Taxable bool `json:"taxable"`
PricingScheme string `json:"pricing_scheme"`
Prices []Price `json:"prices"`
}

type Price struct {
StartingQuantity int `json:"starting_quantity"`
UnitPrice int `json:"unit_price"`
}

func (s *APITestSuite) createMeteredComponent(ctx context.Context, productFamilyID int) models.Component {
component := struct {
MeteredComponent MeteredComponent `json:"metered_component"`
}{
MeteredComponent: MeteredComponent{
Name: "test 2",
UnitName: "test message",
Taxable: false,
PricingScheme: "stairstep",
Prices: []Price{{
StartingQuantity: 1,
UnitPrice: 1,
},
},
},
}

resp, err := s.client.ComponentsController().CreateComponent(
ctx,
productFamilyID,
models.ComponentKindPath_METEREDCOMPONENTS,
interfacePtr(component),
)

s.NoErrorf(err, "create component err")
s.Equalf(http.StatusCreated, resp.Response.StatusCode, "create component err")

return resp.Data.Component
}

func strPtr(v string) *string {
return &v
}

func boolPtr(v bool) *bool {
return &v
}

func intPtr(v int) *int {
return &v
}

func interfacePtr(v interface{}) *interface{} {
return &v
}

func toPtr[T any](v T) *T {
return &v
}

func newDate() string {
t := time.Now().Add(time.Hour)

return fmt.Sprintf("%d-%d-%d", t.Year(), t.Month(), t.Day())
}
182 changes: 182 additions & 0 deletions test/metafields_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package test

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/maxio-com/ab-golang-sdk/models"
)

func (s *APISuite) TestMetafields() {
ctx := context.Background()

customer := s.createCustomer(ctx)
productFamilyID := s.createProductFamily(ctx)
product := s.createProduct(ctx, *productFamilyID.Id)
subscription := s.newSubscription(customer, product, "", []models.CreateSubscriptionComponent{})

resp, err := s.client.SubscriptionsController().CreateSubscription(ctx, &models.CreateSubscriptionRequest{
Subscription: subscription,
})
s.NoError(err)
s.Equal(http.StatusCreated, resp.Response.StatusCode)

subs := resp.Data.Subscription

cases := []struct {
name string
resourceType models.ResourceType
metafields interface{}
assert func(t *testing.T, resp models.ApiResponse[[]models.Metafield], err error)
}{
{
name: "subscriptions",
resourceType: models.ResourceType_SUBSCRIPTIONS,
metafields: []models.Metafield{
{
Name: strPtr("Dropdown field"),
InputType: strPtr("dropdown"),
Scope: &models.MetafieldScope{
PublicShow: toPtr[models.IncludeOption](models.IncludeOption_INCLUDE),
PublicEdit: toPtr[models.IncludeOption](models.IncludeOption_INCLUDE),
},
Enum: models.NewOptional[interface{}](interfacePtr([]string{
"option 1",
"option 2",
})),
},
{
Name: strPtr("Text field"),
},
},
assert: func(t *testing.T, resp models.ApiResponse[[]models.Metafield], err error) {
s.NoError(err)
s.Contains([]int{http.StatusOK, http.StatusCreated}, resp.Response.StatusCode)

dropdownField := resp.Data[0]
s.Equal("Dropdown field", *dropdownField.Name)
s.Equal("dropdown", *dropdownField.InputType)
// s.Len(dropdownField.Enum.Value(), 2) unusable at this stage
s.Equal(models.IncludeOption_INCLUDE, *dropdownField.Scope.PublicShow)
s.Equal(models.IncludeOption_INCLUDE, *dropdownField.Scope.PublicEdit)

textField := resp.Data[1]
s.Equal("Text field", *textField.Name)
s.Equal("text", *textField.InputType)

r, err := s.client.CustomFieldsController().CreateMetadata(
ctx,
models.ResourceType_SUBSCRIPTIONS,
fmt.Sprintf("%d", *subs.Id),
&models.CreateMetadataRequest{
Metadata: []models.CreateMetadata{
{
Name: dropdownField.Name,
Value: strPtr("option 1"),
},
{
Name: textField.Name,
Value: strPtr("something"),
},
},
})
s.NoError(err)
s.Equal(http.StatusOK, r.Response.StatusCode)

rSubs, err := s.client.SubscriptionsController().ListSubscriptions(
ctx,
nil,
nil,
nil,
product.Id,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
map[string]string{
"Dropdown field": "option 1",
},
nil,
nil,
[]models.SubscriptionListInclude{},
)
s.NoError(err)
s.Equal(http.StatusOK, r.Response.StatusCode)

s.Len(rSubs.Data, 1)
s.Equal(subs.Id, rSubs.Data[0].Subscription.Id)
},
},
{
name: "customers",
resourceType: models.ResourceType_CUSTOMERS,
metafields: models.Metafield{
Name: strPtr("Radios field"),
InputType: strPtr("radio"),
Enum: models.NewOptional[interface{}](interfacePtr([]string{
"option 1",
"option 2",
})),
Scope: &models.MetafieldScope{
Csv: toPtr[models.IncludeOption](models.IncludeOption_INCLUDE),
Invoices: toPtr[models.IncludeOption](models.IncludeOption_INCLUDE),
Portal: toPtr[models.IncludeOption](models.IncludeOption_INCLUDE),
},
},
assert: func(t *testing.T, resp models.ApiResponse[[]models.Metafield], err error) {
s.NoError(err)
s.Contains([]int{http.StatusOK, http.StatusCreated}, resp.Response.StatusCode)

radioField := resp.Data[0]
s.Equal("Radios field", *radioField.Name)
s.Equal("radio", *radioField.InputType)

s.Equal(models.IncludeOption_INCLUDE, *radioField.Scope.Csv)
s.Equal(models.IncludeOption_INCLUDE, *radioField.Scope.Invoices)
s.Equal(models.IncludeOption_INCLUDE, *radioField.Scope.Portal)

customerResp, err := s.client.CustomFieldsController().CreateMetadata(
ctx,
models.ResourceType_CUSTOMERS,
fmt.Sprintf("%d", *customer.Id),
&models.CreateMetadataRequest{
Metadata: []models.CreateMetadata{
{
Name: radioField.Name,
Value: strPtr("option 2"),
},
},
},
)
s.NoError(err)
s.Equal(http.StatusOK, customerResp.Response.StatusCode)

// s.Len(customerResp.Data, 1)
// s.Equal(customer.Id, customerResp.Data[0].ResourceId)
// s.Equal(radioField.Name, customerResp.Data[0].Name)
// s.Equal("option 2", *customerResp.Data[0].Value)

// Not able to setup enums
},
},
}

for _, c := range cases {
s.T().Run(c.name, func(t *testing.T) {
resp, err := s.client.CustomFieldsController().CreateMetafields(
ctx,
c.resourceType,
&models.CreateMetafieldsRequest{
Metafields: c.metafields,
},
)

c.assert(t, resp, err)
})
}
}

0 comments on commit 02fa1b0

Please sign in to comment.