Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
# test_graphics.py
2
# tests of sage worksheet that return more than stdout, e.g. svg files
3
4
import conftest
5
import time
6
7
from textwrap import dedent
8
9
import pytest
10
11
# TODO(hal) refactor this later
12
SHA_LEN = 36
13
14
class TestTachyon:
15
def test_t_show0(self, exec2):
16
code = dedent(r"""t = Tachyon(xres=400,yres=400, camera_center=(2,0,0))
17
t.light((4,3,2), 0.2, (1,1,1))
18
t.sphere((0,0,0), 0.5, 't0')""")
19
exec2(code,[])
20
def test_t_show1(self, execblob):
21
execblob("t.show()", want_html = False, file_type='png', ignore_stdout = True)
22
def test_show_t(self, execblob):
23
execblob("show(t)", want_html = False, file_type='png', ignore_stdout = True)
24
def test_t(self, execblob):
25
execblob("t", want_html = False, file_type='png', ignore_stdout = True)
26
27
class TestGraphics:
28
def test_plot(self, execblob):
29
execblob("plot(cos(x),x,0,pi)", want_html = False, file_type = 'svg')
30
31
class TestOctavePlot:
32
def test_octave_plot(self,execblob):
33
# assume octave kernel not running at start of test
34
execblob("%octave\nx = -10:0.1:10;plot (x, sin (x));", file_type = 'png', ignore_stdout = True)
35
36
class TestRPlot:
37
def test_r_smallplot(self,execblob):
38
execblob("%r\nwith(mtcars,plot(wt,mpg))", file_type = 'png')
39
def test_r_bigplot(self,execblob):
40
"lots of points, do not overrun blob size limit"
41
code = """%r
42
N <- 100000
43
xx <- rnorm(N, 5) + 3
44
yy <- rnorm(N, 3) - 1
45
plot(xx, yy, cex=.1)"""
46
execblob("%r\nwith(mtcars,plot(wt,mpg))", file_type = 'png')
47
48
class TestShowGraphs:
49
def test_issue594(self, test_id, sagews):
50
code = """G = Graph(sparse=True)
51
G.allow_multiple_edges(True)
52
G.add_edge(1,2)
53
G.add_edge(2,3)
54
G.add_edge(3,1)
55
for i in range(2):
56
print ("BEFORE PLOT %s"%i)
57
G.show()
58
print ("AFTER PLOT %s"%i)"""
59
m = conftest.message.execute_code(code = code, id = test_id)
60
sagews.send_json(m)
61
# expect 12 messages from worksheet client, including final done:true
62
json_wanted = 6
63
jstep = 0
64
blob_wanted = 2
65
while json_wanted > 0 and blob_wanted > 0:
66
typ, mesg = sagews.recv()
67
assert typ == 'json' or typ == 'blob'
68
if typ == 'json':
69
assert mesg['id'] == test_id
70
json_wanted -= 1
71
jstep += 1
72
if jstep == 1:
73
assert 'stdout' in mesg
74
assert 'BEFORE PLOT 0' in mesg['stdout']
75
continue
76
elif jstep == 2:
77
assert 'file' in mesg
78
continue
79
elif jstep == 3:
80
assert 'stdout' in mesg
81
assert 'AFTER PLOT 0' in mesg['stdout']
82
continue
83
elif jstep == 4:
84
assert 'stdout' in mesg
85
assert 'BEFORE PLOT 1' in mesg['stdout']
86
continue
87
elif jstep == 5:
88
assert 'file' in mesg
89
continue
90
elif jstep == 6:
91
assert 'stdout' in mesg
92
assert 'AFTER PLOT 1' in mesg['stdout']
93
continue
94
else:
95
blob_wanted -= 1
96
file_uuid = mesg[:SHA_LEN]
97
assert file_uuid == conftest.uuidsha1(mesg[SHA_LEN:])
98
m = conftest.message.save_blob(sha1 = file_uuid)
99
sagews.send_json(m)
100
continue
101
conftest.recv_til_done(sagews, test_id)
102
103
104
105