-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a98daa
commit abef016
Showing
5 changed files
with
552 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package dm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rrgmc/litsql/dialect/sqlite" | ||
"github.com/rrgmc/litsql/dialect/sqlite/sm" | ||
"github.com/rrgmc/litsql/expr" | ||
"github.com/rrgmc/litsql/internal/testutils" | ||
) | ||
|
||
func TestDelete(t *testing.T) { | ||
expectedQuery := "DELETE FROM users WHERE id = ?1" | ||
expectedArgs := []any{15} | ||
|
||
query := sqlite.Delete( | ||
From("users"), | ||
WhereClause("id = ?", 15), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestDeleteBasic(t *testing.T) { | ||
expectedQuery := "DELETE FROM ONLY users WHERE id = ?1 RETURNING id" | ||
expectedArgs := []any{15} | ||
|
||
query := sqlite.Delete( | ||
From("users"), | ||
Only(), | ||
WhereClause("id = ?", 15), | ||
Returning("id"), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestDeleteUsing(t *testing.T) { | ||
expectedQuery := "DELETE FROM users USING address AS adr INNER JOIN cities AS ct ON adr.city_id = ct.id WHERE users.address_id = adr.address_id" | ||
var expectedArgs []any | ||
|
||
query := sqlite.Delete( | ||
From("users"), | ||
Using("address AS adr"), | ||
InnerJoin("cities AS ct").On("adr.city_id = ct.id"), | ||
WhereClause("users.address_id = adr.address_id"), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestDeleteUsingQuery(t *testing.T) { | ||
expectedQuery := "DELETE FROM users USING (SELECT address, city, state FROM address WHERE id IN (?1, ?2, ?3)) AS adr WHERE users.address_id = adr.address_id" | ||
expectedArgs := []any{15, 16, 17} | ||
|
||
query := sqlite.Delete( | ||
From("users"), | ||
UsingQuery( | ||
sqlite.Select( | ||
sm.Columns("address", "city", "state"), | ||
sm.From("address"), | ||
sm.WhereClause("id IN (?)", expr.In([]any{15, 16, 17})), | ||
), | ||
).As("adr"), | ||
WhereClause("users.address_id = adr.address_id"), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestDeleteWith(t *testing.T) { | ||
expectedQuery := "WITH city(city_id) AS (SELECT city FROM users WHERE id = ?1) DELETE FROM users WHERE id = ?2" | ||
expectedArgs := []any{2, 15} | ||
|
||
query := sqlite.Delete( | ||
With("city", "city_id").As( | ||
sqlite.Select( | ||
sm.Columns("city"), | ||
sm.From("users"), | ||
sm.WhereClause("id = ?", 2), | ||
), | ||
), | ||
From("users"), | ||
WhereClause("id = ?", 15), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestDeleteApply(t *testing.T) { | ||
expectedQuery := "DELETE FROM users WHERE id = ?1" | ||
expectedArgs := []any{15} | ||
|
||
query := sqlite.Delete( | ||
From("users"), | ||
Apply(func(a sqlite.DeleteModApply) { | ||
a.Apply( | ||
WhereClause("id = ?", 15), | ||
) | ||
}), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package im | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rrgmc/litsql/dialect/sqlite" | ||
"github.com/rrgmc/litsql/dialect/sqlite/sm" | ||
"github.com/rrgmc/litsql/internal/testutils" | ||
) | ||
|
||
func TestInsert(t *testing.T) { | ||
expectedQuery := "INSERT INTO users (id, name) VALUES (?1, ?2)" | ||
expectedArgs := []any{15, "John"} | ||
|
||
query := sqlite.Insert( | ||
Into("users", "id", "name"), | ||
Values(15, "John"), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestInsertBasic(t *testing.T) { | ||
expectedQuery := "INSERT INTO users (id, name) VALUES (?1, ?2), (?3, ?4) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name RETURNING id" | ||
expectedArgs := []any{15, "John", 16, "Mary"} | ||
|
||
query := sqlite.Insert( | ||
Into("users", "id", "name"), | ||
Values(15, "John"), | ||
Values(16, "Mary"), | ||
OnConflict("id").DoUpdate( | ||
ConflictSetString("name", "EXCLUDED.name"), | ||
), | ||
Returning("id"), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestInsertWith(t *testing.T) { | ||
expectedQuery := "WITH city(city_id) AS (SELECT city FROM users WHERE id = ?1) INSERT INTO users (id, name) VALUES (?2, ?3)" | ||
expectedArgs := []any{2, 15, "John"} | ||
|
||
query := sqlite.Insert( | ||
With("city", "city_id").As( | ||
sqlite.Select( | ||
sm.Columns("city"), | ||
sm.From("users"), | ||
sm.WhereClause("id = ?", 2), | ||
), | ||
), | ||
Into("users", "id", "name"), | ||
Values(15, "John"), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} | ||
|
||
func TestInsertApply(t *testing.T) { | ||
expectedQuery := "INSERT INTO users (id, name) VALUES (?1, ?2), (?3, ?4)" | ||
expectedArgs := []any{15, "John", 16, "Mary"} | ||
|
||
query := sqlite.Insert( | ||
Into("users", "id", "name"), | ||
Apply(func(a sqlite.InsertModApply) { | ||
a.Apply( | ||
Values(15, "John"), | ||
) | ||
a.Apply( | ||
Values(16, "Mary"), | ||
) | ||
}), | ||
) | ||
|
||
testutils.TestQuery(t, query, expectedQuery, expectedArgs...) | ||
} |
Oops, something went wrong.