package app import ( "fmt" . "code.hoteas.com/golang/hotime/db" ) // SetupDatabase 根据数据库类型自动初始化测试表和种子数据 // 在 TestMain 中调用一次即可 func SetupDatabase(db *HoTimeDB) { switch db.Type { case "dm", "dameng": SetupDMDatabase(db) case "mysql": SetupMySQLDatabase(db) } } // seedTestData 通用种子数据灌入(DM 和 MySQL 共用) // 使用 ORM 的 Insert 方法,与数据库方言无关 func seedTestData(db *HoTimeDB, tag string) { // 已有数据则跳过 if db.Count("ctg") > 0 { fmt.Printf("[%s Setup] 表中已有数据,跳过数据灌入\n", tag) return } fmt.Printf("[%s Setup] 灌入测试数据...\n", tag) // 分类数据 ctgs := []map[string]interface{}{ {"name": "新闻资讯", "state": 0}, {"name": "技术文档", "state": 0}, {"name": "公告通知", "state": 0}, {"name": "产品介绍", "state": 0}, {"name": "已归档", "state": 1}, } for _, c := range ctgs { c["create_time[#]"] = "NOW()" c["modify_time[#]"] = "NOW()" db.Insert("ctg", c) } // 管理员数据 admins := []map[string]interface{}{ {"name": "超级管理员", "phone": "13800000001", "state": 1, "password": "admin123", "role_id": 1, "title": "系统管理员"}, {"name": "编辑员", "phone": "13800000002", "state": 1, "password": "editor123", "role_id": 2, "title": "内容编辑"}, {"name": "审核员", "phone": "13800000003", "state": 1, "password": "review123", "role_id": 3, "title": "内容审核"}, } for _, a := range admins { a["create_time[#]"] = "NOW()" a["modify_time[#]"] = "NOW()" db.Insert("admin", a) } // 文章数据 — 覆盖不同状态、分类、点击数、排序 articles := []map[string]interface{}{ {"title": "数据库入门指南", "author": "管理员", "content": "国产关系型数据库管理系统入门...", "state": 0, "click_num": 128, "sort": 10, "ctg_id": 1, "admin_id": 1}, {"title": "新闻发布系统上线公告", "author": "编辑员", "content": "本系统已正式上线运行...", "state": 0, "click_num": 256, "sort": 5, "ctg_id": 1, "admin_id": 2}, {"title": "Go语言最佳实践", "author": "管理员", "content": "Go语言在后端开发中的最佳实践总结...", "state": 0, "click_num": 512, "sort": 8, "ctg_id": 2, "admin_id": 1}, {"title": "API接口设计规范", "author": "审核员", "content": "RESTful API设计的基本规范和标准...", "state": 0, "click_num": 64, "sort": 15, "ctg_id": 2, "admin_id": 3}, {"title": "系统维护通知", "author": "管理员", "content": "系统将于本周日凌晨进行维护升级...", "state": 0, "click_num": 32, "sort": 1, "ctg_id": 3, "admin_id": 1}, {"title": "产品功能更新说明", "author": "编辑员", "content": "新版本增加了以下功能...", "state": 0, "click_num": 96, "sort": 12, "ctg_id": 4, "admin_id": 2}, {"title": "数据库性能优化指南", "author": "管理员", "content": "数据库性能优化的常见方法和技巧...", "state": 0, "click_num": 1024, "sort": 3, "ctg_id": 2, "admin_id": 1}, {"title": "缓存机制深入分析", "author": "审核员", "content": "缓存在Web应用中的重要作用...", "state": 0, "click_num": 200, "sort": 7, "ctg_id": 2, "admin_id": 3}, {"title": "旧版公告(已归档)", "author": "管理员", "content": "此公告已过期归档...", "state": 1, "click_num": 10, "sort": 0, "ctg_id": 5, "admin_id": 1}, {"title": "测试文章(隐藏)", "author": "编辑员", "content": "测试用文章不公开显示...", "state": 2, "click_num": 0, "sort": 0, "ctg_id": 1, "admin_id": 2}, {"title": "高点击量文章", "author": "编辑员", "content": "这是一篇点击量很高的文章...", "state": 0, "click_num": 9999, "sort": 2, "ctg_id": 1, "admin_id": 2}, } for _, a := range articles { a["create_time[#]"] = "NOW()" a["modify_time[#]"] = "NOW()" db.Insert("article", a) } fmt.Printf("[%s Setup] 数据灌入完成: ctg=%d, admin=%d, article=%d\n", tag, db.Count("ctg"), db.Count("admin"), db.Count("article")) }