MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / TestStmt

Function TestStmt

client/stmt_test.go:29–181  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

27)
28
29func TestStmt(t *testing.T) {
30 Convey("test statement", t, func() {
31 var stopTestService func()
32 var err error
33 stopTestService, _, err = startTestService()
34 So(err, ShouldBeNil)
35 defer stopTestService()
36
37 var db *sql.DB
38 db, err = sql.Open("covenantsql", "covenantsql://db")
39 So(db, ShouldNotBeNil)
40 So(err, ShouldBeNil)
41
42 _, err = db.Exec("create table test (test int)")
43 So(err, ShouldBeNil)
44 _, err = db.Exec("insert into test values (1)")
45 So(err, ShouldBeNil)
46
47 var stmt *sql.Stmt
48 stmt, err = db.Prepare("select count(1) as cnt from test where test = ?")
49 So(err, ShouldBeNil)
50 So(stmt, ShouldNotBeNil)
51
52 var row *sql.Row
53 var result int
54
55 // query with argument 1
56 row = stmt.QueryRow(1)
57 So(row, ShouldNotBeNil)
58 err = row.Scan(&result)
59 So(err, ShouldBeNil)
60 So(result, ShouldEqual, 1)
61
62 // query with argument 2
63 row = stmt.QueryRow(2)
64 So(row, ShouldNotBeNil)
65 err = row.Scan(&result)
66 So(err, ShouldBeNil)
67 So(result, ShouldEqual, 0)
68
69 // query when statement closed
70 stmt.Close()
71 row = stmt.QueryRow(1)
72 So(row, ShouldNotBeNil)
73 err = row.Scan(&result)
74 So(err, ShouldNotBeNil)
75
76 // exec statement
77 stmt, err = db.Prepare("insert into test values(?)")
78
79 // parameter count equals to placeholders
80 _, err = stmt.Exec(2)
81 So(err, ShouldBeNil)
82 row = db.QueryRow("select count(1) as cnt from test")
83 So(row, ShouldNotBeNil)
84 err = row.Scan(&result)
85 So(err, ShouldBeNil)
86 So(result, ShouldEqual, 2) // test insert success

Callers

nothing calls this directly

Calls 11

startTestServiceFunction · 0.85
ExecuteTxFunction · 0.85
newStmtFunction · 0.85
QueryRowMethod · 0.80
ErrorfMethod · 0.80
ExecMethod · 0.65
PrepareMethod · 0.65
CloseMethod · 0.65
QueryMethod · 0.65
OpenMethod · 0.45
ScanMethod · 0.45

Tested by

no test coverage detected