-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtypedescription_test.go
122 lines (99 loc) · 3.84 KB
/
typedescription_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
package orc
import (
"testing"
)
func TestTypeDescriptionParse(t *testing.T) {
description := NewStringPosition("struct<f1:int,f2:string,f3:decimal(38,10)>")
td, err := description.parseType()
if err != nil {
t.Fatal(err)
}
expected := "struct<f1:int,f2:string,f3:decimal(38,10)>"
if td.String() != expected {
t.Errorf("Test failed, expected %s got %s", expected, td.String())
}
expectedJSON := `{"category": "struct", "id": 0, "max": 3, "fields": {"f1": {"category": "int", "id": 1, "max": 1},"f2": {"category": "string", "id": 2, "max": 2},"f3": {"category": "decimal", "id": 3, "max": 3, "precision": 38, "scale": 10}}}`
if td.ToJSON() != expectedJSON {
t.Errorf("Test failed, expected %s got %s", expected, td.ToJSON())
}
description = NewStringPosition(`struct<f1:int,f2:string,f3:decimal(38,10),f4:array<struct<f5:int,f6:string>>>`)
td, err = description.parseType()
if err != nil {
t.Fatal(err)
}
expected = "struct<f1:int,f2:string,f3:decimal(38,10),f4:array<struct<f5:int,f6:string>>>"
if td.String() != expected {
t.Errorf("Test failed, expected %s got %s", expected, td.String())
}
expectedJSON = `{"category": "struct", "id": 0, "max": 7, "fields": {"f1": {"category": "int", "id": 1, "max": 1},"f2": {"category": "string", "id": 2, "max": 2},"f3": {"category": "decimal", "id": 3, "max": 3, "precision": 38, "scale": 10},"f4": {"category": "array", "id": 4, "max": 7, "children": [{"category": "struct", "id": 5, "max": 7, "fields": {"f5": {"category": "int", "id": 6, "max": 6},"f6": {"category": "string", "id": 7, "max": 7}}}]}}}`
if td.ToJSON() != expectedJSON {
t.Errorf("Test failed, expected %s got %s", expected, td.ToJSON())
}
description = NewStringPosition(`STRUCT<
f1: MAP<STRING,STRING>
>`)
td, err = description.parseType()
if err != nil {
t.Fatal(err)
}
expected = `struct<f1:map<string,string>>`
if td.String() != expected {
t.Errorf("Test failed, expected %s got %s", expected, td.String())
}
expectedJSON = `{"category": "struct", "id": 0, "max": 3, "fields": {"f1": {"category": "map", "id": 1, "max": 3, "children": [{"category": "string", "id": 2, "max": 2},{"category": "string", "id": 3, "max": 3}]}}}`
if td.ToJSON() != expectedJSON {
t.Errorf("Test failed, expected %s got %s", expected, td.ToJSON())
}
}
func TestTypeDescriptionPrint(t *testing.T) {
td, err := NewTypeDescription(
SetCategory(CategoryStruct),
AddField(
"f1",
SetCategory(CategoryInt),
),
AddField(
"f2",
SetCategory(CategoryString),
),
AddField(
"f3",
SetCategory(CategoryDecimal),
),
)
if err != nil {
t.Fatal(err)
}
expected := "struct<f1:int,f2:string,f3:decimal(38,10)>"
if td.String() != expected {
t.Errorf("Test failed, expected %s got %s", expected, td.String())
}
expected = `{"category": "struct", "id": 0, "max": 3, "fields": {"f1": {"category": "int", "id": 1, "max": 1},"f2": {"category": "string", "id": 2, "max": 2},"f3": {"category": "decimal", "id": 3, "max": 3, "precision": 38, "scale": 10}}}`
if td.ToJSON() != expected {
t.Errorf("Test failed, expected %s got %s", expected, td.ToJSON())
}
td2, err := NewTypeDescription(
SetCategory(CategoryStruct),
AddField(
"f1",
SetCategory(CategoryUnion),
AddUnionChild(
SetCategory(CategoryByte),
),
AddUnionChild(
SetCategory(CategoryDecimal),
),
),
)
if err != nil {
t.Fatal(err)
}
expected = "struct<f1:uniontype<tinyint,decimal(38,10)>>"
if td2.String() != expected {
t.Errorf("Test failed, expected %s got %s", expected, td2.String())
}
expected = `{"category": "struct", "id": 0, "max": 3, "fields": {"f1": {"category": "uniontype", "id": 1, "max": 3, "children": [{"category": "tinyint", "id": 2, "max": 2},{"category": "decimal", "id": 3, "max": 3, "precision": 38, "scale": 10}]}}}`
if td2.ToJSON() != expected {
t.Errorf("Test failed, expected %s got %s", expected, td2.ToJSON())
}
}