Skip to content

Commit

Permalink
Add test for SqlSessionResultsBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
xenosf committed Feb 19, 2024
1 parent 7e960f7 commit 901307a
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package teammates.common.datatransfer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testng.annotations.Test;

import teammates.common.util.Const;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackResponse;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.test.BaseTestCase;

/**
* SUT: {@link SqlSessionResultsBundle}.
*/
public class SqlSessionResultsBundleTest extends BaseTestCase {

@Test
public void testGetQuestionResponseMap() {
SqlDataBundle responseBundle = loadSqlDataBundle("/FeedbackSessionResultsBundleTest.json");

List<String> allExpectedResponses = new ArrayList<>();
allExpectedResponses.add(responseBundle.feedbackResponses.get("response1ForQ1S1C1").toString());
allExpectedResponses.add(responseBundle.feedbackResponses.get("response2ForQ1S1C1").toString());

SqlSessionResultsBundle bundle =
new SqlSessionResultsBundle(
new ArrayList<>(responseBundle.feedbackQuestions.values()),
new ArrayList<>(responseBundle.feedbackResponses.values()),
new ArrayList<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new SqlCourseRoster(new ArrayList<>(responseBundle.students.values()),
new ArrayList<>(responseBundle.instructors.values()))
);

______TS("Test question having responses");
FeedbackQuestion fq = responseBundle.feedbackQuestions.get("qn1InSession1InCourse1");
List<FeedbackResponse> allResponses = bundle.getQuestionResponseMap().get(fq);
assertEquals(2, allResponses.size());
List<String> allResponsesString = new ArrayList<>();
allResponsesString.add(allResponses.get(0).toString());
allResponsesString.add(allResponses.get(1).toString());
assertEquals(allExpectedResponses, allResponsesString);

______TS("Test question having no responses");
fq = responseBundle.feedbackQuestions.get("qn3InSession1InCourse1");
allResponses = bundle.getQuestionResponseMap().get(fq);
assertEquals(0, allResponses.size());
}

@Test
public void testGetQuestionMissingResponseMap() {
SqlDataBundle responseBundle = loadSqlDataBundle("/FeedbackSessionResultsBundleTest.json");

List<String> expectedMissingResponses = new ArrayList<>();
expectedMissingResponses.add(responseBundle.feedbackResponses.get("response1ForQ1S1C1").toString());
expectedMissingResponses.add(responseBundle.feedbackResponses.get("response2ForQ1S1C1").toString());

SqlSessionResultsBundle bundle =
new SqlSessionResultsBundle(
new ArrayList<>(responseBundle.feedbackQuestions.values()),
new ArrayList<>(responseBundle.feedbackResponses.values()),
new ArrayList<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new SqlCourseRoster(new ArrayList<>(responseBundle.students.values()),
new ArrayList<>(responseBundle.instructors.values()))
);

______TS("Test question having missing responses");
FeedbackQuestion fq = responseBundle.feedbackQuestions.get("qn1InSession1InCourse1");
List<FeedbackResponse> missingResponses = bundle.getQuestionMissingResponseMap().get(fq);
assertEquals(2, missingResponses.size());
List<String> missingResponsesString = new ArrayList<>();
missingResponsesString.add(missingResponses.get(0).toString());
missingResponsesString.add(missingResponses.get(1).toString());
assertEquals(expectedMissingResponses, missingResponsesString);

______TS("Test question having no missing responses");
fq = responseBundle.feedbackQuestions.get("qn3InSession1InCourse1");
missingResponses = bundle.getQuestionMissingResponseMap().get(fq);
assertEquals(0, missingResponses.size());
}

@Test
public void testIsResponseGiverRecipientVisible_typicalCase_shouldReturnCorrectValues() {

SqlDataBundle responseBundle = loadSqlDataBundle("/FeedbackSessionResultsBundleTest.json");

FeedbackSession session1Course1 = getTypicalFeedbackSessionForCourse(getTypicalCourse());

FeedbackQuestion question1ForS1C1 = getTypicalFeedbackQuestionForSession(session1Course1);
FeedbackQuestion question2ForS1C1 = getTypicalFeedbackQuestionForSession(session1Course1);

FeedbackResponse response1ForQ1S1C1 = getTypicalFeedbackResponseForQuestion(question1ForS1C1);
FeedbackResponse response2ForQ1S1C1 = getTypicalFeedbackResponseForQuestion(question1ForS1C1);
FeedbackResponse response1ForQ2S1C1 = getTypicalFeedbackResponseForQuestion(question2ForS1C1);
FeedbackResponse response2ForQ2S1C1 = getTypicalFeedbackResponseForQuestion(question2ForS1C1);

Map<FeedbackResponse, Boolean> responseGiverVisibilityTable = new HashMap<>();
responseGiverVisibilityTable.put(response1ForQ1S1C1, true);
responseGiverVisibilityTable.put(response2ForQ1S1C1, false);
responseGiverVisibilityTable.put(response1ForQ2S1C1, true);
responseGiverVisibilityTable.put(response2ForQ2S1C1, false);

Map<FeedbackResponse, Boolean> responseRecipientVisibilityTable = new HashMap<>();
responseRecipientVisibilityTable.put(response1ForQ1S1C1, false);
responseRecipientVisibilityTable.put(response2ForQ1S1C1, true);
responseRecipientVisibilityTable.put(response1ForQ2S1C1, true);
responseRecipientVisibilityTable.put(response2ForQ2S1C1, false);

SqlSessionResultsBundle bundle =
new SqlSessionResultsBundle(
new ArrayList<>(responseBundle.feedbackQuestions.values()),
new ArrayList<>(responseBundle.feedbackResponses.values()),
new ArrayList<>(),
responseGiverVisibilityTable,
responseRecipientVisibilityTable,
new HashMap<>(),
new HashMap<>(),
new SqlCourseRoster(new ArrayList<>(responseBundle.students.values()),
new ArrayList<>(responseBundle.instructors.values()))
);

for (Map.Entry<FeedbackResponse, Boolean> visibilityEntry : responseGiverVisibilityTable.entrySet()) {
assertEquals(visibilityEntry.getValue(),
bundle.isResponseGiverVisible(visibilityEntry.getKey()));
}

for (Map.Entry<FeedbackResponse, Boolean> visibilityEntry : responseRecipientVisibilityTable.entrySet()) {
assertEquals(visibilityEntry.getValue(),
bundle.isResponseRecipientVisible(visibilityEntry.getKey()));
}
}

@Test
public void testIsCommentGiverVisible_typicalCase_shouldReturnCorrectValues() {

SqlDataBundle responseBundle = loadSqlDataBundle("/FeedbackSessionResultsBundleTest.json");

Map<Long, Boolean> commentGiverVisibilityTable = new HashMap<>();
commentGiverVisibilityTable.put(1L, true);
commentGiverVisibilityTable.put(2L, false);

SqlSessionResultsBundle bundle =
new SqlSessionResultsBundle(
new ArrayList<>(responseBundle.feedbackQuestions.values()),
new ArrayList<>(responseBundle.feedbackResponses.values()),
new ArrayList<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
commentGiverVisibilityTable,
new SqlCourseRoster(new ArrayList<>(responseBundle.students.values()),
new ArrayList<>(responseBundle.instructors.values()))
);

assertTrue(bundle.isCommentGiverVisible(responseBundle.feedbackResponseComments.get("comment1FromT1C1ToR1Q1S1C1")));
assertFalse(bundle.isCommentGiverVisible(responseBundle.feedbackResponseComments.get("comment2FromT1C1ToR1Q1S1C1")));
}

@Test
public void testGetAnonName_typicalCase_shouldGenerateCorrectly() {
String anonName = SqlSessionResultsBundle.getAnonName(FeedbackParticipantType.STUDENTS, "");
assertTrue(anonName.startsWith(Const.DISPLAYED_NAME_FOR_ANONYMOUS_PARTICIPANT));

anonName = SqlSessionResultsBundle.getAnonName(FeedbackParticipantType.STUDENTS, "[email protected]");
assertTrue(anonName.startsWith(Const.DISPLAYED_NAME_FOR_ANONYMOUS_PARTICIPANT));
}
}
12 changes: 12 additions & 0 deletions src/test/java/teammates/test/BaseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
import teammates.common.datatransfer.SqlDataBundle;
import teammates.common.datatransfer.questions.FeedbackResponseDetails;
import teammates.common.datatransfer.questions.FeedbackTextQuestionDetails;
import teammates.common.datatransfer.questions.FeedbackTextResponseDetails;
import teammates.common.util.Const;
import teammates.common.util.FieldValidator;
import teammates.common.util.JsonUtils;
import teammates.sqllogic.core.DataBundleLogic;
import teammates.storage.sqlentity.Account;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackResponse;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Notification;
Expand Down Expand Up @@ -170,6 +173,15 @@ protected FeedbackQuestion getTypicalFeedbackQuestionForSession(FeedbackSession
new FeedbackTextQuestionDetails("test question text"));
}

protected FeedbackResponse getTypicalFeedbackResponseForQuestion(FeedbackQuestion question) {
return FeedbackResponse.makeResponse(question, "test-giver", getTypicalSection(), "test-recipient",
getTypicalSection(), getTypicalFeedbackResponseDetails());
}

protected FeedbackResponseDetails getTypicalFeedbackResponseDetails() {
return new FeedbackTextResponseDetails();
}

/**
* Populates the feedback question and response IDs within the data bundle.
*
Expand Down

0 comments on commit 901307a

Please sign in to comment.