-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataStore_test.go
94 lines (85 loc) · 2.97 KB
/
dataStore_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
package durcov
import (
"os"
"testing"
"time"
)
func TestDataStore(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.Error(err)
}
dataStore.StoreData(exampleData)
tests := map[string]func(t *testing.T){
"Global data in database matches input": func(t *testing.T) {
var confirmed int64
var deaths int64
var recovered int64
var time time.Time
err := pool.QueryRow("SELECT confirmed, deaths, recovered, collected_at FROM covid_stats WHERE id='GLOBAL';").Scan(&confirmed, &deaths, &recovered, &time)
if err != nil {
t.Error(err)
}
if confirmed != exampleData.global.stats.totalConfirmed {
t.Errorf("global confirmed mismatch. Expected=%d Got=%d", exampleData.global.stats.totalConfirmed, confirmed)
}
if deaths != exampleData.global.stats.totalDeaths {
t.Errorf("global deaths mismatch. Expected=%d Got=%d", exampleData.global.stats.totalDeaths, deaths)
}
if recovered != exampleData.global.stats.totalRecovered {
t.Errorf("global recovered mismatch. Expected=%d Got=%d", exampleData.global.stats.totalRecovered, recovered)
}
if time != exampleData.global.stats.date {
t.Errorf("global collected at time mismatch. Expected=%v Got=%v", exampleData.global.stats.date, time)
}
},
"Countries data in database matches input": func(t *testing.T) {
for _, country := range exampleData.countries {
var name string
var slug string
var id string
var confirmed int64
var deaths int64
var recovered int64
var time time.Time
countryCode := country.code
err := pool.QueryRow("SELECT id, slug, name, confirmed, deaths, recovered, collected_at FROM covid_stats WHERE id=$1;", countryCode).Scan(&id, &slug, &name, &confirmed, &deaths, &recovered, &time)
if err != nil {
t.Error(err)
}
if id != countryCode {
t.Errorf("Country code mismatch. Expected=%s Got=%s", countryCode, id)
}
if name != country.name {
t.Errorf("Country name mismatch. Expected=%s Got=%s", country.name, name)
}
if slug != country.slug {
t.Errorf("Country slug mismatch. Expected=%s Got=%s", country.slug, slug)
}
if confirmed != country.stats.totalConfirmed {
t.Errorf("Country confirmed mismatch. Expected=%d Got=%d", country.stats.totalConfirmed, confirmed)
}
if deaths != country.stats.totalDeaths {
t.Errorf("Country deaths mismatch. Expected=%d Got=%d", country.stats.totalDeaths, deaths)
}
if recovered != country.stats.totalRecovered {
t.Errorf("Country recovered mismatch. Expected=%d Got=%d", country.stats.totalRecovered, recovered)
}
if time != country.stats.date {
t.Errorf("Country collected at time mismatch. Expected=%v Got=%v", country.stats.date, time)
}
}
},
}
for name, test := range tests {
t.Run(name, test)
}
}