()
| 23 | } |
| 24 | |
| 25 | func Example() { |
| 26 | // Open new mock database |
| 27 | db, mock, err := New() |
| 28 | if err != nil { |
| 29 | fmt.Println("error creating mock database") |
| 30 | return |
| 31 | } |
| 32 | // columns to be used for result |
| 33 | columns := []string{"id", "status"} |
| 34 | // expect transaction begin |
| 35 | mock.ExpectBegin() |
| 36 | // expect query to fetch order, match it with regexp |
| 37 | mock.ExpectQuery("SELECT (.+) FROM orders (.+) FOR UPDATE"). |
| 38 | WithArgs(1). |
| 39 | WillReturnRows(NewRows(columns).AddRow(1, 1)) |
| 40 | // expect transaction rollback, since order status is "cancelled" |
| 41 | mock.ExpectRollback() |
| 42 | |
| 43 | // run the cancel order function |
| 44 | someOrderID := 1 |
| 45 | // call a function which executes expected database operations |
| 46 | err = cancelOrder(db, someOrderID) |
| 47 | if err != nil { |
| 48 | fmt.Printf("unexpected error: %s", err) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | // ensure all expectations have been met |
| 53 | if err = mock.ExpectationsWereMet(); err != nil { |
| 54 | fmt.Printf("unmet expectation error: %s", err) |
| 55 | } |
| 56 | // Output: |
| 57 | } |
| 58 | |
| 59 | func TestIssue14EscapeSQL(t *testing.T) { |
| 60 | t.Parallel() |
nothing calls this directly
no test coverage detected
searching dependent graphs…