-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/linyows/warp" | ||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
func TestConst(t *testing.T) { | ||
var expect string | ||
var got string | ||
|
||
expect = "sqlite-plugin" | ||
got = prefix | ||
if got != expect { | ||
t.Errorf("expected %s, got %s", expect, got) | ||
} | ||
|
||
expect = "insert into communications (id, connection_id, occurred_at, direction, data) values ($1, $2, $3, $4, $5)" | ||
got = commQuery | ||
if got != expect { | ||
t.Errorf("expected %s, got %s", expect, got) | ||
} | ||
|
||
expect = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values ($1, $2, $3, $4, $5)" | ||
got = connQuery | ||
if got != expect { | ||
t.Errorf("expected %s, got %s", expect, got) | ||
} | ||
} | ||
|
||
func TestWriter(t *testing.T) { | ||
expectError := "missing dsn for sqlite, please set `DSN`" | ||
sqlite := Sqlite{} | ||
_, err := sqlite.Conn() | ||
|
||
if err != nil && fmt.Sprintf("%s", err) != expectError { | ||
t.Errorf("expected %s, got %s", expectError, err) | ||
} | ||
} | ||
|
||
type AnyID struct{} | ||
|
||
func (a AnyID) Match(v driver.Value) bool { | ||
_, ok := v.(string) | ||
return ok | ||
} | ||
|
||
func TestAfterComm(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
ti := time.Date(2023, time.August, 16, 14, 48, 0, 0, time.UTC) | ||
|
||
mock.ExpectExec("insert into communications").WithArgs( | ||
AnyID{}, | ||
"abcdefg", | ||
ti.Format(warp.TimeFormat), | ||
"--", | ||
[]byte("hello"), | ||
).WillReturnResult(sqlmock.NewResult(1, 1)) | ||
|
||
data := &warp.AfterCommData{ | ||
ConnID: "abcdefg", | ||
OccurredAt: ti, | ||
Data: []byte("hello"), | ||
Direction: "--", | ||
} | ||
|
||
sqlite := Sqlite{pool: db} | ||
sqlite.AfterComm(data) | ||
} | ||
|
||
func TestAfterConn(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
ti := time.Date(2023, time.August, 16, 14, 48, 0, 0, time.UTC) | ||
|
||
mock.ExpectExec("insert into connections").WithArgs( | ||
"abcdefg", | ||
ti.Format(warp.TimeFormat), | ||
[]byte("[email protected]"), | ||
[]byte("[email protected]"), | ||
20, | ||
).WillReturnResult(sqlmock.NewResult(1, 1)) | ||
|
||
data := &warp.AfterConnData{ | ||
ConnID: "abcdefg", | ||
OccurredAt: ti, | ||
MailFrom: []byte("[email protected]"), | ||
MailTo: []byte("[email protected]"), | ||
Elapse: 20, | ||
} | ||
|
||
sqlite := Sqlite{pool: db} | ||
sqlite.AfterConn(data) | ||
} |