TestInsertToDB tests a message insertion into a database
(t *testing.T)
| 13 | |
| 14 | // TestInsertToDB tests a message insertion into a database |
| 15 | func TestInsertToDB(t *testing.T) { |
| 16 | db, mock, err := sqlmock.New() |
| 17 | if err != nil { |
| 18 | t.Fatalf("sqlmock connection: %s", err) |
| 19 | } |
| 20 | defer db.Close() |
| 21 | |
| 22 | now := time.Now().UnixMilli() |
| 23 | var tests = []struct { |
| 24 | message string |
| 25 | source string |
| 26 | time int64 |
| 27 | }{ |
| 28 | {"Hello", "back", now}, |
| 29 | {"Another test!", "back", now}, |
| 30 | {"1", "front", now}, |
| 31 | {" ", "back", now - 60*60*1000}, |
| 32 | } |
| 33 | for i, test := range tests { |
| 34 | mock.ExpectExec("insert into messages").WithArgs(test.message, test.time/1000).WillReturnResult(sqlmock.NewResult(int64(i), 1)) |
| 35 | |
| 36 | d := testArguments(t, test.message, test.source, test.time) |
| 37 | insertToDB(*d, db) |
| 38 | |
| 39 | if err := mock.ExpectationsWereMet(); err != nil { |
| 40 | t.Errorf("unfulfilled expectations: %s", err) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // testArguments produces arguments for the function being tested |
| 46 | func testArguments(t *testing.T, message, source string, time int64) *amqp.Delivery { |
nothing calls this directly
no test coverage detected