1
0

fixtures.go 7.7 KB

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