Bugfixes GetMiscLookupByKey
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
* API endpunkte für Export/Import aus Commit 2dcb4e00faaf316b98eb28e83cc5137bf0d1385d
|
||||
* wouldn't it be a good idea to remove the GameSystem from all the records and define it in a kind of manifest. The values in the manifest could be applied to all records (where needed) during the import session. export_import.go
|
||||
* maintanance view for gsm_cc_class_category_points
|
||||
|
||||
## Refaktor
|
||||
|
||||
* Export Import Module neu grupieren
|
||||
@@ -51,7 +52,12 @@
|
||||
* Middleware erstellen
|
||||
* Regelmechanik
|
||||
|
||||
### Daten
|
||||
* Datenpflege Heimat
|
||||
* Datenpflege Skills
|
||||
* Datenpflege Spells
|
||||
|
||||
# Deployment
|
||||
|
||||
* depoyment verbessern, Konfiguration ausschließlich über .env file oder datenbank
|
||||
* deployment automatisieren
|
||||
@@ -21,11 +21,13 @@ func TestGetCharacterClassLearningPoints(t *testing.T) {
|
||||
t.Fatalf("Failed to migrate structures: %v", err)
|
||||
}
|
||||
|
||||
// Populate test data (character classes and learning points)
|
||||
if err := models.PopulateClassLearningPointsData(); err != nil {
|
||||
t.Logf("Warning: Failed to populate learning points data: %v", err)
|
||||
// Continue anyway - some tests may still work
|
||||
}
|
||||
/*
|
||||
// Populate test data (character classes and learning points)
|
||||
if err := models.PopulateClassLearningPointsData(); err != nil {
|
||||
t.Logf("Warning: Failed to populate learning points data: %v", err)
|
||||
// Continue anyway - some tests may still work
|
||||
}
|
||||
*/
|
||||
|
||||
// Setup Gin in test mode
|
||||
gin.SetMode(gin.TestMode)
|
||||
@@ -227,10 +229,12 @@ func TestGetLearningPointsForClass(t *testing.T) {
|
||||
t.Fatalf("Failed to migrate structures: %v", err)
|
||||
}
|
||||
|
||||
// Populate test data
|
||||
if err := models.PopulateClassLearningPointsData(); err != nil {
|
||||
t.Logf("Warning: Failed to populate learning points data: %v", err)
|
||||
}
|
||||
/*
|
||||
// Populate test data
|
||||
if err := models.PopulateClassLearningPointsData(); err != nil {
|
||||
t.Logf("Warning: Failed to populate learning points data: %v", err)
|
||||
}
|
||||
*/
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -403,10 +407,12 @@ func TestAllCharacterClassesAreDefined(t *testing.T) {
|
||||
t.Fatalf("Failed to migrate structures: %v", err)
|
||||
}
|
||||
|
||||
// Populate test data
|
||||
if err := models.PopulateClassLearningPointsData(); err != nil {
|
||||
t.Logf("Warning: Failed to populate learning points data: %v", err)
|
||||
}
|
||||
/*
|
||||
// Populate test data
|
||||
if err := models.PopulateClassLearningPointsData(); err != nil {
|
||||
t.Logf("Warning: Failed to populate learning points data: %v", err)
|
||||
}
|
||||
*/
|
||||
|
||||
expectedClasses := []string{
|
||||
"Assassine", "Barbar", "Glücksritter", "Händler", "Krieger", "Spitzbube", "Waldläufer",
|
||||
|
||||
@@ -8,9 +8,29 @@ import (
|
||||
)
|
||||
|
||||
// GetMiscLookupByKey retrieves all values for a given key
|
||||
func GetMiscLookupByKey(key string) ([]models.MiscLookup, error) {
|
||||
// Optional order parameter can be: "id", "value", "source", "source_value"
|
||||
// Default is "value" if not specified or invalid
|
||||
func GetMiscLookupByKey(key string, order ...string) ([]models.MiscLookup, error) {
|
||||
var items []models.MiscLookup
|
||||
err := database.DB.Where("`key` = ?", key).Order("value ASC").Find(&items).Error
|
||||
|
||||
// Determine ordering
|
||||
orderBy := "value ASC"
|
||||
if len(order) > 0 && order[0] != "" {
|
||||
switch order[0] {
|
||||
case "id":
|
||||
orderBy = "id ASC"
|
||||
case "value":
|
||||
orderBy = "value ASC"
|
||||
case "source":
|
||||
orderBy = "source_id ASC, value ASC"
|
||||
case "source_value":
|
||||
orderBy = "source_id ASC, value ASC"
|
||||
default:
|
||||
orderBy = "value ASC"
|
||||
}
|
||||
}
|
||||
|
||||
err := database.DB.Where("`key` = ?", key).Order(orderBy).Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
|
||||
@@ -19,30 +19,40 @@ func TestMiscLookup_CreateAndRetrieve(t *testing.T) {
|
||||
err := models.MigrateStructure()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create test data
|
||||
testData := []models.MiscLookup{
|
||||
{Key: "gender", Value: "männlich"},
|
||||
{Key: "gender", Value: "weiblich"},
|
||||
{Key: "races", Value: "Mensch"},
|
||||
{Key: "races", Value: "Elf"},
|
||||
}
|
||||
/*
|
||||
// Create test data
|
||||
testData := []models.MiscLookup{
|
||||
{Key: "gender", Value: "männlich"},
|
||||
{Key: "gender", Value: "weiblich"},
|
||||
{Key: "races", Value: "Mensch"},
|
||||
{Key: "races", Value: "Elf"},
|
||||
}
|
||||
|
||||
// Insert test data
|
||||
for _, item := range testData {
|
||||
err := database.DB.Create(&item).Error
|
||||
require.NoError(t, err)
|
||||
}
|
||||
// Insert test data
|
||||
for _, item := range testData {
|
||||
err := database.DB.Create(&item).Error
|
||||
require.NoError(t, err)
|
||||
}
|
||||
*/
|
||||
|
||||
// Retrieve by key
|
||||
// Retrieve by key - sorted alphabetically by value (default)
|
||||
genders, err := GetMiscLookupByKey("gender")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, genders, 2)
|
||||
assert.Len(t, genders, 3)
|
||||
assert.Equal(t, "divers", genders[0].Value)
|
||||
assert.Equal(t, "männlich", genders[1].Value)
|
||||
assert.Equal(t, "weiblich", genders[2].Value)
|
||||
|
||||
genders, err = GetMiscLookupByKey("gender", "id")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, genders, 3)
|
||||
assert.Equal(t, "männlich", genders[0].Value)
|
||||
assert.Equal(t, "weiblich", genders[1].Value)
|
||||
assert.Equal(t, "divers", genders[2].Value)
|
||||
|
||||
races, err := GetMiscLookupByKey("races")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, races, 2)
|
||||
assert.Len(t, races, 5)
|
||||
}
|
||||
|
||||
func TestGetMiscLookupByKey_NotFound(t *testing.T) {
|
||||
@@ -170,3 +180,75 @@ func TestGetSocialClassBonusPoints(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, bonuses)
|
||||
}
|
||||
|
||||
func TestGetMiscLookupByKey_OrderParameter(t *testing.T) {
|
||||
database.SetupTestDB()
|
||||
err := models.MigrateStructure()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create test data with different IDs and sources
|
||||
testData := []models.MiscLookup{
|
||||
{Key: "test_order", Value: "Zebra", SourceID: 2},
|
||||
{Key: "test_order", Value: "Alpha", SourceID: 1},
|
||||
{Key: "test_order", Value: "Beta", SourceID: 1},
|
||||
}
|
||||
|
||||
for _, item := range testData {
|
||||
err := database.DB.Create(&item).Error
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Test default ordering (by value)
|
||||
items, err := GetMiscLookupByKey("test_order")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 3)
|
||||
assert.Equal(t, "Alpha", items[0].Value)
|
||||
assert.Equal(t, "Beta", items[1].Value)
|
||||
assert.Equal(t, "Zebra", items[2].Value)
|
||||
|
||||
// Test explicit value ordering
|
||||
items, err = GetMiscLookupByKey("test_order", "value")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 3)
|
||||
assert.Equal(t, "Alpha", items[0].Value)
|
||||
|
||||
// Test ID ordering
|
||||
items, err = GetMiscLookupByKey("test_order", "id")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 3)
|
||||
// Should be ordered by ID (creation order)
|
||||
assert.Equal(t, "Zebra", items[0].Value)
|
||||
assert.Equal(t, "Alpha", items[1].Value)
|
||||
assert.Equal(t, "Beta", items[2].Value)
|
||||
|
||||
// Test source ordering
|
||||
items, err = GetMiscLookupByKey("test_order", "source")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 3)
|
||||
// Should be ordered by source_id first, then value
|
||||
assert.Equal(t, uint(1), items[0].SourceID)
|
||||
assert.Equal(t, "Alpha", items[0].Value)
|
||||
assert.Equal(t, uint(1), items[1].SourceID)
|
||||
assert.Equal(t, "Beta", items[1].Value)
|
||||
assert.Equal(t, uint(2), items[2].SourceID)
|
||||
assert.Equal(t, "Zebra", items[2].Value)
|
||||
|
||||
// Test source_value ordering (same as source)
|
||||
items, err = GetMiscLookupByKey("test_order", "source_value")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 3)
|
||||
assert.Equal(t, uint(1), items[0].SourceID)
|
||||
assert.Equal(t, "Alpha", items[0].Value)
|
||||
|
||||
// Test invalid ordering (should default to value)
|
||||
items, err = GetMiscLookupByKey("test_order", "invalid")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 3)
|
||||
assert.Equal(t, "Alpha", items[0].Value)
|
||||
|
||||
// Test empty string ordering (should default to value)
|
||||
items, err = GetMiscLookupByKey("test_order", "")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 3)
|
||||
assert.Equal(t, "Alpha", items[0].Value)
|
||||
}
|
||||
|
||||
@@ -566,6 +566,7 @@ func SetupCheckDev(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Setup Check OK"})
|
||||
}
|
||||
|
||||
/*
|
||||
// PopulateClassLearningPoints populates the class learning points tables from hardcoded data
|
||||
func PopulateClassLearningPoints(c *gin.Context) {
|
||||
logger.Info("Starte Population der Class Learning Points Daten...")
|
||||
@@ -580,6 +581,7 @@ func PopulateClassLearningPoints(c *gin.Context) {
|
||||
logger.Info("Class Learning Points erfolgreich populiert")
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Class learning points data populated successfully"})
|
||||
}
|
||||
*/
|
||||
|
||||
func ReconnectDataBase(c *gin.Context) {
|
||||
logger.Info("Führe Datenbank-Reconnect durch...")
|
||||
|
||||
@@ -15,8 +15,8 @@ func RegisterRoutes(r *gin.RouterGroup) {
|
||||
charGrp.GET("/mktestdata", MakeTestdataFromLive)
|
||||
charGrp.GET("/reconndb", ReconnectDataBase) // Datenbank neu verbinden
|
||||
charGrp.GET("/reloadenv", ReloadENV)
|
||||
charGrp.POST("/transfer-sqlite-to-mariadb", TransferSQLiteToMariaDB) // Transfer data from SQLite to MariaDB
|
||||
charGrp.POST("/populate-class-learning-points", PopulateClassLearningPoints) // Populate class learning points from hardcoded data
|
||||
charGrp.POST("/transfer-sqlite-to-mariadb", TransferSQLiteToMariaDB) // Transfer data from SQLite to MariaDB
|
||||
//charGrp.POST("/populate-class-learning-points", PopulateClassLearningPoints) // Populate class learning points from hardcoded data
|
||||
/*
|
||||
//nur zur einmaligen Ausführung, um das Lernkosten-System zu initialisieren
|
||||
charGrp.POST("/initialize-learning-costs", InitializeLearningCosts)
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"bamort/database"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
/*
|
||||
// PopulateClassLearningPointsData inserts all hardcoded learning points data into the database
|
||||
func PopulateClassLearningPointsData() error {
|
||||
// Define all character class learning data
|
||||
@@ -408,3 +404,4 @@ func PopulateClassLearningPointsData() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user