-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use gnomock to run postgres to test app
- Loading branch information
1 parent
7b6af40
commit ce944f5
Showing
9 changed files
with
250 additions
and
733 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 was deleted.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,69 @@ | ||
package test | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/orlangure/gnomock" | ||
"github.com/orlangure/gnomock/preset/postgres" | ||
) | ||
|
||
var container *gnomock.Container | ||
|
||
func GetTestingDatabase() (*sql.DB, error) { | ||
container, err := getPostgresContainer() | ||
db, err := getMainDatabaseConnection(container) | ||
newDbConnection, err := createRandomDatabaseForTest(db, "tracetest") | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newDbConnection, nil | ||
} | ||
|
||
func getMainDatabaseConnection(container *gnomock.Container) (*sql.DB, error) { | ||
connStr := fmt.Sprintf( | ||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", | ||
container.Host, container.DefaultPort(), "tracetest", "tracetest", "postgres", | ||
) | ||
|
||
return sql.Open("postgres", connStr) | ||
} | ||
|
||
func createRandomDatabaseForTest(db *sql.DB, baseDatabase string) (*sql.DB, error) { | ||
epoch := time.Now().UnixNano() | ||
newDatabaseName := fmt.Sprintf("%s_%d", baseDatabase, epoch) | ||
_, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s WITH TEMPLATE %s", newDatabaseName, baseDatabase)) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create database %s: %w", newDatabaseName, err) | ||
} | ||
|
||
connStr := fmt.Sprintf( | ||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", | ||
container.Host, container.DefaultPort(), "tracetest", "tracetest", newDatabaseName, | ||
) | ||
|
||
return sql.Open("postgres", connStr) | ||
} | ||
|
||
func getPostgresContainer() (*gnomock.Container, error) { | ||
if container != nil { | ||
return container, nil | ||
} | ||
|
||
preset := postgres.Preset( | ||
postgres.WithUser("tracetest", "tracetest"), | ||
postgres.WithDatabase("tracetest"), | ||
) | ||
|
||
dbContainer, err := gnomock.Start(preset) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not start postgres container") | ||
} | ||
|
||
container = dbContainer | ||
|
||
return dbContainer, nil | ||
} |
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