-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
show_create_all_tables_builtin_test.go
98 lines (91 loc) · 2.2 KB
/
show_create_all_tables_builtin_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
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package builtins
import (
"context"
"math"
"sort"
"testing"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/stretchr/testify/require"
)
func TestTopologicalSort(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
monitor := mon.NewMonitor(
"test mon",
mon.MemoryResource,
nil, /* curCount */
nil, /* maxHist */
-1, /* increment */
math.MaxInt64, /* noteworthy */
cluster.MakeTestingClusterSettings(),
)
monitor.Start(ctx, nil, mon.MakeStandaloneBudget(math.MaxInt64))
acc := monitor.MakeBoundAccount()
testCases := []struct {
name string
dependsOnIDs map[int64][]int64
expected []int64
}{
{
name: "basic test",
dependsOnIDs: map[int64][]int64{
1: {1, 2},
2: {},
3: nil,
},
expected: []int64{2, 1, 3},
},
{
name: "depends on references non-existent IDs",
dependsOnIDs: map[int64][]int64{
1: {1, 2},
2: {},
3: nil,
4: {5, 6},
6: {2, 3},
},
expected: []int64{2, 1, 3, 6, 4},
},
{
name: "handles cycles",
dependsOnIDs: map[int64][]int64{
1: {2},
2: {3},
3: {4},
4: {5},
5: {6},
6: {1},
},
expected: []int64{6, 5, 4, 3, 2, 1},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
seen := map[int64]struct{}{}
var allIDs, sorted []int64
for i := range tc.dependsOnIDs {
allIDs = append(allIDs, i)
}
// Sort by ids to guarantee stable output.
sort.Slice(allIDs, func(i, j int) bool {
return allIDs[i] < allIDs[j]
})
for _, i := range allIDs {
err := topologicalSort(ctx, i, tc.dependsOnIDs, seen, &sorted, &acc)
require.NoError(t, err)
}
require.Equal(t, tc.expected, sorted)
})
}
}