jet/tests/insert_test.go

284 lines
6.9 KiB
Go
Raw Normal View History

2019-04-07 09:58:12 +02:00
package tests
import (
"context"
2019-06-21 13:56:57 +02:00
. "github.com/go-jet/jet"
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"
2019-04-07 09:58:12 +02:00
"gotest.tools/assert"
"testing"
"time"
2019-04-07 09:58:12 +02:00
)
func TestInsertValues(t *testing.T) {
cleanUpLinkTable(t)
2019-04-07 09:58:12 +02:00
2019-07-18 17:43:11 +02:00
var expectedSQL = `
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)
RETURNING link.id AS "link.id",
link.url AS "link.url",
link.name AS "link.name",
link.description AS "link.description";
`
2019-04-07 09:58:12 +02:00
insertQuery := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
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-18 17:43:11 +02:00
assertStatementSql(t, insertQuery, expectedSQL,
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
insertedLinks := []model.Link{}
2019-04-07 09:58:12 +02:00
err := insertQuery.Query(db, &insertedLinks)
2019-04-07 09:58:12 +02:00
assert.NilError(t, err)
assert.Equal(t, len(insertedLinks), 3)
2019-04-07 09:58:12 +02:00
assert.DeepEqual(t, insertedLinks[0], model.Link{
ID: 100,
2019-04-07 09:58:12 +02:00
URL: "http://www.postgresqltutorial.com",
Name: "PostgreSQL Tutorial",
})
assert.DeepEqual(t, insertedLinks[1], model.Link{
ID: 101,
URL: "http://www.google.com",
Name: "Google",
2019-04-07 09:58:12 +02:00
})
assert.DeepEqual(t, insertedLinks[2], model.Link{
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)
assert.NilError(t, err)
assert.DeepEqual(t, insertedLinks, allLinks)
2019-04-07 09:58:12 +02:00
}
func TestInsertEmptyColumnList(t *testing.T) {
cleanUpLinkTable(t)
2019-07-18 17:43:11 +02:00
expectedSQL := `
INSERT INTO test_sample.link VALUES
(100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT);
`
stmt := Link.INSERT().
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT)
2019-07-18 17:43:11 +02:00
assertStatementSql(t, stmt, expectedSQL,
100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial")
assertExec(t, stmt, 1)
}
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 = `
INSERT INTO test_sample.link (url, name) VALUES
('http://www.duckduckgo.com', 'Duck Duck go');
`
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).
MODEL(linkData)
2019-04-07 09:58:12 +02:00
2019-07-18 17:43:11 +02:00
assertStatementSql(t, query, expectedSQL, "http://www.duckduckgo.com", "Duck Duck go")
2019-04-07 09:58:12 +02:00
2019-07-18 18:51:13 +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 = `
INSERT INTO test_sample.link VALUES
(1000, 'http://www.duckduckgo.com', 'Duck Duck go', NULL);
`
linkData := model.Link{
ID: 1000,
URL: "http://www.duckduckgo.com",
Name: "Duck Duck go",
}
query := Link.
INSERT().
MODEL(linkData)
assertStatementSql(t, query, expectedSQL, int32(1000), "http://www.duckduckgo.com", "Duck Duck go", nil)
assertExec(t, query, 1)
}
func TestInsertModelsObject(t *testing.T) {
2019-07-18 17:43:11 +02:00
expectedSQL := `
INSERT INTO test_sample.link (url, name) VALUES
('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial'),
('http://www.google.com', 'Google'),
('http://www.yahoo.com', 'Yahoo');
`
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-18 17:43:11 +02:00
assertStatementSql(t, stmt, expectedSQL,
"http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
"http://www.google.com", "Google",
"http://www.yahoo.com", "Yahoo")
assertExec(t, stmt, 3)
}
func TestInsertUsingMutableColumns(t *testing.T) {
2019-07-18 17:43:11 +02:00
var expectedSQL = `
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);
`
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-18 17:43:11 +02:00
assertStatementSql(t, stmt, expectedSQL,
"http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
"http://www.google.com", "Google", nil,
"http://www.google.com", "Google", nil,
"http://www.yahoo.com", "Yahoo", nil)
assertExec(t, stmt, 4)
}
2019-04-07 16:54:06 +02:00
func TestInsertQuery(t *testing.T) {
_, err := Link.DELETE().
WHERE(Link.ID.NOT_EQ(Int(0)).AND(Link.Name.EQ(String("Youtube")))).
Exec(db)
2019-04-07 16:54:06 +02:00
assert.NilError(t, err)
2019-07-18 17:43:11 +02:00
var expectedSQL = `
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",
link.url AS "link.url",
link.name AS "link.name",
link.description AS "link.description";
`
2019-06-11 12:47:35 +02:00
query := Link.
INSERT(Link.URL, Link.Name).
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-18 17:43:11 +02:00
assertStatementSql(t, query, expectedSQL, int64(0))
2019-04-07 16:54:06 +02:00
dest := []model.Link{}
2019-04-07 16:54:06 +02:00
err = query.Query(db, &dest)
2019-04-07 16:54:06 +02:00
assert.NilError(t, err)
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
assert.NilError(t, err)
assert.Equal(t, len(youtubeLinks), 2)
2019-04-07 16:54:06 +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()
time.Sleep(10 * time.Microsecond)
dest := []model.Link{}
err := stmt.QueryContext(ctx, db, &dest)
assert.Error(t, err, "context deadline exceeded")
}
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()
time.Sleep(10 * time.Microsecond)
_, err := stmt.ExecContext(ctx, db)
assert.Error(t, err, "context deadline exceeded")
}