-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
System Tests with full in memory DB support (#57)
This PR implements full system tests / end2end tests using an in memory mysql databse. The motivation for this work was to get coverage for the lines that require a real db connection. These tests are still expensive to run, as the internal DB server needs to be started (~50ms), so only need to have system tests for fully testing a feature. Leave smaller tests for unit tests.
- Loading branch information
1 parent
4f0c2c6
commit 1672494
Showing
8 changed files
with
774 additions
and
18 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
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
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
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
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,90 @@ | ||
package index_test | ||
|
||
import ( | ||
"context" | ||
_ "database/sql" | ||
"embed" | ||
"fmt" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
|
||
sqle "github.com/dolthub/go-mysql-server" | ||
"github.com/dolthub/go-mysql-server/memory" | ||
"github.com/dolthub/go-mysql-server/server" | ||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/matheusgomes28/urchin/common" | ||
"github.com/matheusgomes28/urchin/database" | ||
) | ||
|
||
//go:generate ../../../migrations ./migrations | ||
|
||
//go:embed migrations/*.sql | ||
var embedMigrations embed.FS | ||
|
||
var current_port int = 0 | ||
|
||
func runDatabaseServer(app_settings common.AppSettings) { | ||
pro := createTestDatabase(app_settings.DatabaseName) | ||
engine := sqle.NewDefault(pro) | ||
engine.Analyzer.Catalog.MySQLDb.AddRootAccount() | ||
|
||
session := memory.NewSession(sql.NewBaseSession(), pro) | ||
ctx := sql.NewContext(context.Background(), sql.WithSession(session)) | ||
ctx.SetCurrentDatabase(app_settings.DatabaseName) | ||
|
||
config := server.Config{ | ||
Protocol: "tcp", | ||
Address: fmt.Sprintf("%s:%d", app_settings.DatabaseAddress, app_settings.DatabasePort), | ||
} | ||
s, err := server.NewServer(config, engine, memory.NewSessionBuilder(pro), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err = s.Start(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func createTestDatabase(name string) *memory.DbProvider { | ||
db := memory.NewDatabase(name) | ||
db.BaseDatabase.EnablePrimaryKeyIndexes() | ||
|
||
pro := memory.NewDBProvider(db) | ||
return pro | ||
} | ||
|
||
func waitForDb(app_settings common.AppSettings) (database.SqlDatabase, error) { | ||
|
||
for range 400 { | ||
database, err := database.MakeSqlConnection( | ||
app_settings.DatabaseUser, | ||
app_settings.DatabasePassword, | ||
app_settings.DatabaseAddress, | ||
app_settings.DatabasePort, | ||
app_settings.DatabaseName, | ||
) | ||
|
||
if err == nil { | ||
return database, nil | ||
} | ||
|
||
time.Sleep(25 * time.Millisecond) | ||
} | ||
|
||
return database.SqlDatabase{}, fmt.Errorf("database did not start") | ||
} | ||
|
||
func getAppSettings() common.AppSettings { | ||
app_settings := common.AppSettings{ | ||
DatabaseAddress: "localhost", | ||
DatabasePort: 3336 + current_port, // Initial port | ||
DatabaseUser: "root", | ||
DatabasePassword: "", | ||
DatabaseName: "urchin", | ||
WebserverPort: 8080, | ||
} | ||
current_port++ | ||
|
||
return app_settings | ||
} |
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,65 @@ | ||
package index_test | ||
|
||
import ( | ||
_ "database/sql" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/matheusgomes28/urchin/app" | ||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
func TestIndexPagePing(t *testing.T) { | ||
|
||
// This is gonna be the in-memory mysql | ||
app_settings := getAppSettings() | ||
go runDatabaseServer(app_settings) | ||
database, err := waitForDb(app_settings) | ||
require.Nil(t, err) | ||
goose.SetBaseFS(embedMigrations) | ||
|
||
if err := goose.SetDialect("mysql"); err != nil { | ||
require.Nil(t, err) | ||
} | ||
|
||
if err := goose.Up(database.Connection, "migrations"); err != nil { | ||
require.Nil(t, err) | ||
} | ||
|
||
r := app.SetupRoutes(app_settings, database) | ||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/", nil) | ||
r.ServeHTTP(w, req) | ||
|
||
require.Equal(t, http.StatusOK, w.Code) | ||
} | ||
|
||
func TestIndexPagePostExists(t *testing.T) { | ||
// This is gonna be the in-memory mysql | ||
app_settings := getAppSettings() | ||
go runDatabaseServer(app_settings) | ||
database, err := waitForDb(app_settings) | ||
require.Nil(t, err) | ||
goose.SetBaseFS(embedMigrations) | ||
|
||
if err := goose.SetDialect("mysql"); err != nil { | ||
require.Nil(t, err) | ||
} | ||
|
||
if err := goose.Up(database.Connection, "migrations"); err != nil { | ||
require.Nil(t, err) | ||
} | ||
|
||
r := app.SetupRoutes(app_settings, database) | ||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/", nil) | ||
r.ServeHTTP(w, req) | ||
|
||
require.Equal(t, http.StatusOK, w.Code) | ||
|
||
require.Contains(t, w.Body.String(), "My Very First Post") | ||
} |
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,39 @@ | ||
package index_test | ||
|
||
import ( | ||
_ "database/sql" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/matheusgomes28/urchin/app" | ||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
func TestPostExists(t *testing.T) { | ||
|
||
// This is gonna be the in-memory mysql | ||
app_settings := getAppSettings() | ||
go runDatabaseServer(app_settings) | ||
database, err := waitForDb(app_settings) | ||
require.Nil(t, err) | ||
goose.SetBaseFS(embedMigrations) | ||
|
||
if err := goose.SetDialect("mysql"); err != nil { | ||
require.Nil(t, err) | ||
} | ||
|
||
if err := goose.Up(database.Connection, "migrations"); err != nil { | ||
require.Nil(t, err) | ||
} | ||
|
||
r := app.SetupRoutes(app_settings, database) | ||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/post/1", nil) | ||
r.ServeHTTP(w, req) | ||
|
||
require.Equal(t, http.StatusOK, w.Code) | ||
} |