Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Aug 5, 2018
1 parent cf92839 commit 9508ad8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions drivers/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down Expand Up @@ -305,3 +309,9 @@ func convertColumnNullable(str string) bool {
}
return true
}

func parseCheckConstraints(sql string) []*schema.Constraint {
// TODO
constraints := []*schema.Constraint{}
return constraints
}
45 changes: 45 additions & 0 deletions drivers/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
_ "github.com/mattn/go-sqlite3"
"github.com/xo/dburl"
"path/filepath"
"reflect"
)

var s *schema.Schema
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 9508ad8

Please sign in to comment.