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

Function TestExtendedErrorCodes_ForeignKey

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

Source from the content-addressed store, hash-verified

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