From 2ad2242e44748557db65217b87dda052cbf55f7c Mon Sep 17 00:00:00 2001 From: DS Date: Wed, 13 Mar 2024 02:54:30 +0800 Subject: [PATCH] [#12048] Add tests for FeedbackQuestionsDbIT (#12781) * Add verification during feedback question creation * Add tests for FeedbackQuestionsDb * Fix missing javadocs * Fix feedback question creation logic * Add test * Reuse error message * Add tests for FeedbackQuestionsDbIT --------- Co-authored-by: marquestye Co-authored-by: Wei Qing <48304907+weiquu@users.noreply.github.com> Co-authored-by: Cedric Ong <67156011+cedricongjh@users.noreply.github.com> --- .../storage/sqlapi/FeedbackQuestionsDbIT.java | 70 +++++++++++++++++++ .../BaseTestCaseWithSqlDatabaseAccess.java | 2 + 2 files changed, 72 insertions(+) diff --git a/src/it/java/teammates/it/storage/sqlapi/FeedbackQuestionsDbIT.java b/src/it/java/teammates/it/storage/sqlapi/FeedbackQuestionsDbIT.java index bd6cbd45604..145f4ee4ef0 100644 --- a/src/it/java/teammates/it/storage/sqlapi/FeedbackQuestionsDbIT.java +++ b/src/it/java/teammates/it/storage/sqlapi/FeedbackQuestionsDbIT.java @@ -1,6 +1,7 @@ package teammates.it.storage.sqlapi; import java.util.List; +import java.util.UUID; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; @@ -8,6 +9,8 @@ import teammates.common.datatransfer.FeedbackParticipantType; import teammates.common.datatransfer.SqlDataBundle; +import teammates.common.exception.EntityAlreadyExistsException; +import teammates.common.exception.InvalidParametersException; import teammates.common.util.HibernateUtil; import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess; import teammates.storage.sqlapi.FeedbackQuestionsDb; @@ -39,6 +42,47 @@ protected void setUp() throws Exception { HibernateUtil.flushSession(); } + @Test + public void testGetFeedbackQuestion() { + ______TS("success: typical case"); + FeedbackQuestion expectedFq = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1"); + + FeedbackQuestion actualFq = fqDb.getFeedbackQuestion(expectedFq.getId()); + + assertEquals(expectedFq, actualFq); + + ______TS("failure: does not exist, returns null"); + actualFq = fqDb.getFeedbackQuestion(UUID.randomUUID()); + assertNull(actualFq); + + ______TS("failure: null parameter, assertion error"); + assertThrows(AssertionError.class, () -> fqDb.getFeedbackQuestion(null)); + } + + @Test + public void testCreateFeedbackQuestion() throws EntityAlreadyExistsException, InvalidParametersException { + ______TS("success: typical case"); + FeedbackQuestion expectedFq = getTypicalFeedbackQuestionForSession( + getTypicalFeedbackSessionForCourse(getTypicalCourse())); + + fqDb.createFeedbackQuestion(expectedFq); + verifyPresentInDatabase(expectedFq); + + ______TS("failure: duplicate question, throws error"); + assertThrows(EntityAlreadyExistsException.class, () -> fqDb.createFeedbackQuestion(expectedFq)); + + ______TS("failure: invalid question, throws error"); + FeedbackQuestion invalidFq = getTypicalFeedbackQuestionForSession( + getTypicalFeedbackSessionForCourse(getTypicalCourse())); + invalidFq.setGiverType(FeedbackParticipantType.RECEIVER); + + assertThrows(InvalidParametersException.class, () -> fqDb.createFeedbackQuestion(invalidFq)); + assertNull(fqDb.getFeedbackQuestion(invalidFq.getId())); + + ______TS("failure: null parameter, assertion error"); + assertThrows(AssertionError.class, () -> fqDb.createFeedbackQuestion(null)); + } + @Test public void testGetFeedbackQuestionsForSession() { ______TS("success: typical case"); @@ -56,6 +100,10 @@ public void testGetFeedbackQuestionsForSession() { assertEquals(expectedQuestions.size(), actualQuestions.size()); assertTrue(expectedQuestions.containsAll(actualQuestions)); + + ______TS("failure: session does not exist, returns no questions"); + actualQuestions = fqDb.getFeedbackQuestionsForSession(UUID.randomUUID()); + assertEquals(0, actualQuestions.size()); } @Test @@ -71,6 +119,24 @@ public void testGetFeedbackQuestionsForGiverType() { assertEquals(expectedQuestions.size(), actualQuestions.size()); assertTrue(expectedQuestions.containsAll(actualQuestions)); + + ______TS("failure: session does not exist, returns no questions"); + fs = getTypicalFeedbackSessionForCourse(getTypicalCourse()); + actualQuestions = fqDb.getFeedbackQuestionsForGiverType(fs, FeedbackParticipantType.STUDENTS); + assertEquals(0, actualQuestions.size()); + } + + @Test + public void testDeleteFeedbackQuestion() { + ______TS("success: typical case"); + FeedbackQuestion fq = typicalDataBundle.feedbackQuestions.get("qn1InSession1InCourse1"); + verifyPresentInDatabase(fq); + + fqDb.deleteFeedbackQuestion(fq.getId()); + assertNull(fqDb.getFeedbackQuestion(fq.getId())); + + ______TS("failure: null parameter, assertion error"); + assertThrows(AssertionError.class, () -> fqDb.deleteFeedbackQuestion(null)); } @Test @@ -83,5 +149,9 @@ public void testHasFeedbackQuestionsForGiverType() { fs.getName(), course.getId(), FeedbackParticipantType.STUDENTS); assertTrue(actual); + + ______TS("failure: session/course does not exist, returns false"); + actual = fqDb.hasFeedbackQuestionsForGiverType("session-name", "course-id", FeedbackParticipantType.STUDENTS); + assertFalse(actual); } } diff --git a/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java b/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java index 288282e2534..f6f5adc72f5 100644 --- a/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java +++ b/src/it/java/teammates/it/test/BaseTestCaseWithSqlDatabaseAccess.java @@ -249,6 +249,8 @@ private BaseEntity getEntity(BaseEntity entity) { return logic.getCourse(((Course) entity).getId()); } else if (entity instanceof FeedbackSession) { return logic.getFeedbackSession(((FeedbackSession) entity).getId()); + } else if (entity instanceof FeedbackQuestion) { + return logic.getFeedbackQuestion(((FeedbackQuestion) entity).getId()); } else if (entity instanceof Account) { return logic.getAccount(((Account) entity).getId()); } else if (entity instanceof Notification) {