Generalize Test setup a little more
This commit is contained in:
@@ -15,19 +15,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func initTestDB4Character() {
|
||||
if database.DB == nil {
|
||||
db := SetupTestDB()
|
||||
database.DB = db
|
||||
}
|
||||
if !migrationDone {
|
||||
err := MigrateStructure()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadImageAsBase64 reads an image file and returns it as a Base64 string
|
||||
// with the prefix "data:mimeType;base64,"
|
||||
func ReadImageAsBase64(filePath string) (string, error) {
|
||||
@@ -745,7 +732,7 @@ func charTests(t *testing.T, char *character.Char) {
|
||||
}
|
||||
|
||||
func TestCreateChar(t *testing.T) {
|
||||
initTestDB4Character()
|
||||
SetupTestDB()
|
||||
char := createChar()
|
||||
//char.Name = "Harsk Hammerhuter, Zen2"
|
||||
err := char.First(char.Name)
|
||||
@@ -762,7 +749,7 @@ func TestCreateChar(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadChar(t *testing.T) {
|
||||
initTestDB4Character()
|
||||
SetupTestDB()
|
||||
TestCreateChar(t)
|
||||
char := character.Char{}
|
||||
char.Name = "Harsk Hammerhuter, Zen"
|
||||
@@ -778,7 +765,7 @@ func TestReadChar(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAddAusrüstung(t *testing.T) {
|
||||
initTestDB4Character()
|
||||
SetupTestDB()
|
||||
/*
|
||||
TestCreateChar(t)
|
||||
char := character.Char{}
|
||||
@@ -794,7 +781,7 @@ func TestAddAusrüstung(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportVTT2Char(t *testing.T) {
|
||||
initTestDB4Character()
|
||||
SetupTestDB()
|
||||
/*
|
||||
// loading file to Modell
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bamort/database"
|
||||
"bamort/gsmaster"
|
||||
"bamort/importer"
|
||||
"encoding/json"
|
||||
@@ -12,19 +11,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func initTestDB4Import() {
|
||||
if database.DB == nil {
|
||||
db := SetupTestDB()
|
||||
database.DB = db
|
||||
}
|
||||
if !migrationDone {
|
||||
err := MigrateStructure()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readImportChar(fileName string) (*importer.CharacterImport, error) {
|
||||
// loading file to Modell
|
||||
fileContent, err := os.ReadFile(fileName)
|
||||
@@ -227,7 +213,7 @@ func TestImportVTTStructure(t *testing.T) {
|
||||
|
||||
func TestImportSkill2GSMaster(t *testing.T) {
|
||||
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
@@ -269,7 +255,7 @@ func TestImportSkill2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportWeaponSkill2GSMaster(t *testing.T) {
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -310,7 +296,7 @@ func TestImportWeaponSkill2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportSpell2GSMaster(t *testing.T) {
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -353,7 +339,7 @@ func TestImportSpell2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportWeapon2GSMaster(t *testing.T) {
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -396,7 +382,7 @@ func TestImportWeapon2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportContainer2GSMaster(t *testing.T) {
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -439,7 +425,7 @@ func TestImportContainer2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportTransportation2GSMaster(t *testing.T) {
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -482,7 +468,7 @@ func TestImportTransportation2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportEquipment2GSMaster(t *testing.T) {
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -520,7 +506,7 @@ func TestImportEquipment2GSMaster(t *testing.T) {
|
||||
assert.NoError(t, err, "Expected no error when checkimg Transüportations against gsmaster")
|
||||
}
|
||||
func TestImportBelieve2GSMaster(t *testing.T) {
|
||||
initTestDB4Import()
|
||||
SetupTestDB()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -556,8 +542,7 @@ func TestImportBelieve2GSMaster(t *testing.T) {
|
||||
/*
|
||||
func TestImportFertigkeitenStammdatenSingle(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
|
||||
// loading file to Modell
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
@@ -605,8 +590,7 @@ func TestImportFertigkeitenStammdatenSingle(t *testing.T) {
|
||||
|
||||
func TestImportFertigkeitenStammdatenMulti(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
|
||||
// loading file to Modell
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
@@ -638,8 +622,7 @@ func TestImportFertigkeitenStammdatenMulti(t *testing.T) {
|
||||
|
||||
func TestImportZauberStammdatenSingle(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
|
||||
// loading file to Modell
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
|
||||
@@ -16,11 +16,14 @@ import (
|
||||
var migrationDone bool
|
||||
|
||||
// SetupTestDB creates an in-memory SQLite database for testing
|
||||
func SetupTestDB() *gorm.DB {
|
||||
//*
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect to the test database")
|
||||
func SetupTestDB() {
|
||||
if database.DB == nil {
|
||||
//*
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect to the test database")
|
||||
}
|
||||
database.DB = db
|
||||
}
|
||||
//*/
|
||||
/* //testin with persitant MariaDB
|
||||
@@ -31,8 +34,12 @@ func SetupTestDB() *gorm.DB {
|
||||
panic("failed to connect to the test database")
|
||||
}
|
||||
//*/
|
||||
|
||||
return db
|
||||
if !migrationDone {
|
||||
err := MigrateStructure()
|
||||
if err != nil {
|
||||
panic("failed to MigrateStructure")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func MigrateStructure() error {
|
||||
|
||||
@@ -1,29 +1,14 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bamort/database"
|
||||
"bamort/gsmaster"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func initTestDB4Lookup() {
|
||||
if database.DB == nil {
|
||||
db := SetupTestDB()
|
||||
database.DB = db
|
||||
}
|
||||
if !migrationDone {
|
||||
err := MigrateStructure()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateFilename generates a filename based on the prefix and the current date/time
|
||||
func generateFilename(prefix string, extension string) string {
|
||||
// Get the current date and time
|
||||
@@ -39,7 +24,7 @@ func generateFilename(prefix string, extension string) string {
|
||||
|
||||
/*
|
||||
func TestCreateLookupSkill(t *testing.T) {
|
||||
initTestDB4Lookup()
|
||||
SetupTestDB()
|
||||
stamm := gsmaster.Skill{}
|
||||
stamm.System = "Midgard"
|
||||
stamm.Name = "Lesen"
|
||||
@@ -68,8 +53,7 @@ func TestFindLookupSkill(t *testing.T) {
|
||||
|
||||
func TestCreateLookupWeaponSkill(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Lookup()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
stamm := gsmaster.WeaponSkill{}
|
||||
stamm.System = "Midgard"
|
||||
stamm.Name = "Stichwaffen"
|
||||
@@ -100,8 +84,7 @@ func TestFindLookupWeaponSkill(t *testing.T) {
|
||||
|
||||
func TestCreateLookupSpell(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Lookup()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
stamm := gsmaster.Spell{}
|
||||
stamm.System = "Midgard"
|
||||
stamm.Name = "Unsichtbarkeit"
|
||||
@@ -141,8 +124,7 @@ func TestFindLookupSpell(t *testing.T) {
|
||||
|
||||
func TestCreateLookupEquipment(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Lookup()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
stamm := gsmaster.Equipment{}
|
||||
stamm.System = "Midgard"
|
||||
stamm.Name = "Decke"
|
||||
@@ -171,8 +153,7 @@ func TestFindLookupEquipment(t *testing.T) {
|
||||
|
||||
func TestCreateLookupContainer(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Lookup()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
stamm := gsmaster.Container{}
|
||||
stamm.System = "Midgard"
|
||||
stamm.Name = "Topf"
|
||||
@@ -203,8 +184,7 @@ func TestFindLookupContainer(t *testing.T) {
|
||||
|
||||
func TestCreateLookupTransportation(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Lookup()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
stamm := gsmaster.Transportation{}
|
||||
stamm.System = "Midgard"
|
||||
stamm.Name = "Topf"
|
||||
@@ -235,9 +215,7 @@ func TestFindLookupTransportation(t *testing.T) {
|
||||
*/
|
||||
|
||||
func TestExportGSMasterdata(t *testing.T) {
|
||||
initTestDB4Lookup()
|
||||
//testDB := initTestDB4Lookup()
|
||||
//database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
TestImportSkill2GSMaster(t)
|
||||
TestImportWeaponSkill2GSMaster(t)
|
||||
TestImportSpell2GSMaster(t)
|
||||
@@ -251,9 +229,7 @@ func TestExportGSMasterdata(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportGSMasterdata(t *testing.T) {
|
||||
initTestDB4Lookup()
|
||||
//testDB := initTestDB4Lookup()
|
||||
//database.DB = testDB // Assign test DB to global DB
|
||||
SetupTestDB()
|
||||
err := gsmaster.Import("../testdata/gsmaster_exported_gsdata.json")
|
||||
assert.NoError(t, err, "expexted no Error during Export if gsmaster data")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user