-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompany_test.go
52 lines (40 loc) · 1.21 KB
/
company_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package libgorion
import (
"fmt"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
)
func TestCompany(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a strub Database connection", err)
}
defer db.Close()
rows := sqlmock.NewRows([]string{"Company", "Workers"}).
AddRow("company 1", "2").
AddRow("company 2", "5")
mock.ExpectQuery(testQueryCompanies).WillReturnRows(rows)
lib := &Database{DB: db}
if _, err = lib.Company(); err != nil {
t.Errorf("error was not expected while gets company %q ", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestCompanyFail(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a strub Database connection", err)
}
defer db.Close()
mock.ExpectQuery(testQueryCompanies).
WillReturnError(fmt.Errorf("some error"))
lib := &Database{DB: db}
if _, err = lib.Company(); err == nil {
t.Errorf("was expecting an error, but there was none")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}