Golang sqlmock db tests — SELECT тесты

func TestFindAll(t *testing.T) {
	dbMock, mock, err := sqlmock.New()
	if err != nil {
		t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
	}
	defer dbMock.Close()

	columns := []string{"id", "date", "name", "code"}

	expectedData := Data{
		Id: 1,
		Date:   time.Time{},
		Name:   "Name",
		Code:   "Code",
	}

	dbConn := sqlx.NewDb(dbMock, "")

	mock.ExpectQuery(`SELECT id,date,name,code FROM datas ORDER BY name ASC`).
		WillReturnRows(sqlmock.NewRows(columns).AddRow(expectedData.Id, expectedData.Date, expectedData.Name, expectedData.Code))

	r := NewRepository(dbConn)
	currencies, err := r.FindAll()
	assert.NoError(t, err)

	if err := mock.ExpectationsWereMet(); err != nil {
		t.Error(err)
	}

	expectedListOfDatas := []*Currency{&expectedData}
	assert.Equal(t, expectedListOfDatas, datas)
}

 

Leave a Comment

Ваш адрес email не будет опубликован. Обязательные поля помечены *