added test for Import
This commit is contained in:
+126
-27
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
@@ -23,39 +24,137 @@ Replace user, password, and dbname with your MySQL credentials and database name
|
||||
*/
|
||||
|
||||
func saveCharacterToDB(character *Character) error {
|
||||
// Use GORM to save the character and its relationships
|
||||
err := DB.Transaction(func(tx *gorm.DB) error {
|
||||
// Save the main character
|
||||
// Use GORM transaction to ensure atomicity
|
||||
return DB.Transaction(func(tx *gorm.DB) error {
|
||||
// Save the main character record
|
||||
if err := tx.Create(character).Error; err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to save character: %w", err)
|
||||
}
|
||||
|
||||
// Save Eigenschaften (Attributes)
|
||||
for i := range character.Eigenschaften {
|
||||
character.Eigenschaften[i].CharacterID = character.ID
|
||||
}
|
||||
if err := tx.Create(&character.Eigenschaften).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
/*
|
||||
// Save Eigenschaften (Attributes)
|
||||
for i := range character.Eigenschaften {
|
||||
character.Eigenschaften[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Eigenschaften) > 0 {
|
||||
if err := tx.Create(&character.Eigenschaften).Error; err != nil {
|
||||
return fmt.Errorf("failed to save eigenschaften: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Ausruestung (Equipment)
|
||||
for i := range character.Ausruestung {
|
||||
character.Ausruestung[i].CharacterID = character.ID
|
||||
}
|
||||
if err := tx.Create(&character.Ausruestung).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// Save Ausruestung (Equipment)
|
||||
for i := range character.Ausruestung {
|
||||
character.Ausruestung[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Ausruestung) > 0 {
|
||||
if err := tx.Create(&character.Ausruestung).Error; err != nil {
|
||||
return fmt.Errorf("failed to save ausruestung: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Fertigkeiten (Skills)
|
||||
for i := range character.Fertigkeiten {
|
||||
character.Fertigkeiten[i].CharacterID = character.ID
|
||||
}
|
||||
if err := tx.Create(&character.Fertigkeiten).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// Save Behaeltnisse (Containers)
|
||||
for i := range character.Behaeltnisse {
|
||||
character.Behaeltnisse[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Behaeltnisse) > 0 {
|
||||
if err := tx.Create(&character.Behaeltnisse).Error; err != nil {
|
||||
return fmt.Errorf("failed to save behaeltnisse: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Fertigkeiten (Skills)
|
||||
for i := range character.Fertigkeiten {
|
||||
character.Fertigkeiten[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Fertigkeiten) > 0 {
|
||||
if err := tx.Create(&character.Fertigkeiten).Error; err != nil {
|
||||
return fmt.Errorf("failed to save fertigkeiten: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Waffenfertigkeiten (Weapon Skills)
|
||||
for i := range character.Waffenfertigkeiten {
|
||||
character.Waffenfertigkeiten[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Waffenfertigkeiten) > 0 {
|
||||
if err := tx.Create(&character.Waffenfertigkeiten).Error; err != nil {
|
||||
return fmt.Errorf("failed to save waffenfertigkeiten: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Zauber (Spells)
|
||||
for i := range character.Zauber {
|
||||
character.Zauber[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Zauber) > 0 {
|
||||
if err := tx.Create(&character.Zauber).Error; err != nil {
|
||||
return fmt.Errorf("failed to save zauber: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Waffen (Weapons)
|
||||
for i := range character.Waffen {
|
||||
character.Waffen[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Waffen) > 0 {
|
||||
if err := tx.Create(&character.Waffen).Error; err != nil {
|
||||
return fmt.Errorf("failed to save waffen: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Merkmale (Characteristics)
|
||||
character.Merkmale.CharacterID = character.ID
|
||||
if err := tx.Create(&character.Merkmale).Error; err != nil {
|
||||
return fmt.Errorf("failed to save merkmale: %w", err)
|
||||
}
|
||||
|
||||
// Save Bennies
|
||||
character.Bennies.CharacterID = character.ID
|
||||
if err := tx.Create(&character.Bennies).Error; err != nil {
|
||||
return fmt.Errorf("failed to save bennies: %w", err)
|
||||
}
|
||||
|
||||
// Save Gestalt (Appearance)
|
||||
character.Gestalt.CharacterID = character.ID
|
||||
if err := tx.Create(&character.Gestalt).Error; err != nil {
|
||||
return fmt.Errorf("failed to save gestalt: %w", err)
|
||||
}
|
||||
|
||||
// Save Lp (Life Points)
|
||||
character.Lp.CharacterID = character.ID
|
||||
if err := tx.Create(&character.Lp).Error; err != nil {
|
||||
return fmt.Errorf("failed to save lp: %w", err)
|
||||
}
|
||||
|
||||
// Save Ap (Action Points)
|
||||
character.Ap.CharacterID = character.ID
|
||||
if err := tx.Create(&character.Ap).Error; err != nil {
|
||||
return fmt.Errorf("failed to save ap: %w", err)
|
||||
}
|
||||
|
||||
// Save B (Other Points)
|
||||
character.B.CharacterID = character.ID
|
||||
if err := tx.Create(&character.B).Error; err != nil {
|
||||
return fmt.Errorf("failed to save b: %w", err)
|
||||
}
|
||||
|
||||
// Save Transportmittel (Transportation)
|
||||
for i := range character.Transportmittel {
|
||||
character.Transportmittel[i].CharacterID = character.ID
|
||||
}
|
||||
if len(character.Transportmittel) > 0 {
|
||||
if err := tx.Create(&character.Transportmittel).Error; err != nil {
|
||||
return fmt.Errorf("failed to save transportmittel: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Save Erfahrungsschatz (Experience)
|
||||
character.Erfahrungsschatz.CharacterID = character.ID
|
||||
if err := tx.Create(&character.Erfahrungsschatz).Error; err != nil {
|
||||
return fmt.Errorf("failed to save erfahrungsschatz: %w", err)
|
||||
}
|
||||
*/
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -213,6 +213,7 @@ func UploadFiles(c *gin.Context) {
|
||||
|
||||
// Open and parse JSON
|
||||
var character Character
|
||||
filePath := fmt.Sprintf("./uploads/%s", file_vtt.Filename)
|
||||
fileContent, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read file"})
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func main() {
|
||||
ConnectDatabase()
|
||||
DB.AutoMigrate(&User{}, &Character{}, &Eigenschaften{}) // Add other models here
|
||||
DB.AutoMigrate(&User{}, &Character{}, &Eigenschaft{}) // Add other models here
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
|
||||
+164
-6
@@ -19,9 +19,25 @@ type Character struct {
|
||||
Gewicht int `json:"gewicht"`
|
||||
Glaube string `json:"glaube"`
|
||||
Hand string `json:"hand"`
|
||||
Eigenschaften []Eigenschaft `gorm:"foreignKey:CharacterID" json:"eigenschaften"`
|
||||
Ausruestung []Ausruestung `gorm:"foreignKey:CharacterID" json:"ausruestung"`
|
||||
Fertigkeiten []Fertigkeit `gorm:"foreignKey:CharacterID" json:"fertigkeiten"`
|
||||
Zauber []Zauber `gorm:"foreignKey:CharacterID" json:"zauber"`
|
||||
Lp Lp `gorm:"foreignKey:CharacterID" json:"lp"`
|
||||
Eigenschaften []Eigenschaft `gorm:"foreignKey:CharacterID" json:"eigenschaften"`
|
||||
Merkmale Merkmale `gorm:"foreignKey:CharacterID" json:"merkmale"`
|
||||
/*
|
||||
Ausruestung []Ausruestung `gorm:"foreignKey:CharacterID" json:"ausruestung"`
|
||||
Behaeltnisse []Behaeltniss `gorm:"foreignKey:CharacterID" json:"behaeltnisse"`
|
||||
Waffenfertigkeiten []Waffenfertigkeit `gorm:"foreignKey:CharacterID" json:"waffenfertigkeiten"`
|
||||
Waffen []Waffe `gorm:"foreignKey:CharacterID" json:"waffen"`
|
||||
Bennies Bennies `gorm:"foreignKey:CharacterID" json:"bennies"`
|
||||
Gestalt Gestalt `gorm:"foreignKey:CharacterID" json:"gestalt"`
|
||||
Ap Ap `gorm:"foreignKey:CharacterID" json:"ap"`
|
||||
B B `gorm:"foreignKey:CharacterID" json:"b"`
|
||||
Spezialisierung []string `json:"spezialisierung"`
|
||||
Image string `json:"image,omitempty"`
|
||||
Transportmittel []Transportmittel `gorm:"foreignKey:CharacterID" json:"transportmittel"`
|
||||
Erfahrungsschatz Erfahrungsschatz `gorm:"foreignKey:CharacterID" json:"erfahrungsschatz"`
|
||||
*/
|
||||
}
|
||||
|
||||
type Eigenschaft struct {
|
||||
@@ -35,18 +51,160 @@ type Ausruestung struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Name string `json:"name"`
|
||||
Anzahl int `json:"anzahl"`
|
||||
Gewicht float64 `json:"gewicht"`
|
||||
Wert float64 `json:"wert"`
|
||||
Beschreibung string `json:"beschreibung"`
|
||||
Anzahl int `json:"anzahl"`
|
||||
BeinhaltetIn *string `json:"beinhaltet_in"`
|
||||
Bonus int `json:"bonus,omitempty"`
|
||||
Gewicht float64 `json:"gewicht"`
|
||||
Magisch Magisch `gorm:"polymorphic:Item;polymorphicValue:Ausruestung" json:"magisch"`
|
||||
Wert float64 `json:"wert"`
|
||||
}
|
||||
|
||||
type Fertigkeit struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Name string `json:"name"`
|
||||
Fertigkeitswert int `json:"fertigkeitswert"`
|
||||
Beschreibung string `json:"beschreibung"`
|
||||
Fertigkeitswert int `json:"fertigkeitswert"`
|
||||
Bonus int `json:"bonus,omitempty"`
|
||||
Pp int `json:"pp,omitempty"`
|
||||
Quelle string `json:"quelle"`
|
||||
}
|
||||
|
||||
type Zauber struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Name string `json:"name"`
|
||||
Beschreibung string `json:"beschreibung"`
|
||||
Bonus int `json:"bonus"`
|
||||
Quelle string `json:"quelle"`
|
||||
}
|
||||
|
||||
type Waffenfertigkeit struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Name string `json:"name"`
|
||||
Beschreibung string `json:"beschreibung"`
|
||||
Bonus int `json:"bonus"`
|
||||
Fertigkeitswert int `json:"fertigkeitswert"`
|
||||
Pp int `json:"pp"`
|
||||
Quelle string `json:"quelle"`
|
||||
}
|
||||
|
||||
type Waffe struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Name string `json:"name"`
|
||||
Beschreibung string `json:"beschreibung"`
|
||||
Abwb int `json:"abwb"`
|
||||
Anb int `json:"anb"`
|
||||
Anzahl int `json:"anzahl"`
|
||||
BeinhaltetIn *string `json:"beinhaltet_in"`
|
||||
Gewicht float64 `json:"gewicht"`
|
||||
Magisch Magisch `gorm:"polymorphic:Item;polymorphicValue:Ausruestung" json:"magisch"`
|
||||
NameFuerSpezialisierung string `json:"nameFuerSpezialisierung"`
|
||||
Schb int `json:"schb"`
|
||||
Wert float64 `json:"wert"`
|
||||
}
|
||||
|
||||
type Merkmale struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Augenfarbe string `json:"augenfarbe"`
|
||||
Haarfarbe string `json:"haarfarbe"`
|
||||
Sonstige string `json:"sonstige"`
|
||||
}
|
||||
|
||||
type Lp struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Max int `json:"max"`
|
||||
Value int `json:"value"`
|
||||
}
|
||||
|
||||
type Gestalt struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Breite string `json:"breite"`
|
||||
Groesse string `json:"groesse"`
|
||||
}
|
||||
|
||||
type Erfahrungsschatz struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Value int `json:"value"`
|
||||
}
|
||||
|
||||
/*
|
||||
type Eigenschaften struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Au int `json:"au"`
|
||||
Gs int `json:"gs"`
|
||||
Gw int `json:"gw"`
|
||||
In int `json:"in"`
|
||||
Ko int `json:"ko"`
|
||||
Pa int `json:"pa"`
|
||||
St int `json:"st"`
|
||||
Wk int `json:"wk"`
|
||||
Zt int `json:"zt"`
|
||||
}
|
||||
*/
|
||||
|
||||
type Bennies struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Gg int `json:"gg"`
|
||||
Gp int `json:"gp"`
|
||||
Sg int `json:"sg"`
|
||||
}
|
||||
|
||||
type Behaeltniss struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Name string `json:"name"`
|
||||
Beschreibung string `json:"beschreibung"`
|
||||
BeinhaltetIn any `json:"beinhaltet_in"`
|
||||
Gewicht float64 `json:"gewicht"`
|
||||
Magisch Magisch `gorm:"polymorphic:Item;polymorphicValue:Ausruestung" json:"magisch"`
|
||||
Tragkraft float64 `json:"tragkraft"`
|
||||
Volumen float64 `json:"volumen"`
|
||||
Wert float64 `json:"wert"`
|
||||
}
|
||||
|
||||
type Ap struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Max int `json:"max"`
|
||||
Value int `json:"value"`
|
||||
}
|
||||
|
||||
type B struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Max int `json:"max"`
|
||||
Value int `json:"value"`
|
||||
}
|
||||
|
||||
type Transportmittel struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
CharacterID uint `gorm:"index" json:"character_id"`
|
||||
Name string `json:"name"`
|
||||
Beschreibung string `json:"beschreibung"`
|
||||
BeinhaltetIn any `json:"beinhaltet_in"`
|
||||
Gewicht int `json:"gewicht"`
|
||||
Tragkraft float64 `json:"tragkraft"`
|
||||
Wert float64 `json:"wert"`
|
||||
Magisch Magisch `gorm:"polymorphic:Item;polymorphicValue:Ausruestung" json:"magisch"`
|
||||
}
|
||||
|
||||
type Magisch struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
ItemType string `gorm:"index" json:"item_type"` // Type of the referenced item (e.g., "Ausruestung")
|
||||
ItemID uint `gorm:"index" json:"item_id"` // ID of the referenced item
|
||||
Abw int `json:"abw"`
|
||||
Ausgebrannt bool `json:"ausgebrannt"`
|
||||
IstMagisch bool `json:"ist_magisch"`
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// Auto-migrate the schemas for all related models
|
||||
db.AutoMigrate(&Character{}, &Fertigkeit{}, &Zauber{}, &Lp{}, &Eigenschaft{}, &Merkmale{}) //, &Ausruestung{}, &Behaeltniss{}, &Waffenfertigkeit{},
|
||||
// &Waffe{}, &Ap{}, &B{}, &Transportmittel{}, &Erfahrungsschatz{}, &Magisch{})
|
||||
return db
|
||||
}
|
||||
|
||||
func TestSaveCharacterToDB(t *testing.T) {
|
||||
// Setup test database
|
||||
testDB := SetupTestDB()
|
||||
DB = testDB // Assign test DB to global DB
|
||||
|
||||
// Define a sample character for testing
|
||||
character := &Character{
|
||||
Name: "Test Character",
|
||||
Rasse: "Elf",
|
||||
Typ: "Mage",
|
||||
Alter: 100,
|
||||
Anrede: "Lord",
|
||||
Grad: 5,
|
||||
Groesse: 180,
|
||||
Gewicht: 70,
|
||||
Glaube: "None",
|
||||
Hand: "Right",
|
||||
Eigenschaften: []Eigenschaft{
|
||||
{Name: "Au", Value: 50},
|
||||
{Name: "St", Value: 80},
|
||||
{Name: "Zt", Value: 100},
|
||||
},
|
||||
/*
|
||||
Ausruestung: []Ausruestung{
|
||||
{Name: "Staff", Beschreibung: "Magic Staff", Anzahl: 1, Gewicht: 2.5, Wert: 500},
|
||||
},
|
||||
Behaeltnisse: []Behaeltniss{
|
||||
{Name: "Backpack", Beschreibung: "Leather backpack", Gewicht: 1.5, Tragkraft: 10, Volumen: 20, Wert: 50},
|
||||
},
|
||||
*/
|
||||
Fertigkeiten: []Fertigkeit{
|
||||
{Name: "Stehlen", Beschreibung: "jemandem etwas wegnehmen ohne das der es merkt", Fertigkeitswert: 6},
|
||||
{Name: "Geländelauf", Beschreibung: "Lauf um Hindernisse herum", Fertigkeitswert: 12},
|
||||
},
|
||||
Zauber: []Zauber{
|
||||
{Name: "Fireball", Beschreibung: "Cast a fireball", Bonus: 0, Quelle: "Ark 20"},
|
||||
},
|
||||
Lp: Lp{
|
||||
Max: 100,
|
||||
Value: 80,
|
||||
},
|
||||
Merkmale: Merkmale{
|
||||
Augenfarbe: "Blau",
|
||||
Haarfarbe: "Blonde",
|
||||
Sonstige: "Scar on the left cheek",
|
||||
},
|
||||
/*
|
||||
Ap: Ap{
|
||||
Max: 50,
|
||||
Value: 40,
|
||||
},
|
||||
*/
|
||||
}
|
||||
|
||||
fmt.Println(character)
|
||||
|
||||
// Call the function being tested
|
||||
err := saveCharacterToDB(character)
|
||||
assert.NoError(t, err, "Expected no error when saving character to DB")
|
||||
fmt.Println(character)
|
||||
|
||||
// Verify that the character was saved
|
||||
var savedCharacter Character
|
||||
//err = DB.Preload("Eigenschaften").Preload("Ausruestung").Preload("Behaeltnisse").
|
||||
// Preload("Fertigkeiten").Preload("Merkmale").Preload("Lp").Preload("Ap").
|
||||
err = DB.Preload("Fertigkeiten").Preload("Zauber").Preload("Lp").
|
||||
Preload("Eigenschaften").Preload("Merkmale").
|
||||
First(&savedCharacter, "name = ?", "Test Character").Error
|
||||
assert.NoError(t, err, "Expected to find the character in the database")
|
||||
assert.Equal(t, "Test Character", savedCharacter.Name)
|
||||
assert.Equal(t, "Elf", savedCharacter.Rasse)
|
||||
assert.Equal(t, "Stehlen", savedCharacter.Fertigkeiten[0].Name)
|
||||
assert.Equal(t, "Fireball", savedCharacter.Zauber[0].Name)
|
||||
assert.Equal(t, 80, savedCharacter.Lp.Value)
|
||||
assert.Equal(t, 3, len(savedCharacter.Eigenschaften))
|
||||
assert.Equal(t, "Au", savedCharacter.Eigenschaften[0].Name)
|
||||
assert.Equal(t, 50, savedCharacter.Eigenschaften[0].Value)
|
||||
assert.Equal(t, "Blau", savedCharacter.Merkmale.Augenfarbe)
|
||||
/*
|
||||
assert.Equal(t, 1, len(savedCharacter.Ausruestung))
|
||||
assert.Equal(t, "Staff", savedCharacter.Ausruestung[0].Name)
|
||||
assert.Equal(t, "Blue", savedCharacter.Merkmale.Augenfarbe)
|
||||
assert.Equal(t, 40, savedCharacter.Ap.Value)
|
||||
*/
|
||||
}
|
||||
@@ -6,6 +6,9 @@ go get -u github.com/gin-gonic/gin
|
||||
go get -u gorm.io/gorm
|
||||
go get -u gorm.io/driver/mysql
|
||||
go get github.com/gin-contrib/cors
|
||||
go get -u github.com/stretchr/testify
|
||||
go get -u github.com/stretchr/testify/assert
|
||||
go get -u gorm.io/driver/sqlite
|
||||
|
||||
|
||||
cd bamort
|
||||
|
||||
Reference in New Issue
Block a user