-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathdashboard_test.go
More file actions
75 lines (62 loc) · 2.44 KB
/
Copy pathdashboard_test.go
File metadata and controls
75 lines (62 loc) · 2.44 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
package dresources
import (
"encoding/json"
"strings"
"testing"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/libs/structs/structpath"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDashboardState_JSONSerialization_PublishedField(t *testing.T) {
state := &DashboardState{
DashboardConfig: resources.DashboardConfig{
DisplayName: "test-dashboard",
WarehouseId: "warehouse123",
},
Published: true,
}
data, err := json.Marshal(state)
require.NoError(t, err)
assert.Contains(t, string(data), `"published":true`)
}
func TestDashboardCompactState(t *testing.T) {
state := &DashboardState{
DashboardConfig: resources.DashboardConfig{
DisplayName: "test-dashboard",
Etag: "etag-123",
SerializedDashboard: `{"pages":[{"name":"p1"}]}`,
},
}
out, err := CompactState(GetResourceConfig("dashboards"), state)
require.NoError(t, err)
compacted := out.(*DashboardState)
// serialized_dashboard is replaced by a content hash; other fields are preserved.
require.IsType(t, "", compacted.SerializedDashboard)
assert.True(t, strings.HasPrefix(compacted.SerializedDashboard.(string), stateHashPrefix))
assert.Equal(t, "test-dashboard", compacted.DisplayName)
assert.Equal(t, "etag-123", compacted.Etag)
// The original state is not mutated.
assert.Equal(t, `{"pages":[{"name":"p1"}]}`, state.SerializedDashboard)
// Compacting is idempotent.
out2, err := CompactState(GetResourceConfig("dashboards"), compacted)
require.NoError(t, err)
assert.Equal(t, compacted.SerializedDashboard, out2.(*DashboardState).SerializedDashboard)
}
// TestDashboardSerializedDashboardStateRules guards the SHA-only invariant. The field
// must be declared hashed_in_state (so it is persisted as a hash) and, because the hash
// can never equal the full-content remote value, it must also be ignore_remote_changes.
func TestDashboardSerializedDashboardStateRules(t *testing.T) {
cfg := GetResourceConfig("dashboards")
path := structpath.NewStringKey(nil, "serialized_dashboard")
hasRule := func(rules []FieldRule) bool {
for _, rule := range rules {
if path.HasPatternPrefix(rule.Field) {
return true
}
}
return false
}
assert.True(t, hasRule(cfg.HashedInState), "serialized_dashboard must be declared hashed_in_state")
assert.True(t, hasRule(cfg.IgnoreRemoteChanges), "serialized_dashboard must be ignore_remote_changes for SHA-only state to be correct")
}