Skip to content

Commit

Permalink
subscription tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kmigielek committed Jan 23, 2024
1 parent 74026f9 commit fdd64d3
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 187 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ env:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.18', '1.19', '1.20', '1.21.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install dependencies
run: |
go get .
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/apimatic/go-core-runtime v0.0.13 h1:KBrOoUbgdIYbjH+TrQNXkbK7QGBovdX43wr7q5Ta6yE=
github.com/apimatic/go-core-runtime v0.0.13/go.mod h1:kyqGg2v3OTV7o2fXHgbHLZPMinqZvIqw1JwdEd64OzM=
166 changes: 6 additions & 160 deletions test/api_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package test

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

"github.com/caarlos0/env/v10"
"github.com/jaswdr/faker"
advancedbilling "github.com/maxio-com/ab-golang-sdk"
"github.com/maxio-com/ab-golang-sdk/models"
"github.com/stretchr/testify/suite"
)

Expand All @@ -21,15 +16,19 @@ type Config struct {
Subdomain string `env:"TEST_SUBDOMAIN,required"`
}

type APITestSuite struct {
type APISuite struct {
suite.Suite
fkr faker.Faker

client advancedbilling.ClientInterface
unauthorizedClient advancedbilling.ClientInterface
}

func (s *APITestSuite) SetupTest() {
func TestAPITestSuite(t *testing.T) {
suite.Run(t, new(APISuite))
}

func (s *APISuite) SetupTest() {
s.fkr = faker.New()

cfg := Config{}
Expand All @@ -52,156 +51,3 @@ func (s *APITestSuite) SetupTest() {
s.client = advancedbilling.NewClient(config)
s.unauthorizedClient = advancedbilling.NewClient(configUnauthorized)
}

func TestAPITestSuite(t *testing.T) {
suite.Run(t, new(APITestSuite))
}

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())
}
163 changes: 163 additions & 0 deletions test/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package test

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

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

func (s *APISuite) 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 *APISuite) 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 *APISuite) createProduct(ctx context.Context, productFamilyID int) models.Product {
resp, err := s.client.ProductsController().CreateProduct(ctx, productFamilyID, &models.CreateOrUpdateProductRequest{
Product: models.CreateOrUpdateProduct{
Name: s.fkr.RandomStringWithLength(30),
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 *APISuite) createCoupon(ctx context.Context, productFamilyID int) models.Coupon {
coupon := &models.Coupon{
Name: strPtr("100\\% off first month of usage"),
Code: strPtr("100OFF" + s.fkr.RandomStringWithLength(30)),
Description: strPtr("100\\% off one-time"),
Percentage: models.NewOptional[string](strPtr("50")),
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 *APISuite) 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())
}

func timePtr(v time.Time) *time.Time {
return &v
}
2 changes: 1 addition & 1 deletion test/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/maxio-com/ab-golang-sdk/models"
)

func (s *APITestSuite) TestReadSite() {
func (s *APISuite) TestReadSite() {
cases := []struct {
name string
client advancedbilling.ClientInterface
Expand Down
Loading

0 comments on commit fdd64d3

Please sign in to comment.