MCPcopy Create free account
hub / github.com/DATA-DOG/go-sqlmock / Example

Function Example

sqlmock_test.go:25–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23}
24
25func 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
59func TestIssue14EscapeSQL(t *testing.T) {
60 t.Parallel()

Callers

nothing calls this directly

Calls 10

NewFunction · 0.85
NewRowsFunction · 0.85
AddRowMethod · 0.80
cancelOrderFunction · 0.70
ExpectBeginMethod · 0.65
ExpectQueryMethod · 0.65
ExpectRollbackMethod · 0.65
ExpectationsWereMetMethod · 0.65
WillReturnRowsMethod · 0.45
WithArgsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…