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

Function TestExtendedErrorCodes_NotNull

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

Source from the content-addressed store, hash-verified

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