MCPcopy Create free account
hub / github.com/ebosas/microservices / TestInsertToDB

Function TestInsertToDB

cmd/database/database_test.go:15–43  ·  view source on GitHub ↗

TestInsertToDB tests a message insertion into a database

(t *testing.T)

Source from the content-addressed store, hash-verified

13
14// TestInsertToDB tests a message insertion into a database
15func 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
46func testArguments(t *testing.T, message, source string, time int64) *amqp.Delivery {

Callers

nothing calls this directly

Calls 3

insertToDBFunction · 0.85
CloseMethod · 0.80
testArgumentsFunction · 0.70

Tested by

no test coverage detected