diff options
Diffstat (limited to 'cost_test.go')
| -rw-r--r-- | cost_test.go | 417 |
1 files changed, 189 insertions, 228 deletions
diff --git a/cost_test.go b/cost_test.go index a8aeb3d..1abe63c 100644 --- a/cost_test.go +++ b/cost_test.go @@ -1,247 +1,208 @@ package lulu import ( + _ "embed" "testing" "github.com/shopspring/decimal" + "github.com/stretchr/testify/require" + "golang.org/x/text/currency" ) -func TestMarshalUnmarshalPrintJobCostReq(t *testing.T) { - j := `{ - "line_items": [ - { - "page_count": 32, - "pod_package_id": "0600X0900.BW.STD.PB.060UW444.MXX", - "quantity": 20 - }, { - "page_count": 324, - "pod_package_id": "0425X0687.BW.STD.PB.060UW444.GXX", - "quantity": 200 - } - ], - "shipping_address": { - "city": "Lübeck", - "country_code": "DE", - "postcode": "23552", - "state_code": "", - "street1": "Holstenstr. 40", - "phone_number": "844-212-0689", - "name": "", - "street2": "", - "is_business": false - - }, - "shipping_option": "EXPRESS" - }` - addr := printJobCostReq{ - []printJobCostLineItem{ - { - 32, - PkgId{UsTrade, Mono, Standard, Perfect, P60UncoatedWhite, Matte, NoLinen, NoFoil}, - 20, - }, { - 324, - PkgId{Pocketbook, Mono, Standard, Perfect, P60UncoatedWhite, Gloss, NoLinen, NoFoil}, - 200, - }, - }, - ShippingAddress{ - City: "Lübeck", - CountryCode: "DE", - PostCode: "23552", - Street1: "Holstenstr. 40", - Phone: "844-212-0689", - // TODO: does the API mind the extra fields? - }, - Express, - } - requireMarshalJsonEq(t, j, addr) - requireUnmarshalJsonEq(t, addr, j) -} +var ( + //go:embed testdata/printjobcostreq.json + printJobCostReqJson string -func TestUnmarshalPrintJobCost(t *testing.T) { - j := `{ - "shipping_address": { - "city": "Lübeck", - "country_code": "DE", - "is_business": false, - "name": "Hans Dampf", - "phone_number": "844-212-0689", - "postcode": "23552", - "state_code": "", - "street1": "Holstenstr. 40", - "street2": "", - "warnings": { - "type": "validation_warning", - "path": "external", - "code": "REPLACED", - "message": "street1: Holstenstr. 40 -> Holstenstraße 40" - }, - "suggested_address": { - "country_code": "DE", - "state_code": null, - "postcode": 23552, - "city": "Lübeck", - "street1": "Holstenstraße 40\"", - "street2": null - } - }, - "currency": "USD", - "fees": [ - { - "currency": "USD", - "fee_type": "HANDLING_FEE", - "sku": "HANDLING_FEE", - "tax_rate": "0.060000", - "total_cost_excl_tax": "14.00", - "total_cost_incl_tax": "14.84", - "total_tax": "0.84" - }, - { - "currency": "USD", - "fee_type": "FULFILLMENT_FEE", - "sku": "FULFILLMENT_FEE", - "tax_rate": "0.060000", - "total_cost_excl_tax": "0.75", - "total_cost_incl_tax": "0.80", - "total_tax": "0.05" - } - ], - "fulfillment_cost": { - "tax_rate": "0.06", - "total_cost_excl_tax": "0.75", - "total_cost_incl_tax": "0.80", - "total_tax": "0.05" - }, - "line_item_costs": [ - { - "cost_excl_discounts": "2.55", - "discounts": [], - "quantity": 20, - "tax_rate": "0.060000", - "total_cost_excl_discounts": "51.00", - "total_cost_excl_tax": "51.00", - "total_cost_incl_tax": "54.06", - "total_tax": "3.06", - "unit_tier_cost": null - }, - { - "cost_excl_discounts": "9.26", - "discounts": [ - { - "amount": "92.60", - "description": "Volume Discount 5%" - } - ], - "quantity": 200, - "tax_rate": "0.060000", - "total_cost_excl_discounts": "1852.00", - "total_cost_excl_tax": "1759.40", - "total_cost_incl_tax": "1864.96", - "total_tax": "105.56", - "unit_tier_cost": null - } - ], - "shipping_cost": { - "tax_rate": "0.06", - "total_cost_excl_tax": "318.44", - "total_cost_incl_tax": "337.55", - "total_tax": "19.11" - }, - "total_cost_excl_tax": "2129.59", - "total_cost_incl_tax": "2257.37", - "total_discount_amount": "92.60", - "total_tax": "127.78" - }` + //go:embed testdata/printjobcostresp.json + printJobCostRespJson string +) - cost := PrintJobCost{ - Addr: ShippingAddress{ - City: "Lübeck", - CountryCode: "DE", - IsBusiness: false, - Name: "Hans Dampf", - Phone: "844-212-0689", - PostCode: "23552", - StateCode: "", - Street1: "Holstenstr. 40", - Street2: "", - }, - SuggestedAddr: ShippingAddress{ - CountryCode: "DE", - StateCode: "", - PostCode: "23552", - City: "Lübeck", - Street1: "Holstenstraße 40\"", - Street2: "", +var printJobCostReqSample = printJobCostReq{ + []PrintJobCostLineItem{ + { + 32, + PkgId{UsTrade, Mono, Standard, Perfect, P60UncoatedWhite, Matte, NoLinen, NoFoil}, + 20, + }, { + 324, + PkgId{Pocketbook, Mono, Standard, Perfect, P60UncoatedWhite, Gloss, NoLinen, NoFoil}, + 200, }, - AddrWarning: ShippingAddressWarning{ - "validation_warning", - "external", - "REPLACED", - "street1: Holstenstr. 40 -> Holstenstraße 40", - }, - Currency: "USD", - Fees: []Fee{ - { - Currency: "USD", - Type: "HANDLING_FEE", - Sku: "HANDLING_FEE", - TaxRate: decimal.RequireFromString("0.060000"), - TotalCostExclTax: decimal.RequireFromString("14.00"), - TotalCostInclTax: decimal.RequireFromString("14.84"), - TotalTax: decimal.RequireFromString("0.84"), - }, { - Currency: "USD", - Type: "FULFILLMENT_FEE", - Sku: "FULFILLMENT_FEE", - TaxRate: decimal.RequireFromString("0.060000"), - TotalCostExclTax: decimal.RequireFromString("0.75"), - TotalCostInclTax: decimal.RequireFromString("0.80"), - TotalTax: decimal.RequireFromString("0.05"), - }, - }, - FulfillmentCost: FulfillmentCost{ - TaxRate: decimal.RequireFromString("0.06"), + }, + printJobCostReqShipAddr{ + City: "Lübeck", + Country: "DE", + PostCode: "23552", + State: "", + Street1: "Holstenstr. 40", + Phone: "844-212-0689", + }, + Express, +} + +var printJobCostSample = PrintJobCost{ + Addr: ShippingAddress{ + City: "Lübeck", + PostCode: "23552", + Street1: "Holstenstr. 40", + Phone: "844-212-0689", + State: "", + Country: "DE", + IsBusiness: false, + Name: ". .", + }, + SuggestedAddr: ShippingAddress{ + Country: "DE", + State: "", + PostCode: "23552", + City: "Lübeck", + Street1: "Holstenstraße 40", + Street2: "", + }, + AddrWarnings: []ShippingAddressWarning{{ + "validation_warning", + "external", + "REPLACED", + "street1: Holstenstr. 40 -> Holstenstraße 40", + }}, + Fees: []Fee{ + { + Currency: "USD", + Type: "HANDLING_FEE", + Sku: "HANDLING_FEE", + TaxRate: decimal.RequireFromString("0.060000"), + TotalCostExclTax: decimal.RequireFromString("14.00"), + TotalCostInclTax: decimal.RequireFromString("14.84"), + TotalTax: decimal.RequireFromString("0.84"), + }, { + Currency: "USD", + Type: "FULFILLMENT_FEE", + Sku: "FULFILLMENT_FEE", + TaxRate: decimal.RequireFromString("0.060000"), TotalCostExclTax: decimal.RequireFromString("0.75"), TotalCostInclTax: decimal.RequireFromString("0.80"), TotalTax: decimal.RequireFromString("0.05"), }, - LineItemCosts: []LineItemCost{ - { - CostExclDiscounts: decimal.RequireFromString("2.55"), - Discounts: []Discount{}, - Quantity: 20, - TaxRate: decimal.RequireFromString("0.060000"), - TotalCostExclDiscounts: decimal.RequireFromString("51.00"), - TotalCostExclTax: decimal.RequireFromString("51.00"), - TotalCostInclTax: decimal.RequireFromString("54.06"), - TotalTax: decimal.RequireFromString("3.06"), - UnitTierCost: nil, - }, { - CostExclDiscounts: decimal.RequireFromString("9.26"), - Discounts: []Discount{ - {decimal.RequireFromString("92.60"), "Volume Discount 5%"}, - }, - Quantity: 200, - TaxRate: decimal.RequireFromString("0.060000"), - TotalCostExclDiscounts: decimal.RequireFromString("1852.00"), - TotalCostExclTax: decimal.RequireFromString("1759.40"), - TotalCostInclTax: decimal.RequireFromString("1864.96"), - TotalTax: decimal.RequireFromString("105.56"), - UnitTierCost: nil, - }, - }, - ShipCost: FulfillmentCost{ - TaxRate: decimal.RequireFromString("0.06"), - TotalCostExclTax: decimal.RequireFromString("318.44"), - TotalCostInclTax: decimal.RequireFromString("337.55"), - TotalTax: decimal.RequireFromString("19.11"), + }, + LineItemCosts: []LineItemCost{ + { + CostExclDiscounts: decimal.RequireFromString("4.95"), + TotalTax: decimal.RequireFromString("6.93"), + TaxRate: decimal.RequireFromString("0.070000"), + Quantity: 20, + TotalCostExclTax: decimal.RequireFromString("99.00"), + TotalCostExclDiscounts: decimal.RequireFromString("99.00"), + TotalCostInclTax: decimal.RequireFromString("105.93"), + Discounts: []Discount{}, + UnitTierCost: decimal.RequireFromString("4.95"), + }, { + CostExclDiscounts: decimal.RequireFromString("16.19"), + TotalTax: decimal.RequireFromString("215.33"), + TaxRate: decimal.RequireFromString("0.070000"), + Quantity: 200, + TotalCostExclTax: decimal.RequireFromString("3076.10"), + TotalCostExclDiscounts: decimal.RequireFromString("3238.00"), + TotalCostInclTax: decimal.RequireFromString("3291.43"), + Discounts: []Discount{{decimal.RequireFromString("161.90"), "Volume Discount 5%"}}, + UnitTierCost: decimal.RequireFromString("16.19"), }, - TotalCostExclTax: decimal.RequireFromString("2129.59"), - TotalCostInclTax: decimal.RequireFromString("2257.37"), - TotalDiscount: decimal.RequireFromString("92.60"), - TotalTax: decimal.RequireFromString("127.78"), + }, + ShipCost: FulfillmentCost{ + TotalCostExclTax: decimal.RequireFromString("1085.50"), + TotalCostInclTax: decimal.RequireFromString("1161.49"), + TotalTax: decimal.RequireFromString("75.99"), + TaxRate: decimal.RequireFromString("0.19"), + }, + FulfillmentCost: FulfillmentCost{ + TotalCostExclTax: decimal.RequireFromString("1.05"), + TotalCostInclTax: decimal.RequireFromString("1.12"), + TotalTax: decimal.RequireFromString("0.07"), + TaxRate: decimal.RequireFromString("0.19"), + }, + TotalTax: decimal.RequireFromString("298.32"), + TotalCostExclTax: decimal.RequireFromString("4261.65"), + TotalCostInclTax: decimal.RequireFromString("4559.97"), + TotalDiscount: decimal.RequireFromString("161.90"), + Currency: "CAD", +} + +func TestMarshalPrintJobCostReq(t *testing.T) { + requireMarshalJsonEq(t, printJobCostReqJson, printJobCostReqSample) +} + +func TestUnmarshalPrintJobCost(t *testing.T) { + requireUnmarshalJsonEq(t, printJobCostSample, printJobCostRespJson) +} + +func TestPrintJobCost(t *testing.T) { + c := newClient(t) + items := printJobCostReqSample.LineItems + addr := ShippingAddress{ + City: "Lübeck", + Country: "DE", + PostCode: "23552", + State: "", + Street1: "Holstenstr. 40", + Phone: "844-212-0689", + } + shiplvl := printJobCostReqSample.ShipOpt + cost, err := c.PrintJobCost(items, addr, shiplvl) + require.NoError(t, err) + require.Equal(t, "Lübeck", cost.Addr.City) + require.Equal(t, "23552", cost.Addr.PostCode) + require.Equal(t, "Holstenstr. 40", cost.Addr.Street1) + require.Equal(t, "844-212-0689", cost.Addr.Phone) + require.Empty(t, cost.Addr.State) + require.Equal(t, "DE", cost.Addr.Country) + require.False(t, cost.Addr.IsBusiness) + require.Equal(t, printJobCostSample.SuggestedAddr, cost.SuggestedAddr) + require.Equal(t, printJobCostSample.AddrWarnings, cost.AddrWarnings) + requireCurrency(t, cost.Currency) + for _, fee := range cost.Fees { + requireCurrency(t, fee.Currency) + require.NotEmpty(t, fee.Type) + require.NotEmpty(t, fee.Sku) + requireNotZeroDec(t, fee.TaxRate) + requireNotZeroDec(t, fee.TotalCostExclTax) + requireNotZeroDec(t, fee.TotalCostInclTax) + requireNotZeroDec(t, fee.TotalTax) + } + requireNotZeroDec(t, cost.FulfillmentCost.TaxRate) + requireNotZeroDec(t, cost.FulfillmentCost.TotalCostExclTax) + requireNotZeroDec(t, cost.FulfillmentCost.TotalCostInclTax) + requireNotZeroDec(t, cost.FulfillmentCost.TotalTax) + require.NotEmpty(t, cost.LineItemCosts) + for _, lic := range cost.LineItemCosts { + requireNotZeroDec(t, lic.CostExclDiscounts) + for _, discount := range lic.Discounts { + require.Equal(t, "Volume Discount 5%", discount.Description) + requireNotZeroDec(t, discount.Amount) + } + require.NotZero(t, lic.Quantity) + requireNotZeroDec(t, lic.TaxRate) + requireNotZeroDec(t, lic.TotalCostExclDiscounts) + requireNotZeroDec(t, lic.TotalCostExclTax) + requireNotZeroDec(t, lic.TotalCostInclTax) + requireNotZeroDec(t, lic.TotalTax) + requireNotZeroDec(t, lic.UnitTierCost) } + requireNotZeroDec(t, cost.ShipCost.TaxRate) + requireNotZeroDec(t, cost.ShipCost.TotalCostExclTax) + requireNotZeroDec(t, cost.ShipCost.TotalCostInclTax) + requireNotZeroDec(t, cost.ShipCost.TotalTax) + requireNotZeroDec(t, cost.TotalCostExclTax) + requireNotZeroDec(t, cost.TotalCostInclTax) + requireNotZeroDec(t, cost.TotalDiscount) + requireNotZeroDec(t, cost.TotalTax) +} + +func requireCurrency(t *testing.T, s string) { + t.Helper() + _, err := currency.ParseISO(s) + require.NoError(t, err) +} - requireUnmarshalJsonEq(t, cost, j) +func requireNotZeroDec(t *testing.T, d decimal.Decimal) { + t.Helper() + require.False(t, d.IsZero()) } |