-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexport_test.go
152 lines (127 loc) · 5.9 KB
/
export_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package datatable_test
import (
"encoding/json"
"testing"
"github.com/datasweet/datatable"
"github.com/stretchr/testify/assert"
)
func sampleForExport(t *testing.T) *datatable.DataTable {
customers := datatable.New("Customers")
err := customers.AddColumn("id", datatable.Int)
assert.NoError(t, err)
err = customers.AddColumn("prenom", datatable.String)
assert.NoError(t, err)
err = customers.AddColumn("nom", datatable.String)
assert.NoError(t, err)
//dc.Hidden(true)
err = customers.AddColumn("expr_nom", datatable.String, datatable.Expr("`prenom` ~ ' ' ~ UPPER(`nom`)"))
assert.NoError(t, err)
//dc.Label("nom")
err = customers.AddColumn("email", datatable.String)
assert.NoError(t, err)
err = customers.AddColumn("ville", datatable.String)
assert.NoError(t, err)
customers.AppendRow(1, "Aimée", "Marechal", nil, "aime.marechal@example.com", "Paris")
customers.AppendRow(2, "Esmée", "Lefort", nil, "esmee.lefort@example.com", "Lyon")
customers.AppendRow(3, "Marine", "Prevost", nil, "m.prevost@example.com", "Lille")
customers.AppendRow(4, "Luc", "Rolland", nil, "lucrolland@example.com", "Marseille")
// Change structs
assert.NoError(t, customers.RenameColumn("id", "Client ID"))
customers.HideColumn("prenom")
customers.HideColumn("nom")
assert.Error(t, customers.RenameColumn("expr_nom", "nom"))
assert.NoError(t, customers.RenameColumn("expr_nom", "Nom"))
checkTable(t, customers,
"Client ID", "Nom", "email", "ville",
1, "Aimée MARECHAL", "aime.marechal@example.com", "Paris",
2, "Esmée LEFORT", "esmee.lefort@example.com", "Lyon",
3, "Marine PREVOST", "m.prevost@example.com", "Lille",
4, "Luc ROLLAND", "lucrolland@example.com", "Marseille",
)
return customers
}
func TestToTable(t *testing.T) {
dt := sampleForExport(t)
out := dt.ToTable()
assert.NotNil(t, out)
expected := `[
["Client ID", "Nom", "email", "ville"],
[1, "Aimée MARECHAL", "aime.marechal@example.com", "Paris"],
[2, "Esmée LEFORT", "esmee.lefort@example.com", "Lyon"],
[3, "Marine PREVOST", "m.prevost@example.com", "Lille"],
[4, "Luc ROLLAND", "lucrolland@example.com", "Marseille"]
]`
bytes, err := json.Marshal(out)
assert.NoError(t, err)
assert.JSONEq(t, expected, string(bytes))
out2 := dt.ToTable(datatable.ExportHidden(true))
assert.NotNil(t, out2)
expected2 := `[
["Client ID", "prenom", "nom", "Nom", "email", "ville"],
[1, "Aimée", "Marechal", "Aimée MARECHAL", "aime.marechal@example.com", "Paris"],
[2, "Esmée", "Lefort", "Esmée LEFORT", "esmee.lefort@example.com", "Lyon"],
[3, "Marine", "Prevost", "Marine PREVOST", "m.prevost@example.com", "Lille"],
[4, "Luc", "Rolland", "Luc ROLLAND", "lucrolland@example.com", "Marseille"]
]`
bytes, err = json.Marshal(out2)
assert.NoError(t, err)
assert.JSONEq(t, expected2, string(bytes))
}
func TestToMap(t *testing.T) {
dt := sampleForExport(t)
out := dt.ToMap()
assert.NotNil(t, out)
expected := `[
{ "Client ID":1, "Nom":"Aimée MARECHAL", "email":"aime.marechal@example.com", "ville":"Paris" },
{ "Client ID":2, "Nom":"Esmée LEFORT", "email":"esmee.lefort@example.com", "ville":"Lyon" },
{ "Client ID":3, "Nom":"Marine PREVOST", "email":"m.prevost@example.com", "ville":"Lille" },
{ "Client ID":4, "Nom":"Luc ROLLAND", "email":"lucrolland@example.com", "ville":"Marseille" }
]`
bytes, err := json.Marshal(out)
assert.NoError(t, err)
assert.JSONEq(t, expected, string(bytes))
out2 := dt.ToMap(datatable.ExportHidden(true))
assert.NotNil(t, out2)
expected2 := `[
{ "Client ID":1, "prenom": "Aimée", "nom": "Marechal", "Nom":"Aimée MARECHAL", "email":"aime.marechal@example.com", "ville":"Paris" },
{ "Client ID":2, "prenom": "Esmée", "nom": "Lefort", "Nom":"Esmée LEFORT", "email":"esmee.lefort@example.com", "ville":"Lyon" },
{ "Client ID":3, "prenom": "Marine", "nom": "Prevost", "Nom":"Marine PREVOST", "email":"m.prevost@example.com", "ville":"Lille" },
{ "Client ID":4, "prenom": "Luc", "nom": "Rolland", "Nom":"Luc ROLLAND", "email":"lucrolland@example.com", "ville":"Marseille" }
]`
bytes, err = json.Marshal(out2)
assert.NoError(t, err)
assert.JSONEq(t, expected2, string(bytes))
}
func TestToSchema(t *testing.T) {
dt := sampleForExport(t)
schema := dt.ToSchema()
assert.NotNil(t, schema)
assert.Equal(t, "Customers", schema.Name)
assert.Equal(t, []datatable.SchemaColumn{
datatable.SchemaColumn{"Client ID", "NullInt"},
datatable.SchemaColumn{"Nom", "NullString"},
datatable.SchemaColumn{"email", "NullString"},
datatable.SchemaColumn{"ville", "NullString"},
}, schema.Columns)
assert.Len(t, schema.Rows, 4)
assert.Equal(t, []interface{}{1, "Aimée MARECHAL", "aime.marechal@example.com", "Paris"}, schema.Rows[0])
assert.Equal(t, []interface{}{2, "Esmée LEFORT", "esmee.lefort@example.com", "Lyon"}, schema.Rows[1])
assert.Equal(t, []interface{}{3, "Marine PREVOST", "m.prevost@example.com", "Lille"}, schema.Rows[2])
assert.Equal(t, []interface{}{4, "Luc ROLLAND", "lucrolland@example.com", "Marseille"}, schema.Rows[3])
schema2 := dt.ToSchema(datatable.ExportHidden(true))
assert.NotNil(t, schema2)
assert.Equal(t, "Customers", schema2.Name)
assert.Equal(t, []datatable.SchemaColumn{
datatable.SchemaColumn{"Client ID", "NullInt"},
datatable.SchemaColumn{"prenom", "NullString"},
datatable.SchemaColumn{"nom", "NullString"},
datatable.SchemaColumn{"Nom", "NullString"},
datatable.SchemaColumn{"email", "NullString"},
datatable.SchemaColumn{"ville", "NullString"},
}, schema2.Columns)
assert.Len(t, schema2.Rows, 4)
assert.Equal(t, []interface{}{1, "Aimée", "Marechal", "Aimée MARECHAL", "aime.marechal@example.com", "Paris"}, schema2.Rows[0])
assert.Equal(t, []interface{}{2, "Esmée", "Lefort", "Esmée LEFORT", "esmee.lefort@example.com", "Lyon"}, schema2.Rows[1])
assert.Equal(t, []interface{}{3, "Marine", "Prevost", "Marine PREVOST", "m.prevost@example.com", "Lille"}, schema2.Rows[2])
assert.Equal(t, []interface{}{4, "Luc", "Rolland", "Luc ROLLAND", "lucrolland@example.com", "Marseille"}, schema2.Rows[3])
}