Skip to content

Commit

Permalink
chore: add lagoondb ProjectGroupIDs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smlx committed Oct 10, 2024
1 parent b3f1036 commit 343e6fd
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion internal/lagoondb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func TestLastUsed(t *testing.T) {
} else {
assert.NoError(tt, err, name)
}
// check expectations
// expect an error here in the expectError case because WithArgs has not
// be called with the expected value.
err = mock.ExpectationsWereMet()
if tc.expectError {
assert.Error(tt, err, name)
Expand All @@ -59,3 +60,57 @@ func TestLastUsed(t *testing.T) {
})
}
}

func TestProjectGroupIDs(t *testing.T) {
var testCases = map[string]struct {
projectID int
expectError bool
rows *sqlmock.Rows
error error
}{
"single-group project": {
projectID: 12,
expectError: false,
rows: sqlmock.NewRows([]string{"group_id"}).
AddRow("d79a42a6-a5b0-4d37-a1dd-44c2b1f6fddc"),
},
"multi-group project": {
projectID: 12,
expectError: false,
rows: sqlmock.NewRows([]string{"group_id"}).
AddRow("486765ce-14ec-4ad8-a454-e026b8cc52a4").
AddRow("d79a42a6-a5b0-4d37-a1dd-44c2b1f6fddc"),
},
"no results": {
projectID: 12,
expectError: true,
rows: sqlmock.NewRows([]string{"group_id"}),
error: lagoondb.ErrNoResult,
},
}
for name, tc := range testCases {
t.Run(name, func(tt *testing.T) {
// set up mocks
mockDB, mock, err := sqlmock.New()
assert.NoError(tt, err, name)
mock.ExpectQuery(
`SELECT group_id ` +
`FROM kc_group_projects ` +
`WHERE project_id = (.+)`).
WithArgs(tc.projectID).
WillReturnRows(tc.rows).
WillReturnError(tc.error)
// execute expected database operations
db := lagoondb.NewClientFromDB(mockDB)
_, err = db.ProjectGroupIDs(context.Background(), tc.projectID)
if tc.expectError {
assert.Error(tt, err, name)
} else {
assert.NoError(tt, err, name)
}
// check expectations
err = mock.ExpectationsWereMet()
assert.NoError(tt, err, name)
})
}
}

0 comments on commit 343e6fd

Please sign in to comment.