Skip to content

Commit

Permalink
dialect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Jun 12, 2024
1 parent 9a98daa commit abef016
Show file tree
Hide file tree
Showing 5 changed files with 552 additions and 12 deletions.
12 changes: 0 additions & 12 deletions dialect/mysql/im/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ func TestInsertBasic(t *testing.T) {
testutils.TestQuery(t, query, expectedQuery, expectedArgs...)
}

func TestInsertOverriding(t *testing.T) {
expectedQuery := "INSERT INTO users (id, name) VALUES (?, ?)"
expectedArgs := []any{15, "John"}

query := mysql.Insert(
Into("users", "id", "name"),
Values(15, "John"),
)

testutils.TestQuery(t, query, expectedQuery, expectedArgs...)
}

func TestInsertWith(t *testing.T) {
expectedQuery := "WITH city(city_id) AS (SELECT city FROM users WHERE id = ?) INSERT INTO users (id, name) VALUES (?, ?)"
expectedArgs := []any{2, 15, "John"}
Expand Down
104 changes: 104 additions & 0 deletions dialect/sqlite/dm/delete_test.go
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...)
}
76 changes: 76 additions & 0 deletions dialect/sqlite/im/insert_test.go
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...)
}
Loading

0 comments on commit abef016

Please sign in to comment.