MCPcopy Create free account
hub / github.com/mattn/go-sqlite3 / TestExtendedErrorCodes_ForeignKey

Function TestExtendedErrorCodes_ForeignKey

error_test.go:86–130  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

84}
85
86func TestExtendedErrorCodes_ForeignKey(t *testing.T) {
87 dirName, err := ioutil.TempDir("", "sqlite3-err")
88 if err != nil {
89 t.Fatal(err)
90 }
91 defer os.RemoveAll(dirName)
92
93 dbFileName := path.Join(dirName, "test.db")
94 db, err := sql.Open("sqlite3", dbFileName)
95 if err != nil {
96 t.Error(err)
97 }
98 defer db.Close()
99
100 _, err = db.Exec("PRAGMA foreign_keys=ON;")
101 if err != nil {
102 t.Errorf("PRAGMA foreign_keys=ON: %v", err)
103 }
104
105 _, err = db.Exec(`CREATE TABLE Foo (
106 id INTEGER PRIMARY KEY AUTOINCREMENT,
107 value INTEGER NOT NULL,
108 ref INTEGER NULL REFERENCES Foo (id),
109 UNIQUE(value)
110 );`)
111 if err != nil {
112 t.Error(err)
113 }
114
115 _, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (100, 100);")
116 if err == nil {
117 t.Error("No error!")
118 } else {
119 sqliteErr := err.(Error)
120 if sqliteErr.Code != ErrConstraint {
121 t.Errorf("Wrong basic error code: %d != %d",
122 sqliteErr.Code, ErrConstraint)
123 }
124 if sqliteErr.ExtendedCode != ErrConstraintForeignKey {
125 t.Errorf("Wrong extended error code: %d != %d",
126 sqliteErr.ExtendedCode, ErrConstraintForeignKey)
127 }
128 }
129
130}
131
132func TestExtendedErrorCodes_NotNull(t *testing.T) {
133 dirName, err := ioutil.TempDir("", "sqlite3-err")

Callers

nothing calls this directly

Calls 4

OpenMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45
ExecMethod · 0.45

Tested by

no test coverage detected