Skip to content

Commit

Permalink
refactor: split unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoheikkila committed May 13, 2024
1 parent 38b9988 commit 8f58141
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions hours_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func TestDailyHours(t *testing.T) {
var timeEntries TimeEntries
expect = 7.5

// var c config.Config
// h := Init(&c)
body, err := os.ReadFile(testDataFile)
if err != nil {
t.Errorf("[ERROR] %v", err)
Expand Down Expand Up @@ -47,37 +45,39 @@ func TestDailyHours(t *testing.T) {
}
}

func TestDailyTotals(t *testing.T) {
var expectHours, expectSaldo, expectOvertime float64
expectHours = 5.5
expectSaldo = 0.0
expectOvertime = -2.0
func TestDailyTotalsReturnsExpectedHours(t *testing.T) {
expectedHours := 5.5
timeEntries, _ := readTimeEntriesFromFile()
date, _ := time.Parse("2006-01-02", "2019-04-16")

body, _ := os.ReadFile(testDataFile)
actualHours, _, _ := timeEntries.DailyTotals(date)

var timeEntries TimeEntries
err := json.Unmarshal(body, &timeEntries)
if err != nil {
t.Errorf("[ERROR] %v", err)
if actualHours != expectedHours {
t.Errorf("DailyTotals.Hours: expecting %v, got %v!", expectedHours, actualHours)
}
}

func TestDailyTotalsReturnsExpectedSaldo(t *testing.T) {
expectedSaldo := 0.0
timeEntries, _ := readTimeEntriesFromFile()
date, _ := time.Parse("2006-01-02", "2019-04-16")
resultHours, resultSaldo, resultOvertime := timeEntries.DailyTotals(date)
if resultHours != expectHours {
t.Errorf("DailyTotals.Hours: expecting %v, got %v!", expectHours, resultHours)
} else {
t.Logf("[PASS] DailyTotals.Hours: expecting %v, got %v!", expectHours, resultHours)
}

if resultSaldo != expectSaldo {
t.Errorf("DailyTotals.Saldo: expecting %v, got %v!", expectSaldo, resultSaldo)
} else {
t.Logf("[PASS] DailyTotals.Saldo: expecting %v, got %v!", expectSaldo, resultSaldo)
_, actualSaldo, _ := timeEntries.DailyTotals(date)

if actualSaldo != expectedSaldo {
t.Errorf("Incorrect saldo: expected %v, got %v!", expectedSaldo, actualSaldo)
}
if resultOvertime != expectOvertime {
t.Errorf("DailyTotals.Overtime: expecting %v, got %v!", expectOvertime, resultOvertime)
} else {
t.Logf("[PASS] DailyTotals.Overtime: expecting %v, got %v!", expectOvertime, resultOvertime)
}

func TestDailyTotalsReturnsExpectedOvertime(t *testing.T) {
expectedOvertime := -2.0
timeEntries, _ := readTimeEntriesFromFile()
date, _ := time.Parse("2006-01-02", "2019-04-16")

_, _, actualOvertime := timeEntries.DailyTotals(date)

if actualOvertime != expectedOvertime {
t.Errorf("Incorrect overtime: expecting %v, got %v!", expectedOvertime, actualOvertime)
}
}

Expand Down Expand Up @@ -105,3 +105,16 @@ func TestTotalHours(t *testing.T) {
t.Logf("[PASS] TotalHours: expecting %v, got %v!", expect, result)
}
}

func readTimeEntriesFromFile() (TimeEntries, error) {
body, _ := os.ReadFile(testDataFile)

var timeEntries TimeEntries
err := json.Unmarshal(body, &timeEntries)

if err != nil {
return timeEntries, err
}

return timeEntries, err
}

0 comments on commit 8f58141

Please sign in to comment.