Skip to content

Commit

Permalink
πŸ§‘β€πŸ”¬ improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Oct 30, 2024
1 parent b32c2c0 commit 2744766
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 216 deletions.
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os

import pytest
from codeboxapi import CodeBox

LOCALBOX = CodeBox(api_key="local")


@pytest.fixture(
scope="session",
params=["local", "docker", os.getenv("CODEBOX_API_KEY")],
)
def codebox(request: pytest.FixtureRequest) -> CodeBox:
if request.param == "local":
return LOCALBOX

if request.param == "docker" and os.system("docker ps > /dev/null 2>&1") != 0:
pytest.skip("Docker is not running")

return CodeBox(api_key=request.param)
175 changes: 0 additions & 175 deletions tests/parametric_test.py

This file was deleted.

30 changes: 4 additions & 26 deletions tests/test_v01.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import asyncio
import os

import pytest
from codeboxapi import CodeBox


def test_codebox():
codebox = CodeBox(api_key=os.getenv("CODEBOX_API_KEY"))
assert run_sync(codebox), "Failed to run sync codebox remotely"
assert asyncio.run(run_async(codebox)), "Failed to run async codebox remotely"


def test_localbox():
codebox = CodeBox(api_key="local")
assert run_sync(codebox), "Failed to run sync codebox locally"
assert asyncio.run(run_async(codebox)), "Failed to run async codebox locally"


def run_sync(codebox: CodeBox) -> bool:
def test_sync(codebox: CodeBox) -> None:
try:
assert codebox.start() == "started"
print("Started")
Expand Down Expand Up @@ -59,10 +45,9 @@ def run_sync(codebox: CodeBox) -> bool:
assert codebox.stop() == "stopped"
print("Stopped")

return True


async def run_async(codebox: CodeBox) -> bool:
@pytest.mark.asyncio
async def test_async(codebox: CodeBox) -> None:
try:
assert await codebox.astart() == "started"
print("Started")
Expand Down Expand Up @@ -107,10 +92,3 @@ async def run_async(codebox: CodeBox) -> bool:
finally:
assert await codebox.astop() == "stopped"
print("Stopped")

return True


if __name__ == "__main__":
test_codebox()
test_localbox()
30 changes: 15 additions & 15 deletions tests/test_v02.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import os
import time

import pytest
from codeboxapi import CodeBox, ExecChunk, ExecResult, RemoteFile


@pytest.fixture(
scope="session",
params=[
"local",
"docker",
os.getenv("CODEBOX_API_KEY"),
],
)
def codebox(request):
if request.param == "docker" and os.system("docker ps > /dev/null 2>&1") != 0:
pytest.skip("Docker is not running")
return CodeBox(api_key=request.param) # api_key=request.param)


def test_sync_codebox_lifecycle(codebox: CodeBox):
assert codebox.healthcheck() == "healthy", "CodeBox should be healthy"

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


def test_local_box_singleton():
from codeboxapi.local import LocalBox

with pytest.raises(RuntimeError) as exc_info:
_ = LocalBox()

assert "Only one LocalBox instance can exist at a time" in str(exc_info.value)

with pytest.raises(RuntimeError) as exc_info:
_ = CodeBox(api_key="local")

assert "codeboxapi.com" in str(exc_info.value)


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 2744766

Please sign in to comment.