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

Function TestExtendedErrorCodes_NotNull

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

Source from the content-addressed store, hash-verified

126}
127
128func TestExtendedErrorCodes_NotNull(t *testing.T) {
129 dirName, err := ioutil.TempDir("", "sqlite3-err")
130 if err != nil {
131 t.Fatal(err)
132 }
133 defer os.RemoveAll(dirName)
134
135 dbFileName := path.Join(dirName, "test.db")
136 db, err := sql.Open("sqlite3", dbFileName)
137 if err != nil {
138 t.Error(err)
139 }
140 defer db.Close()
141
142 _, err = db.Exec("PRAGMA foreign_keys=ON;")
143 if err != nil {
144 t.Errorf("PRAGMA foreign_keys=ON: %v", err)
145 }
146
147 _, err = db.Exec(`CREATE TABLE Foo (
148 id INTEGER PRIMARY KEY AUTOINCREMENT,
149 value INTEGER NOT NULL,
150 ref INTEGER NULL REFERENCES Foo (id),
151 UNIQUE(value)
152 );`)
153 if err != nil {
154 t.Error(err)
155 }
156
157 res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);")
158 if err != nil {
159 t.Fatalf("Creating first row: %v", err)
160 }
161
162 id, err := res.LastInsertId()
163 if err != nil {
164 t.Fatalf("Retrieving last insert id: %v", err)
165 }
166
167 _, err = db.Exec("INSERT INTO Foo (ref) VALUES (?);", id)
168 if err == nil {
169 t.Error("No error!")
170 } else {
171 sqliteErr := err.(Error)
172 if sqliteErr.Code != ErrConstraint {
173 t.Errorf("Wrong basic error code: %d != %d",
174 sqliteErr.Code, ErrConstraint)
175 }
176 if sqliteErr.ExtendedCode != ErrConstraintNotNull {
177 t.Errorf("Wrong extended error code: %d != %d",
178 sqliteErr.ExtendedCode, ErrConstraintNotNull)
179 }
180 }
181
182}
183
184func TestExtendedErrorCodes_Unique(t *testing.T) {
185 dirName, err := ioutil.TempDir("", "sqlite3-err")

Callers

nothing calls this directly

Calls 5

LastInsertIdMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45
ExecMethod · 0.45

Tested by

no test coverage detected