Skip to content

Commit 22d27f8

Browse files
committed
chore: implement unit test to test trigger with >0 workspaces
1 parent 891cbae commit 22d27f8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

coderd/database/querier_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/coder/coder/v2/coderd/database"
2222
"github.com/coder/coder/v2/coderd/database/db2sdk"
2323
"github.com/coder/coder/v2/coderd/database/dbauthz"
24+
"github.com/coder/coder/v2/coderd/database/dbfake"
2425
"github.com/coder/coder/v2/coderd/database/dbgen"
2526
"github.com/coder/coder/v2/coderd/database/dbtestutil"
2627
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -2916,6 +2917,40 @@ func TestGetUserStatusCounts(t *testing.T) {
29162917
}
29172918
}
29182919

2920+
func TestOrganizationDeleteTrigger(t *testing.T) {
2921+
t.Parallel()
2922+
2923+
if !dbtestutil.WillUsePostgres() {
2924+
t.SkipNow()
2925+
}
2926+
2927+
t.Run("WorkspaceExists", func(t *testing.T) {
2928+
db, _ := dbtestutil.NewDB(t)
2929+
2930+
orgA := dbgen.Organization(t, db, database.Organization{})
2931+
_, err := db.InsertAllUsersGroup(context.Background(), orgA.ID)
2932+
require.NoError(t, err)
2933+
2934+
user := dbgen.User(t, db, database.User{})
2935+
2936+
dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
2937+
OrganizationID: orgA.ID,
2938+
OwnerID: user.ID,
2939+
}).Do()
2940+
2941+
ctx := testutil.Context(t, testutil.WaitShort)
2942+
err = db.UpdateOrganizationDeletedByID(ctx, database.UpdateOrganizationDeletedByIDParams{
2943+
UpdatedAt: dbtime.Now(),
2944+
ID: orgA.ID,
2945+
})
2946+
require.Error(t, err)
2947+
// cannot delete organization: organization has 1 workspaces and 1 templates that must be deleted first
2948+
require.ErrorContains(t, err, "cannot delete organization")
2949+
require.ErrorContains(t, err, "has 1 workspaces")
2950+
require.ErrorContains(t, err, "1 templates")
2951+
})
2952+
}
2953+
29192954
func requireUsersMatch(t testing.TB, expected []database.User, found []database.GetUsersRow, msg string) {
29202955
t.Helper()
29212956
require.ElementsMatch(t, expected, database.ConvertUserRows(found), msg)

0 commit comments

Comments
 (0)