forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels_test.go
More file actions
88 lines (75 loc) · 3 KB
/
models_test.go
File metadata and controls
88 lines (75 loc) · 3 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
package database_test
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/codersdk"
)
// TestAuditDBEnumsCovered ensures that all enums in the database are covered by the codersdk enums
// for audit log strings.
func TestAuditDBEnumsCovered(t *testing.T) {
t.Parallel()
dbTypes := database.AllResourceTypeValues()
for _, ty := range dbTypes {
str := codersdk.ResourceType(ty).FriendlyString()
require.NotEqualf(t, "unknown", str, "ResourceType %q not covered by codersdk.ResourceType", ty)
}
}
// TestViewSubsetTemplate ensures TemplateTable is a subset of Template
func TestViewSubsetTemplate(t *testing.T) {
t.Parallel()
table := reflect.TypeOf(database.TemplateTable{})
joined := reflect.TypeOf(database.Template{})
tableFields := allFields(table)
joinedFields := allFields(joined)
if !assert.Subset(t, fieldNames(joinedFields), fieldNames(tableFields), "table is not subset") {
t.Log("Some fields were added to the Template Table without updating the 'template_with_users' view.")
t.Log("See migration 000138_join_users.up.sql to create the view.")
}
}
// TestViewSubsetTemplateVersion ensures TemplateVersionTable is a subset TemplateVersion
func TestViewSubsetTemplateVersion(t *testing.T) {
t.Parallel()
table := reflect.TypeOf(database.TemplateVersionTable{})
joined := reflect.TypeOf(database.TemplateVersion{})
tableFields := allFields(table)
joinedFields := allFields(joined)
if !assert.Subset(t, fieldNames(joinedFields), fieldNames(tableFields), "table is not subset") {
t.Log("Some fields were added to the TemplateVersion Table without updating the 'template_version_with_user' view.")
t.Log("See migration 000141_join_users_build_version.up.sql to create the view.")
}
}
// TestViewSubsetWorkspaceBuild ensures WorkspaceBuildTable is a subset of WorkspaceBuild
func TestViewSubsetWorkspaceBuild(t *testing.T) {
t.Parallel()
table := reflect.TypeOf(database.WorkspaceBuildTable{})
joined := reflect.TypeOf(database.WorkspaceBuild{})
tableFields := allFields(table)
joinedFields := allFields(joined)
if !assert.Subset(t, fieldNames(joinedFields), fieldNames(tableFields), "table is not subset") {
t.Log("Some fields were added to the WorkspaceBuild Table without updating the 'workspace_build_with_user' view.")
t.Log("See migration 000141_join_users_build_version.up.sql to create the view.")
}
}
func fieldNames(fields []reflect.StructField) []string {
names := make([]string, len(fields))
for i, field := range fields {
names[i] = field.Name
}
return names
}
func allFields(rt reflect.Type) []reflect.StructField {
fields := make([]reflect.StructField, 0, rt.NumField())
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
if field.Anonymous && field.Type.Kind() == reflect.Struct {
// Recurse into anonymous struct fields.
fields = append(fields, allFields(field.Type)...)
continue
}
fields = append(fields, rt.Field(i))
}
return fields
}