Skip to content

Commit bbbe7aa

Browse files
committed
add guard rail
1 parent e2d1545 commit bbbe7aa

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

coderd/database/modelqueries_internal_test.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package database
22

33
import (
4-
"fmt"
54
"testing"
65

76
"github.com/stretchr/testify/require"
@@ -19,16 +18,35 @@ func TestIsAuthorizedQuery(t *testing.T) {
1918

2019
// TestWorkspaceTableConvert verifies all workspace fields are converted
2120
// when reducing a `Workspace` to a `WorkspaceTable`.
21+
// This test is a guard rail to prevent developer oversight mistakes.
2222
func TestWorkspaceTableConvert(t *testing.T) {
2323
t.Parallel()
2424

25+
staticRandoms := &testutil.Random{
26+
String: func() string { return "foo" },
27+
Bool: func() bool { return true },
28+
Int: func() int64 { return 500 },
29+
Uint: func() uint64 { return 126 },
30+
Float: func() float64 { return 3.14 },
31+
Complex: func() complex128 { return 6.24 },
32+
}
33+
34+
// This feels a bit janky, but it works.
35+
// If you use 'PopulateStruct' to create 2 workspaces, using the same
36+
// "random" values for each type. Then they should be identical.
37+
//
38+
// So if 'workspace.WorkspaceTable()' was missing any fields in its
39+
// conversion, the comparison would fail.
40+
2541
var workspace Workspace
26-
err := testutil.PopulateStruct(&workspace, nil)
42+
err := testutil.PopulateStruct(&workspace, staticRandoms)
2743
require.NoError(t, err)
2844

29-
workspace.WorkspaceTable()
30-
require.JSONEq(t)
31-
32-
fmt.Println(workspace)
45+
var subset WorkspaceTable
46+
err = testutil.PopulateStruct(&subset, staticRandoms)
47+
require.NoError(t, err)
3348

49+
require.Equal(t, workspace.WorkspaceTable(), subset,
50+
"'workspace.WorkspaceTable()' is not missing at least 1 field when converting to 'WorkspaceTable'. "+
51+
"To resolve this, go to the 'func (w Workspace) WorkspaceTable()' and ensure all fields are converted.")
3452
}

0 commit comments

Comments
 (0)