From 2b8691ff4056987639734629926273c672a77fd3 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 11 Oct 2021 15:57:37 +0200 Subject: [PATCH] Run server test with PostgreSQL too --- server_test.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/server_test.go b/server_test.go index 4de7515..c6b172f 100644 --- a/server_test.go +++ b/server_test.go @@ -1,7 +1,9 @@ package soju import ( + "database/sql" "net" + "os" "testing" "golang.org/x/crypto/bcrypt" @@ -15,7 +17,7 @@ const ( testPassword = testUsername ) -func createTempDB(t *testing.T) Database { +func createTempSqliteDB(t *testing.T) Database { db, err := OpenDB("sqlite3", ":memory:") if err != nil { t.Fatalf("failed to create temporary SQLite database: %v", err) @@ -27,6 +29,15 @@ func createTempDB(t *testing.T) Database { return db } +func createTempPostgresDB(t *testing.T) Database { + db := &PostgresDB{db: openTempPostgresDB(t)} + if err := db.upgrade(); err != nil { + t.Fatalf("failed to upgrade PostgreSQL database: %v", err) + } + + return db +} + func createTestUser(t *testing.T, db Database) *User { hashed, err := bcrypt.GenerateFromPassword([]byte(testPassword), bcrypt.DefaultCost) if err != nil { @@ -141,8 +152,7 @@ func registerUpstreamConn(t *testing.T, c ircConn) { }) } -func TestServer(t *testing.T) { - db := createTempDB(t) +func testServer(t *testing.T, db Database) { user := createTestUser(t, db) network, upstream := createTestUpstream(t, db, user) defer upstream.Close() @@ -184,3 +194,15 @@ func TestServer(t *testing.T) { t.Fatalf("invalid NOTICE text: want %q, got: %v", noticeText, msg) } } + +func TestServer(t *testing.T) { + t.Run("sqlite", func(t *testing.T) { + db := createTempSqliteDB(t) + testServer(t, db) + }) + + t.Run("postgres", func(t *testing.T) { + db := createTempPostgresDB(t) + testServer(t, db) + }) +}