Skip to content

Commit 66418d8

Browse files
committed
Add some basic tests that run against a local copy of XPipe
1 parent e784a65 commit 66418d8

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ python = "^3.10"
1111
requests = "^2.32.3"
1212
aiohttp = "^3.9.5"
1313

14-
[tool.poetry.dev-dependencies]
14+
[tool.poetry.group.dev.dependencies]
15+
pytest = "^8.2.2"
16+
pytest-asyncio = "^0.23.7"
1517

1618
[build-system]
1719
requires = ["poetry-core>=1.0.0"]

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
asyncio_mode = auto

tests/test_clients.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import pytest
2+
import os
3+
from xpipe_client.clients import Client, AsyncClient
4+
5+
6+
@pytest.fixture
7+
def sync_local_client():
8+
return Client()
9+
10+
11+
@pytest.fixture
12+
async def async_local_client():
13+
async with AsyncClient() as async_client:
14+
yield async_client
15+
16+
17+
def test_sync_local_login(sync_local_client: Client):
18+
assert sync_local_client.session is None
19+
sync_local_client.renew_session()
20+
assert sync_local_client.session is not None
21+
22+
23+
async def test_async_local_login(async_local_client: AsyncClient):
24+
assert async_local_client.session is None
25+
await async_local_client.renew_session()
26+
assert async_local_client.session is not None
27+
28+
29+
def test_sync_apikey_login():
30+
apikey = os.environ.get("XPIPE_APIKEY", None)
31+
assert apikey is not None
32+
client = Client(token=apikey)
33+
assert client.session is None
34+
client.renew_session()
35+
assert client.session is not None
36+
37+
38+
async def test_async_apikey_login():
39+
apikey = os.environ.get("XPIPE_APIKEY", None)
40+
assert apikey is not None
41+
client = AsyncClient(token=apikey)
42+
assert client.session is None
43+
await client.renew_session()
44+
assert client.session is not None
45+
46+
47+
def test_connection_query(sync_local_client: Client):
48+
connections = sync_local_client.connection_query(connections="")
49+
assert len(connections) > 0
50+
assert connections[0].get("connection", None) is not None
51+
52+
53+
async def test_async_connection_query(async_local_client: AsyncClient):
54+
connections = await async_local_client.connection_query(connections="")
55+
assert len(connections) > 0
56+
assert connections[0].get("connection", None) is not None
57+
58+
59+
def test_shell_start(sync_local_client: Client):
60+
local_connection = sync_local_client.connection_query(connections="")[0]["connection"]
61+
sync_local_client.shell_start(local_connection)
62+
63+
64+
async def test_async_shell_start(async_local_client: AsyncClient):
65+
connections = await async_local_client.connection_query(connections="")
66+
local_connection = connections[0]["connection"]
67+
await async_local_client.shell_start(local_connection)
68+
69+
70+
def test_shell_stop(sync_local_client: Client):
71+
local_connection = sync_local_client.connection_query(connections="")[0]["connection"]
72+
sync_local_client.shell_start(local_connection)
73+
sync_local_client.shell_stop(local_connection)
74+
75+
76+
async def test_async_shell_stop(async_local_client: AsyncClient):
77+
connections = await async_local_client.connection_query(connections="")
78+
local_connection = connections[0]["connection"]
79+
await async_local_client.shell_start(local_connection)
80+
await async_local_client.shell_stop(local_connection)
81+
82+
83+
def test_shell_exec(sync_local_client: Client):
84+
local_connection = sync_local_client.connection_query(connections="")[0]["connection"]
85+
sync_local_client.shell_start(local_connection)
86+
retval = sync_local_client.shell_exec(local_connection, "echo hello world")
87+
assert retval == {'exitCode': 0, "stdout": "hello world", "stderr": ""}
88+
sync_local_client.shell_stop(local_connection)
89+
90+
91+
async def test_async_shell_exec(async_local_client: AsyncClient):
92+
local_connection = (await async_local_client.connection_query(connections=""))[0]["connection"]
93+
await async_local_client.shell_start(local_connection)
94+
retval = await async_local_client.shell_exec(local_connection, "echo hello world")
95+
assert retval == {'exitCode': 0, "stdout": "hello world", "stderr": ""}
96+
await async_local_client.shell_stop(local_connection)

xpipe_client/clients.py

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def shell_exec(self, conn_uuid: str, command: str):
9898
class AsyncClient(Client):
9999
aiohttp_session: Optional[aiohttp.ClientSession] = None
100100

101+
async def __aenter__(self):
102+
if not self.aiohttp_session:
103+
self.aiohttp_session = aiohttp.ClientSession()
104+
return self
105+
106+
async def __aexit__(self, exc_type, exc_val, exc_tb):
107+
if self.aiohttp_session:
108+
await self.aiohttp_session.close()
109+
101110
async def renew_session(self):
102111
if not self.aiohttp_session:
103112
self.aiohttp_session = aiohttp.ClientSession()

0 commit comments

Comments
 (0)