2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.env.python;
18 import com.google.common.collect.ImmutableSet;
19 import com.intellij.util.Consumer;
20 import com.intellij.xdebugger.XDebugSession;
21 import com.intellij.xdebugger.XDebuggerTestUtil;
22 import com.jetbrains.env.PyEnvTestCase;
23 import com.jetbrains.env.Staging;
24 import com.jetbrains.env.python.debug.PyDebuggerTask;
25 import com.jetbrains.python.debugger.ArrayChunk;
26 import com.jetbrains.python.debugger.PyDebugValue;
27 import com.jetbrains.python.debugger.PyDebuggerException;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 import org.junit.Test;
32 import java.lang.reflect.InvocationTargetException;
33 import java.util.List;
36 import static com.intellij.testFramework.UsefulTestCase.assertSameElements;
37 import static org.junit.Assert.assertEquals;
40 * Created by Yuli Fiterman on 5/10/2016.
42 public class PythonDataViewerTest extends PyEnvTestCase {
46 public void testDataFrameChunkRetrieval() throws Exception {
47 runPythonTest(new PyDataFrameDebuggerTask(getRelativeTestDataPath(), "test_dataframe.py", ImmutableSet.of(7, 15, 22)) {
49 public void testing() throws Exception {
50 doTest("df1", 3, 5, null);
52 doTest("df2", 3, 6, arrayChunk -> {
53 List<ArrayChunk.ColHeader> colHeaders = arrayChunk.getColHeaders();
54 assertSameElements(colHeaders.stream().map(ArrayChunk.ColHeader::getLabel).toArray(),
55 "LABELS", "One_X", "One_Y", "Two_X", "Two_Y", "row");
58 doTest("df3", 7, 3, arrayChunk -> {
59 ArrayChunk.ColHeader header = arrayChunk.getColHeaders().get(2);
60 assertEquals("Sales", header.getLabel());
61 assertEquals(16, (int)Integer.valueOf(header.getMax()));
62 assertEquals(1, (int)Integer.valueOf(header.getMin()));
70 public void testMultiIndexDataFrame() throws Exception {
71 runPythonTest(new PyDataFrameDebuggerTask(getRelativeTestDataPath(), "test_dataframe_multiindex.py", ImmutableSet.of(5, 10)) {
73 public void testing() throws Exception {
74 doTest("frame1", 4, 2, arrayChunk -> assertSameElements(arrayChunk.getRowLabels(),
75 "s/2", "s/3", "d/2", "d/3"));
76 doTest("frame2", 4, 4, arrayChunk -> {
77 List<ArrayChunk.ColHeader> headers = arrayChunk.getColHeaders();
78 assertSameElements(headers.stream().map(ArrayChunk.ColHeader::getLabel).toArray(), "1/1", "1/B", "2/1", "2/B");
84 private static class PyDataFrameDebuggerTask extends PyDebuggerTask {
86 private Set<Integer> myLines;
88 public PyDataFrameDebuggerTask(@Nullable String relativeTestDataPath, String scriptName, Set<Integer> lines) {
89 super(relativeTestDataPath, scriptName);
93 protected void testShape(ArrayChunk arrayChunk, int expectedRows, int expectedColumns) {
94 assertEquals(expectedRows, arrayChunk.getRows());
95 assertEquals(expectedColumns, arrayChunk.getColumns());
98 protected void doTest(String name, int expectedRows, int expectedColumns, @Nullable Consumer<ArrayChunk> test)
99 throws InvocationTargetException, InterruptedException, PyDebuggerException {
101 ArrayChunk arrayChunk = getDefaultChunk(name, mySession);
102 testShape(arrayChunk, expectedRows, expectedColumns);
104 test.consume(arrayChunk);
110 public void before() throws Exception {
111 for (Integer line : myLines) {
112 toggleBreakpoint(getScriptName(), line);
118 public Set<String> getTags() {
119 return ImmutableSet.of("pandas");
123 private static ArrayChunk getDefaultChunk(String varName, XDebugSession session) throws PyDebuggerException {
124 PyDebugValue dbgVal = (PyDebugValue)XDebuggerTestUtil.evaluate(session, varName).first;
125 return dbgVal.getFrameAccessor().getArrayItems(dbgVal, 0, 0, -1, -1, ".%5f");
128 private static String getRelativeTestDataPath() {