forked from kshedden/datareader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstata_reader_test.go
123 lines (103 loc) · 2.21 KB
/
stata_reader_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
package datareader
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func stataBaseTest(fnameCsv, fnameStata string) bool {
f, err := os.Open(filepath.Join("test_files", "data", fnameCsv))
if err != nil {
logerr(err)
return false
}
defer f.Close()
rt := NewCSVReader(f)
rt.HasHeader = true
dt, err := rt.Read(-1)
if err != nil {
logerr(err)
return false
}
r, err := os.Open(filepath.Join("test_files", "data", fnameStata))
if err != nil {
logerr(err)
return false
}
stata, err := NewStataReader(r)
if err != nil {
logerr(err)
return false
}
defer r.Close()
stata.InsertCategoryLabels = false
// Both test files have 10 rows.
if stata.RowCount() != 10 {
return false
}
// The test files have the same column names
if len(stata.ColumnNames()) != 100 {
return false
}
for j, na := range stata.ColumnNames() {
if na != fmt.Sprintf("column%d", j+1) {
return false
}
}
ds, err := stata.Read(-1)
if err != nil {
return false
}
for j := 0; j < len(ds); j++ {
ds[j].UpcastNumeric()
}
formats := stata.Formats
base := time.Date(1960, 1, 1, 0, 0, 0, 0, time.UTC)
for j := 0; j < len(ds); j++ {
ds[j] = ds[j].UpcastNumeric()
if strings.Contains(formats[j], "%td") {
dt[j] = dt[j].ForceNumeric()
dt[j], err = dt[j].DateFromDuration(base, "days")
if err != nil {
logerr(err)
return false
}
}
}
fl, jx, ix := SeriesArray(ds).AllClose(dt, 1e-6)
if !fl {
if ix == -1 {
fmt.Printf("Unequal lengths\n")
} else if ix == -2 {
fmt.Printf("Unequal types\n")
} else {
fmt.Printf("Unequal values at column %d row %d\n", jx, ix)
ds[jx].Print()
dt[jx].Print()
}
return false
}
return true
}
func TestStata1(t *testing.T) {
fnames := []string{"test1_115.dta", "test1_115b.dta", "test1_117.dta", "test1_118.dta"}
for _, fname := range fnames {
r := stataBaseTest("test1.csv", fname)
if !r {
fmt.Printf("Failed on file '%s'", fname)
t.Fail()
}
}
}
func TestStata2(t *testing.T) {
fnames := []string{"test2_115.dta", "test2_115b.dta", "test2_117.dta", "test2_118.dta"}
for _, fname := range fnames {
r := stataBaseTest("test2.csv", fname)
if !r {
fmt.Printf("Failed on file '%s'", fname)
t.Fail()
}
}
}