Skip to content

Commit 9e5aec8

Browse files
committed
fix(py/genkit): fix resource test failures
- Replace MockInput classes with ResourceInput instances in tests - Use resource() function instead of Action constructor for dynamic providers - Change provider_fn return type from Action|None to object to avoid Pydantic schema generation errors - Remove unused Action import All 729 tests now pass.
1 parent 925958f commit 9e5aec8

2 files changed

Lines changed: 11 additions & 28 deletions

File tree

py/packages/genkit/tests/genkit/ai/resource_integration_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from genkit.blocks.generate import generate_action
2525
from genkit.blocks.resource import ResourceInput, ResourceOutput, define_resource
26-
from genkit.core.action import Action, ActionRunContext
26+
from genkit.core.action import ActionRunContext
2727
from genkit.core.registry import ActionKind, Registry
2828
from genkit.core.typing import (
2929
GenerateActionOptions,
@@ -79,15 +79,17 @@ async def test_dynamic_action_provider_resource() -> None:
7979
registry = Registry()
8080

8181
# Register a dynamic provider that handles any "dynamic://*" uri
82-
def provider_fn(input: dict[str, object], ctx: ActionRunContext) -> Action | None:
82+
def provider_fn(input: dict[str, object], ctx: ActionRunContext) -> object:
83+
from genkit.blocks.resource import resource
84+
8385
kind = cast(ActionKind, input['kind'])
8486
name = cast(str, input['name'])
8587
if kind == ActionKind.RESOURCE and name.startswith('dynamic://'):
8688

8789
async def dyn_res_fn(input: ResourceInput, ctx: ActionRunContext) -> ResourceOutput:
8890
return ResourceOutput(content=[Part(root=TextPart(text=f'Dynamic content for {input.uri}'))])
8991

90-
return Action(kind=ActionKind.RESOURCE, name=name, fn=dyn_res_fn)
92+
return resource({'uri': name}, dyn_res_fn)
9193
return None
9294

9395
# Register the provider as an action (it effectively acts as a factory)

py/packages/genkit/tests/genkit/ai/resource_test.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,8 @@ async def my_resource_fn(input: ResourceInput, ctx: ActionRunContext) -> dict[st
203203
res = resource({'uri': 'http://example.com/foo'}, my_resource_fn)
204204
assert res.matches is not None
205205

206-
class MockInput:
207-
uri = 'http://example.com/foo'
208-
209-
assert res.matches(MockInput())
210-
211-
class MockInputBad:
212-
uri = 'http://example.com/bar'
213-
214-
assert not res.matches(MockInputBad())
206+
assert res.matches(ResourceInput(uri='http://example.com/foo'))
207+
assert not res.matches(ResourceInput(uri='http://example.com/bar'))
215208

216209

217210
def test_template_matching() -> None:
@@ -228,16 +221,10 @@ async def my_resource_fn(input: ResourceInput, ctx: ActionRunContext) -> dict[st
228221
res = resource({'template': 'http://example.com/items/{id}'}, my_resource_fn)
229222
assert res.matches is not None
230223

231-
class MockInput:
232-
uri = 'http://example.com/items/123'
233-
234-
assert res.matches(MockInput())
235-
236-
class MockInputBad:
237-
uri = 'http://example.com/items/123/details'
224+
assert res.matches(ResourceInput(uri='http://example.com/items/123'))
238225

239226
# Should not match because of strict end anchor or slash handling in our regex
240-
assert not res.matches(MockInputBad())
227+
assert not res.matches(ResourceInput(uri='http://example.com/items/123/details'))
241228

242229

243230
def test_reserved_expansion_matching() -> None:
@@ -254,16 +241,10 @@ async def my_resource_fn(input: ResourceInput, ctx: ActionRunContext) -> dict[st
254241
res = resource({'template': 'http://example.com/files/{+path}'}, my_resource_fn)
255242
assert res.matches is not None
256243

257-
class MockInput:
258-
uri = 'http://example.com/files/foo/bar/baz.txt'
259-
260-
assert res.matches(MockInput())
244+
assert res.matches(ResourceInput(uri='http://example.com/files/foo/bar/baz.txt'))
261245

262246
# Regular template {path} regex ([^/]+) should NOT match slashes
263247
res_simple = resource({'template': 'http://example.com/items/{id}'}, my_resource_fn)
264248
assert res_simple.matches is not None
265249

266-
class MockInputComplex:
267-
uri = 'http://example.com/items/foo/bar'
268-
269-
assert not res_simple.matches(MockInputComplex())
250+
assert not res_simple.matches(ResourceInput(uri='http://example.com/items/foo/bar'))

0 commit comments

Comments
 (0)