Test importing the json

This commit is contained in:
2024-12-24 23:38:53 +01:00
parent eb1d2ba5fc
commit bff31d0ebe
2 changed files with 89 additions and 8 deletions
+44
View File
@@ -0,0 +1,44 @@
package main
import (
"encoding/json"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestImportJSONToDB(t *testing.T) {
// Setup test database
//testDB := SetupTestDB()
//DB = testDB // Assign test DB to global DB
fileName := fmt.Sprintf("./uploads/%s", "test.json")
fileContent, err := os.ReadFile(fileName)
assert.NoError(t, err, "Expected no error when reading file "+fileName)
character := CharacterImport{}
err = json.Unmarshal(fileContent, &character)
assert.NoError(t, err, "Expected no error when Unmarshal filecontent")
assert.Equal(t, "Harsk Hammerhuter, Zen", character.Name)
assert.Equal(t, "Zwerg", character.Rasse)
assert.Equal(t, "Hören", character.Fertigkeiten[0].Name)
assert.Equal(t, 0, len(character.Zauber))
assert.Equal(t, 17, character.Lp.Value)
assert.Equal(t, 96, character.Eigenschaften.Gs)
assert.Equal(t, 74, character.Eigenschaften.Au)
assert.Equal(t, 21, len(character.Ausruestung))
assert.Equal(t, "Lederrüstung", character.Ausruestung[0].Name)
assert.Equal(t, "blau", character.Merkmale.Augenfarbe)
assert.Equal(t, "Lederrucksack", character.Behaeltnisse[0].Name)
assert.Equal(t, "Armbrust:schwer", character.Waffen[0].Name)
assert.Equal(t, 31, character.Ap.Value)
assert.Equal(t, "Armbrüste", character.Waffenfertigkeiten[0].Name)
assert.Equal(t, 3, len(character.Spezialisierung))
assert.Equal(t, "Kriegshammer", character.Spezialisierung[0])
assert.Equal(t, "Armbrust:schwer", character.Spezialisierung[1])
/*
*/
}
+45 -8
View File
@@ -8,7 +8,8 @@ type User struct {
}
type Character struct {
ID uint `gorm:"primaryKey" json:"id"`
ID uint `gorm:"primaryKey" json:"dbid"`
ImportID string `json:"id"`
Name string `json:"name"`
Rasse string `json:"rasse"`
Typ string `json:"typ"`
@@ -48,7 +49,8 @@ type Eigenschaft struct {
}
type Ausruestung struct {
ID uint `gorm:"primaryKey"`
ID uint `gorm:"primaryKey"json:"dbid"`
ImportID string `json:"id"`
CharacterID uint `gorm:"index" json:"character_id"`
Name string `json:"name"`
Beschreibung string `json:"beschreibung"`
@@ -61,7 +63,8 @@ type Ausruestung struct {
}
type Fertigkeit struct {
ID uint `gorm:"primaryKey"`
ID uint `gorm:"primaryKey"json:"dbid"`
ImportID string `json:"id"`
CharacterID uint `gorm:"index" json:"character_id"`
Name string `json:"name"`
Beschreibung string `json:"beschreibung"`
@@ -81,7 +84,8 @@ type Zauber struct {
}
type Waffenfertigkeit struct {
ID uint `gorm:"primaryKey"`
ID uint `gorm:"primaryKey"json:"dbid"`
ImportID string `json:"id"`
CharacterID uint `gorm:"index" json:"character_id"`
Name string `json:"name"`
Beschreibung string `json:"beschreibung"`
@@ -92,7 +96,8 @@ type Waffenfertigkeit struct {
}
type Waffe struct {
ID uint `gorm:"primaryKey"`
ID uint `gorm:"primaryKey"json:"dbid"`
ImportID string `json:"id"`
CharacterID uint `gorm:"index" json:"character_id"`
Name string `json:"name"`
Beschreibung string `json:"beschreibung"`
@@ -135,7 +140,6 @@ type Erfahrungsschatz struct {
Value int `json:"value"`
}
/*
type Eigenschaften struct {
ID uint `gorm:"primaryKey"`
CharacterID uint `gorm:"index" json:"character_id"`
@@ -149,7 +153,6 @@ type Eigenschaften struct {
Wk int `json:"wk"`
Zt int `json:"zt"`
}
*/
type Bennies struct {
ID uint `gorm:"primaryKey"`
@@ -160,7 +163,8 @@ type Bennies struct {
}
type Behaeltniss struct {
ID uint `gorm:"primaryKey"`
ID uint `gorm:"primaryKey"json:"dbid"`
ImportID string `json:"id"`
CharacterID uint `gorm:"index" json:"character_id"`
Name string `json:"name"`
Beschreibung string `json:"beschreibung"`
@@ -243,3 +247,36 @@ type MagischTransport struct {
Define models for each table
Add other models for Ausruestung, Fertigkeiten, etc., following the same pattern.
*/
type CharacterImport struct {
ID uint `gorm:"primaryKey" json:"dbid"`
ImportID string `json:"id"`
Name string `json:"name"`
Rasse string `json:"rasse"`
Typ string `json:"typ"`
Alter int `json:"alter"`
Anrede string `json:"anrede"`
Grad int `json:"grad"`
Groesse int `json:"groesse"`
Gewicht int `json:"gewicht"`
Glaube string `json:"glaube"`
Hand string `json:"hand"`
Fertigkeiten []Fertigkeit `gorm:"foreignKey:CharacterID" json:"fertigkeiten"`
Zauber []Zauber `gorm:"foreignKey:CharacterID" json:"zauber"`
Lp Lp `gorm:"foreignKey:CharacterID" json:"lp"`
Eigenschaften Eigenschaften `gorm:"foreignKey:CharacterID" json:"eigenschaften"`
Merkmale Merkmale `gorm:"foreignKey:CharacterID" json:"merkmale"`
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"`
Erfahrungsschatz Erfahrungsschatz `gorm:"foreignKey:CharacterID" json:"erfahrungsschatz"`
Transportmittel []Transportation `gorm:"foreignKey:CharacterID" json:"transportmittel"`
Ausruestung []Ausruestung `gorm:"foreignKey:CharacterID" json:"ausruestung"`
Behaeltnisse []Behaeltniss `gorm:"foreignKey:CharacterID" json:"behaeltnisse"`
Waffen []Waffe `gorm:"foreignKey:CharacterID" json:"waffen"`
Waffenfertigkeiten []Waffenfertigkeit `gorm:"foreignKey:CharacterID" json:"waffenfertigkeiten"`
Spezialisierung StringArray `gorm:"type:TEXT" json:"spezialisierung"`
/*
Image string `json:"image,omitempty"`
*/
}