Skip to content

Commit

Permalink
TST Convert some package tests to use new test system (pyodide#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalcde authored Jan 13, 2021
1 parent 816aaa0 commit 170c52d
Show file tree
Hide file tree
Showing 12 changed files with 423 additions and 409 deletions.
17 changes: 8 additions & 9 deletions packages/Jinja2/test_jinja2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
def test_jinja2(selenium):
selenium.load_package("Jinja2")
selenium.run(
"""
import jinja2
from pyodide_build.testing import run_in_pyodide

template = jinja2.Template('Hello {{ name }}!')
"""
)
content = selenium.run("""template.render(name='Zach')""")

@run_in_pyodide(packages=["Jinja2"])
def test_jinja2():
import jinja2

template = jinja2.Template("Hello {{ name }}!")
content = template.render(name="Zach")
assert content == "Hello Zach!"
44 changes: 23 additions & 21 deletions packages/cloudpickle/test_cloudpickle.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
def test_cloudpickle(selenium):
selenium.load_package("cloudpickle")
selenium.run(
r"""
import cloudpickle
squared = lambda x: x ** 2
pickled_lambda = cloudpickle.dumps(squared)
import pickle
new_squared = pickle.loads(pickled_lambda)
assert new_squared(2) == 4
CONSTANT = 42
def my_function(data: int) -> int:
return data + CONSTANT
pickled_function = cloudpickle.dumps(my_function)
depickled_function = pickle.loads(pickled_function)
assert depickled_function(43) == 85
"""
)
from pyodide_build.testing import run_in_pyodide


@run_in_pyodide(packages=["cloudpickle"])
def test_cloudpickle():
import cloudpickle

squared = lambda x: x ** 2
pickled_lambda = cloudpickle.dumps(squared)

import pickle

new_squared = pickle.loads(pickled_lambda)
assert new_squared(2) == 4

CONSTANT = 42

def my_function(data: int) -> int:
return data + CONSTANT

pickled_function = cloudpickle.dumps(my_function)
depickled_function = pickle.loads(pickled_function)
assert depickled_function(43) == 85
27 changes: 13 additions & 14 deletions packages/imageio/test_imageio.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
def test_imageio(selenium):
selenium.load_package(["numpy", "imageio"])
selenium.run(
r"""
import numpy as np
import imageio
from pyodide_build.testing import run_in_pyodide

filename = "/tmp/foo.tif"
image_in = np.random.randint(0, 65535, size=(100, 36), dtype=np.uint16)
imageio.imwrite(filename, image_in)
image_out = imageio.imread(filename)
assert image_out.shape == (100, 36)
np.testing.assert_equal(image_in, image_out)
"""
)

@run_in_pyodide(standalone=True, packages=["numpy", "imageio"])
def test_imageio():
import numpy as np
import imageio

filename = "/tmp/foo.tif"
image_in = np.random.randint(0, 65535, size=(100, 36), dtype=np.uint16)
imageio.imwrite(filename, image_in)
image_out = imageio.imread(filename)
assert image_out.shape == (100, 36)
np.testing.assert_equal(image_in, image_out)
91 changes: 44 additions & 47 deletions packages/msgpack/test_pack.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
def test_pack(selenium_standalone):
selenium = selenium_standalone
selenium.load_package(["msgpack"])
selenium.run(
"""
from msgpack import packb, unpackb, Unpacker, Packer, pack
from pyodide_build.testing import run_in_pyodide


def check(data, use_list=False):
re = unpackb(packb(data), use_list=use_list, strict_map_key=False)
assert re == data
@run_in_pyodide(standalone=True, packages=["msgpack"])
def test_pack():
from msgpack import packb, unpackb, Unpacker, Packer, pack

test_data = [
0,
1,
127,
128,
255,
256,
65535,
65536,
4294967295,
4294967296,
-1,
-32,
-33,
-128,
-129,
-32768,
-32769,
-4294967296,
-4294967297,
1.0,
b"",
b"a",
b"a" * 31,
b"a" * 32,
None,
True,
False,
(),
((),),
((), None),
{None: 0},
(1 << 23),
]
for td in test_data:
check(td)
"""
)
def check(data, use_list=False):
re = unpackb(packb(data), use_list=use_list, strict_map_key=False)
assert re == data

test_data = [
0,
1,
127,
128,
255,
256,
65535,
65536,
4294967295,
4294967296,
-1,
-32,
-33,
-128,
-129,
-32768,
-32769,
-4294967296,
-4294967297,
1.0,
b"",
b"a",
b"a" * 31,
b"a" * 32,
None,
True,
False,
(),
((),),
((), None),
{None: 0},
(1 << 23),
]
for td in test_data:
check(td)
22 changes: 10 additions & 12 deletions packages/networkx/test_networkx.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
def test_networkx_basicgraph(selenium_standalone, request):
selenium = selenium_standalone
selenium.load_package(["networkx"])
cmd = """
import networkx as nx
from pyodide_build.testing import run_in_pyodide

G = nx.Graph()
G.add_nodes_from([1,2,3])
G.add_edges_from([(1,2), (1,3)])

assert G.number_of_nodes() == 3
assert G.number_of_edges() == 2
"""
@run_in_pyodide(standalone=True, packages=["networkx"])
def test_networkx_basicgraph():
import networkx as nx

selenium.run(cmd)
G = nx.Graph()
G.add_nodes_from([1, 2, 3])
G.add_edges_from([(1, 2), (1, 3)])

assert G.number_of_nodes() == 3
assert G.number_of_edges() == 2
84 changes: 47 additions & 37 deletions packages/nlopt/test_nlopt.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,60 @@
def test_nlopt(selenium):
selenium.load_package("nlopt")
assert selenium.run(
"""
import numpy as np
import nlopt
from pyodide_build.testing import run_in_pyodide

# objective function
def f(x, grad):
x0 = x[0]
x1 = x[1]
y = 67.8306620138889-13.5689721666667*x0-3.83269458333333*x1+\
0.720841066666667*x0**2+0.3427605*x0*x1+\
0.0640322916666664*x1**2

grad[0] = 1.44168213333333*x0 + 0.3427605*x1 - 13.5689721666667
grad[1] = 0.3427605*x0 + 0.128064583333333*x1 - 3.83269458333333
@run_in_pyodide(packages=["nlopt"])
def test_nlopt():
import numpy as np
import nlopt

return y
# objective function
def f(x, grad):
x0 = x[0]
x1 = x[1]
y = (
67.8306620138889
- 13.5689721666667 * x0
- 3.83269458333333 * x1
+ 0.720841066666667 * x0 ** 2
+ 0.3427605 * x0 * x1
+ 0.0640322916666664 * x1 ** 2
)

# inequality constraint (constrained to be <= 0)
def h(x, grad):
x0 = x[0]
x1 = x[1]
z = -3.72589930555515+128.965158333333*x0+0.341479166666643*x1-\
0.19642666666667*x0**2+2.78692500000002*x0*x1-\
0.0000104166666686543*x1**2-468.897287036862
grad[0] = 1.44168213333333 * x0 + 0.3427605 * x1 - 13.5689721666667
grad[1] = 0.3427605 * x0 + 0.128064583333333 * x1 - 3.83269458333333

grad[0] = -0.39285333333334*x0 + 2.78692500000002*x1 + 128.965158333333
grad[1] = 2.78692500000002*x0 - 2.08333333373086e-5*x1 + 0.341479166666643
return y

return z
# inequality constraint (constrained to be <= 0)
def h(x, grad):
x0 = x[0]
x1 = x[1]
z = (
-3.72589930555515
+ 128.965158333333 * x0
+ 0.341479166666643 * x1
- 0.19642666666667 * x0 ** 2
+ 2.78692500000002 * x0 * x1
- 0.0000104166666686543 * x1 ** 2
- 468.897287036862
)

opt = nlopt.opt(nlopt.LD_SLSQP, 2)
opt.set_min_objective(f)
grad[0] = -0.39285333333334 * x0 + 2.78692500000002 * x1 + 128.965158333333
grad[1] = 2.78692500000002 * x0 - 2.08333333373086e-5 * x1 + 0.341479166666643

opt.set_lower_bounds(np.array([2.5, 7]))
opt.set_upper_bounds(np.array([7.5, 15]))
return z

opt.add_inequality_constraint(h)
opt = nlopt.opt(nlopt.LD_SLSQP, 2)
opt.set_min_objective(f)

opt.set_ftol_rel(1.0e-6)
opt.set_lower_bounds(np.array([2.5, 7]))
opt.set_upper_bounds(np.array([7.5, 15]))

x0 = np.array([5, 11])
opt.add_inequality_constraint(h)

xopt = opt.optimize(x0)
opt.set_ftol_rel(1.0e-6)

np.linalg.norm(xopt - np.array([2.746310775, 15.0])) < 1e-7
"""
)
x0 = np.array([5, 11])

xopt = opt.optimize(x0)

assert np.linalg.norm(xopt - np.array([2.746310775, 15.0])) < 1e-7
Loading

0 comments on commit 170c52d

Please sign in to comment.