Skip to content

Commit 551331d

Browse files
committed
Display test cases
1 parent 0ba50ee commit 551331d

File tree

5 files changed

+68
-64
lines changed

5 files changed

+68
-64
lines changed

backend/question-service/src/controllers/questionController.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import Question, { IQuestion } from "../models/Question.ts";
33
import QuestionTemplate, {
44
IQuestionTemplate,
55
} from "../models/QuestionTemplate.ts";
6-
import { checkIsExistingQuestion, sortAlphabetically } from "../utils/utils.ts";
6+
import {
7+
checkIsExistingQuestion,
8+
getFileContent,
9+
sortAlphabetically,
10+
} from "../utils/utils.ts";
711
import {
812
DUPLICATE_QUESTION_MESSAGE,
913
QN_DESC_EXCEED_CHAR_LIMIT_MESSAGE,
@@ -85,7 +89,10 @@ export const createQuestion = async (
8589

8690
res.status(201).json({
8791
message: QN_CREATED_MESSAGE,
88-
question: formatQuestionIndivResponse(newQuestion, newQuestionTemplate),
92+
question: await formatQuestionIndivResponse(
93+
newQuestion,
94+
newQuestionTemplate,
95+
),
8996
});
9097
} catch (error) {
9198
console.log(error);
@@ -243,7 +250,7 @@ export const updateQuestion = async (
243250

244251
res.status(200).json({
245252
message: "Question updated successfully",
246-
question: formatQuestionIndivResponse(
253+
question: await formatQuestionIndivResponse(
247254
updatedQuestion as IQuestion,
248255
updatedQuestionTemplate as IQuestionTemplate,
249256
),
@@ -352,7 +359,7 @@ export const readQuestionIndiv = async (
352359

353360
res.status(200).json({
354361
message: QN_RETRIEVED_MESSAGE,
355-
question: formatQuestionIndivResponse(
362+
question: await formatQuestionIndivResponse(
356363
questionDetails,
357364
questionTemplate as IQuestionTemplate,
358365
),
@@ -390,7 +397,7 @@ export const readRandomQuestion = async (
390397

391398
res.status(200).json({
392399
message: QN_RETRIEVED_MESSAGE,
393-
question: formatQuestionIndivResponse(
400+
question: await formatQuestionIndivResponse(
394401
chosenQuestion,
395402
questionTemplate as IQuestionTemplate,
396403
),
@@ -433,18 +440,27 @@ const formatQuestionResponse = (question: IQuestion) => {
433440
};
434441
};
435442

436-
const formatQuestionIndivResponse = (
443+
const formatQuestionIndivResponse = async (
437444
question: IQuestion,
438445
questionTemplate: IQuestionTemplate,
439446
) => {
447+
const testcaseDelimiter = "\n";
448+
const inputs = (await getFileContent(question.testcaseInputFileUrl)).split(
449+
testcaseDelimiter,
450+
);
451+
const outputs = (await getFileContent(question.testcaseOutputFileUrl)).split(
452+
testcaseDelimiter,
453+
);
440454
return {
441455
id: question._id,
442456
title: question.title,
443457
description: question.description,
444458
complexity: question.complexity,
445459
categories: question.category,
446-
testcaseInputFileUrl: question.testcaseInputFileUrl,
447-
testcaseOutputFileUrl: question.testcaseOutputFileUrl,
460+
// testcaseInputFileUrl: question.testcaseInputFileUrl,
461+
// testcaseOutputFileUrl: question.testcaseOutputFileUrl,
462+
inputs,
463+
outputs,
448464
pythonTemplate: questionTemplate ? questionTemplate.pythonTemplate : "",
449465
javaTemplate: questionTemplate ? questionTemplate.javaTemplate : "",
450466
cTemplate: questionTemplate ? questionTemplate.cTemplate : "",

backend/question-service/src/utils/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import axios from "axios";
12
import mongoose from "mongoose";
23
import { v4 as uuidv4 } from "uuid";
34

45
import { bucket } from "../config/firebase";
5-
66
import Question from "../models/Question";
77

88
export const checkIsExistingQuestion = async (
@@ -50,6 +50,16 @@ export const uploadFileToFirebase = async (
5050
});
5151
};
5252

53+
export const getFileContent = async (url: string): Promise<string> => {
54+
try {
55+
const { data } = await axios.get(url);
56+
return data;
57+
} catch (error) {
58+
console.error(error);
59+
return "";
60+
}
61+
};
62+
5363
export const sortAlphabetically = (arr: string[]) => {
5464
return [...arr].sort((a, b) =>
5565
a.localeCompare(b, undefined, { sensitivity: "base" }),

frontend/src/components/TestCase/index.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Box, styled, Typography } from "@mui/material";
22

33
type TestCaseProps = {
44
input: string;
5-
output: string;
5+
output?: string;
66
stdout: string;
7-
result: string;
7+
result?: string;
88
};
99

1010
const StyledBox = styled(Box)(({ theme }) => ({
@@ -30,16 +30,20 @@ const TestCase: React.FC<TestCaseProps> = ({
3030
<Typography variant="overline">Input</Typography>
3131
<StyledTypography fontFamily={"monospace"}>{input}</StyledTypography>
3232
</StyledBox>
33+
{output && (
34+
<StyledBox>
35+
<Typography variant="overline">Output</Typography>
36+
<StyledTypography fontFamily={"monospace"}>{output}</StyledTypography>
37+
</StyledBox>
38+
)}
39+
{stdout && (
40+
<StyledBox>
41+
<Typography variant="overline">Stdout</Typography>
42+
<StyledTypography fontFamily={"monospace"}>{stdout}</StyledTypography>
43+
</StyledBox>
44+
)}
3345
<StyledBox>
34-
<Typography variant="overline">Output</Typography>
35-
<StyledTypography fontFamily={"monospace"}>{output}</StyledTypography>
36-
</StyledBox>
37-
<StyledBox>
38-
<Typography variant="overline">Standard output</Typography>
39-
<StyledTypography fontFamily={"monospace"}>{stdout}</StyledTypography>
40-
</StyledBox>
41-
<StyledBox>
42-
<Typography variant="overline">Result</Typography>
46+
<Typography variant="overline">Expected</Typography>
4347
<StyledTypography fontFamily={"monospace"}>{result}</StyledTypography>
4448
</StyledBox>
4549
</Box>

frontend/src/pages/CollabSandbox/index.tsx

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,6 @@ import Chat from "../../components/Chat";
2727
import TabPanel from "../../components/TabPanel";
2828
import TestCase from "../../components/TestCase";
2929

30-
// hardcode for now...
31-
32-
type TestCase = {
33-
input: string;
34-
output: string;
35-
stdout: string;
36-
result: string;
37-
};
38-
39-
const testcases: TestCase[] = [
40-
{
41-
input: "1 2 3 4",
42-
output: "1 2 3 4",
43-
stdout: "1\n2\n3\n4",
44-
result: "1 2 3 4",
45-
},
46-
{
47-
input: "5 6 7 8",
48-
output: "5 6 7 8",
49-
stdout: "5\n6\n7\n8",
50-
result: "5 6 7 8",
51-
},
52-
];
53-
5430
const CollabSandbox: React.FC = () => {
5531
const [showErrorScreen, setShowErrorScreen] = useState<boolean>(false);
5632

@@ -219,7 +195,7 @@ const CollabSandbox: React.FC = () => {
219195
</Tabs>
220196
<TabPanel selected={selectedTab} value="tests">
221197
<Box sx={(theme) => ({ margin: theme.spacing(2, 0) })}>
222-
{[...Array(testcases.length)]
198+
{[...Array(selectedQuestion.inputs.length)]
223199
.map((_, index) => index + 1)
224200
.map((i) => (
225201
<Button
@@ -235,11 +211,12 @@ const CollabSandbox: React.FC = () => {
235211
</Button>
236212
))}
237213
</Box>
214+
{/* display result of each test case in the output (result) and stdout (any print statements executed) */}
238215
<TestCase
239-
input={testcases[selectedTestcase].input}
240-
output={testcases[selectedTestcase].output}
241-
stdout={testcases[selectedTestcase].stdout}
242-
result={testcases[selectedTestcase].result}
216+
input={selectedQuestion.inputs[selectedTestcase]}
217+
output={""}
218+
stdout={""}
219+
result={selectedQuestion.outputs[selectedTestcase]}
243220
/>
244221
</TabPanel>
245222
<TabPanel selected={selectedTab} value="chat">

frontend/src/reducers/questionReducer.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ type QuestionDetail = {
1818
description: string;
1919
complexity: string;
2020
categories: Array<string>;
21+
inputs: Array<string>;
22+
outputs: Array<string>;
2123
pythonTemplate: string;
2224
javaTemplate: string;
2325
cTemplate: string;
2426
};
2527

26-
type QuestionDetailWithUrl = QuestionDetail & {
27-
testcaseInputFileUrl: string;
28-
testcaseOutputFileUrl: string;
29-
};
28+
// type QuestionDetailWithUrl = QuestionDetail & {
29+
// testcaseInputFileUrl: string;
30+
// testcaseOutputFileUrl: string;
31+
// };
3032

3133
type QuestionListDetail = {
3234
id: string;
@@ -56,26 +58,21 @@ enum QuestionActionTypes {
5658

5759
type QuestionActions = {
5860
type: QuestionActionTypes;
59-
payload:
60-
| QuestionList
61-
| QuestionDetail
62-
| QuestionDetailWithUrl
63-
| string[]
64-
| string;
61+
payload: QuestionList | QuestionDetail | QuestionDetail | string[] | string;
6562
};
6663

6764
type QuestionsState = {
6865
questionCategories: Array<string>;
6966
questions: Array<QuestionListDetail>;
7067
questionCount: number;
71-
selectedQuestion: QuestionDetailWithUrl | null;
68+
selectedQuestion: QuestionDetail | null;
7269
questionCategoriesError: string | null;
7370
questionListError: string | null;
7471
selectedQuestionError: string | null;
7572
};
7673

7774
// eslint-disable-next-line @typescript-eslint/no-explicit-any
78-
const isQuestion = (question: any): question is QuestionDetailWithUrl => {
75+
const isQuestion = (question: any): question is QuestionDetail => {
7976
if (!question || typeof question !== "object") {
8077
return false;
8178
}
@@ -272,7 +269,7 @@ export const getQuestionById = (
272269

273270
export const updateQuestionById = async (
274271
questionId: string,
275-
question: Omit<QuestionDetailWithUrl, "id">,
272+
question: Omit<QuestionDetail, "id">,
276273
testcaseFiles: TestcaseFiles,
277274
dispatch: Dispatch<QuestionActions>
278275
): Promise<boolean> => {
@@ -309,8 +306,8 @@ export const updateQuestionById = async (
309306
description: question.description,
310307
complexity: question.complexity,
311308
category: question.categories,
312-
testcaseInputFileUrl: question.testcaseInputFileUrl,
313-
testcaseOutputFileUrl: question.testcaseOutputFileUrl,
309+
// testcaseInputFileUrl: question.testcaseInputFileUrl,
310+
// testcaseOutputFileUrl: question.testcaseOutputFileUrl,
314311
...urls,
315312
pythonTemplate: question.pythonTemplate,
316313
javaTemplate: question.javaTemplate,

0 commit comments

Comments
 (0)