2019-07-30 11:45:10 +02:00
|
|
|
package postgres
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
import (
|
2019-07-20 13:33:25 +02:00
|
|
|
"context"
|
2019-07-29 18:08:53 +02:00
|
|
|
"github.com/go-jet/jet/internal/testutils"
|
2019-08-03 14:10:47 +02:00
|
|
|
. "github.com/go-jet/jet/postgres"
|
2019-06-24 10:01:34 +02:00
|
|
|
"github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model"
|
|
|
|
|
. "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/table"
|
2020-05-09 11:00:22 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-12 18:53:57 +02:00
|
|
|
"math/rand"
|
2019-04-07 09:58:12 +02:00
|
|
|
"testing"
|
2019-07-20 13:33:25 +02:00
|
|
|
"time"
|
2019-04-07 09:58:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestInsertValues(t *testing.T) {
|
2019-06-14 14:35:50 +02:00
|
|
|
cleanUpLinkTable(t)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
var expectedSQL = `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO test_sample.link (id, url, name, description)
|
|
|
|
|
VALUES (100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
|
|
|
|
(101, 'http://www.google.com', 'Google', DEFAULT),
|
|
|
|
|
(102, 'http://www.yahoo.com', 'Yahoo', NULL)
|
2019-06-14 14:35:50 +02:00
|
|
|
RETURNING link.id AS "link.id",
|
2019-06-30 17:16:00 +02:00
|
|
|
link.url AS "link.url",
|
|
|
|
|
link.name AS "link.name",
|
|
|
|
|
link.description AS "link.description";
|
2019-06-14 14:35:50 +02:00
|
|
|
`
|
2019-06-30 11:53:35 +02:00
|
|
|
insertQuery := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
2019-06-14 14:35:50 +02:00
|
|
|
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
|
|
|
|
VALUES(102, "http://www.yahoo.com", "Yahoo", nil).
|
|
|
|
|
RETURNING(Link.AllColumns)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, insertQuery, expectedSQL,
|
2019-06-14 14:35:50 +02:00
|
|
|
100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
|
|
|
|
101, "http://www.google.com", "Google",
|
|
|
|
|
102, "http://www.yahoo.com", "Yahoo", nil)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
insertedLinks := []model.Link{}
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
err := insertQuery.Query(db, &insertedLinks)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.NoError(t, err)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Equal(t, len(insertedLinks), 3)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2020-02-11 10:25:13 +01:00
|
|
|
testutils.AssertDeepEqual(t, insertedLinks[0], model.Link{
|
2019-06-14 14:35:50 +02:00
|
|
|
ID: 100,
|
2019-04-07 09:58:12 +02:00
|
|
|
URL: "http://www.postgresqltutorial.com",
|
|
|
|
|
Name: "PostgreSQL Tutorial",
|
|
|
|
|
})
|
|
|
|
|
|
2020-02-11 10:25:13 +01:00
|
|
|
testutils.AssertDeepEqual(t, insertedLinks[1], model.Link{
|
2019-06-14 14:35:50 +02:00
|
|
|
ID: 101,
|
|
|
|
|
URL: "http://www.google.com",
|
|
|
|
|
Name: "Google",
|
2019-04-07 09:58:12 +02:00
|
|
|
})
|
2019-06-14 14:35:50 +02:00
|
|
|
|
2020-02-11 10:25:13 +01:00
|
|
|
testutils.AssertDeepEqual(t, insertedLinks[2], model.Link{
|
2019-06-14 14:35:50 +02:00
|
|
|
ID: 102,
|
|
|
|
|
URL: "http://www.yahoo.com",
|
|
|
|
|
Name: "Yahoo",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
allLinks := []model.Link{}
|
|
|
|
|
|
|
|
|
|
err = Link.SELECT(Link.AllColumns).
|
|
|
|
|
WHERE(Link.ID.GT_EQ(Int(100))).
|
|
|
|
|
ORDER_BY(Link.ID).
|
|
|
|
|
Query(db, &allLinks)
|
|
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.NoError(t, err)
|
2019-06-14 14:35:50 +02:00
|
|
|
|
2020-02-11 10:25:13 +01:00
|
|
|
testutils.AssertDeepEqual(t, insertedLinks, allLinks)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-30 11:53:35 +02:00
|
|
|
func TestInsertEmptyColumnList(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO test_sample.link
|
|
|
|
|
VALUES (100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT);
|
2019-06-30 11:53:35 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
stmt := Link.INSERT().
|
|
|
|
|
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, expectedSQL,
|
2019-06-30 11:53:35 +02:00
|
|
|
100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial")
|
|
|
|
|
|
2019-08-02 11:08:24 +02:00
|
|
|
AssertExec(t, stmt, 1)
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, stmt)
|
2019-06-30 11:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-12 18:53:57 +02:00
|
|
|
func TestInsertOnConflict(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
t.Run("do nothing", func(t *testing.T) {
|
|
|
|
|
employee := model.Employee{EmployeeID: rand.Int31()}
|
|
|
|
|
|
|
|
|
|
stmt := Employee.INSERT(Employee.AllColumns).
|
|
|
|
|
MODEL(employee).
|
|
|
|
|
MODEL(employee).
|
|
|
|
|
ON_CONFLICT(Employee.EmployeeID).DO_NOTHING()
|
|
|
|
|
|
|
|
|
|
testutils.AssertStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5),
|
|
|
|
|
($6, $7, $8, $9, $10)
|
|
|
|
|
ON CONFLICT (employee_id) DO NOTHING;
|
|
|
|
|
`)
|
|
|
|
|
AssertExec(t, stmt, 1)
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, stmt)
|
2020-04-12 18:53:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("on constraint do nothing", func(t *testing.T) {
|
|
|
|
|
employee := model.Employee{EmployeeID: rand.Int31()}
|
|
|
|
|
|
|
|
|
|
stmt := Employee.INSERT(Employee.AllColumns).
|
|
|
|
|
MODEL(employee).
|
|
|
|
|
MODEL(employee).
|
|
|
|
|
ON_CONFLICT().ON_CONSTRAINT("employee_pkey").DO_NOTHING()
|
|
|
|
|
|
|
|
|
|
testutils.AssertStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO test_sample.employee (employee_id, first_name, last_name, employment_date, manager_id)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5),
|
|
|
|
|
($6, $7, $8, $9, $10)
|
|
|
|
|
ON CONFLICT ON CONSTRAINT employee_pkey DO NOTHING;
|
|
|
|
|
`)
|
|
|
|
|
AssertExec(t, stmt, 1)
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, stmt)
|
2020-04-12 18:53:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("do update", func(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
stmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
|
|
|
|
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
VALUES(200, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
ON_CONFLICT(Link.ID).DO_UPDATE(
|
2020-05-03 20:46:21 +02:00
|
|
|
SET(
|
|
|
|
|
Link.ID.SET(Link.EXCLUDED.ID),
|
|
|
|
|
Link.URL.SET(String("http://www.postgresqltutorial2.com")),
|
|
|
|
|
),
|
2020-04-12 18:53:57 +02:00
|
|
|
).
|
|
|
|
|
RETURNING(Link.AllColumns)
|
|
|
|
|
|
|
|
|
|
testutils.AssertStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO test_sample.link (id, url, name, description)
|
|
|
|
|
VALUES ($1, $2, $3, DEFAULT),
|
|
|
|
|
($4, $5, $6, DEFAULT)
|
|
|
|
|
ON CONFLICT (id) DO UPDATE
|
|
|
|
|
SET id = excluded.id,
|
|
|
|
|
url = $7
|
|
|
|
|
RETURNING link.id AS "link.id",
|
|
|
|
|
link.url AS "link.url",
|
|
|
|
|
link.name AS "link.name",
|
|
|
|
|
link.description AS "link.description";
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
AssertExec(t, stmt, 2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("on constraint do update", func(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
stmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
|
|
|
|
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
VALUES(200, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
ON_CONFLICT().ON_CONSTRAINT("link_pkey").DO_UPDATE(
|
2020-05-03 20:46:21 +02:00
|
|
|
SET(
|
|
|
|
|
Link.ID.SET(Link.EXCLUDED.ID),
|
|
|
|
|
Link.URL.SET(String("http://www.postgresqltutorial2.com")),
|
|
|
|
|
),
|
2020-04-12 18:53:57 +02:00
|
|
|
).
|
|
|
|
|
RETURNING(Link.AllColumns)
|
|
|
|
|
|
|
|
|
|
testutils.AssertStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO test_sample.link (id, url, name, description)
|
|
|
|
|
VALUES ($1, $2, $3, DEFAULT),
|
|
|
|
|
($4, $5, $6, DEFAULT)
|
|
|
|
|
ON CONFLICT ON CONSTRAINT link_pkey DO UPDATE
|
|
|
|
|
SET id = excluded.id,
|
|
|
|
|
url = $7
|
|
|
|
|
RETURNING link.id AS "link.id",
|
|
|
|
|
link.url AS "link.url",
|
|
|
|
|
link.name AS "link.name",
|
|
|
|
|
link.description AS "link.description";
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
AssertExec(t, stmt, 2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("do update complex", func(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
|
|
|
|
|
stmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
|
|
|
|
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
ON_CONFLICT(Link.ID).WHERE(Link.ID.MUL(Int(2)).GT(Int(10))).DO_UPDATE(
|
2020-05-03 20:46:21 +02:00
|
|
|
SET(
|
|
|
|
|
Link.ID.SET(
|
|
|
|
|
IntExp(SELECT(MAXi(Link.ID).ADD(Int(1))).
|
|
|
|
|
FROM(Link)),
|
|
|
|
|
),
|
|
|
|
|
ColumnList{Link.Name, Link.Description}.SET(ROW(Link.EXCLUDED.Name, String("new description"))),
|
|
|
|
|
).WHERE(Link.Description.IS_NOT_NULL()),
|
2020-04-12 18:53:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
testutils.AssertDebugStatementSql(t, stmt, `
|
|
|
|
|
INSERT INTO test_sample.link (id, url, name, description)
|
|
|
|
|
VALUES (100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT)
|
|
|
|
|
ON CONFLICT (id) WHERE (id * 2) > 10 DO UPDATE
|
|
|
|
|
SET id = (
|
|
|
|
|
SELECT MAX(link.id) + 1
|
|
|
|
|
FROM test_sample.link
|
|
|
|
|
),
|
|
|
|
|
(name, description) = ROW(excluded.name, 'new description')
|
|
|
|
|
WHERE link.description IS NOT NULL;
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
|
|
AssertExec(t, stmt, 1)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 11:53:35 +02:00
|
|
|
func TestInsertModelObject(t *testing.T) {
|
2019-07-13 13:17:28 +02:00
|
|
|
cleanUpLinkTable(t)
|
2019-07-18 17:43:11 +02:00
|
|
|
var expectedSQL = `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO test_sample.link (url, name)
|
|
|
|
|
VALUES ('http://www.duckduckgo.com', 'Duck Duck go');
|
2019-06-14 14:35:50 +02:00
|
|
|
`
|
|
|
|
|
|
2019-04-07 09:58:12 +02:00
|
|
|
linkData := model.Link{
|
|
|
|
|
URL: "http://www.duckduckgo.com",
|
|
|
|
|
Name: "Duck Duck go",
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
query := Link.
|
|
|
|
|
INSERT(Link.URL, Link.Name).
|
2019-06-30 11:53:35 +02:00
|
|
|
MODEL(linkData)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, "http://www.duckduckgo.com", "Duck Duck go")
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-08-02 11:08:24 +02:00
|
|
|
AssertExec(t, query, 1)
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-07-20 11:22:01 +02:00
|
|
|
func TestInsertModelObjectEmptyColumnList(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
var expectedSQL = `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO test_sample.link
|
|
|
|
|
VALUES (1000, 'http://www.duckduckgo.com', 'Duck Duck go', NULL);
|
2019-07-20 11:22:01 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
linkData := model.Link{
|
|
|
|
|
ID: 1000,
|
|
|
|
|
URL: "http://www.duckduckgo.com",
|
|
|
|
|
Name: "Duck Duck go",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query := Link.
|
|
|
|
|
INSERT().
|
|
|
|
|
MODEL(linkData)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int32(1000), "http://www.duckduckgo.com", "Duck Duck go", nil)
|
2019-07-20 11:22:01 +02:00
|
|
|
|
2019-08-02 11:08:24 +02:00
|
|
|
AssertExec(t, query, 1)
|
2019-07-20 11:22:01 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-30 11:53:35 +02:00
|
|
|
func TestInsertModelsObject(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO test_sample.link (url, name)
|
|
|
|
|
VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial'),
|
|
|
|
|
('http://www.google.com', 'Google'),
|
|
|
|
|
('http://www.yahoo.com', 'Yahoo');
|
2019-06-30 11:53:35 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
tutorial := model.Link{
|
|
|
|
|
URL: "http://www.postgresqltutorial.com",
|
|
|
|
|
Name: "PostgreSQL Tutorial",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
google := model.Link{
|
|
|
|
|
URL: "http://www.google.com",
|
|
|
|
|
Name: "Google",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yahoo := model.Link{
|
|
|
|
|
URL: "http://www.yahoo.com",
|
|
|
|
|
Name: "Yahoo",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := Link.
|
|
|
|
|
INSERT(Link.URL, Link.Name).
|
|
|
|
|
MODELS([]model.Link{tutorial, google, yahoo})
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, expectedSQL,
|
2019-06-30 11:53:35 +02:00
|
|
|
"http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
|
|
|
|
"http://www.google.com", "Google",
|
|
|
|
|
"http://www.yahoo.com", "Yahoo")
|
|
|
|
|
|
2019-08-02 11:08:24 +02:00
|
|
|
AssertExec(t, stmt, 3)
|
2019-06-30 11:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertUsingMutableColumns(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
var expectedSQL = `
|
2020-04-12 18:53:57 +02:00
|
|
|
INSERT INTO test_sample.link (url, name, description)
|
|
|
|
|
VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
|
|
|
|
('http://www.google.com', 'Google', NULL),
|
|
|
|
|
('http://www.google.com', 'Google', NULL),
|
|
|
|
|
('http://www.yahoo.com', 'Yahoo', NULL);
|
2019-06-30 11:53:35 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
google := model.Link{
|
|
|
|
|
URL: "http://www.google.com",
|
|
|
|
|
Name: "Google",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yahoo := model.Link{
|
|
|
|
|
URL: "http://www.yahoo.com",
|
|
|
|
|
Name: "Yahoo",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := Link.
|
|
|
|
|
INSERT(Link.MutableColumns).
|
|
|
|
|
VALUES("http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
MODEL(google).
|
|
|
|
|
MODELS([]model.Link{google, yahoo})
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, expectedSQL,
|
2019-06-30 11:53:35 +02:00
|
|
|
"http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
|
|
|
|
"http://www.google.com", "Google", nil,
|
|
|
|
|
"http://www.google.com", "Google", nil,
|
|
|
|
|
"http://www.yahoo.com", "Yahoo", nil)
|
|
|
|
|
|
2019-08-02 11:08:24 +02:00
|
|
|
AssertExec(t, stmt, 4)
|
2019-06-30 11:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-07 16:54:06 +02:00
|
|
|
func TestInsertQuery(t *testing.T) {
|
2019-06-14 14:35:50 +02:00
|
|
|
_, err := Link.DELETE().
|
|
|
|
|
WHERE(Link.ID.NOT_EQ(Int(0)).AND(Link.Name.EQ(String("Youtube")))).
|
|
|
|
|
Exec(db)
|
2020-05-09 11:00:22 +02:00
|
|
|
require.NoError(t, err)
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
var expectedSQL = `
|
2019-06-14 14:35:50 +02:00
|
|
|
INSERT INTO test_sample.link (url, name) (
|
|
|
|
|
SELECT link.url AS "link.url",
|
|
|
|
|
link.name AS "link.name"
|
|
|
|
|
FROM test_sample.link
|
|
|
|
|
WHERE link.id = 0
|
|
|
|
|
)
|
|
|
|
|
RETURNING link.id AS "link.id",
|
2019-06-30 17:16:00 +02:00
|
|
|
link.url AS "link.url",
|
|
|
|
|
link.name AS "link.name",
|
|
|
|
|
link.description AS "link.description";
|
2019-06-14 14:35:50 +02:00
|
|
|
`
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
query := Link.
|
|
|
|
|
INSERT(Link.URL, Link.Name).
|
2019-06-14 14:35:50 +02:00
|
|
|
QUERY(
|
|
|
|
|
SELECT(Link.URL, Link.Name).
|
|
|
|
|
FROM(Link).
|
|
|
|
|
WHERE(Link.ID.EQ(Int(0))),
|
|
|
|
|
).
|
|
|
|
|
RETURNING(Link.AllColumns)
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(0))
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
dest := []model.Link{}
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
err = query.Query(db, &dest)
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.NoError(t, err)
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
youtubeLinks := []model.Link{}
|
|
|
|
|
err = Link.
|
|
|
|
|
SELECT(Link.AllColumns).
|
|
|
|
|
WHERE(Link.Name.EQ(String("Youtube"))).
|
|
|
|
|
Query(db, &youtubeLinks)
|
2019-04-07 16:54:06 +02:00
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, len(youtubeLinks), 2)
|
2019-04-07 16:54:06 +02:00
|
|
|
}
|
2019-07-20 13:33:25 +02:00
|
|
|
|
|
|
|
|
func TestInsertWithQueryContext(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
|
|
|
|
|
stmt := Link.INSERT().
|
|
|
|
|
VALUES(1100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
|
|
|
|
RETURNING(Link.AllColumns)
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2019-07-20 14:23:42 +02:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2019-07-20 13:33:25 +02:00
|
|
|
|
|
|
|
|
dest := []model.Link{}
|
|
|
|
|
err := stmt.QueryContext(ctx, db, &dest)
|
|
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Error(t, err, "context deadline exceeded")
|
2019-07-20 13:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInsertWithExecContext(t *testing.T) {
|
|
|
|
|
cleanUpLinkTable(t)
|
|
|
|
|
|
|
|
|
|
stmt := Link.INSERT().
|
|
|
|
|
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT)
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2019-07-20 14:23:42 +02:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2019-07-20 13:33:25 +02:00
|
|
|
|
|
|
|
|
_, err := stmt.ExecContext(ctx, db)
|
|
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Error(t, err, "context deadline exceeded")
|
2019-07-20 13:33:25 +02:00
|
|
|
}
|