Skip to content

Commit

Permalink
implementing function
Browse files Browse the repository at this point in the history
  • Loading branch information
riquellopes committed Feb 2, 2019
1 parent ca1398c commit 024a0db
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
27 changes: 27 additions & 0 deletions duck/uncle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duck

import (
"fmt"
"os"

org "github.com/riquellopes/tiopatinhas/organizze"
Expand All @@ -22,3 +23,29 @@ func HowMuchDidISpend() (int, error) {
}
return total, err
}

// Uncle -
type Uncle struct {
GoalCents int
MonthBudget int
}

// Alert -
func (u *Uncle) Alert() (string, error) {
// // os.Getenv("DUCK_GOAL_CENTS")
// strconv.Atoi(os.Getenv("DUCK_MONTH_BUDGET_CENTS"))
// Goal monthly that I need to have every months.
goalByMonth := u.GoalCents / 12

// Call service to get all spents of current month.
spents, err := HowMuchDidISpend()
moneyLeft := u.MonthBudget + spents

if spents == 0 || moneyLeft > goalByMonth {
return "You are walking in the right way.", nil
}
// Calc to get monthly variaction.
variaction := 100 - ((moneyLeft * 100) / goalByMonth)
// return "You are 10% off your monthly goal!", err
return fmt.Sprintf("You are %d%% off your monthly goal!", variaction), err
}
77 changes: 77 additions & 0 deletions duck/uncle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,80 @@ func Test_should_get_0_when_call_the_func(t *testing.T) {

assert.Equal(t, money, 0)
}

func Test_should_get_10_porcent_msg(t *testing.T) {
defer gock.Off()

response := []org.Transaction{
// R$ 500,00
{ID: 1, AmountCents: -50000},
// R$ 500,00
{ID: 2, AmountCents: -50000},
// R$ 1000,00
{ID: 3, AmountCents: -100000},
// R$ 2000,00
{ID: 4, AmountCents: -200000},
}

gock.New(org.URI).
Get("/transactions").
Reply(200).
JSON(response)

uncle := Uncle{
// R$ 20.000,00
GoalCents: 2000000,
// R$ 5.000,00
MonthBudget: 500000,
}

way, _ := uncle.Alert()
assert.Equal(t, way, "You are 40% off your monthly goal!")
}

func Test_should_get_ok_msg(t *testing.T) {
defer gock.Off()

response := []org.Transaction{}

gock.New(org.URI).
Get("/transactions").
Reply(200).
JSON(response)

uncle := Uncle{
// R$ 20.000,00
GoalCents: 2000000,
// R$ 5.000,00
MonthBudget: 500000,
}

way, _ := uncle.Alert()

assert.Equal(t, way, "You are walking in the right way.")
}

func Test_should_get_ok_msg_when_financial_ok(t *testing.T) {
defer gock.Off()

response := []org.Transaction{
// R$ 200,00
{ID: 95, Description: "Just a little cafe on mall.", AmountCents: -20000},
}

gock.New(org.URI).
Get("/transactions").
Reply(200).
JSON(response)

uncle := Uncle{
// R$ 20.000,00
GoalCents: 2000000,
// R$ 5.000,00
MonthBudget: 500000,
}

way, _ := uncle.Alert()

assert.Equal(t, way, "You are walking in the right way.")
}

0 comments on commit 024a0db

Please sign in to comment.