Skip to content

Commit b594169

Browse files
authored
test: test against human api (#12)
1 parent 2e55b15 commit b594169

File tree

5 files changed

+298
-253
lines changed

5 files changed

+298
-253
lines changed

scaleway-async/tests/test_registry_v1.py

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

scaleway-async/tests/test_test_v1.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import unittest
2+
from contextlib import AsyncExitStack
3+
from typing import Optional
4+
5+
import utils
6+
7+
from scaleway_async import Client, WaitForOptions
8+
from scaleway_async.test.v1 import EyeColors, Human, HumanStatus, TestV1API
9+
10+
11+
class TestTestV1(unittest.IsolatedAsyncioTestCase):
12+
async def asyncSetUp(self) -> None:
13+
client = Client.from_config_file_and_env()
14+
self.api = TestV1API(client, bypass_validation=True)
15+
16+
res = await self.api.register(username="scaleway-sdk-python")
17+
client.access_key = res.access_key
18+
client.secret_key = res.secret_key
19+
20+
async def test_create_human(self) -> None:
21+
name = utils.random_name()
22+
23+
async with AsyncExitStack() as stack:
24+
human = await self.api.create_human(
25+
height=1.0,
26+
shoe_size=1.0,
27+
altitude_in_meter=1,
28+
altitude_in_millimeter=1,
29+
fingers_count=1,
30+
hair_count=1,
31+
is_happy=True,
32+
eyes_color=EyeColors.BROWN,
33+
name=name,
34+
)
35+
stack.push_async_callback(self.api.delete_human, human_id=human.id)
36+
37+
self.assertEqual(human.name, name)
38+
39+
async def test_list_humans(self) -> None:
40+
humans = await self.api.list_humans()
41+
self.assertTrue(type(humans.humans) is list)
42+
43+
async def test_list_humans_all(self) -> None:
44+
humans = await self.api.list_humans_all()
45+
self.assertTrue(type(humans) is list)
46+
47+
async def test_get_human(self) -> None:
48+
name = utils.random_name()
49+
50+
async with AsyncExitStack() as stack:
51+
human = await self.api.create_human(
52+
height=1.0,
53+
shoe_size=1.0,
54+
altitude_in_meter=1,
55+
altitude_in_millimeter=1,
56+
fingers_count=1,
57+
hair_count=1,
58+
is_happy=True,
59+
eyes_color=EyeColors.BROWN,
60+
name=name,
61+
)
62+
stack.push_async_callback(self.api.delete_human, human_id=human.id)
63+
64+
human2 = await self.api.get_human(human_id=human.id)
65+
self.assertEqual(human.id, human2.id)
66+
67+
async def test_update_human(self) -> None:
68+
name = utils.random_name()
69+
70+
async with AsyncExitStack() as stack:
71+
human = await self.api.create_human(
72+
height=1.0,
73+
shoe_size=1.0,
74+
altitude_in_meter=1,
75+
altitude_in_millimeter=1,
76+
fingers_count=1,
77+
hair_count=1,
78+
is_happy=True,
79+
eyes_color=EyeColors.BROWN,
80+
name=name,
81+
)
82+
stack.push_async_callback(self.api.delete_human, human_id=human.id)
83+
84+
human2 = await self.api.update_human(
85+
human_id=human.id,
86+
height=2.0,
87+
eyes_color=EyeColors.BLUE,
88+
)
89+
90+
self.assertEqual(human.id, human2.id)
91+
self.assertEqual(human2.height, 2.0)
92+
self.assertEqual(human2.eyes_color, EyeColors.BLUE)
93+
94+
async def test_delete_human(self) -> None:
95+
name = utils.random_name()
96+
97+
human: Optional[Human] = None
98+
99+
async with AsyncExitStack() as stack:
100+
human = await self.api.create_human(
101+
height=1.0,
102+
shoe_size=1.0,
103+
altitude_in_meter=1,
104+
altitude_in_millimeter=1,
105+
fingers_count=1,
106+
hair_count=1,
107+
is_happy=True,
108+
eyes_color=EyeColors.BROWN,
109+
name=name,
110+
)
111+
stack.push_async_callback(self.api.delete_human, human_id=human.id)
112+
113+
if human is None:
114+
raise Exception("human is None")
115+
116+
try:
117+
await self.api.wait_for_human(human_id=human.id)
118+
except Exception as e:
119+
self.assertNotIsInstance(e, TimeoutError)
120+
pass
121+
122+
async def test_run_human(self):
123+
name = utils.random_name()
124+
125+
async with AsyncExitStack() as stack:
126+
human = await self.api.create_human(
127+
height=1.0,
128+
shoe_size=1.0,
129+
altitude_in_meter=1,
130+
altitude_in_millimeter=1,
131+
fingers_count=1,
132+
hair_count=1,
133+
is_happy=True,
134+
eyes_color=EyeColors.BROWN,
135+
name=name,
136+
)
137+
stack.push_async_callback(self.api.delete_human, human_id=human.id)
138+
139+
await self.api.run_human(human_id=human.id)
140+
human = await self.api.wait_for_human(
141+
human_id=human.id,
142+
options=WaitForOptions(
143+
stop=lambda human: human.status == HumanStatus.RUNNING
144+
),
145+
)
146+
147+
self.assertEqual(human.status, HumanStatus.RUNNING)

scaleway-core/scaleway_core/api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ class ValidationError(ScalewayException):
103103

104104

105105
class API:
106-
def __init__(self, client: Client):
107-
client.validate()
106+
def __init__(self, client: Client, *, bypass_validation: bool = False):
107+
if not bypass_validation:
108+
client.validate()
109+
108110
self.client = client
109111
self._log = logging.getLogger(__name__)
110112

0 commit comments

Comments
 (0)