77
88from fastapi .testclient import TestClient
99import 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
1411from labthings_fastapi .outputs .blob import blob_type
1512from 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
114112def 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
144142def 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 :
0 commit comments