7
7
"github.com/stretchr/testify/require"
8
8
9
9
"github.com/anyproto/anytype-heart/core/block/editor/state"
10
+ "github.com/anyproto/anytype-heart/core/block/simple"
10
11
"github.com/anyproto/anytype-heart/core/block/simple/base"
11
12
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
12
13
)
@@ -18,34 +19,41 @@ func TestNormalize(t *testing.T) {
18
19
want * state.State
19
20
}{
20
21
{
21
- name : "empty" ,
22
+ name : "empty table should remain empty " ,
22
23
source : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {}),
23
24
want : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {}),
24
25
},
25
26
{
26
- name : "invalid ids" ,
27
+ name : "cells with invalid ids are moved under the table " ,
27
28
source : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {
28
29
{"row1-c11" , "row1-col2" },
29
- {"row2-col3" },
30
+ {"row2-col3" , "cell" },
30
31
}),
31
32
want : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {
32
- {"row1-col2" },
33
- {},
34
- }),
33
+ {"row1-c11" , "row1-col2" },
34
+ {"row2-col3" , "cell" },
35
+ }, withChangedChildren (map [string ][]string {
36
+ "root" : {"table" , "row2-col3" , "cell" , "row1-c11" },
37
+ "row1" : {"row1-col2" },
38
+ "row2" : {},
39
+ })),
35
40
},
36
41
{
37
- name : "wrong column order" ,
42
+ name : "wrong cells order -> do sorting and move invalid cells under the table " ,
38
43
source : mkTestTable ([]string {"col1" , "col2" , "col3" }, []string {"row1" , "row2" }, [][]string {
39
44
{"row1-col3" , "row1-col1" , "row1-col2" },
40
45
{"row2-col3" , "row2-c1" , "row2-col1" },
41
46
}),
42
47
want : mkTestTable ([]string {"col1" , "col2" , "col3" }, []string {"row1" , "row2" }, [][]string {
43
48
{"row1-col1" , "row1-col2" , "row1-col3" },
44
- {"row2-col1" , "row2-col3" },
45
- }),
49
+ {"row2-col3" , "row2-c1" , "row2-col1" },
50
+ }, withChangedChildren (map [string ][]string {
51
+ "root" : {"table" , "row2-c1" },
52
+ "row2" : {"row2-col1" , "row2-col3" },
53
+ })),
46
54
},
47
55
{
48
- name : "wrong place for header rows" ,
56
+ name : "wrong place for header rows -> do sorting " ,
49
57
source : mkTestTable ([]string {"col1" , "col2" , "col3" }, []string {"row1" , "row2" , "row3" }, nil ,
50
58
withRowBlockContents (map [string ]* model.BlockContentTableRow {
51
59
"row3" : {IsHeader : true },
@@ -55,45 +63,74 @@ func TestNormalize(t *testing.T) {
55
63
"row3" : {IsHeader : true },
56
64
})),
57
65
},
66
+ {
67
+ name : "cell is a child of rows, not row -> move under the table" ,
68
+ source : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {
69
+ {"row1-col1" , "row1-col2" }, {"row2-col1" , "row2-col2" },
70
+ }, withChangedChildren (map [string ][]string {
71
+ "rows" : {"row1" , "row1-col2" , "row2" },
72
+ "row1" : {"row1-col1" },
73
+ })),
74
+ want : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {
75
+ {"row1-col1" , "row1-col2" }, {"row2-col1" , "row2-col2" },
76
+ }, withChangedChildren (map [string ][]string {
77
+ "root" : {"table" , "row1-col2" },
78
+ "row1" : {"row1-col1" },
79
+ })),
80
+ },
81
+ {
82
+ name : "columns contain invalid children -> move under the table" ,
83
+ source : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {
84
+ {"row1-col1" , "row1-col2" }, {"row2-col1" , "row2-col2" },
85
+ }, withChangedChildren (map [string ][]string {
86
+ "columns" : {"col1" , "col2" , "row1-col2" },
87
+ "row1" : {"row1-col1" },
88
+ })),
89
+ want : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {
90
+ {"row1-col1" , "row1-col2" }, {"row2-col1" , "row2-col2" },
91
+ }, withChangedChildren (map [string ][]string {
92
+ "root" : {"table" , "row1-col2" },
93
+ "row1" : {"row1-col1" },
94
+ })),
95
+ },
96
+ {
97
+ name : "table block contains invalid children -> table is dropped" ,
98
+ source : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {}, withChangedChildren (map [string ][]string {
99
+ "table" : {"columns" },
100
+ })),
101
+ want : state .NewDoc ("root" , map [string ]simple.Block {"root" : simple .New (& model.Block {Id : "root" })}).NewState (),
102
+ },
103
+ {
104
+ name : "missed column is recreated" ,
105
+ source : mkTestTable ([]string {"col1" }, []string {"row1" , "row2" }, [][]string {}, withChangedChildren (map [string ][]string {
106
+ "columns" : {"col1" , "col2" },
107
+ })),
108
+ want : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {}),
109
+ },
110
+ {
111
+ name : "missed row is recreated" ,
112
+ source : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" }, [][]string {}, withChangedChildren (map [string ][]string {
113
+ "rows" : {"row1" , "row2" },
114
+ })),
115
+ want : mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" }, [][]string {}),
116
+ },
58
117
} {
59
118
t .Run (tc .name , func (t * testing.T ) {
60
- tb , err := NewTable (tc .source , "table" )
119
+ // given
120
+ st := tc .source .Copy ()
121
+ tb := st .Pick ("table" )
122
+ require .NotNil (t , tb )
61
123
62
- require .NoError (t , err )
124
+ // when
125
+ err := tb .(Block ).Normalize (st )
63
126
64
- st := tc .source .Copy ()
65
- err = tb .block .(Block ).Normalize (st )
127
+ // then
66
128
require .NoError (t , err )
67
-
68
129
assert .Equal (t , tc .want .Blocks (), st .Blocks ())
69
130
})
70
131
}
71
132
}
72
133
73
- func TestNormalizeAbsentRow (t * testing.T ) {
74
- source := mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" , "row3" }, [][]string {
75
- {"row1-c11" , "row1-col2" },
76
- {"row2-col3" },
77
- })
78
- source .CleanupBlock ("row3" )
79
-
80
- want := mkTestTable ([]string {"col1" , "col2" }, []string {"row1" , "row2" , "row3" }, [][]string {
81
- {"row1-col2" },
82
- {},
83
- {},
84
- })
85
-
86
- tb , err := NewTable (source , "table" )
87
-
88
- require .NoError (t , err )
89
-
90
- st := source .Copy ()
91
- err = tb .block .(Block ).Normalize (st )
92
- require .NoError (t , err )
93
-
94
- assert .Equal (t , want .Blocks (), st .Blocks ())
95
- }
96
-
97
134
func TestDuplicate (t * testing.T ) {
98
135
s := mkTestTable ([]string {"col1" , "col2" , "col3" }, []string {"row1" , "row2" },
99
136
[][]string {
0 commit comments