-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from dxe/alex/devcontainer-docker-compose
Use containerized database in devcontainer and all unit tests
- Loading branch information
Showing
13 changed files
with
149 additions
and
265 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Docker Compose configuration for development environment, for users of the | ||
# devcontainer. This file defines the devcontainer container itself as well as | ||
# adding configuration to other docker compose configuration. | ||
|
||
services: | ||
# https://code.visualstudio.com/docs/devcontainers/create-dev-container | ||
devcontainer: | ||
image: "mcr.microsoft.com/devcontainers/base:1.0.9-bookworm" | ||
volumes: | ||
# Mounts the project folder to '/workspace', referenced by | ||
# 'workspaceFolder' in devcontainer.json. | ||
- ..:/workspace:cached | ||
networks: | ||
- dev-net | ||
# Override default command to keep devcontainer alive as default command | ||
# could exit immediately. Command taken from VS Code devcontainer docs. | ||
command: /bin/sh -c "while sleep 1000; do :; done" | ||
environment: | ||
- DB_PROTOCOL=tcp(mysql:3306) | ||
mysql: # extending server/compose.yaml | ||
networks: # make accessible to devcontainer via network | ||
- dev-net | ||
|
||
networks: | ||
dev-net: | ||
driver: bridge |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version: "3.1" | ||
name: dxe-adb | ||
|
||
services: | ||
mysql: | ||
|
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,61 @@ | ||
package form_processor | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/dxe/adb/config" | ||
"github.com/dxe/adb/model" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// Do not use same DB as used by `models` package because go tests in different | ||
// packages run in parallel. | ||
const dbName = "adb_test_forms_db" | ||
|
||
// createTempDb creates a database for testing. Please call `dropTempDb` when | ||
// tests finish. | ||
func createTempDb(name string) { | ||
db, err := sql.Open("mysql", config.DataSourceBase+"/") | ||
if err != nil { | ||
log.Fatalf("Error connecting to database: %v", err) | ||
} | ||
defer db.Close() | ||
|
||
// Create the database | ||
createDBQuery := "CREATE DATABASE IF NOT EXISTS " + name | ||
_, err = db.Exec(createDBQuery) | ||
if err != nil { | ||
log.Fatalf("Error creating database: %v", err) | ||
} | ||
} | ||
func dropTempDb(name string) { | ||
db, err := sql.Open("mysql", config.DataSourceBase+"/") | ||
if err != nil { | ||
log.Fatalf("Error connecting to database: %v", err) | ||
} | ||
defer db.Close() | ||
|
||
// Create the database | ||
createDBQuery := "DROP DATABASE " + name | ||
_, err = db.Exec(createDBQuery) | ||
if err != nil { | ||
log.Fatalf("Error creating database: %v", err) | ||
} | ||
} | ||
func useTestDb() *sqlx.DB { | ||
db := model.NewDB(config.DataSourceBase + "/" + dbName + "?parseTime=true") | ||
model.WipeDatabase(db) | ||
return db | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
// Use a dedicated database for this package's tests. Tests in this package | ||
// must run in serial, but may run in parallel with tests in other packages. | ||
createTempDb(dbName) | ||
defer dropTempDb(dbName) | ||
|
||
os.Exit(m.Run()) | ||
} |
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
Oops, something went wrong.