Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add guard rail
  • Loading branch information
Emyrk committed Oct 22, 2024
commit 90f2474649f99492c32ef306729066f463a2bba8
30 changes: 24 additions & 6 deletions coderd/database/modelqueries_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package database

import (
"fmt"
"testing"

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

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

staticRandoms := &testutil.Random{
String: func() string { return "foo" },
Bool: func() bool { return true },
Int: func() int64 { return 500 },
Uint: func() uint64 { return 126 },
Float: func() float64 { return 3.14 },
Complex: func() complex128 { return 6.24 },
}

// This feels a bit janky, but it works.
// If you use 'PopulateStruct' to create 2 workspaces, using the same
// "random" values for each type. Then they should be identical.
//
// So if 'workspace.WorkspaceTable()' was missing any fields in its
// conversion, the comparison would fail.

var workspace Workspace
err := testutil.PopulateStruct(&workspace, nil)
err := testutil.PopulateStruct(&workspace, staticRandoms)
require.NoError(t, err)

workspace.WorkspaceTable()
require.JSONEq(t)

fmt.Println(workspace)
var subset WorkspaceTable
err = testutil.PopulateStruct(&subset, staticRandoms)
require.NoError(t, err)

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