From 8e7e48a9ecf07a881b215c777b069b3076fa101d Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Tue, 8 Oct 2024 14:23:19 +0800 Subject: [PATCH] chore: add lagoondb ProjectGroupIDs tests --- internal/lagoondb/client_test.go | 57 +++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/internal/lagoondb/client_test.go b/internal/lagoondb/client_test.go index 1b74cf9..1fe3c74 100644 --- a/internal/lagoondb/client_test.go +++ b/internal/lagoondb/client_test.go @@ -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) @@ -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) + }) + } +}