add format version to stepic format
[idea/community.git] / python / educational-core / student / testSrc / com / jetbrains / edu / learning / stepic / StudyStepicFormatTest.java
1 package com.jetbrains.edu.learning.stepic;
2
3 import com.intellij.openapi.util.io.FileUtil;
4 import com.intellij.testFramework.PlatformTestUtil;
5 import com.intellij.util.containers.ContainerUtil;
6 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
7 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
8 import com.jetbrains.edu.learning.courseFormat.TaskFile;
9 import org.jetbrains.annotations.NotNull;
10 import org.junit.Test;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17
18 import static org.junit.Assert.*;
19
20
21 public class StudyStepicFormatTest {
22
23   @Test
24   public void fromFirstVersion() throws IOException {
25     doStepOptionsCreationTest("1.json");
26   }
27
28   @Test
29   public void fromSecondVersion() throws IOException {
30     doStepOptionsCreationTest("2.json");
31   }
32
33   @Test
34   public void testWithSubtasks() throws IOException {
35     StepicWrappers.StepOptions stepOptions = doStepOptionsCreationTest("3.json");
36     assertEquals(1, stepOptions.lastSubtaskIndex);
37   }
38
39
40   private static StepicWrappers.StepOptions doStepOptionsCreationTest(String fileName) throws IOException {
41     String responseString =
42       FileUtil.loadFile(new File(getTestDataPath(), fileName));
43     StepicWrappers.StepSource stepSource =
44       EduStepicClient.deserializeStepicResponse(StepicWrappers.StepContainer.class, responseString).steps.get(0);
45     StepicWrappers.StepOptions options = stepSource.block.options;
46     List<TaskFile> files = options.files;
47     assertTrue("Wrong number of task files", files.size() == 1);
48     List<AnswerPlaceholder> placeholders = files.get(0).getAnswerPlaceholders();
49     assertTrue("Wrong number of placeholders", placeholders.size() == 1);
50     Map<Integer, AnswerPlaceholderSubtaskInfo> infos = placeholders.get(0).getSubtaskInfos();
51     assertNotNull(infos);
52     assertEquals(Collections.singletonList("Type your name here."), infos.get(0).getHints());
53     assertEquals("Liana", infos.get(0).getPossibleAnswer());
54     return options;
55   }
56
57   @Test
58   public void testAvailableCourses() throws IOException {
59     String responseString = FileUtil.loadFile(new File(getTestDataPath(), "courses.json"));
60     StepicWrappers.CoursesContainer container =
61       EduStepicClient.deserializeStepicResponse(StepicWrappers.CoursesContainer.class, responseString);
62     assertNotNull(container.courses);
63     assertTrue("Incorrect number of courses", container.courses.size() == 4);
64     List<CourseInfo> filtered = ContainerUtil.filter(container.courses, info -> EduStepicConnector.canBeOpened(info));
65     assertEquals(ContainerUtil.newArrayList("Adaptive Python", "Introduction to Python", "format2"), ContainerUtil.map(filtered, CourseInfo::getName));
66   }
67
68   @NotNull
69   private static String getTestDataPath() {
70     return FileUtil.join(PlatformTestUtil.getCommunityPath(), "python/educational-core/student/testData/stepic");
71   }
72 }