Skip to content

Commit

Permalink
Add custom set returning function test.
Browse files Browse the repository at this point in the history
  • Loading branch information
go-jet committed Nov 3, 2024
1 parent cf0923f commit 5d2c232
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion postgres/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

// RawStatement creates new sql statements from raw query and optional map of named arguments
func RawStatement(rawQuery string, namedArguments ...RawArgs) Statement {
func RawStatement(rawQuery string, namedArguments ...RawArgs) jet.SerializerStatement {
return jet.RawStatement(Dialect, rawQuery, namedArguments...)
}
2 changes: 1 addition & 1 deletion sqlite/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

// RawStatement creates new sql statements from raw query and optional map of named arguments
func RawStatement(rawQuery string, namedArguments ...RawArgs) Statement {
func RawStatement(rawQuery string, namedArguments ...RawArgs) jet.SerializerStatement {
return jet.RawStatement(Dialect, rawQuery, namedArguments...)
}
5 changes: 4 additions & 1 deletion tests/mysql/raw_statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func TestRawStatementSelectWithArguments(t *testing.T) {
}

func TestRawStatementRows(t *testing.T) {
stmt := RawStatement(`

var stmt Statement

stmt = RawStatement(`
SELECT actor.actor_id AS "actor.actor_id",
actor.first_name AS "actor.first_name",
actor.last_name AS "actor.last_name",
Expand Down
4 changes: 3 additions & 1 deletion tests/postgres/raw_statements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ RETURNING link.id AS "link.id",
}

func TestRawStatementRows(t *testing.T) {
stmt := RawStatement(`
var stmt Statement

stmt = RawStatement(`
SELECT actor.actor_id AS "actor.actor_id",
actor.first_name AS "actor.first_name",
actor.last_name AS "actor.last_name",
Expand Down
62 changes: 62 additions & 0 deletions tests/postgres/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3867,6 +3867,68 @@ ORDER BY film.film_id;
`)
}

func TestCustomSetReturningFunction(t *testing.T) {
skipForCockroachDB(t) // no set Set-Returning Functions

inventoryID := IntegerColumn("inventoryID")
filmsInStock := CTE("film_in_stock", inventoryID)

stmt := WITH(
filmsInStock.AS(
RawStatement("SELECT * FROM dvds.film_in_stock(#filmID, #storeID)",
RawArgs{
"#filmID": 1,
"#storeID": 2,
}),
),
)(
SELECT(
Inventory.AllColumns,
).FROM(Inventory.
INNER_JOIN(filmsInStock, Inventory.InventoryID.EQ(inventoryID)),
),
)

testutils.AssertStatementSql(t, stmt, `
WITH film_in_stock ("inventoryID") AS (SELECT * FROM dvds.film_in_stock($1, $2)
)
SELECT inventory.inventory_id AS "inventory.inventory_id",
inventory.film_id AS "inventory.film_id",
inventory.store_id AS "inventory.store_id",
inventory.last_update AS "inventory.last_update"
FROM dvds.inventory
INNER JOIN film_in_stock ON (inventory.inventory_id = film_in_stock."inventoryID");
`)

var dest []model.Inventory

err := stmt.Query(db, &dest)

require.NoError(t, err)
testutils.AssertJSON(t, dest, `
[
{
"InventoryID": 5,
"FilmID": 1,
"StoreID": 2,
"LastUpdate": "2006-02-15T10:09:17Z"
},
{
"InventoryID": 7,
"FilmID": 1,
"StoreID": 2,
"LastUpdate": "2006-02-15T10:09:17Z"
},
{
"InventoryID": 8,
"FilmID": 1,
"StoreID": 2,
"LastUpdate": "2006-02-15T10:09:17Z"
}
]
`)
}

var customer0 = model.Customer{
CustomerID: 1,
StoreID: 1,
Expand Down
5 changes: 4 additions & 1 deletion tests/sqlite/raw_statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func TestRawStatementSelectWithArguments(t *testing.T) {
}

func TestRawStatementRows(t *testing.T) {
stmt := RawStatement(`

var stmt Statement

stmt = RawStatement(`
SELECT actor.actor_id AS "actor.actor_id",
actor.first_name AS "actor.first_name",
actor.last_name AS "actor.last_name",
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata
Submodule testdata updated 1 files
+10 −10 init/postgres/dvds.sql

0 comments on commit 5d2c232

Please sign in to comment.