-
Notifications
You must be signed in to change notification settings - Fork 4
/
bubbletea-heatmap_test.go
125 lines (95 loc) · 3.15 KB
/
bubbletea-heatmap_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
121
122
123
124
125
package bubbleteaheatmap
import (
"encoding/json"
"log"
"os"
"testing"
"time"
)
func readFromFile(filename string) []CalDataPoint {
var fileData []CalDataPoint
// Get Data from File
content, err := os.ReadFile(filename)
if err != nil {
log.Fatal("Error when opening file: ", err)
}
err = json.Unmarshal(content, &fileData)
if err != nil {
log.Fatal("Error during Unmarshall(): ", err)
}
return fileData
}
func TestGetIndexDate(t *testing.T) {
now := time.Now()
expectedX, expectedY := getDateIndex(now)
actualDate := getIndexDate(expectedX, expectedY)
actualX, actualY := getDateIndex(actualDate)
if actualDate.Month() != now.Month() &&
actualDate.Year() != now.Year() &&
actualDate.Day() != now.Day() {
t.Fatalf("Date doesn't match: %v %v", actualDate, now)
}
if actualX != expectedX || actualY != expectedY {
t.Fatalf("Index Date doesn't match")
}
}
func TestManyDates(t *testing.T) {
now := time.Now()
for i := 0; i < 350; i++ {
expectedX, expectedY := getDateIndex(now)
actualDate := getIndexDate(expectedX, expectedY)
actualX, actualY := getDateIndex(actualDate)
if actualDate.Month() != now.Month() ||
actualDate.Year() != now.Year() ||
actualDate.Day() != now.Day() {
t.Fatalf("Date doesn't match: %v %v", actualDate, now)
}
if actualX != expectedX || actualY != expectedY {
t.Fatalf("Index Date doesn't match. now: %v \n actualDate: %v \n actualXY: %v, %v \n expectedXY: %v, %v",
now,
actualDate,
actualX, actualY,
expectedX, expectedY)
}
now = now.AddDate(0, 0, -1)
}
}
func TestFileDates(t *testing.T) {
// Get list of dates from file
calData := readFromFile("./tests/test.json")
for _, v := range calData {
expectedX, expectedY := getDateIndex(v.Date)
actualDate := getIndexDate(expectedX, expectedY)
actualX, actualY := getDateIndex(actualDate)
if actualDate.Month() != v.Date.Local().Month() ||
actualDate.Year() != v.Date.Local().Year() ||
actualDate.Day() != v.Date.Local().Day() {
t.Fatalf("Date doesn't match: \nresult: %v %v (%v %v) \nexpected:%v %v (%v %v)", actualDate, actualDate.Weekday(), actualX, actualY, v.Date.Local(), v.Date.Local().Weekday(), expectedX, expectedY)
}
if actualX != expectedX || actualY != expectedY {
t.Fatalf("Index Date doesn't match. testdate: %v \n actualDate: %v \n actualXY: %v, %v \n expectedXY: %v, %v",
v.Date,
actualDate,
actualX, actualY,
expectedX, expectedY)
}
}
}
func TestDateOutsideRange(t *testing.T) {
now := time.Now()
dateInRange := now.AddDate(0, -1, -1)
var testData []CalDataPoint
parseCalToView(testData)
testData = append(testData, CalDataPoint{Date: dateInRange, Value: 1.0})
parseCalToView(testData)
testData = append(testData, CalDataPoint{Date: now, Value: 0.0})
parseCalToView(testData)
dateOutsideRange := now.AddDate(-1, -1, 0)
dataOutsideRange := CalDataPoint{Date: dateOutsideRange, Value: 0.5}
testData = append(testData, dataOutsideRange)
parseCalToView(testData)
dateOutsideRangeFuture := now.AddDate(0, 1, 0)
dataOutsideRangeFuture := CalDataPoint{Date: dateOutsideRangeFuture, Value: 0.5}
testData = append(testData, dataOutsideRangeFuture)
parseCalToView(testData)
}