(t *testing.T)
| 28 | ) |
| 29 | |
| 30 | func TestConn(t *testing.T) { |
| 31 | log.SetLevel(log.DebugLevel) |
| 32 | Convey("test connection", t, func() { |
| 33 | var ( |
| 34 | stopTestService func() |
| 35 | ok bool |
| 36 | err error |
| 37 | ) |
| 38 | stopTestService, _, err = startTestService() |
| 39 | So(err, ShouldBeNil) |
| 40 | defer stopTestService() |
| 41 | |
| 42 | var db *sql.DB |
| 43 | db, err = sql.Open("covenantsql", "covenantsql://db?update_interval=400ms") |
| 44 | So(db, ShouldNotBeNil) |
| 45 | So(err, ShouldBeNil) |
| 46 | |
| 47 | ctx := WithReceipt(context.Background()) |
| 48 | rec, ok := GetReceipt(ctx) |
| 49 | So(ok, ShouldBeFalse) |
| 50 | So(rec, ShouldBeNil) |
| 51 | |
| 52 | _, err = db.ExecContext(ctx, "create table test (test int)") |
| 53 | So(err, ShouldBeNil) |
| 54 | rec, ok = GetReceipt(ctx) |
| 55 | So(ok, ShouldBeTrue) |
| 56 | So(rec, ShouldNotBeNil) |
| 57 | |
| 58 | _, err = db.ExecContext(ctx, "insert into test values (1)") |
| 59 | So(err, ShouldBeNil) |
| 60 | rec2, ok := GetReceipt(ctx) |
| 61 | So(ok, ShouldBeTrue) |
| 62 | So(rec2, ShouldNotBeNil) |
| 63 | So(rec, ShouldNotEqual, rec2) // receipt should be reset |
| 64 | |
| 65 | // test with query |
| 66 | var rows *sql.Rows |
| 67 | var result int |
| 68 | rows, err = db.QueryContext(ctx, "select * from test") |
| 69 | So(err, ShouldBeNil) |
| 70 | So(rows, ShouldNotBeNil) |
| 71 | So(rows.Next(), ShouldBeTrue) |
| 72 | rec, ok = GetReceipt(ctx) |
| 73 | So(ok, ShouldBeTrue) |
| 74 | So(rec, ShouldNotBeNil) |
| 75 | |
| 76 | err = rows.Scan(&result) |
| 77 | So(err, ShouldBeNil) |
| 78 | So(result, ShouldEqual, 1) |
| 79 | So(rows.Next(), ShouldBeFalse) |
| 80 | rows.Close() |
| 81 | |
| 82 | testRowCount := func(expected int) { |
| 83 | var row *sql.Row |
| 84 | var err error |
| 85 | var result int |
| 86 | row = db.QueryRow("select count(1) as cnt from test") |
| 87 | So(row, ShouldNotBeNil) |
nothing calls this directly
no test coverage detected