error_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package sqlite3
  6. import (
  7. "database/sql"
  8. "io/ioutil"
  9. "os"
  10. "path"
  11. "testing"
  12. )
  13. func TestSimpleError(t *testing.T) {
  14. e := ErrError.Error()
  15. if e != "SQL logic error or missing database" && e != "SQL logic error" {
  16. t.Error("wrong error code: " + e)
  17. }
  18. }
  19. func TestCorruptDbErrors(t *testing.T) {
  20. dirName, err := ioutil.TempDir("", "sqlite3")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. defer os.RemoveAll(dirName)
  25. dbFileName := path.Join(dirName, "test.db")
  26. f, err := os.Create(dbFileName)
  27. if err != nil {
  28. t.Error(err)
  29. }
  30. f.Write([]byte{1, 2, 3, 4, 5})
  31. f.Close()
  32. db, err := sql.Open("sqlite3", dbFileName)
  33. if err == nil {
  34. _, err = db.Exec("drop table foo")
  35. }
  36. sqliteErr := err.(Error)
  37. if sqliteErr.Code != ErrNotADB {
  38. t.Error("wrong error code for corrupted DB")
  39. }
  40. if err.Error() == "" {
  41. t.Error("wrong error string for corrupted DB")
  42. }
  43. db.Close()
  44. }
  45. func TestSqlLogicErrors(t *testing.T) {
  46. dirName, err := ioutil.TempDir("", "sqlite3")
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. defer os.RemoveAll(dirName)
  51. dbFileName := path.Join(dirName, "test.db")
  52. db, err := sql.Open("sqlite3", dbFileName)
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. defer db.Close()
  57. _, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)")
  58. if err != nil {
  59. t.Error(err)
  60. }
  61. const expectedErr = "table Foo already exists"
  62. _, err = db.Exec("CREATE TABLE Foo (id INTEGER PRIMARY KEY)")
  63. if err.Error() != expectedErr {
  64. t.Errorf("Unexpected error: %s, expected %s", err.Error(), expectedErr)
  65. }
  66. }
  67. func TestExtendedErrorCodes_ForeignKey(t *testing.T) {
  68. dirName, err := ioutil.TempDir("", "sqlite3-err")
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer os.RemoveAll(dirName)
  73. dbFileName := path.Join(dirName, "test.db")
  74. db, err := sql.Open("sqlite3", dbFileName)
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. defer db.Close()
  79. _, err = db.Exec("PRAGMA foreign_keys=ON;")
  80. if err != nil {
  81. t.Errorf("PRAGMA foreign_keys=ON: %v", err)
  82. }
  83. _, err = db.Exec(`CREATE TABLE Foo (
  84. id INTEGER PRIMARY KEY AUTOINCREMENT,
  85. value INTEGER NOT NULL,
  86. ref INTEGER NULL REFERENCES Foo (id),
  87. UNIQUE(value)
  88. );`)
  89. if err != nil {
  90. t.Error(err)
  91. }
  92. _, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (100, 100);")
  93. if err == nil {
  94. t.Error("No error!")
  95. } else {
  96. sqliteErr := err.(Error)
  97. if sqliteErr.Code != ErrConstraint {
  98. t.Errorf("Wrong basic error code: %d != %d",
  99. sqliteErr.Code, ErrConstraint)
  100. }
  101. if sqliteErr.ExtendedCode != ErrConstraintForeignKey {
  102. t.Errorf("Wrong extended error code: %d != %d",
  103. sqliteErr.ExtendedCode, ErrConstraintForeignKey)
  104. }
  105. }
  106. }
  107. func TestExtendedErrorCodes_NotNull(t *testing.T) {
  108. dirName, err := ioutil.TempDir("", "sqlite3-err")
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. defer os.RemoveAll(dirName)
  113. dbFileName := path.Join(dirName, "test.db")
  114. db, err := sql.Open("sqlite3", dbFileName)
  115. if err != nil {
  116. t.Error(err)
  117. }
  118. defer db.Close()
  119. _, err = db.Exec("PRAGMA foreign_keys=ON;")
  120. if err != nil {
  121. t.Errorf("PRAGMA foreign_keys=ON: %v", err)
  122. }
  123. _, err = db.Exec(`CREATE TABLE Foo (
  124. id INTEGER PRIMARY KEY AUTOINCREMENT,
  125. value INTEGER NOT NULL,
  126. ref INTEGER NULL REFERENCES Foo (id),
  127. UNIQUE(value)
  128. );`)
  129. if err != nil {
  130. t.Error(err)
  131. }
  132. res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);")
  133. if err != nil {
  134. t.Fatalf("Creating first row: %v", err)
  135. }
  136. id, err := res.LastInsertId()
  137. if err != nil {
  138. t.Fatalf("Retrieving last insert id: %v", err)
  139. }
  140. _, err = db.Exec("INSERT INTO Foo (ref) VALUES (?);", id)
  141. if err == nil {
  142. t.Error("No error!")
  143. } else {
  144. sqliteErr := err.(Error)
  145. if sqliteErr.Code != ErrConstraint {
  146. t.Errorf("Wrong basic error code: %d != %d",
  147. sqliteErr.Code, ErrConstraint)
  148. }
  149. if sqliteErr.ExtendedCode != ErrConstraintNotNull {
  150. t.Errorf("Wrong extended error code: %d != %d",
  151. sqliteErr.ExtendedCode, ErrConstraintNotNull)
  152. }
  153. }
  154. }
  155. func TestExtendedErrorCodes_Unique(t *testing.T) {
  156. dirName, err := ioutil.TempDir("", "sqlite3-err")
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. defer os.RemoveAll(dirName)
  161. dbFileName := path.Join(dirName, "test.db")
  162. db, err := sql.Open("sqlite3", dbFileName)
  163. if err != nil {
  164. t.Error(err)
  165. }
  166. defer db.Close()
  167. _, err = db.Exec("PRAGMA foreign_keys=ON;")
  168. if err != nil {
  169. t.Errorf("PRAGMA foreign_keys=ON: %v", err)
  170. }
  171. _, err = db.Exec(`CREATE TABLE Foo (
  172. id INTEGER PRIMARY KEY AUTOINCREMENT,
  173. value INTEGER NOT NULL,
  174. ref INTEGER NULL REFERENCES Foo (id),
  175. UNIQUE(value)
  176. );`)
  177. if err != nil {
  178. t.Error(err)
  179. }
  180. res, err := db.Exec("INSERT INTO Foo (value) VALUES (100);")
  181. if err != nil {
  182. t.Fatalf("Creating first row: %v", err)
  183. }
  184. id, err := res.LastInsertId()
  185. if err != nil {
  186. t.Fatalf("Retrieving last insert id: %v", err)
  187. }
  188. _, err = db.Exec("INSERT INTO Foo (ref, value) VALUES (?, 100);", id)
  189. if err == nil {
  190. t.Error("No error!")
  191. } else {
  192. sqliteErr := err.(Error)
  193. if sqliteErr.Code != ErrConstraint {
  194. t.Errorf("Wrong basic error code: %d != %d",
  195. sqliteErr.Code, ErrConstraint)
  196. }
  197. if sqliteErr.ExtendedCode != ErrConstraintUnique {
  198. t.Errorf("Wrong extended error code: %d != %d",
  199. sqliteErr.ExtendedCode, ErrConstraintUnique)
  200. }
  201. extended := sqliteErr.Code.Extend(3).Error()
  202. expected := "constraint failed"
  203. if extended != expected {
  204. t.Errorf("Wrong basic error code: %q != %q",
  205. extended, expected)
  206. }
  207. }
  208. }