...

Source file src/github.com/DATA-DOG/go-sqlmock/examples/orders/orders_test.go

Documentation: github.com/DATA-DOG/go-sqlmock/examples/orders

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/DATA-DOG/go-sqlmock"
     8  )
     9  
    10  // will test that order with a different status, cannot be cancelled
    11  func TestShouldNotCancelOrderWithNonPendingStatus(t *testing.T) {
    12  	// open database stub
    13  	db, mock, err := sqlmock.New()
    14  	if err != nil {
    15  		t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
    16  	}
    17  	defer db.Close()
    18  
    19  	// columns are prefixed with "o" since we used sqlstruct to generate them
    20  	columns := []string{"o_id", "o_status"}
    21  	// expect transaction begin
    22  	mock.ExpectBegin()
    23  	// expect query to fetch order and user, match it with regexp
    24  	mock.ExpectQuery("SELECT (.+) FROM orders AS o INNER JOIN users AS u (.+) FOR UPDATE").
    25  		WithArgs(1).
    26  		WillReturnRows(sqlmock.NewRows(columns).FromCSVString("1,1"))
    27  	// expect transaction rollback, since order status is "cancelled"
    28  	mock.ExpectRollback()
    29  
    30  	// run the cancel order function
    31  	err = cancelOrder(1, db)
    32  	if err != nil {
    33  		t.Errorf("Expected no error, but got %s instead", err)
    34  	}
    35  	// we make sure that all expectations were met
    36  	if err := mock.ExpectationsWereMet(); err != nil {
    37  		t.Errorf("there were unfulfilled expectations: %s", err)
    38  	}
    39  }
    40  
    41  // will test order cancellation
    42  func TestShouldRefundUserWhenOrderIsCancelled(t *testing.T) {
    43  	// open database stub
    44  	db, mock, err := sqlmock.New()
    45  	if err != nil {
    46  		t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
    47  	}
    48  	defer db.Close()
    49  
    50  	// columns are prefixed with "o" since we used sqlstruct to generate them
    51  	columns := []string{"o_id", "o_status", "o_value", "o_reserved_fee", "u_id", "u_balance"}
    52  	// expect transaction begin
    53  	mock.ExpectBegin()
    54  	// expect query to fetch order and user, match it with regexp
    55  	mock.ExpectQuery("SELECT (.+) FROM orders AS o INNER JOIN users AS u (.+) FOR UPDATE").
    56  		WithArgs(1).
    57  		WillReturnRows(sqlmock.NewRows(columns).AddRow(1, 0, 25.75, 3.25, 2, 10.00))
    58  	// expect user balance update
    59  	mock.ExpectPrepare("UPDATE users SET balance").ExpectExec().
    60  		WithArgs(25.75+3.25, 2).                  // refund amount, user id
    61  		WillReturnResult(sqlmock.NewResult(0, 1)) // no insert id, 1 affected row
    62  	// expect order status update
    63  	mock.ExpectPrepare("UPDATE orders SET status").ExpectExec().
    64  		WithArgs(ORDER_CANCELLED, 1).             // status, id
    65  		WillReturnResult(sqlmock.NewResult(0, 1)) // no insert id, 1 affected row
    66  	// expect a transaction commit
    67  	mock.ExpectCommit()
    68  
    69  	// run the cancel order function
    70  	err = cancelOrder(1, db)
    71  	if err != nil {
    72  		t.Errorf("Expected no error, but got %s instead", err)
    73  	}
    74  	// we make sure that all expectations were met
    75  	if err := mock.ExpectationsWereMet(); err != nil {
    76  		t.Errorf("there were unfulfilled expectations: %s", err)
    77  	}
    78  }
    79  
    80  // will test order cancellation
    81  func TestShouldRollbackOnError(t *testing.T) {
    82  	// open database stub
    83  	db, mock, err := sqlmock.New()
    84  	if err != nil {
    85  		t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
    86  	}
    87  	defer db.Close()
    88  
    89  	// expect transaction begin
    90  	mock.ExpectBegin()
    91  	// expect query to fetch order and user, match it with regexp
    92  	mock.ExpectQuery("SELECT (.+) FROM orders AS o INNER JOIN users AS u (.+) FOR UPDATE").
    93  		WithArgs(1).
    94  		WillReturnError(fmt.Errorf("Some error"))
    95  	// should rollback since error was returned from query execution
    96  	mock.ExpectRollback()
    97  
    98  	// run the cancel order function
    99  	err = cancelOrder(1, db)
   100  	// error should return back
   101  	if err == nil {
   102  		t.Error("Expected error, but got none")
   103  	}
   104  	// we make sure that all expectations were met
   105  	if err := mock.ExpectationsWereMet(); err != nil {
   106  		t.Errorf("there were unfulfilled expectations: %s", err)
   107  	}
   108  }
   109  

View as plain text