FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-python/pythonFiles/tests/ipython/test_variables.py at master · thy09/vscode-python · GitHub
thy09
/
vscode-python
Public
forked from
microsoft/vscode-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-python
/
pythonFiles
/
tests
/
ipython
/
test_variables.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
167 lines (151 loc) · 5.08 KB
Breadcrumbs
vscode-python
/
pythonFiles
/
tests
/
ipython
/
test_variables.py
Copy path
File metadata and controls
167 lines (151 loc) · 5.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import
pytest
import
sys
import
os
import
json
from
.
scripts
import
(
get_variable_value
,
get_variables
,
get_data_frame_info
,
get_data_frame_rows
,
check_for_ipython
,
)
import
imp
haveIPython
=
check_for_ipython
()
@
pytest
.
mark
.
skipif
(
not
haveIPython
,
reason
=
"Can't run variable tests without IPython console"
)
def
test_variable_list
(
capsys
):
from
IPython
import
get_ipython
# Execute a single cell before we get the variables.
get_ipython
().
run_cell
(
"x = 3
\r
\n
y = 4
\r
\n
z=5"
)
vars
=
get_variables
(
capsys
)
have_x
=
False
have_y
=
False
have_z
=
False
for
sub
in
vars
:
have_x
|=
sub
[
"name"
]
==
"x"
have_y
|=
sub
[
"name"
]
==
"y"
have_z
|=
sub
[
"name"
]
==
"z"
assert
have_x
assert
have_y
assert
have_z
@
pytest
.
mark
.
skipif
(
not
haveIPython
,
reason
=
"Can't run variable tests without IPython console"
)
def
test_variable_value
(
capsys
):
from
IPython
import
get_ipython
# Execute a single cell before we get the variables. This is the variable we'll look for.
get_ipython
().
run_cell
(
"x = 3"
)
vars
=
get_variables
(
capsys
)
varx_value
=
get_variable_value
(
vars
,
"x"
,
capsys
)
assert
varx_value
assert
varx_value
==
"3"
@
pytest
.
mark
.
skipif
(
not
haveIPython
,
reason
=
"Can't run variable tests without IPython console"
)
def
test_dataframe_info
(
capsys
):
from
IPython
import
get_ipython
# Setup some different types
get_ipython
().
run_cell
(
"""
import pandas as pd
import numpy as np
ls = list([10, 20, 30, 40])
df = pd.DataFrame(ls)
se = pd.Series(ls)
np1 = np.array(ls)
np2 = np.array([[1, 2, 3], [4, 5, 6]])
dict1 = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
obj = {}
col = pd.Series(data=np.random.random_sample((7,))*100)
dfInit = {}
idx = pd.date_range('2007-01-01', periods=7, freq='M')
for i in range(30):
dfInit[i] = col
dfInit['idx'] = idx
df2 = pd.DataFrame(dfInit).set_index('idx')
df3 = df2.iloc[:, [0,1]]
se2 = df2.loc[df2.index[0], :]
"""
)
vars
=
get_variables
(
capsys
)
df
=
get_variable_value
(
vars
,
"df"
,
capsys
)
se
=
get_variable_value
(
vars
,
"se"
,
capsys
)
np
=
get_variable_value
(
vars
,
"np1"
,
capsys
)
np2
=
get_variable_value
(
vars
,
"np2"
,
capsys
)
ls
=
get_variable_value
(
vars
,
"ls"
,
capsys
)
obj
=
get_variable_value
(
vars
,
"obj"
,
capsys
)
df3
=
get_variable_value
(
vars
,
"df3"
,
capsys
)
se2
=
get_variable_value
(
vars
,
"se2"
,
capsys
)
dict1
=
get_variable_value
(
vars
,
"dict1"
,
capsys
)
assert
df
assert
se
assert
np
assert
ls
assert
obj
assert
df3
assert
se2
assert
dict1
verify_dataframe_info
(
vars
,
"df"
,
"index"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"se"
,
"index"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"np1"
,
"index"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"ls"
,
"index"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"np2"
,
"index"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"obj"
,
"index"
,
capsys
,
False
)
verify_dataframe_info
(
vars
,
"df3"
,
"idx"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"se2"
,
"index"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"df2"
,
"idx"
,
capsys
,
True
)
verify_dataframe_info
(
vars
,
"dict1"
,
"index"
,
capsys
,
True
)
def
verify_dataframe_info
(
vars
,
name
,
indexColumn
,
capsys
,
hasInfo
):
info
=
get_data_frame_info
(
vars
,
name
,
capsys
)
assert
info
assert
"columns"
in
info
assert
len
(
info
[
"columns"
])
>
0
if
hasInfo
else
True
assert
"rowCount"
in
info
if
hasInfo
:
assert
info
[
"rowCount"
]
>
0
assert
info
[
"indexColumn"
]
assert
info
[
"indexColumn"
]
==
indexColumn
@
pytest
.
mark
.
skipif
(
not
haveIPython
,
reason
=
"Can't run variable tests without IPython console"
)
def
test_dataframe_rows
(
capsys
):
from
IPython
import
get_ipython
# Setup some different types
path
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
file
=
os
.
path
.
abspath
(
os
.
path
.
join
(
path
,
"random.csv"
))
file
=
file
.
replace
(
"
\\
"
,
"
\\
\\
"
)
dfstr
=
"import pandas as pd
\r
\n
df = pd.read_csv('{}')"
.
format
(
file
)
get_ipython
().
run_cell
(
dfstr
)
vars
=
get_variables
(
capsys
)
df
=
get_variable_value
(
vars
,
"df"
,
capsys
)
assert
df
info
=
get_data_frame_info
(
vars
,
"df"
,
capsys
)
assert
"rowCount"
in
info
assert
info
[
"rowCount"
]
==
6000
rows
=
get_data_frame_rows
(
info
,
100
,
200
,
capsys
)
assert
rows
assert
rows
[
"data"
][
0
][
"+h2"
]
==
"Fy3 W[pMT["
get_ipython
().
run_cell
(
"""
import pandas as pd
import numpy as np
ls = list([10, 20, 30, 40])
df = pd.DataFrame(ls)
se = pd.Series(ls)
np1 = np.array(ls)
np2 = np.array([[1, 2, 3], [4, 5, 6]])
obj = {}
"""
)
vars
=
get_variables
(
capsys
)
np2
=
get_variable_value
(
vars
,
"np2"
,
capsys
)
assert
np2
info
=
get_data_frame_info
(
vars
,
"np2"
,
capsys
)
assert
"rowCount"
in
info
assert
info
[
"rowCount"
]
==
2
rows
=
get_data_frame_rows
(
info
,
0
,
2
,
capsys
)
assert
rows
assert
rows
[
"data"
][
0
]
Back
|
FazBrowse Home
|
New Git URL