Skip to content

Commit 0c2bc00

Browse files
committed
🧪 improve tests
1 parent b7aca1f commit 0c2bc00

File tree

3 files changed

+111
-34
lines changed

3 files changed

+111
-34
lines changed

tests/general_test.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import asyncio
2+
3+
from codeboxapi import CodeBox
4+
5+
6+
def test_codebox():
7+
codebox = CodeBox()
8+
assert run_sync(codebox), "Failed to run sync codebox remotely"
9+
assert asyncio.run(run_async(codebox)), "Failed to run async codebox remotely"
10+
11+
12+
def test_localbox():
13+
codebox = CodeBox(local=True)
14+
assert run_sync(codebox), "Failed to run sync codebox locally"
15+
assert asyncio.run(run_async(codebox)), "Failed to run async codebox locally"
16+
17+
18+
def run_sync(codebox: CodeBox) -> bool:
19+
try:
20+
assert codebox.start() == "started"
21+
22+
assert codebox.status() == "running"
23+
24+
assert codebox.run("print('Hello World!')") == "Hello World!\n"
25+
26+
file_name = "test_file.txt"
27+
assert file_name in str(codebox.upload(file_name, b"Hello World!"))
28+
29+
assert codebox.download(file_name).content == b"Hello World!"
30+
31+
package_name = "matplotlib"
32+
assert package_name in str(codebox.install(package_name))
33+
assert (
34+
"error"
35+
!= codebox.run("import matplotlib; print(matplotlib.__version__)").type
36+
)
37+
38+
o = codebox.run(
39+
"import matplotlib.pyplot as plt;"
40+
"plt.plot([1, 2, 3, 4], [1, 4, 2, 3]); plt.show()"
41+
)
42+
assert o.type == "image/png"
43+
44+
finally:
45+
assert codebox.stop() == "stopped"
46+
47+
return True
48+
49+
50+
async def run_async(codebox: CodeBox) -> bool:
51+
try:
52+
assert await codebox.astart() == "started"
53+
54+
assert await codebox.astatus() == "running"
55+
56+
assert await codebox.arun("print('Hello World!')") == "Hello World!\n"
57+
58+
file_name = "test_file.txt"
59+
assert file_name in str(await codebox.aupload(file_name, b"Hello World!"))
60+
61+
assert (await codebox.adownload(file_name)).content == b"Hello World!"
62+
63+
package_name = "matplotlib"
64+
assert package_name in str(await codebox.ainstall(package_name))
65+
assert (
66+
"error"
67+
!= (
68+
await codebox.arun("import matplotlib; print(matplotlib.__version__)")
69+
).type
70+
)
71+
72+
o = await codebox.arun(
73+
"import matplotlib.pyplot as plt;"
74+
"plt.plot([1, 2, 3, 4], [1, 4, 2, 3]); plt.show()"
75+
)
76+
assert o.type == "image/png"
77+
78+
finally:
79+
assert await codebox.astop() == "stopped"
80+
81+
return True
82+
83+
84+
if __name__ == "__main__":
85+
test_codebox()
86+
test_localbox()

tests/run_examples_test.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
11
import asyncio
2+
import os
23
import sys
34
from pathlib import Path
45

6+
from dotenv import load_dotenv
57

6-
async def run_example(file: Path):
7-
# Add the examples directory to the path
8-
sys.path.append(str(file.parent))
98

10-
# Run the example
11-
process = await asyncio.create_subprocess_exec("python", file.absolute())
12-
print(f"Running example {file}...")
9+
async def run_example(file: Path, local: bool = False):
10+
process = await asyncio.create_subprocess_exec(
11+
Path(sys.executable).absolute(),
12+
file.absolute(),
13+
env={"CODEBOX_API_KEY": "local" if local else os.environ["CODEBOX_API_KEY"]},
14+
)
1315
await process.wait()
1416

15-
# check the return code
1617
if process.returncode != 0:
1718
raise Exception(f"Example {file} failed with return code {process.returncode}")
1819

19-
# Remove the examples directory from the path
20-
sys.path.remove(str(file.parent))
21-
2220

2321
async def run_examples():
24-
example_files = list(Path("examples").glob("**/*.py"))
25-
# Create a task for each example file
26-
tasks = [asyncio.create_task(run_example(file)) for file in example_files]
27-
# Wait for all tasks to complete
28-
await asyncio.gather(*tasks)
22+
if os.environ.get("CODEBOX_API_KEY") is None:
23+
return print("Skipping remote examples because CODEBOX_API_KEY is not set")
24+
25+
await asyncio.gather(
26+
*[
27+
asyncio.create_task(run_example(file))
28+
for file in list(Path("examples").glob("**/*.py"))
29+
]
30+
)
31+
32+
33+
async def run_examples_local():
34+
for file in list(Path("examples").glob("**/*.py")):
35+
await run_example(file, local=True)
2936

3037

3138
def test_run_examples():
3239
"""Integration test for running the examples."""
33-
# TODO: Run the examples booth local and remote
40+
load_dotenv()
41+
os.environ["CODEBOX_TEST"] = "True"
3442
# TODO: Use ENV variable to reuse the same remote codebox
3543
# TODO: Use ENV variable to disable image and text output when testing
3644
asyncio.run(run_examples())
45+
asyncio.run(run_examples_local())
3746

3847

3948
if __name__ == "__main__":

tests/simple_test.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)