1
0

fixtures.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package testutil
  2. import (
  3. "embed"
  4. "gogs.dmsc.dev/arp/models"
  5. "gorm.io/driver/sqlite"
  6. "gorm.io/gorm"
  7. )
  8. //go:embed init_tests.sql
  9. var initSQLFS embed.FS
  10. // SetupTestDB creates an in-memory SQLite database for testing
  11. func SetupTestDB() (*gorm.DB, error) {
  12. db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
  13. if err != nil {
  14. return nil, err
  15. }
  16. // Run auto-migration for all models
  17. err = db.AutoMigrate(
  18. &models.User{},
  19. &models.Role{},
  20. &models.Permission{},
  21. &models.Service{},
  22. &models.Task{},
  23. &models.TaskStatus{},
  24. &models.Message{},
  25. &models.Note{},
  26. &models.WorkflowTemplate{},
  27. &models.WorkflowInstance{},
  28. &models.WorkflowNode{},
  29. &models.WorkflowEdge{},
  30. )
  31. if err != nil {
  32. return nil, err
  33. }
  34. return db, nil
  35. }
  36. // BootstrapTestDB initializes the database with initial data from init_tests.sql
  37. func BootstrapTestDB(db *gorm.DB) error {
  38. // Read the init SQL file
  39. sqlContent, err := initSQLFS.ReadFile("init_tests.sql")
  40. if err != nil {
  41. return err
  42. }
  43. // Execute the SQL
  44. return db.Exec(string(sqlContent)).Error
  45. }
  46. // SetupAndBootstrapTestDB creates an in-memory database and bootstraps it with initial data
  47. func SetupAndBootstrapTestDB() (*gorm.DB, error) {
  48. db, err := SetupTestDB()
  49. if err != nil {
  50. return nil, err
  51. }
  52. if err := BootstrapTestDB(db); err != nil {
  53. return nil, err
  54. }
  55. return db, nil
  56. }
  57. // SeedData contains all hardcoded test fixtures
  58. type SeedData struct {
  59. Permissions []PermissionFixture
  60. Roles []RoleFixture
  61. Users []UserFixture
  62. TaskStatuses []TaskStatusFixture
  63. Services []ServiceFixture
  64. Tasks []TaskFixture
  65. Notes []NoteFixture
  66. Messages []MessageFixture
  67. }
  68. // PermissionFixture represents test permission data
  69. type PermissionFixture struct {
  70. Code string
  71. Description string
  72. }
  73. // RoleFixture represents test role data
  74. type RoleFixture struct {
  75. Name string
  76. Description string
  77. PermissionCodes []string
  78. }
  79. // UserFixture represents test user data
  80. type UserFixture struct {
  81. Email string
  82. Password string
  83. RoleNames []string
  84. }
  85. // TaskStatusFixture represents test task status data
  86. type TaskStatusFixture struct {
  87. Code string
  88. Label string
  89. }
  90. // ServiceFixture represents test service data
  91. type ServiceFixture struct {
  92. Name string
  93. Description string
  94. CreatorEmail string
  95. ParticipantEmails []string
  96. }
  97. // TaskFixture represents test task data
  98. type TaskFixture struct {
  99. Title string
  100. Content string
  101. CreatorEmail string
  102. AssigneeEmail string
  103. StatusCode string
  104. Priority string
  105. }
  106. // NoteFixture represents test note data
  107. type NoteFixture struct {
  108. Title string
  109. Content string
  110. UserEmail string
  111. ServiceName string
  112. }
  113. // MessageFixture represents test message data
  114. type MessageFixture struct {
  115. SenderEmail string
  116. Content string
  117. ReceiverEmails []string
  118. }
  119. // GetSeedData returns the hardcoded seed data for testing
  120. func GetSeedData() SeedData {
  121. return SeedData{
  122. Permissions: []PermissionFixture{
  123. {Code: "user:read", Description: "Read user information"},
  124. {Code: "user:write", Description: "Create and update users"},
  125. {Code: "task:read", Description: "Read task information"},
  126. {Code: "task:write", Description: "Create and update tasks"},
  127. {Code: "service:read", Description: "Read service information"},
  128. {Code: "service:write", Description: "Create and update services"},
  129. {Code: "note:read", Description: "Read notes"},
  130. {Code: "note:write", Description: "Create and update notes"},
  131. },
  132. Roles: []RoleFixture{
  133. {
  134. Name: "admin",
  135. Description: "Administrator with full access",
  136. PermissionCodes: []string{"user:read", "user:write", "task:read", "task:write", "service:read", "service:write", "note:read", "note:write"},
  137. },
  138. {
  139. Name: "member",
  140. Description: "Team member with read access and limited write access",
  141. PermissionCodes: []string{"user:read", "task:read", "task:write", "service:read", "note:read", "note:write"},
  142. },
  143. {
  144. Name: "viewer",
  145. Description: "Read-only access",
  146. PermissionCodes: []string{"user:read", "task:read", "service:read", "note:read"},
  147. },
  148. },
  149. Users: []UserFixture{
  150. // Note: admin@example.com is bootstrapped via init_tests.sql
  151. {
  152. Email: "member1@example.com",
  153. Password: "member1-hashed-password",
  154. RoleNames: []string{"member"},
  155. },
  156. {
  157. Email: "member2@example.com",
  158. Password: "member2-hashed-password",
  159. RoleNames: []string{"member"},
  160. },
  161. {
  162. Email: "viewer@example.com",
  163. Password: "viewer-hashed-password",
  164. RoleNames: []string{"viewer"},
  165. },
  166. },
  167. TaskStatuses: []TaskStatusFixture{
  168. {Code: "open", Label: "Open"},
  169. {Code: "in_progress", Label: "In Progress"},
  170. {Code: "review", Label: "Under Review"},
  171. {Code: "done", Label: "Completed"},
  172. },
  173. Services: []ServiceFixture{
  174. {
  175. Name: "Project Alpha",
  176. Description: "Main project for alpha development",
  177. CreatorEmail: "admin@example.com",
  178. ParticipantEmails: []string{"admin@example.com", "member1@example.com", "member2@example.com"},
  179. },
  180. {
  181. Name: "Project Beta",
  182. Description: "Secondary project for beta testing",
  183. CreatorEmail: "admin@example.com",
  184. ParticipantEmails: []string{"member1@example.com", "viewer@example.com"},
  185. },
  186. },
  187. Tasks: []TaskFixture{
  188. {
  189. Title: "Setup development environment",
  190. Content: "Initialize the development environment with all required tools and dependencies",
  191. CreatorEmail: "admin@example.com",
  192. AssigneeEmail: "member1@example.com",
  193. StatusCode: "done",
  194. Priority: "high",
  195. },
  196. {
  197. Title: "Implement user authentication",
  198. Content: "Create the authentication module with login, logout, and password reset functionality",
  199. CreatorEmail: "admin@example.com",
  200. AssigneeEmail: "member1@example.com",
  201. StatusCode: "in_progress",
  202. Priority: "high",
  203. },
  204. {
  205. Title: "Write API documentation",
  206. Content: "Document all API endpoints with request/response examples",
  207. CreatorEmail: "member1@example.com",
  208. AssigneeEmail: "member2@example.com",
  209. StatusCode: "review",
  210. Priority: "medium",
  211. },
  212. {
  213. Title: "Setup CI/CD pipeline",
  214. Content: "Configure continuous integration and deployment pipeline",
  215. CreatorEmail: "admin@example.com",
  216. AssigneeEmail: "",
  217. StatusCode: "open",
  218. Priority: "medium",
  219. },
  220. {
  221. Title: "Performance optimization",
  222. Content: "Optimize database queries and implement caching",
  223. CreatorEmail: "member2@example.com",
  224. AssigneeEmail: "",
  225. StatusCode: "open",
  226. Priority: "low",
  227. },
  228. },
  229. Notes: []NoteFixture{
  230. {
  231. Title: "Architecture decisions",
  232. Content: "Document key architectural decisions and their rationale",
  233. UserEmail: "admin@example.com",
  234. ServiceName: "Project Alpha",
  235. },
  236. {
  237. Title: "Meeting notes - Sprint 1",
  238. Content: "Notes from the first sprint planning meeting",
  239. UserEmail: "member1@example.com",
  240. ServiceName: "Project Alpha",
  241. },
  242. {
  243. Title: "Testing strategy",
  244. Content: "Outline the testing approach for the beta release",
  245. UserEmail: "member1@example.com",
  246. ServiceName: "Project Beta",
  247. },
  248. },
  249. Messages: []MessageFixture{
  250. {
  251. SenderEmail: "admin@example.com",
  252. Content: "Welcome to the project channel!",
  253. ReceiverEmails: []string{"member1@example.com"},
  254. },
  255. {
  256. SenderEmail: "member1@example.com",
  257. Content: "Thanks! Excited to get started.",
  258. ReceiverEmails: []string{"admin@example.com"},
  259. },
  260. {
  261. SenderEmail: "member1@example.com",
  262. Content: "Hey, let us discuss the API documentation.",
  263. ReceiverEmails: []string{"member2@example.com"},
  264. },
  265. {
  266. SenderEmail: "member2@example.com",
  267. Content: "Sure, I will start drafting it today.",
  268. ReceiverEmails: []string{"member1@example.com"},
  269. },
  270. {
  271. SenderEmail: "admin@example.com",
  272. Content: "Team announcement: Sprint review tomorrow at 2pm.",
  273. ReceiverEmails: []string{"member1@example.com", "member2@example.com", "viewer@example.com"},
  274. },
  275. },
  276. }
  277. }