yao/seed/seed_test.go
Max 8b9cd9f951 Add seed file system support and deprecate legacy registrations
- Introduced a new read-only file system for seed data, enhancing initial data seeding capabilities.
- Updated the file system registration to include the seed root and clarified comments regarding the use of app, data, system, DSL, and script registrations, marking them for future deprecation.
- Improved code clarity and maintainability by restructuring file system registrations.
2025-10-20 10:34:52 +08:00

324 lines
9.1 KiB
Go

package seed
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/model"
"github.com/yaoapp/gou/process"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/test"
)
// TestSeedImportCSV tests importing roles from CSV file
func TestSeedImportCSV(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// Import CSV
p := process.New("seeds.import", "roles.csv", "__yao.role")
result := p.Run()
// Verify result
assert.NotNil(t, result)
resultMap, ok := result.(*ImportResult)
assert.True(t, ok, "Result should be ImportResult")
assert.Greater(t, resultMap.Total, 0, "Should import at least 1 record")
assert.Greater(t, resultMap.Success, 0, "Should have successful imports")
assert.Equal(t, resultMap.Total, resultMap.Success+resultMap.Failure+resultMap.Ignore,
"Total should equal sum of success, failure, and ignore")
// Verify data in database
roles, err := mod.Get(model.QueryParam{})
assert.Nil(t, err)
assert.Greater(t, len(roles), 0, "Should have roles in database")
// Check specific role
adminRoles, _ := mod.Get(model.QueryParam{
Wheres: []model.QueryWhere{
{Column: "role_id", Value: "admin"},
},
})
if len(adminRoles) > 0 {
assert.Equal(t, "admin", adminRoles[0].Get("role_id"))
assert.Equal(t, "Administrator", adminRoles[0].Get("name"))
}
}
// TestSeedImportJSON tests importing roles from JSON file
func TestSeedImportJSON(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// Import JSON
p := process.New("seeds.import", "roles.json", "__yao.role")
result := p.Run()
// Verify result
assert.NotNil(t, result)
resultMap, ok := result.(*ImportResult)
assert.True(t, ok, "Result should be ImportResult")
assert.Greater(t, resultMap.Total, 0, "Should import at least 1 record")
assert.Greater(t, resultMap.Success, 0, "Should have successful imports")
// Verify data in database
roles, err := mod.Get(model.QueryParam{})
assert.Nil(t, err)
assert.Greater(t, len(roles), 0, "Should have roles in database")
// Check that permissions JSON was imported correctly
adminRoles, _ := mod.Get(model.QueryParam{
Wheres: []model.QueryWhere{
{Column: "role_id", Value: "admin"},
},
})
if len(adminRoles) > 0 {
assert.Equal(t, "admin", adminRoles[0].Get("role_id"))
assert.NotNil(t, adminRoles[0].Get("permissions"), "Should have permissions")
}
}
// TestSeedImportXLSX tests importing roles from XLSX file
func TestSeedImportXLSX(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// Import XLSX file
p := process.New("seeds.import", "roles.xlsx", "__yao.role")
result := p.Run()
// Verify result
assert.NotNil(t, result)
resultMap, ok := result.(*ImportResult)
assert.True(t, ok, "Result should be ImportResult")
assert.Greater(t, resultMap.Total, 0, "Should import at least 1 record")
assert.Greater(t, resultMap.Success, 0, "Should have successful imports")
// Verify data in database
roles, err := mod.Get(model.QueryParam{})
assert.Nil(t, err)
assert.Greater(t, len(roles), 0, "Should have roles in database")
// Check specific role
adminRoles, _ := mod.Get(model.QueryParam{
Wheres: []model.QueryWhere{
{Column: "role_id", Value: "admin"},
},
})
if len(adminRoles) > 0 {
assert.Equal(t, "admin", adminRoles[0].Get("role_id"))
assert.Equal(t, "Administrator", adminRoles[0].Get("name"))
}
}
// TestSeedImportYao tests importing roles from Yao file (JSONC)
func TestSeedImportYao(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// Import Yao file
p := process.New("seeds.import", "roles.yao", "__yao.role")
result := p.Run()
// Verify result
assert.NotNil(t, result)
resultMap, ok := result.(*ImportResult)
assert.True(t, ok, "Result should be ImportResult")
assert.Greater(t, resultMap.Total, 0, "Should import at least 1 record")
assert.Greater(t, resultMap.Success, 0, "Should have successful imports")
// Verify data in database
roles, err := mod.Get(model.QueryParam{})
assert.Nil(t, err)
assert.Greater(t, len(roles), 0, "Should have roles in database")
}
// TestSeedImportWithOptions tests importing with custom options
func TestSeedImportWithOptions(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// Import with batch mode
p := process.New("seeds.import", "roles.json", "__yao.role", map[string]interface{}{
"chunk_size": 2,
"duplicate": "ignore",
"mode": "batch",
})
result := p.Run()
// Verify result
assert.NotNil(t, result)
resultMap, ok := result.(*ImportResult)
assert.True(t, ok)
assert.Greater(t, resultMap.Success, 0)
// Try importing again with ignore strategy
p2 := process.New("seeds.import", "roles.json", "__yao.role", map[string]interface{}{
"duplicate": "ignore",
})
result2 := p2.Run()
resultMap2, ok := result2.(*ImportResult)
assert.True(t, ok)
// With ignore strategy, duplicates should be ignored
assert.Greater(t, resultMap2.Ignore, 0, "Should have ignored duplicates")
}
// TestSeedImportEachMode tests importing with each mode (single record)
func TestSeedImportEachMode(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// Import with each mode
p := process.New("seeds.import", "roles.json", "__yao.role", map[string]interface{}{
"mode": "each",
"duplicate": "ignore",
})
result := p.Run()
// Verify result
assert.NotNil(t, result)
resultMap, ok := result.(*ImportResult)
assert.True(t, ok)
assert.Greater(t, resultMap.Success, 0)
// Verify data in database
roles, err := mod.Get(model.QueryParam{})
assert.Nil(t, err)
assert.Greater(t, len(roles), 0)
}
// TestSeedImportDuplicateIgnore tests importing with ignore duplicate strategy
func TestSeedImportDuplicateIgnore(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// First import
p1 := process.New("seeds.import", "roles.json", "__yao.role")
result1 := p1.Run()
resultMap1, ok := result1.(*ImportResult)
assert.True(t, ok)
firstSuccess := resultMap1.Success
// Get the IDs of imported records
roles1, err := mod.Get(model.QueryParam{
Select: []interface{}{"id", "role_id"},
})
assert.Nil(t, err)
assert.Greater(t, len(roles1), 0)
// Second import with ignore mode
p2 := process.New("seeds.import", "roles.json", "__yao.role", map[string]interface{}{
"mode": "each",
"duplicate": "ignore",
})
result2 := p2.Run()
// Verify result
assert.NotNil(t, result2)
resultMap2, ok := result2.(*ImportResult)
assert.True(t, ok)
// With ignore strategy, duplicates should be ignored
assert.Greater(t, resultMap2.Ignore, 0, "Should have ignored duplicates")
// Verify count hasn't changed (ignored duplicates, not created new)
roles2, err := mod.Get(model.QueryParam{})
assert.Nil(t, err)
assert.Equal(t, firstSuccess, len(roles2), "Should have same number of roles after re-import")
}
// TestSeedImportChunkSize tests chunk processing
func TestSeedImportChunkSize(t *testing.T) {
test.Prepare(t, config.Conf)
defer test.Clean()
// Ensure __yao.role model exists
if !model.Exists("__yao.role") {
t.Skip("__yao.role model not loaded, skipping test")
}
// Clear existing roles
mod := model.Select("__yao.role")
_, _ = mod.DestroyWhere(model.QueryParam{})
// Import with small chunk size
p := process.New("seeds.import", "roles.json", "__yao.role", map[string]interface{}{
"chunk_size": 1, // Process one record at a time
"mode": "batch",
})
result := p.Run()
// Verify result
assert.NotNil(t, result)
resultMap, ok := result.(*ImportResult)
assert.True(t, ok)
assert.Greater(t, resultMap.Success, 0)
// Verify all data imported correctly
roles, err := mod.Get(model.QueryParam{})
assert.Nil(t, err)
assert.Greater(t, len(roles), 0)
}