package lulu import ( _ "embed" "testing" "github.com/shopspring/decimal" "github.com/stretchr/testify/require" "golang.org/x/text/currency" ) var ( //go:embed testdata/printjobcostreq.json printJobCostReqJson string //go:embed testdata/printjobcostresp.json printJobCostRespJson string ) 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, }, }, 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("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"), }, }, 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) } func requireNotZeroDec(t *testing.T, d decimal.Decimal) { t.Helper() require.False(t, d.IsZero()) }