forked from sqlc-dev/sqlc
-
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.
test(MySQL): Add test for default column values
Refs: sqlc-dev#1197 sqlc-dev#1754
- Loading branch information
1 parent
7a70322
commit a857fe8
Showing
1 changed file
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package dolphin | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/kyleconroy/sqlc/internal/sql/sqlerr" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestUpdateErrors(t *testing.T) { | ||
p := NewParser() | ||
for i, tc := range []struct { | ||
stmt string | ||
err *sqlerr.Error | ||
}{ | ||
{ | ||
` | ||
CREATE TABLE foo (bar int); | ||
CREATE TABLE foo (bar int); | ||
`, | ||
sqlerr.RelationExists("foo"), | ||
}, | ||
} { | ||
test := tc | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
stmts, err := p.Parse(strings.NewReader(test.stmt)) | ||
if err != nil { | ||
t.Log(test.stmt) | ||
t.Fatal(err) | ||
} | ||
|
||
c := NewCatalog() | ||
err = c.Build(stmts) | ||
if err == nil { | ||
t.Log(test.stmt) | ||
t.Fatal("err was nil") | ||
} | ||
|
||
var actual *sqlerr.Error | ||
if !errors.As(err, &actual) { | ||
t.Fatalf("err is not *sqlerr.Error: %#v", err) | ||
} | ||
|
||
if diff := cmp.Diff(test.err.Error(), actual.Error()); diff != "" { | ||
t.Log(test.stmt) | ||
t.Errorf("error mismatch: \n%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSuccessfulUpdate(t *testing.T) { | ||
p := NewParser() | ||
for i, tc := range []struct { | ||
stmt string | ||
}{ | ||
{ | ||
` | ||
CREATE TABLE authors ( | ||
id INT PRIMARY KEY, | ||
name text NOT NULL, | ||
bio text NOT NULL DEFAULT (bio_func()) | ||
); | ||
`, | ||
}, | ||
{ | ||
` | ||
CREATE TABLE IF NOT EXISTS organizations | ||
( | ||
id VARCHAR(36) DEFAULT (UUID()) NOT NULL PRIMARY KEY | ||
); | ||
`, | ||
}, | ||
} { | ||
test := tc | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
stmts, err := p.Parse(strings.NewReader(test.stmt)) | ||
if err != nil { | ||
t.Log(test.stmt) | ||
t.Fatal(err) | ||
} | ||
|
||
c := NewCatalog() | ||
err = c.Build(stmts) | ||
if err != nil { | ||
t.Log(test.stmt) | ||
t.Log(err) | ||
t.Fatal("err should have been nil") | ||
} | ||
}) | ||
} | ||
} |