Add MutablColumns list into autogen sql builder table.
This commit is contained in:
parent
67e6fca0ce
commit
d6c59deb1f
11 changed files with 219 additions and 59 deletions
|
|
@ -26,8 +26,8 @@ func TestAllTypesSelect(t *testing.T) {
|
|||
|
||||
func TestAllTypesInsertModel(t *testing.T) {
|
||||
query := AllTypes.INSERT(AllTypes.AllColumns).
|
||||
USING(allTypesRow0).
|
||||
USING(&allTypesRow1).
|
||||
MODEL(allTypesRow0).
|
||||
MODEL(&allTypesRow1).
|
||||
RETURNING(AllTypes.AllColumns)
|
||||
|
||||
dest := []model.AllTypes{}
|
||||
|
|
|
|||
|
|
@ -144,11 +144,10 @@ VALUES (1, 1, 300, 300, 50000, 5000, 11.44, 11.44, 55.77, 55.77, 99.1, 99.1, 111
|
|||
DROP TABLE IF EXISTS test_sample.link;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS test_sample.link (
|
||||
ID serial PRIMARY KEY,
|
||||
id serial PRIMARY KEY,
|
||||
url VARCHAR (255) NOT NULL,
|
||||
name VARCHAR (255) NOT NULL,
|
||||
description VARCHAR (255),
|
||||
rel VARCHAR (50)
|
||||
description VARCHAR (255)
|
||||
);
|
||||
|
||||
INSERT INTO test_sample.link (ID, url, name, description) VALUES
|
||||
|
|
|
|||
|
|
@ -9,22 +9,20 @@ import (
|
|||
)
|
||||
|
||||
func TestInsertValues(t *testing.T) {
|
||||
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
var expectedSql = `
|
||||
INSERT INTO test_sample.link (id, url, name, rel) VALUES
|
||||
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",
|
||||
link.rel AS "link.rel";
|
||||
link.description AS "link.description";
|
||||
`
|
||||
|
||||
insertQuery := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Rel).
|
||||
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).
|
||||
|
|
@ -47,21 +45,18 @@ RETURNING link.id AS "link.id",
|
|||
ID: 100,
|
||||
URL: "http://www.postgresqltutorial.com",
|
||||
Name: "PostgreSQL Tutorial",
|
||||
Rel: nil,
|
||||
})
|
||||
|
||||
assert.DeepEqual(t, insertedLinks[1], model.Link{
|
||||
ID: 101,
|
||||
URL: "http://www.google.com",
|
||||
Name: "Google",
|
||||
Rel: nil,
|
||||
})
|
||||
|
||||
assert.DeepEqual(t, insertedLinks[2], model.Link{
|
||||
ID: 102,
|
||||
URL: "http://www.yahoo.com",
|
||||
Name: "Yahoo",
|
||||
Rel: nil,
|
||||
})
|
||||
|
||||
allLinks := []model.Link{}
|
||||
|
|
@ -76,7 +71,24 @@ RETURNING link.id AS "link.id",
|
|||
assert.DeepEqual(t, insertedLinks, allLinks)
|
||||
}
|
||||
|
||||
func TestInsertDataObject(t *testing.T) {
|
||||
func TestInsertEmptyColumnList(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
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)
|
||||
|
||||
assertStatementSql(t, stmt, expectedSql,
|
||||
100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial")
|
||||
|
||||
assertExec(t, stmt, 1)
|
||||
}
|
||||
|
||||
func TestInsertModelObject(t *testing.T) {
|
||||
var expectedSql = `
|
||||
INSERT INTO test_sample.link (url, name) VALUES
|
||||
('http://www.duckduckgo.com', 'Duck Duck go');
|
||||
|
|
@ -85,12 +97,11 @@ INSERT INTO test_sample.link (url, name) VALUES
|
|||
linkData := model.Link{
|
||||
URL: "http://www.duckduckgo.com",
|
||||
Name: "Duck Duck go",
|
||||
Rel: nil,
|
||||
}
|
||||
|
||||
query := Link.
|
||||
INSERT(Link.URL, Link.Name).
|
||||
USING(linkData)
|
||||
MODEL(linkData)
|
||||
|
||||
assertStatementSql(t, query, expectedSql, "http://www.duckduckgo.com", "Duck Duck go")
|
||||
|
||||
|
|
@ -103,6 +114,75 @@ INSERT INTO test_sample.link (url, name) VALUES
|
|||
assert.Equal(t, rowsAffected, int64(1))
|
||||
}
|
||||
|
||||
func TestInsertModelsObject(t *testing.T) {
|
||||
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})
|
||||
|
||||
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) {
|
||||
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})
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func TestInsertQuery(t *testing.T) {
|
||||
_, err := Link.DELETE().
|
||||
WHERE(Link.ID.NOT_EQ(Int(0)).AND(Link.Name.EQ(String("Youtube")))).
|
||||
|
|
@ -119,8 +199,7 @@ INSERT INTO test_sample.link (url, name) (
|
|||
RETURNING link.id AS "link.id",
|
||||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description",
|
||||
link.rel AS "link.rel";
|
||||
link.description AS "link.description";
|
||||
`
|
||||
|
||||
query := Link.
|
||||
|
|
|
|||
|
|
@ -86,8 +86,7 @@ WHERE link.name = 'Ask'
|
|||
RETURNING link.id AS "link.id",
|
||||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description",
|
||||
link.rel AS "link.rel";
|
||||
link.description AS "link.description";
|
||||
`
|
||||
|
||||
stmt := Link.
|
||||
|
|
@ -120,12 +119,11 @@ func TestUpdateWithSelect(t *testing.T) {
|
|||
|
||||
expectedSql := `
|
||||
UPDATE test_sample.link
|
||||
SET (id, url, name, description, rel) = (
|
||||
SET (id, url, name, description) = (
|
||||
SELECT link.id AS "link.id",
|
||||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description",
|
||||
link.rel AS "link.rel"
|
||||
link.description AS "link.description"
|
||||
FROM test_sample.link
|
||||
WHERE link.id = 0
|
||||
)
|
||||
|
|
@ -148,7 +146,7 @@ func TestUpdateWithInvalidSelect(t *testing.T) {
|
|||
|
||||
var expectedSql = `
|
||||
UPDATE test_sample.link
|
||||
SET (id, url, name, description, rel) = (
|
||||
SET (id, url, name, description) = (
|
||||
SELECT link.id AS "link.id",
|
||||
link.name AS "link.name"
|
||||
FROM test_sample.link
|
||||
|
|
@ -177,10 +175,10 @@ func TestUpdateWithModelData(t *testing.T) {
|
|||
|
||||
expectedSql := `
|
||||
UPDATE test_sample.link
|
||||
SET (id, url, name, description, rel) = (201, 'http://www.duckduckgo.com', 'DuckDuckGo', NULL, NULL)
|
||||
SET (id, url, name, description) = (201, 'http://www.duckduckgo.com', 'DuckDuckGo', NULL)
|
||||
WHERE link.id = 201;
|
||||
`
|
||||
assertStatementSql(t, stmt, expectedSql, int32(201), "http://www.duckduckgo.com", "DuckDuckGo", nil, nil, int64(201))
|
||||
assertStatementSql(t, stmt, expectedSql, int32(201), "http://www.duckduckgo.com", "DuckDuckGo", nil, int64(201))
|
||||
|
||||
assertExec(t, stmt, 1)
|
||||
}
|
||||
|
|
@ -195,7 +193,7 @@ func TestUpdateWithModelDataAndPredefinedColumnList(t *testing.T) {
|
|||
Name: "DuckDuckGo",
|
||||
}
|
||||
|
||||
updateColumnList := ColumnList{Link.Rel, Link.Name, Link.URL}
|
||||
updateColumnList := ColumnList{Link.Description, Link.Name, Link.URL}
|
||||
|
||||
stmt := Link.
|
||||
UPDATE(updateColumnList).
|
||||
|
|
@ -204,7 +202,7 @@ func TestUpdateWithModelDataAndPredefinedColumnList(t *testing.T) {
|
|||
|
||||
var expectedSql = `
|
||||
UPDATE test_sample.link
|
||||
SET (rel, name, url) = (NULL, 'DuckDuckGo', 'http://www.duckduckgo.com')
|
||||
SET (description, name, url) = (NULL, 'DuckDuckGo', 'http://www.duckduckgo.com')
|
||||
WHERE link.id = 201;
|
||||
`
|
||||
assertStatementSql(t, stmt, expectedSql, nil, "DuckDuckGo", "http://www.duckduckgo.com", int64(201))
|
||||
|
|
@ -252,7 +250,7 @@ func setupLinkTableForUpdateTest(t *testing.T) {
|
|||
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
_, err := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Rel).
|
||||
_, err := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||
VALUES(200, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES(201, "http://www.ask.com", "Ask", DEFAULT).
|
||||
VALUES(202, "http://www.ask.com", "Ask", DEFAULT).
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue