Skip to content

Instantly share code, notes, and snippets.

@rafa-acioly
Created November 6, 2018 15:53
Show Gist options
  • Save rafa-acioly/c482ba87db89303705d9ef55b89dad72 to your computer and use it in GitHub Desktop.
Save rafa-acioly/c482ba87db89303705d9ef55b89dad72 to your computer and use it in GitHub Desktop.
pointer go
// LOOP DE TESTE
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
record := Call{
StartPeriod: tc.start,
EndPeriod: tc.end,
}
price, _ := CalcAmount(record)
amountAsDecimal, _ := decimal.NewFromFloat(tc.expectedAmount).Float64()
billingAmount, _ := price.Float64()
if billingAmount != amountAsDecimal {
t.Errorf(errorMessage, tc.testName, amountAsDecimal, billingAmount)
}
})
}
// METEDO QUE REALIZA O CALCULO
func CalcAmount(c Call) (decimal.Decimal, error) {
decimal.DivisionPrecision = 2
diff, err := c.Duration()
if err != nil {
return decimal.Zero, err
}
var amount decimal.Decimal
if !c.InReducedTariffPeriod() {
exactMinutes := math.Floor(diff.Minutes())
amount = amount.Add(
decimal.NewFromFloat(exactMinutes).Mul(minuteFee),
)
}
amount = amount.Add(connectionFee)
return amount, nil
}
func (c *Call) Duration() (time.Duration, error) {
if c.StartPeriod.IsZero() || c.EndPeriod.IsZero() {
return 0, errInvalidCallPeriod
}
// if the call is not in reduced period the fee per minute
// should be calc only until 22:00
if !c.InReducedTariffPeriod() {
h, m, s, ns := 22, 00, 00, 00
c.EndPeriod = time.Date(
c.StartPeriod.Year(), c.StartPeriod.Month(), c.StartPeriod.Day(),
h, m, s, ns, time.UTC,
)
}
return c.EndPeriod.Sub(c.StartPeriod), nil
}
//{
// testName: "2 minutes of call",
// start: time.Date(2019, 9, 21, 21, 57, 13, 0, time.UTC),
// end: time.Date(2019, 9, 22, 10, 11, 10, 0, time.UTC),
// expectedAmount: 0.54,
//},
//{
// testName: "1 minute of call",
// start: time.Date(2019, 9, 21, 10, 10, 10, 0, time.UTC),
// end: time.Date(2019, 9, 21, 10, 11, 10, 0, time.UTC),
// expectedAmount: 0.45,
//},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment