-
Notifications
You must be signed in to change notification settings - Fork 1
/
hours_test.go
120 lines (95 loc) · 2.84 KB
/
hours_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package harvest
import (
"os"
"testing"
"encoding/json"
"time"
)
var (
testDataFile = "test/test_timeEntries.json"
)
func TestDailyHours(t *testing.T) {
var expect float64
var timeEntries TimeEntries
expect = 7.5
body, err := os.ReadFile(testDataFile)
if err != nil {
t.Errorf("[ERROR] %v", err)
}
err = json.Unmarshal(body, &timeEntries)
if err != nil {
t.Errorf("[ERROR] %v", err)
}
date, _ := time.Parse("2006-01-02", "2019-04-15")
result := timeEntries.DailyHours(date)
if result != expect {
t.Errorf("DailyHours: expecting %v, got %v!", expect, result)
} else {
t.Logf("[PASS] DailyHours: expecting %v, got %v!", expect, result)
}
expect = 5.5
date, _ = time.Parse("2006-01-02", "2019-04-16")
result = timeEntries.DailyHours(date)
if result != expect {
t.Errorf("DailyHours: expecting %v, got %v!", expect, result)
} else {
t.Logf("[PASS] DailyHours: expecting %v, got %v!", expect, result)
}
}
func TestDailyTotalsReturnsExpectedHours(t *testing.T) {
expectedHours := 5.5
timeEntries, _ := readTimeEntriesFromFile()
date, _ := time.Parse("2006-01-02", "2019-04-16")
actualHours, _, _ := timeEntries.DailyTotals(date)
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")
_, actualSaldo, _ := timeEntries.DailyTotals(date)
if actualSaldo != expectedSaldo {
t.Errorf("Incorrect saldo: expected %v, got %v!", expectedSaldo, actualSaldo)
}
}
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)
}
}
func TestTotalHours(t *testing.T) {
var expect float64
var timeEntries TimeEntries
expectWeekOne := 47.0
expectWeekTwo := 39.5
expect = expectWeekOne + expectWeekTwo
body, err := os.ReadFile(testDataFile)
if err != nil {
t.Errorf("[ERROR] Could not read test data.\n[ERROR] %v", err)
}
err = json.Unmarshal(body, &timeEntries)
if err != nil {
t.Errorf("[ERROR] Could not unmarshal test data.\n[ERROR] %v", err)
}
result := timeEntries.TotalHours()
if result != expect {
t.Errorf("TotalHours: expecting %v, got %v!", expect, result)
} else {
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
}