Skip to content

Commit 212ef78

Browse files
committed
started work on table tests
1 parent e516fe9 commit 212ef78

4 files changed

Lines changed: 59 additions & 25 deletions

File tree

csvw/table/table.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ func New(jsonTable map[string]interface{}) (*Table, error) {
7474
}
7575
}
7676
var pk []string
77-
for _, col := range jsonTable["tableSchema"].(map[string]interface{})["primaryKey"].([]interface{}) {
78-
val, ok := col.(string)
79-
if ok {
80-
pk = append(pk, val)
77+
jsonPk, ok := jsonTable["tableSchema"].(map[string]interface{})["primaryKey"]
78+
if ok {
79+
for _, col := range jsonPk.([]interface{}) {
80+
val, ok := col.(string)
81+
if ok {
82+
pk = append(pk, val)
83+
}
8184
}
8285
}
8386
res := &Table{

csvw/table/table_test.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package table
22

3-
/*
4-
func makeTable(url string, schema string) Table {
5-
var result map[string]interface{}
3+
import (
4+
"encoding/json"
5+
"os"
6+
"testing"
7+
)
68

7-
err := json.Unmarshal([]byte(jsonString), &result)
9+
func makeTable(fname string) Table {
10+
data, err := os.ReadFile("testdata/" + fname)
11+
if err != nil {
12+
panic(err)
13+
}
14+
var result map[string]interface{}
15+
err = json.Unmarshal(data, &result)
816
if err != nil {
917
panic(err)
1018
}
@@ -15,22 +23,27 @@ func makeTable(url string, schema string) Table {
1523
return *tbl
1624
}
1725

18-
func TestTable_t(t *testing.T) {
19-
var tests = []struct {
20-
jsonCol string
21-
input string
22-
}{
23-
{`{}`, "string"},
24-
{`{"datatype": "boolean"}`, "boolean"},
25-
{`{"datatype": {"base": "boolean"}}`, "boolean"},
26-
}
27-
for _, tt := range tests {
28-
t.Run("Datatype", func(t *testing.T) {
29-
col := makeCol(tt.jsonCol)
30-
if tt.input != col.Datatype.Base {
31-
t.Errorf(`problem: %q vs %q`, tt.input, col.Datatype.Base)
32-
}
33-
})
26+
func TestTable_simple(t *testing.T) {
27+
tbl := makeTable("table_simple.json")
28+
if len(tbl.PrimaryKey) > 0 {
29+
t.Errorf(`problem: %q vs %q`, len(tbl.PrimaryKey), 0)
30+
}
31+
if len(tbl.Columns) != 1 {
32+
t.Errorf(`problem: %q vs %q`, len(tbl.Columns), 1)
33+
}
34+
if tbl.CanonicalName != "table_simple.csv" {
35+
t.Errorf(`problem: %q vs %q`, len(tbl.CanonicalName), "table_simple.csv")
36+
}
37+
result := make(chan TableRead, 1)
38+
go tbl.Read("testdata/", result)
39+
tableRead := <-result
40+
if tableRead.Err != nil || tableRead.Url != "table_simple.csv" {
41+
t.Errorf(`problem`)
42+
}
43+
if len(tbl.Data) != 3 {
44+
t.Errorf(`problem`)
45+
}
46+
if tbl.Data[0]["ID"] != "a" {
47+
t.Errorf(`problem`)
3448
}
3549
}
36-
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ID
2+
a
3+
b
4+
c
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"url": "table_simple.csv",
3+
"tableSchema": {
4+
"columns": [
5+
{
6+
"datatype": {
7+
"base": "string",
8+
"format": "[a-zA-Z0-9_\\-]+"
9+
},
10+
"name": "ID"
11+
}
12+
]
13+
}
14+
}

0 commit comments

Comments
 (0)