test_basthon_modules.py 4.19 KB
Newer Older
1
from pathlib import Path
Romain Casati's avatar
Romain Casati committed
2
3
import base64
import time
Romain Casati's avatar
Romain Casati committed
4
5
import re

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

_test_data = Path(__file__).parent / "data"


def test_all(selenium):
    # ensure all basthon modules are tested
    tested = set(g[len('test_'):] for g in globals()
                 if g.startswith('test_') and g != 'test_all')
    result = selenium.run_basthon("""
    from basthon import packages
    set(packages._internal_pkgs.keys())""")['result']
    internal = eval(result['result']['text/plain'])
    assert tested == internal


def test_turtle(selenium):
    # already tested in test_hacked_modules
    assert True


def test_folium(selenium):
    # already tested in test_hacked_modules
    assert True


def test_graphviz(selenium):
Romain Casati's avatar
Romain Casati committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    result = selenium.run_basthon("""
    from graphviz import Digraph
    import basthon

    g = Digraph('G')
    g.edge('Hello', 'World')
    basthon.display(g)
    print(g.source)
    """)
    assert result['stderr'] == ""
    assert result['stdout'] == "digraph G {\n\tHello -> World\n}\n"
    assert result['display']['display_type'] == 'multiple'
    svg = result['display']['content']['image/svg+xml']
    with open(_test_data / "graphviz.svg") as f:
        target = f.read()
    assert target == svg
48
49


Romain Casati's avatar
Romain Casati committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def test_requests(selenium):
    result = selenium.run_basthon("""
    import requests

    url = "http://httpbin.org/html"
    response = requests.get(url)
    print(response.text)
    response.headers
    """)
    assert result['stderr'] == ""
    with open(_test_data / "requests.txt") as f:
        target = f.read()
    assert target == result['stdout']
    headers = eval(result['result']['result']['text/plain'])
    assert headers['content-type'] == 'text/html; charset=utf-8'


def test_proj4py(selenium):
Romain Casati's avatar
Romain Casati committed
68
69
70
71
72
73
74
75
76
77
78
    result = selenium.run_basthon("""
    import proj4py

    # from WGS84 to Lambert93
    proj = proj4py.proj4('EPSG:4326', 'EPSG:2154')
    proj.forward((46.57824382381372, 2.468613626422624))
    """)
    assert result['stderr'] == ""
    assert result['stdout'] == ""
    target = eval(result['result']['result']['text/plain'])
    assert target == (659306.8946611215, 6608826.400123728)
Romain Casati's avatar
Romain Casati committed
79
80
81


def test_IPython(selenium):
Romain Casati's avatar
Romain Casati committed
82
83
84
85
86
87
88
    result = selenium.run_basthon("""
    import IPython
    import IPython.display
    from IPython.display import display, display_image
    """)
    assert result['stderr'] == ""
    assert result['stdout'] == ""
Romain Casati's avatar
Romain Casati committed
89
90


91
def test_p5(selenium):
Romain Casati's avatar
Romain Casati committed
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
    selenium.run_basthon("""
    from p5 import *

    x = 100
    y = 100

    def setup():
        createCanvas(200, 200)

    def draw():
        background(0)
        fill(255)
        rect(x, y, 50, 50)

    run()
    """, return_data=False)
    basename = f"p5_{selenium.driver.capabilities['browserName']}"
    # can't access content like this
    # elem = result['content']
    # because of selenium's StaleElementReferenceException
    # bypassing it via JS
    html = selenium.run_js("return window._basthon_eval_data.display.content.outerHTML")
    with open(_test_data / f"{basename}.html") as f:
        target = f.read()
    assert html == target
    selenium.run_js("document.body.appendChild(window._basthon_eval_data.display.content);")
    # this should be replaced with a clean selenium wait
    time.sleep(1)
    png = selenium.run_js("""
    const elem = window._basthon_eval_data.display.content;
    return elem.getElementsByTagName('canvas')[0].toDataURL('image/png');
    """)
    png = base64.b64decode(png[len('data:image/png;base64,'):])
    with open(_test_data / f"{basename}.png", 'rb') as f:
        target = f.read()
    assert png == target
128
129

    selenium.run_basthon("stop()")
Romain Casati's avatar
Romain Casati committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151


def test_tutor(selenium):
    result = selenium.run_basthon("""
from tutor import tutor

a = 5
a = a + 1

tutor()""")
    assert result['stderr'] == ""
    assert result['stdout'] == ""
    assert result['display']['display_type'] == 'tutor'
    assert result['display']['iframe-id'] == 'basthon-pythontutor-iframe-0'
    # webserver starts at arbitrary port so this makes absolute paths
    # inconsistent between calls...
    # we remove the host:port part from URLs
    iframe = re.sub(r"http%3A//127.0.0.1%3A\d*",
                    "", result['display']['content'])
    with open(_test_data / "tutor-iframe.html") as f:
        target = f.read()
    assert target == iframe