Skip to content

Commit f05fbfc

Browse files
committed
server tests
1 parent 96659c9 commit f05fbfc

File tree

1 file changed

+118
-1
lines changed

1 file changed

+118
-1
lines changed

tests/test_server.py

+118-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pytest
22
from papercast.server import Server
33
from papercast.pipelines import Pipeline
4-
from papercast.base import BaseProcessor
4+
from papercast.base import BaseProcessor, Production
5+
from fastapi import HTTPException
56

67

78
class TestServer:
@@ -29,3 +30,119 @@ def test_get_pipeline(self):
2930
pipelines={"test": Pipeline("test")},
3031
)
3132
assert server._get_pipeline("test") is not None
33+
34+
@pytest.mark.asyncio
35+
async def test_add_valid_input_to_default(self):
36+
class MyProcessor(BaseProcessor):
37+
input_types = {
38+
"input1": int,
39+
}
40+
41+
def process(self, input: Production) -> Production:
42+
return input
43+
44+
processor = MyProcessor()
45+
46+
pipeline = Pipeline("test")
47+
48+
pipeline.add_processor("test", processor)
49+
50+
server = Server(
51+
pipelines={"default": pipeline},
52+
)
53+
result = await server._add({"input1": 5})
54+
assert result is not None
55+
assert result == {"message": "Document(s) added to pipeline"}
56+
57+
@pytest.mark.asyncio
58+
@pytest.mark.xfail
59+
async def test_add_invalid_input_to_default(self):
60+
# TODO This test should at least warn the user that the input is invalid
61+
# Currently it runs without issue
62+
# Though I think the responsiblity of checking
63+
# the input should be on the pipeline or the processor
64+
class MyProcessor(BaseProcessor):
65+
input_types = {
66+
"input1": int,
67+
}
68+
69+
def process(self, input: Production) -> Production:
70+
return input
71+
72+
processor = MyProcessor()
73+
74+
pipeline = Pipeline("test")
75+
76+
pipeline.add_processor("test", processor)
77+
78+
server = Server(
79+
pipelines={"default": pipeline},
80+
)
81+
with pytest.raises(HTTPException) as exc_info:
82+
result = await server._add({"incorrect_input_key": 5})
83+
84+
@pytest.mark.asyncio
85+
async def test_add_valid_input_to_non_default_pipeline(self):
86+
class MyProcessor(BaseProcessor):
87+
input_types = {
88+
"input1": int,
89+
}
90+
91+
def process(self, input: Production) -> Production:
92+
return input
93+
94+
processor = MyProcessor()
95+
96+
pipeline = Pipeline("non_default")
97+
pipeline.add_processor("test", processor)
98+
99+
server = Server(
100+
pipelines={"default": Pipeline("default"), "non_default": pipeline},
101+
)
102+
result = await server._add({"pipeline": "non_default", "input1": 5})
103+
assert result is not None
104+
assert result == {"message": "Document(s) added to pipeline"}
105+
106+
@pytest.mark.asyncio
107+
@pytest.mark.xfail
108+
async def test_add_invalid_input_to_non_default_pipeline(self):
109+
# TODO This test should at least warn the user that the input is invalid
110+
# Currently it runs without issue
111+
# Though I think the responsiblity of checking
112+
# the input should be on the pipeline or the processor
113+
class MyProcessor(BaseProcessor):
114+
input_types = {
115+
"input1": int,
116+
}
117+
118+
def process(self, input: Production) -> Production:
119+
return input
120+
121+
processor = MyProcessor()
122+
123+
pipeline = Pipeline("non_default")
124+
pipeline.add_processor("test", processor)
125+
126+
server = Server(
127+
pipelines={"default": Pipeline("default"), "non_default": pipeline},
128+
)
129+
130+
with pytest.raises(HTTPException) as exc_info:
131+
await server._add({"pipeline": "non_default", "incorrect_input_key": 5})
132+
133+
@pytest.mark.asyncio
134+
async def test_add_input_to_missing_pipeline(self):
135+
server = Server(
136+
pipelines={},
137+
)
138+
with pytest.raises(HTTPException) as exc_info:
139+
await server._add({"pipeline": "missing"})
140+
141+
@pytest.mark.asyncio
142+
async def test_pipeline_not_specified_and_no_default_pipeline(self):
143+
server = Server(
144+
pipelines={},
145+
)
146+
147+
with pytest.raises(HTTPException) as exc_info:
148+
await server._add({})

0 commit comments

Comments
 (0)