-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataView_test.go
104 lines (96 loc) · 3.03 KB
/
dataView_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
package durcov
import (
"os"
"testing"
)
func TestDataView(t *testing.T) {
dbURL := os.Getenv("TEST_DATABASE_URL")
pool, err := GetPgxPool(dbURL)
if err != nil {
t.Error(err)
}
defer pool.Close()
dataStore := CovidDataStore{}
dataStore.SetDBConnection(pool)
exampleData, err := ExampleTestData()
if err != nil {
t.Fatal(err)
}
err = dataStore.StoreData(exampleData)
if err != nil {
t.Fatal(err)
}
dataView := CovidBotView{}
dataView.SetDBConnection(pool)
tests := map[string]func(t *testing.T){
"Total global deaths view": func(t *testing.T) {
globalDeaths, err := dataView.LatestGlobalView(Deaths)
if err != nil {
t.Error(err)
}
if globalDeaths != exampleData.global.stats.totalDeaths {
t.Errorf("Total global deaths mismatch. Expected=%d Got=%d", exampleData.global.stats.totalDeaths, globalDeaths)
}
},
"Total global active view": func(t *testing.T) {
globalActive, err := dataView.LatestGlobalView(Active)
if err != nil {
t.Error(err)
}
expectdActive := calculateActive(
exampleData.global.stats.totalConfirmed,
exampleData.global.stats.totalDeaths,
exampleData.global.stats.totalRecovered,
)
if globalActive != expectdActive {
t.Errorf("Total global active mismatch. Expected=%d Got=%d", expectdActive, globalActive)
}
},
"Countries deaths view": func(t *testing.T) {
for _, country := range exampleData.countries {
countryName, countryDeaths, err := dataView.LatestCountryView(country.code, Deaths)
if err != nil {
t.Error(err)
}
if countryDeaths != country.stats.totalDeaths {
t.Errorf("country deaths mismatch for country=%s. Expected=%d Got=%d", country.name, country.stats.totalDeaths, countryDeaths)
}
if countryName != country.name {
t.Errorf("country name mismatch. Expected=%s Got=%s", country.name, countryName)
}
}
},
"Countries active view": func(t *testing.T) {
for _, country := range exampleData.countries {
countryName, countryActive, err := dataView.LatestCountryView(country.code, Active)
if err != nil {
t.Error(err)
}
expectedActive := calculateActive(
country.stats.totalConfirmed,
country.stats.totalDeaths,
country.stats.totalRecovered,
)
if countryActive != expectedActive {
t.Errorf("country active mismatch for country=%s. Expected=%d Got=%d", country.name, expectedActive, countryActive)
}
if countryName != country.name {
t.Errorf("country name mismatch. Expected=%s Got=%s", country.name, countryName)
}
}
},
"Unmathced country code returns specific error": func(t *testing.T) {
_, _, err := dataView.LatestCountryView("--", Active)
if err, ok := err.(*NoCountryMatchedError); !ok {
t.Errorf("Unexpected error. Expected=*NoCountryMatchedError Got=%T", err)
}
_, _, err = dataView.LatestCountryView("--", Deaths)
if err, ok := err.(*NoCountryMatchedError); !ok {
t.Errorf("Unexpected error. Expected=*NoCountryMatchedError Got=%T", err)
}
},
}
for name, test := range tests {
t.Run(name, test)
}
}