Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
# test_sagews.py
2
# basic tests of sage worksheet using TCP protocol with sage_server
3
import socket
4
import conftest
5
import os
6
import re
7
8
from textwrap import dedent
9
10
import pytest
11
12
@pytest.mark.skip(reason="waiting until #1835 is fixed")
13
class TestLex:
14
def test_lex_1(self, execdoc):
15
execdoc("x = random? # bar")
16
def test_lex_2(self, execdoc):
17
execdoc("x = random? # plot?",pattern='random')
18
def test_lex_3(self, exec2):
19
exec2("x = 1 # plot?\nx","1\n")
20
def test_lex_4(self, exec2):
21
exec2('x="random?" # plot?\nx',"'random?'\n")
22
def test_lex_5(self, exec2):
23
code = dedent(r'''
24
x = """
25
salvus?
26
""";pi''')
27
exec2(code, "pi\n")
28
29
class TestSageVersion:
30
def test_sage_vsn(self, exec2):
31
code = "sage.misc.banner.banner()"
32
patn = "version 8.0"
33
exec2(code, pattern = patn)
34
35
class TestDecorators:
36
def test_simple_dec(self, exec2):
37
code = dedent(r"""
38
def d2(f): return lambda x: f(x)+'-'+f(x)
39
@d2
40
def s(str): return str.upper()
41
s('spam')""")
42
exec2(code, "'SPAM-SPAM'\n")
43
44
def test_multiple_dec(self, exec2):
45
code = dedent(r"""
46
def dummy(f): return f
47
@dummy
48
@dummy
49
def f(x): return 2*x+1
50
f(2)""")
51
exec2(code, "5\n")
52
53
class TestLinearAlgebra:
54
def test_solve_right(self, exec2):
55
code = dedent(r"""
56
A=matrix([[1,2,6],[1,2,0],[1,-2,3]])
57
b=vector([1,-1,1])
58
A.solve_right(b)""")
59
exec2(code,"(-1/2, -1/4, 1/3)")
60
61
def test_kernel(self, exec2):
62
code = dedent(r"""
63
A=matrix([[1,2,3],[1,2,3],[1,2,3]])
64
kernel(A)""")
65
pat = "\[ 1 0 -1\]\n\[ 0 1 -1\]"
66
exec2(code, pattern = pat)
67
68
def test_charpoly(self, exec2):
69
code = dedent(r"""
70
A=matrix([[1,2,3],[1,2,3],[1,2,3]])
71
A.charpoly()""")
72
exec2(code, "x^3 - 6*x^2\n")
73
74
def test_eigenvalues(self, exec2):
75
code = dedent(r"""
76
A=matrix([[1,2,3],[1,2,3],[1,2,3]])
77
A=matrix([[1,2,3],[1,2,3],[1,2,3]])
78
A.eigenvalues()""")
79
exec2(code, "[6, 0, 0]\n")
80
81
class TestBasic:
82
def test_connection_type(self, sagews):
83
print("type %s"%type(sagews))
84
assert isinstance(sagews, conftest.ConnectionJSON)
85
return
86
87
def test_set_file_env(self, exec2):
88
code = "os.chdir(salvus.data[\'path\']);__file__=salvus.data[\'file\']"
89
exec2(code)
90
91
def test_sage_assignment(self, exec2):
92
code = "x = 42\nx\n"
93
output = "42\n"
94
exec2(code, output)
95
96
def test_issue70(self, exec2):
97
code = dedent(r"""
98
for i in range(1):
99
pass
100
'x'
101
""")
102
output = dedent(r"""
103
'x'
104
""").lstrip()
105
exec2(code, output)
106
107
def test_issue819(self, exec2):
108
code = dedent(r"""
109
def never_called(a):
110
print 'should not execute 1', a
111
# comment
112
# comment at indent 0
113
print 'should not execute 2', a
114
22
115
""")
116
output = "22\n"
117
exec2(code, output)
118
119
def test_search_doc(self, exec2):
120
code = "search_doc('laurent')"
121
html = "https://www.google.com/search\?q=site%3Adoc.sagemath.org\+laurent\&oq=site%3Adoc.sagemath.org"
122
exec2(code, html_pattern = html)
123
124
def test_show_doc(self, test_id, sagews):
125
# issue 476
126
code = "show?"
127
patn = dedent("""
128
import smc_sagews.graphics
129
smc_sagews.graphics.graph_to_d3_jsonable?""")
130
m = conftest.message.execute_code(code = code, id = test_id)
131
sagews.send_json(m)
132
# ignore stderr message about deprecation warning
133
for ix in [0,1]:
134
typ, mesg = sagews.recv()
135
assert typ == 'json'
136
assert mesg['id'] == test_id
137
if 'stderr' in mesg:
138
continue
139
assert 'code' in mesg
140
assert 'source' in mesg['code']
141
assert re.sub('\s+','',patn) in re.sub('\s+','',mesg['code']['source'])
142
conftest.recv_til_done(sagews, test_id)
143
break
144
145
# https://github.com/sagemathinc/cocalc/issues/1107
146
def test_sage_underscore_1(self, exec2):
147
exec2("2/5","2/5\n")
148
def test_sage_underscore_2(self, exec2):
149
exec2("_","2/5\n")
150
151
# https://github.com/sagemathinc/cocalc/issues/978
152
def test_mode_comments_1(self, exec2):
153
exec2(dedent("""
154
def f(s):
155
print "s='%s'"%s"""))
156
def test_mode_comments_2(self, exec2):
157
exec2(dedent("""
158
%f
159
123
160
# foo
161
456"""), dedent("""
162
s='123
163
# foo
164
456'
165
""").lstrip())
166
167
def test_block_parser(self, exec2):
168
"""
169
.. NOTE::
170
171
This function supplies a list of expected outputs to `exec2`.
172
"""
173
exec2(dedent("""
174
pi.n().round()
175
[x for x in [1,2,3] if x<3]
176
for z in ['a','b']:
177
z
178
else:
179
z"""), ["3\n","[1, 2]\n","'a'\n'b'\n'b'\n"])
180
181
class TestIntrospect:
182
# test names end with SMC issue number
183
def test_sage_autocomplete_1188(self, execintrospect):
184
execintrospect('2016.fa', ["ctor","ctorial"], "fa")
185
def test_sage_autocomplete_295_setup(self, exec2):
186
exec2("aaa=Rings()._super_categories_for_classes;len(aaa[0].axioms())","6\n")
187
def test_sage_autocomplete_295a(self, execintrospect):
188
execintrospect('for a in aa', ["a"], "aa")
189
def test_sage_autocomplete_295b(self, execintrospect):
190
execintrospect('3 * aa', ["a"], "aa")
191
def test_sage_autocomplete_701_setup(self, exec2):
192
exec2(dedent("""
193
class Xyz:
194
numerical_attribute = 42
195
x1 = Xyz()
196
x1.numerical_attribute.next_prime()"""),"43\n")
197
def test_sage_autocomplete_701a(self, execintrospect):
198
execintrospect('3 / x1.nu', ["merical_attribute"], "nu")
199
def test_sage_autocomplete_701b(self, execintrospect):
200
execintrospect('aa', ["a"], "aa")
201
def test_sage_autocomplete_701c(self, execintrospect):
202
execintrospect('[aa', ["a"], "aa")
203
def test_sage_autocomplete_701d(self, execintrospect):
204
execintrospect('( aa', ["a"], "aa")
205
def test_sage_autocomplete_734a(self, execintrospect):
206
f = '*_factors'
207
execintrospect(f, ["cunningham_prime_factors", "prime_factors"], f)
208
def test_sage_autocomplete_734b(self, execintrospect):
209
f = '*le_pr*'
210
execintrospect(f, ["next_probable_prime"], f)
211
def test_sage_autocomplete_734c(self, execintrospect):
212
execintrospect('list.re*e', ["remove", "reverse"], 're*e')
213
def test_sage_autocomplete_1225a(self, execintrospect):
214
execintrospect('z = 12.5 * units.len', ["gth"], 'len')
215
def test_sage_autocomplete_1225b_setup(self, exec2):
216
exec2(dedent("""
217
class TC:
218
def __init__(self, xval):
219
self.x = xval
220
y = TC(49)
221
"""))
222
def test_sage_autocomplete_1225b(self, execintrospect):
223
execintrospect('z = 12 * y.', ["x"], '')
224
def test_sage_autocomplete_1252a(self, execintrospect):
225
execintrospect('2*sqr', ["t"], 'sqr')
226
def test_sage_autocomplete_1252b(self, execintrospect):
227
execintrospect('2+sqr', ["t"], 'sqr')
228
229
class TestAttach:
230
def test_define_paf(self, exec2):
231
exec2(dedent(r"""
232
def paf():
233
print("attached files: %d"%len(attached_files()))
234
print("\n".join(attached_files()))
235
paf()"""),"attached files: 0\n\n")
236
def test_attach_sage_1(self, exec2, test_ro_data_dir):
237
fn = os.path.join(test_ro_data_dir, 'a.sage')
238
exec2("%attach {}\npaf()".format(fn), pattern="attached files: 1\n.*/a.sage\n")
239
def test_attach_sage_2(self, exec2):
240
exec2("f1('foo')","f1 arg = 'foo'\ntest f1 1\n")
241
def test_attach_py_1(self, exec2, test_ro_data_dir):
242
fn = os.path.join(test_ro_data_dir, 'a.py')
243
exec2("%attach {}\npaf()".format(fn), pattern="attached files: 2\n.*/a.py\n.*/a.sage\n")
244
def test_attach_py_2(self, exec2):
245
exec2("f2('foo')","test f2 1\n")
246
def test_attach_html_1(self, execblob, test_ro_data_dir):
247
fn = os.path.join(test_ro_data_dir, 'a.html')
248
execblob("%attach {}".format(fn), want_html=False, want_javascript=True, file_type='html')
249
def test_attach_html_2(self, exec2):
250
exec2("paf()", pattern="attached files: 3\n.*/a.html\n.*/a.py\n.*/a.sage\n")
251
def test_detach_1(self, exec2):
252
exec2("detach(attached_files())")
253
def test_detach_2(self, exec2):
254
exec2("paf()","attached files: 0\n\n")
255
256
class TestSearchSrc:
257
def test_search_src_simple(self, execinteract):
258
execinteract('search_src("convolution")')
259
260
def test_search_src_max_chars(self, execinteract):
261
execinteract('search_src("full cremonadatabase", max_chars = 1000)')
262
263
class TestIdentifiers:
264
"""
265
see SMC issue #63
266
"""
267
def test_ident_set_file_env(self, exec2):
268
"""emulate initial code block sent from UI, needed for first show_identifiers"""
269
code = "os.chdir(salvus.data[\'path\']);__file__=salvus.data[\'file\']"
270
exec2(code)
271
def test_show_identifiers_initial(self, exec2):
272
exec2("show_identifiers()","[]\n")
273
274
def test_show_identifiers_vars(self, exec2):
275
code = dedent(r"""
276
k = ['a','b','c']
277
A = {'a':'foo','b':'bar','c':'baz'}
278
z = 99
279
sorted(show_identifiers())""")
280
exec2(code, "['A', 'k', 'z']\n")
281
282
def test_save_and_reset(self,exec2,data_path):
283
code = dedent(r"""
284
save_session('%s')
285
reset()
286
show_identifiers()""")%data_path.join('session').strpath
287
exec2(code,"[]\n")
288
def test_load_session1(self,exec2,data_path):
289
code = dedent(r"""
290
pretty_print = 8
291
view = 9
292
load_session('%s')
293
sorted(show_identifiers())""")%data_path.join('session').strpath
294
output = "['A', 'k', 'pretty_print', 'view', 'z']\n"
295
exec2(code,output)
296
def test_load_session2(self,exec2):
297
exec2("pretty_print,view","(8, 9)\n")
298
299
def test_redefine_sage(self,exec2):
300
code = dedent(r"""
301
reset()
302
sage=1
303
show_identifiers()""")
304
exec2(code,"['sage']\n")
305
306