Skip to content

Commit 5ad1e40

Browse files
committed
Update imports in test suite
By using the top level imports in our tests, we make it more likely we'll catch any issues if something changes.
1 parent 06fe7cf commit 5ad1e40

16 files changed

+106
-136
lines changed

tests/test_action_cancel.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@
44

55
import uuid
66
from fastapi.testclient import TestClient
7-
from labthings_fastapi.server import ThingServer
87
from temp_client import poll_task, task_href
9-
from labthings_fastapi.thing import Thing
10-
from labthings_fastapi.decorators import thing_action
11-
from labthings_fastapi.descriptors import ThingProperty
12-
from labthings_fastapi.dependencies.invocation import CancelHook
8+
import labthings_fastapi as lt
139

1410

15-
class ThingOne(Thing):
16-
counter = ThingProperty(int, 0, observable=False)
11+
class ThingOne(lt.Thing):
12+
counter = lt.ThingProperty(int, 0, observable=False)
1713

18-
@thing_action
19-
def count_slowly(self, cancel: CancelHook, n: int = 10):
14+
@lt.thing_action
15+
def count_slowly(self, cancel: lt.CancelHook, n: int = 10):
2016
for i in range(n):
2117
cancel.sleep(0.1)
2218
self.counter += 1
2319

2420

2521
def test_invocation_cancel():
26-
server = ThingServer()
22+
server = lt.ThingServer()
2723
thing_one = ThingOne()
2824
server.add_thing(thing_one, "/thing_one")
2925
with TestClient(server.app) as client:

tests/test_action_logging.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,26 @@
44

55
import logging
66
from fastapi.testclient import TestClient
7-
from labthings_fastapi.server import ThingServer
87
from temp_client import poll_task
9-
from labthings_fastapi.thing import Thing
10-
from labthings_fastapi.decorators import thing_action
11-
from labthings_fastapi.dependencies.invocation import InvocationLogger
8+
import labthings_fastapi as lt
129
from labthings_fastapi.actions.invocation_model import LogRecordModel
1310

1411

15-
class ThingOne(Thing):
12+
class ThingOne(lt.Thing):
1613
LOG_MESSAGES = [
1714
"message 1",
1815
"message 2",
1916
]
2017

21-
@thing_action
22-
def action_one(self, logger: InvocationLogger):
18+
@lt.thing_action
19+
def action_one(self, logger: lt.InvocationLogger):
2320
for m in self.LOG_MESSAGES:
2421
logger.info(m)
2522

2623

2724
def test_invocation_logging(caplog):
2825
caplog.set_level(logging.INFO)
29-
server = ThingServer()
26+
server = lt.ThingServer()
3027
server.add_thing(ThingOne(), "/thing_one")
3128
with TestClient(server.app) as client:
3229
r = client.post("/thing_one/action_one")

tests/test_action_manager.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
from fastapi.testclient import TestClient
22
import pytest
33
import httpx
4-
from labthings_fastapi.server import ThingServer
54
from temp_client import poll_task
65
import time
7-
from labthings_fastapi.thing import Thing
8-
from labthings_fastapi.decorators import thing_action
9-
from labthings_fastapi.descriptors import ThingProperty
6+
import labthings_fastapi as lt
107

118

12-
class TestThing(Thing):
13-
@thing_action(retention_time=0.01)
9+
class TestThing(lt.Thing):
10+
@lt.thing_action(retention_time=0.01)
1411
def increment_counter(self):
1512
"""Increment the counter"""
1613
self.counter += 1
1714

18-
counter = ThingProperty(
15+
counter = lt.ThingProperty(
1916
model=int, initial_value=0, readonly=True, description="A pointless counter"
2017
)
2118

2219

2320
thing = TestThing()
24-
server = ThingServer()
21+
server = lt.ThingServer()
2522
server.add_thing(thing, "/thing")
2623

2724

tests/test_actions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from fastapi.testclient import TestClient
22
import pytest
3-
from labthings_fastapi.server import ThingServer
43
from temp_client import poll_task, get_link
5-
from labthings_fastapi.decorators import thing_action
64
from labthings_fastapi.example_things import MyThing
5+
import labthings_fastapi as lt
76

87
thing = MyThing()
9-
server = ThingServer()
8+
server = lt.ThingServer()
109
server.add_thing(thing, "/thing")
1110

1211

@@ -49,7 +48,7 @@ def test_varargs():
4948
"""Test that we can't use *args in an action"""
5049
with pytest.raises(TypeError):
5150

52-
@thing_action
51+
@lt.thing_action
5352
def action_with_varargs(self, *args) -> None:
5453
"""An action that takes *args"""
5554
pass

tests/test_blob_output.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,59 @@
77

88
from fastapi.testclient import TestClient
99
import pytest
10-
from labthings_fastapi.server import ThingServer
11-
from labthings_fastapi.thing import Thing
12-
from labthings_fastapi.decorators import thing_action
13-
from labthings_fastapi.dependencies.thing import direct_thing_client_dependency
10+
import labthings_fastapi as lt
1411
from labthings_fastapi.outputs.blob import blob_type
1512
from labthings_fastapi.client import ThingClient
1613

1714

18-
TextBlob = blob_type(media_type="text/plain")
15+
class TextBlob(lt.Blob):
16+
media_type: str = "text/plain"
1917

2018

21-
class ThingOne(Thing):
19+
class ThingOne(lt.Thing):
2220
ACTION_ONE_RESULT = b"Action one result!"
2321

2422
def __init__(self):
2523
self._temp_directory = TemporaryDirectory()
2624

27-
@thing_action
25+
@lt.thing_action
2826
def action_one(self) -> TextBlob:
2927
"""An action that makes a blob response from bytes"""
3028
return TextBlob.from_bytes(self.ACTION_ONE_RESULT)
3129

32-
@thing_action
30+
@lt.thing_action
3331
def action_two(self) -> TextBlob:
3432
"""An action that makes a blob response from a file and tempdir"""
3533
td = TemporaryDirectory()
3634
with open(os.path.join(td.name, "serverside"), "wb") as f:
3735
f.write(self.ACTION_ONE_RESULT)
3836
return TextBlob.from_temporary_directory(td, "serverside")
3937

40-
@thing_action
38+
@lt.thing_action
4139
def action_three(self) -> TextBlob:
4240
"""An action that makes a blob response from a file"""
4341
fpath = os.path.join(self._temp_directory.name, "serverside")
4442
with open(fpath, "wb") as f:
4543
f.write(self.ACTION_ONE_RESULT)
4644
return TextBlob.from_file(fpath)
4745

48-
@thing_action
46+
@lt.thing_action
4947
def passthrough_blob(self, blob: TextBlob) -> TextBlob:
5048
"""An action that passes through a blob response"""
5149
return blob
5250

5351

54-
ThingOneDep = direct_thing_client_dependency(ThingOne, "/thing_one/")
52+
ThingOneDep = lt.direct_thing_client_dependency(ThingOne, "/thing_one/")
5553

5654

57-
class ThingTwo(Thing):
58-
@thing_action
55+
class ThingTwo(lt.Thing):
56+
@lt.thing_action
5957
def check_both(self, thing_one: ThingOneDep) -> bool:
6058
"""An action that checks the output of ThingOne"""
6159
check_actions(thing_one)
6260
return True
6361

64-
@thing_action
62+
@lt.thing_action
6563
def check_passthrough(self, thing_one: ThingOneDep) -> bool:
6664
"""An action that checks the passthrough of ThingOne"""
6765
output = thing_one.action_one()
@@ -98,7 +96,7 @@ def test_blob_output_client():
9896
9997
This uses the internal thing client mechanism.
10098
"""
101-
server = ThingServer()
99+
server = lt.ThingServer()
102100
server.add_thing(ThingOne(), "/thing_one")
103101
with TestClient(server.app) as client:
104102
tc = ThingClient.from_url("/thing_one/", client=client)
@@ -113,7 +111,7 @@ def test_blob_output_direct():
113111

114112
def test_blob_output_inserver():
115113
"""Test that the blob output works the same when used directly"""
116-
server = ThingServer()
114+
server = lt.ThingServer()
117115
server.add_thing(ThingOne(), "/thing_one")
118116
server.add_thing(ThingTwo(), "/thing_two")
119117
with TestClient(server.app) as client:
@@ -143,7 +141,7 @@ def check_actions(thing):
143141

144142
def test_blob_input():
145143
"""Check that blobs can be used as input."""
146-
server = ThingServer()
144+
server = lt.ThingServer()
147145
server.add_thing(ThingOne(), "/thing_one")
148146
server.add_thing(ThingTwo(), "/thing_two")
149147
with TestClient(server.app) as client:

tests/test_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from fastapi import Depends, FastAPI, Request
8-
from labthings_fastapi.dependencies.invocation import InvocationID
8+
from labthings_fastapi import InvocationID
99
from labthings_fastapi.file_manager import FileManagerDep
1010
from fastapi.testclient import TestClient
1111
from module_with_deps import FancyIDDep

tests/test_dependencies_2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from fastapi import Depends, FastAPI
1919
from fastapi.testclient import TestClient
2020
from module_with_deps import FancyIDDep, FancyID, ClassDependsOnFancyID
21-
from labthings_fastapi.dependencies.invocation import InvocationID, invocation_id
21+
import labthings_fastapi as lt
22+
from labthings_fastapi.dependencies.invocation import invocation_id
2223
from labthings_fastapi.file_manager import FileManager
2324
from uuid import UUID
2425

@@ -122,7 +123,7 @@ def endpoint(id: DepClass = Depends()) -> bool:
122123

123124

124125
def test_invocation_id():
125-
"""Add an endpoint that uses a dependency from another file"""
126+
"""Add an endpoint that uses a dependency imported from another file"""
126127
app = FastAPI()
127128

128129
@app.post("/endpoint")
@@ -135,11 +136,11 @@ def invoke_fancy(id: Annotated[UUID, Depends(invocation_id)]) -> bool:
135136

136137

137138
def test_invocation_id_alias():
138-
"""Add an endpoint that uses a dependency from another file"""
139+
"""Add an endpoint that uses a dependency alias from another file"""
139140
app = FastAPI()
140141

141142
@app.post("/endpoint")
142-
def endpoint(id: InvocationID) -> bool:
143+
def endpoint(id: lt.InvocationID) -> bool:
143144
return True
144145

145146
with TestClient(app) as client:

tests/test_dependency_metadata.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44

55
from typing import Any, Mapping
66
from fastapi.testclient import TestClient
7-
from labthings_fastapi.server import ThingServer
87
from temp_client import poll_task
9-
from labthings_fastapi.thing import Thing
10-
from labthings_fastapi.decorators import thing_action, thing_property
11-
from labthings_fastapi.dependencies.thing import direct_thing_client_dependency
128
from labthings_fastapi.dependencies.metadata import GetThingStates
9+
import labthings_fastapi as lt
1310

1411

15-
class ThingOne(Thing):
12+
class ThingOne(lt.Thing):
1613
def __init__(self):
17-
Thing.__init__(self)
14+
lt.Thing.__init__(self)
1815
self._a = 0
1916

20-
@thing_property
17+
@lt.thing_property
2118
def a(self):
2219
return self._a
2320

@@ -30,17 +27,17 @@ def thing_state(self):
3027
return {"a": self.a}
3128

3229

33-
ThingOneDep = direct_thing_client_dependency(ThingOne, "/thing_one/")
30+
ThingOneDep = lt.direct_thing_client_dependency(ThingOne, "/thing_one/")
3431

3532

36-
class ThingTwo(Thing):
33+
class ThingTwo(lt.Thing):
3734
A_VALUES = [1, 2, 3]
3835

3936
@property
4037
def thing_state(self):
4138
return {"a": 1}
4239

43-
@thing_action
40+
@lt.thing_action
4441
def count_and_watch(
4542
self, thing_one: ThingOneDep, get_metadata: GetThingStates
4643
) -> Mapping[str, Mapping[str, Any]]:
@@ -52,7 +49,7 @@ def count_and_watch(
5249

5350

5451
def test_fresh_metadata():
55-
server = ThingServer()
52+
server = lt.ThingServer()
5653
server.add_thing(ThingOne(), "/thing_one/")
5754
server.add_thing(ThingTwo(), "/thing_two/")
5855
with TestClient(server.app) as client:

tests/test_endpoint_decorator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from fastapi.testclient import TestClient
2-
from labthings_fastapi.server import ThingServer
3-
from labthings_fastapi.thing import Thing
4-
from labthings_fastapi.decorators import fastapi_endpoint
2+
from labthings_fastapi import ThingServer, Thing, fastapi_endpoint
53
from pydantic import BaseModel
64

75

tests/test_numpy_type.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import numpy as np
55

66
from labthings_fastapi.types.numpy import NDArray, DenumpifyingDict
7-
from labthings_fastapi.thing import Thing
8-
from labthings_fastapi.decorators import thing_action
7+
import labthings_fastapi as lt
98

109

1110
class ArrayModel(RootModel):
@@ -63,8 +62,8 @@ class Model(BaseModel):
6362
m.model_dump_json()
6463

6564

66-
class MyNumpyThing(Thing):
67-
@thing_action
65+
class MyNumpyThing(lt.Thing):
66+
@lt.thing_action
6867
def action_with_arrays(self, a: NDArray) -> NDArray:
6968
return a * 2
7069

0 commit comments

Comments
 (0)