From 9508ad8e513e4b8f3ec7eb117bf0ac6d08f2bb0a Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 6 Aug 2018 08:24:29 +0900 Subject: [PATCH] Add test --- drivers/sqlite/sqlite.go | 10 ++++++++ drivers/sqlite/sqlite_test.go | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/drivers/sqlite/sqlite.go b/drivers/sqlite/sqlite.go index af530f5f..da73c94c 100644 --- a/drivers/sqlite/sqlite.go +++ b/drivers/sqlite/sqlite.go @@ -259,6 +259,10 @@ WHERE name != 'sqlite_sequence' AND (type = 'table' OR type = 'view');`) } // constraints(CHECK) + checkConstraints := parseCheckConstraints(tableDef) + for _, c := range checkConstraints { + constraints = append(constraints, c) + } // triggers triggerRows, err := db.Query(` @@ -305,3 +309,9 @@ func convertColumnNullable(str string) bool { } return true } + +func parseCheckConstraints(sql string) []*schema.Constraint { + // TODO + constraints := []*schema.Constraint{} + return constraints +} diff --git a/drivers/sqlite/sqlite_test.go b/drivers/sqlite/sqlite_test.go index 1e305e8d..1120da8b 100644 --- a/drivers/sqlite/sqlite_test.go +++ b/drivers/sqlite/sqlite_test.go @@ -10,6 +10,7 @@ import ( _ "github.com/mattn/go-sqlite3" "github.com/xo/dburl" "path/filepath" + "reflect" ) var s *schema.Schema @@ -42,3 +43,47 @@ func TestAnalyzeView(t *testing.T) { t.Errorf("actual not empty string.") } } + +func TestParseCheckConstraints(t *testing.T) { + sql := `CREATE TABLE check_constraints ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + col TEXT CHECK(length(col) > 4), + brackets TEXT UNIQUE NOT NULL CHECK(((length(brackets) > 4))), + checkcheck TEXT UNIQUE NOT NULL CHECK(length(checkcheck) > 4), + downcase TEXT UNIQUE NOT NULL check(length(downcase) > 4), + nl TEXT UNIQUE NOT + NULL check(length(nl) > 4 OR + nl != 'ln') +);` + expected := []*schema.Constraint{ + &schema.Constraint{ + Name: "-", + Type: "CHECK", + Def: "CHECK(length(col) > 4)", + }, + &schema.Constraint{ + Name: "-", + Type: "CHECK", + Def: "CHECK(((length(brackets) > 4)))", + }, + &schema.Constraint{ + Name: "-", + Type: "CHECK", + Def: "CHECK(length(checkcheck) > 4)", + }, + &schema.Constraint{ + Name: "-", + Type: "CHECK", + Def: "check(length(downcase) > 4)", + }, + &schema.Constraint{ + Name: "-", + Type: "CHECK", + Def: "check(length(nl) > 4 OR nl != 'ln')", + }, + } + actual := parseCheckConstraints(sql) + if !reflect.DeepEqual(actual, expected) { + t.Errorf("got: %v\nwant: %v", actual, expected) + } +}