Skip to content

Commit

Permalink
honor defaults
Browse files Browse the repository at this point in the history
This commit was sponsored by Moshez, Sergio Bost, Sean Reifschneider,
and my other patrons.  If you want to join them, you can support my
work at https://glyph.im/patrons/.
  • Loading branch information
glyph committed Mar 28, 2024
1 parent 55b8b48 commit 24ba49b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dbxs/_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ async def body() -> Any:
styledSQL, styledMap = precomputedSQL[conn.paramstyle]
cur = await conn.cursor()
bound = sig.bind(None, *args, **kw)
bound.apply_defaults()
await cur.execute(styledSQL, styledMap.queryArguments(bound))
maybeAgen: Any = self.load(proxySelf, cur)
if isawaitable(maybeAgen):
Expand Down
20 changes: 20 additions & 0 deletions src/dbxs/test/test_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ async def oopsQueryNotStatement(self) -> None:
Oops, it's a query, not a statement, it returns values.
"""

@query(sql="select {value}", load=one(lambda db, x: str(x)))
async def echoValue(self, value: int = 3) -> str:
"""
Echo the given value back, with a default provided.
"""

@query(
sql="insert into foo (baz) values ({baz}) returning bar, baz",
load=one(Foo),
Expand Down Expand Up @@ -153,6 +159,20 @@ async def test_happyPath(self, pool: MemoryPool) -> None:
self.assertEqual(result, result2)
self.assertEqual(result3, [Foo(db, 1, 3), Foo(db, 2, 4)])

@immediateTest()
async def test_defaultParamValue(self, pool: MemoryPool) -> None:
"""
Default parameters specified by the access Protocol are incorporated
into the query placeholders.
"""
async with transaction(pool.connectable) as c:
await schemaAndData(c)
db = accessFoo(c)
result = await db.echoValue(7)
self.assertEqual(result, "7")
result = await db.echoValue()
self.assertEqual(result, "3")

@immediateTest()
async def test_wrongResultArity(self, pool: MemoryPool) -> None:
"""
Expand Down

0 comments on commit 24ba49b

Please sign in to comment.