Generalize Test setup
This commit is contained in:
@@ -6,6 +6,7 @@ func MigrateStructure() error {
|
||||
err := database.DB.AutoMigrate(
|
||||
&Skill{},
|
||||
&WeaponSkill{},
|
||||
&Spell{},
|
||||
&Equipment{},
|
||||
&Weapon{},
|
||||
&Container{},
|
||||
|
||||
@@ -13,29 +13,19 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func initTestDB4Character() *gorm.DB {
|
||||
db := SetupTestDB()
|
||||
db.AutoMigrate(
|
||||
&character.Char{},
|
||||
&character.Lp{},
|
||||
&character.Ap{},
|
||||
&character.B{},
|
||||
&character.Merkmale{},
|
||||
&character.Eigenschaft{},
|
||||
&character.Bennies{},
|
||||
&character.Erfahrungsschatz{},
|
||||
&skills.Fertigkeit{},
|
||||
&skills.Waffenfertigkeit{},
|
||||
&skills.Zauber{},
|
||||
&equipment.Waffe{},
|
||||
&equipment.Behaeltniss{},
|
||||
&equipment.Transportation{},
|
||||
&equipment.Ausruestung{},
|
||||
)
|
||||
return db
|
||||
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
|
||||
@@ -755,9 +745,7 @@ func charTests(t *testing.T, char *character.Char) {
|
||||
}
|
||||
|
||||
func TestCreateChar(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Character()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
initTestDB4Character()
|
||||
char := createChar()
|
||||
//char.Name = "Harsk Hammerhuter, Zen2"
|
||||
err := char.First(char.Name)
|
||||
@@ -774,10 +762,7 @@ func TestCreateChar(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadChar(t *testing.T) {
|
||||
// Setup test database
|
||||
//testDB := initTestDB4Character()
|
||||
//database.DB = testDB // Assign test DB to global DB
|
||||
|
||||
initTestDB4Character()
|
||||
TestCreateChar(t)
|
||||
char := character.Char{}
|
||||
char.Name = "Harsk Hammerhuter, Zen"
|
||||
@@ -793,9 +778,7 @@ func TestReadChar(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAddAusrüstung(t *testing.T) {
|
||||
// Setup test database
|
||||
//testDB := initTestDB4Character()
|
||||
//database.DB = testDB // Assign test DB to global DB
|
||||
initTestDB4Character()
|
||||
/*
|
||||
TestCreateChar(t)
|
||||
char := character.Char{}
|
||||
@@ -811,9 +794,7 @@ func TestAddAusrüstung(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportVTT2Char(t *testing.T) {
|
||||
// Setup test database
|
||||
//testDB := SetupTestDB()
|
||||
//DB = testDB // Assign test DB to global DB
|
||||
initTestDB4Character()
|
||||
/*
|
||||
// loading file to Modell
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
|
||||
@@ -10,22 +10,19 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func initTestDB4Import() *gorm.DB {
|
||||
db := SetupTestDB()
|
||||
db.AutoMigrate(
|
||||
&gsmaster.Skill{},
|
||||
&gsmaster.WeaponSkill{},
|
||||
&gsmaster.Spell{},
|
||||
&gsmaster.Weapon{},
|
||||
&gsmaster.Container{},
|
||||
&gsmaster.Transportation{},
|
||||
&gsmaster.Equipment{},
|
||||
&gsmaster.Believe{},
|
||||
)
|
||||
return db
|
||||
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) {
|
||||
@@ -229,10 +226,9 @@ func TestImportVTTStructure(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportSkill2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
|
||||
initTestDB4Import()
|
||||
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -273,10 +269,7 @@ func TestImportSkill2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportWeaponSkill2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Import()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -317,10 +310,7 @@ func TestImportWeaponSkill2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportSpell2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Import()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -363,10 +353,7 @@ func TestImportSpell2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportWeapon2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Import()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -409,10 +396,7 @@ func TestImportWeapon2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportContainer2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Import()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -455,10 +439,7 @@ func TestImportContainer2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportTransportation2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Import()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -501,10 +482,7 @@ func TestImportTransportation2GSMaster(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportEquipment2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Import()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
@@ -542,10 +520,7 @@ func TestImportEquipment2GSMaster(t *testing.T) {
|
||||
assert.NoError(t, err, "Expected no error when checkimg Transüportations against gsmaster")
|
||||
}
|
||||
func TestImportBelieve2GSMaster(t *testing.T) {
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Import()
|
||||
fileName := fmt.Sprintf("../testdata/%s", "VTT_Import1.json")
|
||||
character, err := readImportChar(fileName)
|
||||
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bamort/character"
|
||||
"bamort/database"
|
||||
"bamort/equipment"
|
||||
"bamort/gsmaster"
|
||||
"bamort/importer"
|
||||
"bamort/skills"
|
||||
"bamort/user"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var migrationDone bool
|
||||
|
||||
// SetupTestDB creates an in-memory SQLite database for testing
|
||||
func SetupTestDB() *gorm.DB {
|
||||
//*
|
||||
@@ -24,3 +34,37 @@ func SetupTestDB() *gorm.DB {
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func MigrateStructure() error {
|
||||
err := database.MigrateStructure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = character.MigrateStructure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = equipment.MigrateStructure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = gsmaster.MigrateStructure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = importer.MigrateStructure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = skills.MigrateStructure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = user.MigrateStructure()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
migrationDone = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,26 +4,24 @@ import (
|
||||
"bamort/database"
|
||||
"bamort/gsmaster"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func initTestDB4Lookup() *gorm.DB {
|
||||
db := SetupTestDB()
|
||||
db.AutoMigrate(
|
||||
&gsmaster.Skill{}, //needed for stammdaten.CheckFertigkeit
|
||||
&gsmaster.Spell{}, //needed for stammdaten.CheckZauber
|
||||
&gsmaster.WeaponSkill{}, //needed for stammdaten.CheckWaffenFertigkeit
|
||||
&gsmaster.Equipment{}, //needed for stammdaten.Check...
|
||||
&gsmaster.Container{}, //needed for stammdaten.Check...
|
||||
&gsmaster.Transportation{}, //needed for stammdaten.Check...
|
||||
&gsmaster.Weapon{},
|
||||
&gsmaster.Believe{},
|
||||
)
|
||||
return db
|
||||
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
|
||||
@@ -39,11 +37,9 @@ func generateFilename(prefix string, extension string) string {
|
||||
return fmt.Sprintf("%s_%s.%s", prefix, timestamp, extension)
|
||||
}
|
||||
|
||||
/*s
|
||||
/*
|
||||
func TestCreateLookupSkill(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := initTestDB4Lookup()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
initTestDB4Lookup()
|
||||
stamm := gsmaster.Skill{}
|
||||
stamm.System = "Midgard"
|
||||
stamm.Name = "Lesen"
|
||||
@@ -239,11 +235,7 @@ func TestFindLookupTransportation(t *testing.T) {
|
||||
*/
|
||||
|
||||
func TestExportGSMasterdata(t *testing.T) {
|
||||
// Setup test database
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Lookup()
|
||||
//testDB := initTestDB4Lookup()
|
||||
//database.DB = testDB // Assign test DB to global DB
|
||||
TestImportSkill2GSMaster(t)
|
||||
@@ -259,11 +251,7 @@ func TestExportGSMasterdata(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestImportGSMasterdata(t *testing.T) {
|
||||
// Setup test database
|
||||
if database.DB == nil {
|
||||
testDB := initTestDB4Import()
|
||||
database.DB = testDB // Assign test DB to global DB
|
||||
}
|
||||
initTestDB4Lookup()
|
||||
//testDB := initTestDB4Lookup()
|
||||
//database.DB = testDB // Assign test DB to global DB
|
||||
err := gsmaster.Import("../testdata/gsmaster_exported_gsdata.json")
|
||||
|
||||
Reference in New Issue
Block a user