Skip to content

Commit 2744766

Browse files
committed
πŸ§‘β€πŸ”¬ improve tests
1 parent b32c2c0 commit 2744766

File tree

4 files changed

+39
-216
lines changed

4 files changed

+39
-216
lines changed

β€Žtests/conftest.pyβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
import pytest
4+
from codeboxapi import CodeBox
5+
6+
LOCALBOX = CodeBox(api_key="local")
7+
8+
9+
@pytest.fixture(
10+
scope="session",
11+
params=["local", "docker", os.getenv("CODEBOX_API_KEY")],
12+
)
13+
def codebox(request: pytest.FixtureRequest) -> CodeBox:
14+
if request.param == "local":
15+
return LOCALBOX
16+
17+
if request.param == "docker" and os.system("docker ps > /dev/null 2>&1") != 0:
18+
pytest.skip("Docker is not running")
19+
20+
return CodeBox(api_key=request.param)

β€Žtests/parametric_test.pyβ€Ž

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

β€Žtests/test_v01.pyβ€Ž

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
import asyncio
2-
import os
3-
1+
import pytest
42
from codeboxapi import CodeBox
53

64

7-
def test_codebox():
8-
codebox = CodeBox(api_key=os.getenv("CODEBOX_API_KEY"))
9-
assert run_sync(codebox), "Failed to run sync codebox remotely"
10-
assert asyncio.run(run_async(codebox)), "Failed to run async codebox remotely"
11-
12-
13-
def test_localbox():
14-
codebox = CodeBox(api_key="local")
15-
assert run_sync(codebox), "Failed to run sync codebox locally"
16-
assert asyncio.run(run_async(codebox)), "Failed to run async codebox locally"
17-
18-
19-
def run_sync(codebox: CodeBox) -> bool:
5+
def test_sync(codebox: CodeBox) -> None:
206
try:
217
assert codebox.start() == "started"
228
print("Started")
@@ -59,10 +45,9 @@ def run_sync(codebox: CodeBox) -> bool:
5945
assert codebox.stop() == "stopped"
6046
print("Stopped")
6147

62-
return True
6348

64-
65-
async def run_async(codebox: CodeBox) -> bool:
49+
@pytest.mark.asyncio
50+
async def test_async(codebox: CodeBox) -> None:
6651
try:
6752
assert await codebox.astart() == "started"
6853
print("Started")
@@ -107,10 +92,3 @@ async def run_async(codebox: CodeBox) -> bool:
10792
finally:
10893
assert await codebox.astop() == "stopped"
10994
print("Stopped")
110-
111-
return True
112-
113-
114-
if __name__ == "__main__":
115-
test_codebox()
116-
test_localbox()

β€Žtests/test_v02.pyβ€Ž

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
import os
21
import time
32

43
import pytest
54
from codeboxapi import CodeBox, ExecChunk, ExecResult, RemoteFile
65

76

8-
@pytest.fixture(
9-
scope="session",
10-
params=[
11-
"local",
12-
"docker",
13-
os.getenv("CODEBOX_API_KEY"),
14-
],
15-
)
16-
def codebox(request):
17-
if request.param == "docker" and os.system("docker ps > /dev/null 2>&1") != 0:
18-
pytest.skip("Docker is not running")
19-
return CodeBox(api_key=request.param) # api_key=request.param)
20-
21-
227
def test_sync_codebox_lifecycle(codebox: CodeBox):
238
assert codebox.healthcheck() == "healthy", "CodeBox should be healthy"
249

@@ -248,6 +233,7 @@ async def test_async_stream_exec(codebox: CodeBox):
248233
chunks[i][1] < chunks[i + 1][1] for i in range(len(chunks) - 1)
249234
), "Chunks should arrive with delay (ipython)"
250235
# Verify delay is approximately 0.01s
236+
print([abs(chunks[i + 1][1] - chunks[i][1] - 0.01) for i in range(len(chunks) - 1)])
251237
assert all(
252238
abs(chunks[i + 1][1] - chunks[i][1] - 0.01) < 0.005
253239
for i in range(len(chunks) - 1)
@@ -324,5 +310,19 @@ async def test_async_bash_commands(codebox: CodeBox):
324310
assert result.text.strip() == "Hello!", "Execution result should be 'Hello!'"
325311

326312

313+
def test_local_box_singleton():
314+
from codeboxapi.local import LocalBox
315+
316+
with pytest.raises(RuntimeError) as exc_info:
317+
_ = LocalBox()
318+
319+
assert "Only one LocalBox instance can exist at a time" in str(exc_info.value)
320+
321+
with pytest.raises(RuntimeError) as exc_info:
322+
_ = CodeBox(api_key="local")
323+
324+
assert "codeboxapi.com" in str(exc_info.value)
325+
326+
327327
if __name__ == "__main__":
328328
pytest.main([__file__])

0 commit comments

Comments
Β (0)