-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathblock_data_table_test.go
More file actions
181 lines (158 loc) · 4.76 KB
/
block_data_table_test.go
File metadata and controls
181 lines (158 loc) · 4.76 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package slack
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewDataTableBlock(t *testing.T) {
block := NewDataTableBlock("A Fabulous Table",
DataTableBlockOptionBlockID("dt-1"),
).
WithPageSize(10).
WithRowHeaderColumnIndex(1)
assert.Equal(t, MBTDataTable, block.BlockType())
assert.Equal(t, "data_table", string(block.Type))
assert.Equal(t, "dt-1", block.ID())
assert.Equal(t, "A Fabulous Table", block.Caption)
assert.Equal(t, 10, block.PageSize)
assert.Equal(t, 1, block.RowHeaderColumnIndex)
assert.Empty(t, block.Rows)
}
func TestNewDataTableBlockWithNilOption(t *testing.T) {
assert.NotPanics(t, func() {
NewDataTableBlock("caption", nil)
}, "should not panic when nil option passed")
}
func TestDataTableBlockAddRow(t *testing.T) {
block := NewDataTableBlock("caption")
block.AddRow(NewDataTableRawTextCell("Name"), NewDataTableRawTextCell("Score"))
block.AddRow(
NewDataTableRawTextCell("Helly"),
NewDataTableRawNumberCell(42).WithText("forty-two"),
)
require.Len(t, block.Rows, 2)
require.Len(t, block.Rows[0], 2)
require.Len(t, block.Rows[1], 2)
assert.Equal(t, DataTableCellRawText, block.Rows[0][0].DataTableCellType())
assert.Equal(t, DataTableCellRawNumber, block.Rows[1][1].DataTableCellType())
num, ok := block.Rows[1][1].(*DataTableRawNumberCell)
require.True(t, ok)
assert.Equal(t, float64(42), num.Value)
assert.Equal(t, "forty-two", num.Text)
}
func TestDataTableBlockJSONRoundTrip(t *testing.T) {
payload := `{
"type": "data_table",
"block_id": "dt-1",
"caption": "A Fabulous Table",
"page_size": 5,
"rows": [
[
{"type": "raw_text", "text": "Name"},
{"type": "raw_text", "text": "Department"},
{"type": "raw_text", "text": "Badge"}
],
[
{"type": "raw_text", "text": "Data Refinement Department"},
{"type": "raw_text", "text": "MDR"},
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [
{"type": "text", "text": "Blue", "style": {"bold": true}}
]
}
]
}
],
[
{"type": "raw_text", "text": "Wellness Department"},
{"type": "raw_number", "value": 7, "text": "seven"},
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [
{"type": "text", "text": "Limited", "style": {"bold": true}}
]
}
]
}
]
]
}`
var block DataTableBlock
require.NoError(t, json.Unmarshal([]byte(payload), &block))
assert.Equal(t, MBTDataTable, block.BlockType())
assert.Equal(t, "dt-1", block.ID())
assert.Equal(t, "A Fabulous Table", block.Caption)
assert.Equal(t, 5, block.PageSize)
require.Len(t, block.Rows, 3)
require.Len(t, block.Rows[0], 3)
header, ok := block.Rows[0][0].(*DataTableRawTextCell)
require.True(t, ok)
assert.Equal(t, "Name", header.Text)
num, ok := block.Rows[2][1].(*DataTableRawNumberCell)
require.True(t, ok)
assert.Equal(t, float64(7), num.Value)
assert.Equal(t, "seven", num.Text)
rich, ok := block.Rows[1][2].(*DataTableRichTextCell)
require.True(t, ok)
require.Len(t, rich.Elements, 1)
section, ok := rich.Elements[0].(*RichTextSection)
require.True(t, ok)
require.Len(t, section.Elements, 1)
text, ok := section.Elements[0].(*RichTextSectionTextElement)
require.True(t, ok)
assert.Equal(t, "Blue", text.Text)
require.NotNil(t, text.Style)
assert.True(t, text.Style.Bold)
marshalled, err := json.Marshal(block)
require.NoError(t, err)
var expected, actual map[string]any
require.NoError(t, json.Unmarshal([]byte(payload), &expected))
require.NoError(t, json.Unmarshal(marshalled, &actual))
assert.Equal(t, expected, actual)
}
func TestDataTableBlockUnmarshalViaBlocks(t *testing.T) {
payload := `[
{
"type": "data_table",
"caption": "Tiny Table",
"rows": [
[{"type": "raw_text", "text": "Col"}],
[{"type": "raw_number", "value": 1}]
]
}
]`
var blocks Blocks
require.NoError(t, json.Unmarshal([]byte(payload), &blocks))
require.Len(t, blocks.BlockSet, 1)
dt, ok := blocks.BlockSet[0].(*DataTableBlock)
require.True(t, ok, "expected *DataTableBlock, got %T", blocks.BlockSet[0])
assert.Equal(t, MBTDataTable, dt.BlockType())
assert.Equal(t, "Tiny Table", dt.Caption)
require.Len(t, dt.Rows, 2)
num, ok := dt.Rows[1][0].(*DataTableRawNumberCell)
require.True(t, ok)
assert.Equal(t, float64(1), num.Value)
assert.Empty(t, num.Text)
}
func TestDataTableBlockUnknownCellType(t *testing.T) {
payload := `{
"type": "data_table",
"caption": "Bad Cell",
"rows": [
[{"type": "raw_text", "text": "ok"}],
[{"type": "mystery_cell", "text": "huh"}]
]
}`
var block DataTableBlock
err := json.Unmarshal([]byte(payload), &block)
require.Error(t, err)
assert.Contains(t, err.Error(), "mystery_cell")
}