exam_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package file
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "git.andreafazzi.eu/andrea/probo/models"
  8. "git.andreafazzi.eu/andrea/probo/store"
  9. "github.com/remogatto/prettytest"
  10. )
  11. type examTestSuite struct {
  12. prettytest.Suite
  13. }
  14. func (t *examTestSuite) TestCreate() {
  15. participantStore, err := NewParticipantFileStore(
  16. &FileStoreConfig[*models.Participant, *store.ParticipantStore]{
  17. FilePathConfig: FilePathConfig{"testdata/exams/participants", "participant", ".json"},
  18. IndexDirFunc: DefaultIndexDirFunc[*models.Participant, *store.ParticipantStore],
  19. CreateEntityFunc: func() *models.Participant {
  20. return &models.Participant{
  21. Attributes: make(map[string]string),
  22. }
  23. },
  24. },
  25. )
  26. t.Nil(err)
  27. quizStore, err := NewQuizFileStore(
  28. &FileStoreConfig[*models.Quiz, *store.QuizStore]{
  29. FilePathConfig: FilePathConfig{"testdata/exams/quizzes", "quiz", ".md"},
  30. IndexDirFunc: DefaultQuizIndexDirFunc,
  31. },
  32. )
  33. t.Nil(err)
  34. if !t.Failed() {
  35. t.Equal(3, len(participantStore.ReadAll()))
  36. examStore, err := NewDefaultExamFileStore()
  37. t.Nil(err)
  38. if !t.Failed() {
  39. g := new(models.Group)
  40. c := new(models.Collection)
  41. participants := participantStore.Storer.FilterInGroup(g, &models.ParticipantFilter{
  42. Attributes: map[string]string{"class": "1 D LIN"},
  43. })
  44. quizzes := quizStore.Storer.FilterInCollection(c, &models.Filter{
  45. Tags: []*models.Tag{
  46. {Name: "#tag1"},
  47. },
  48. })
  49. for _, p := range participants {
  50. e := new(models.Exam)
  51. e.Participant = p
  52. e.Quizzes = quizzes
  53. _, err = examStore.Create(e)
  54. t.Nil(err)
  55. defer os.Remove(examStore.GetPath(e))
  56. examFromDisk, err := readExamFromJSON(e.GetID())
  57. t.Nil(err)
  58. if !t.Failed() {
  59. t.Not(t.Nil(examFromDisk.Participant))
  60. if !t.Failed() {
  61. t.Equal("Smith", examFromDisk.Participant.Lastname)
  62. t.Equal(2, len(examFromDisk.Quizzes))
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. func readExamFromJSON(examID string) (*models.Exam, error) {
  70. // Build the path to the JSON file
  71. jsonPath := fmt.Sprintf("testdata/exams/exam_%s.json", examID)
  72. // Open the JSON file
  73. file, err := os.Open(jsonPath)
  74. if err != nil {
  75. return nil, fmt.Errorf("failed to open JSON file: %w", err)
  76. }
  77. defer file.Close()
  78. // Read the JSON data from the file
  79. jsonData, err := io.ReadAll(file)
  80. if err != nil {
  81. return nil, fmt.Errorf("failed to read JSON data: %w", err)
  82. }
  83. // Unmarshal the JSON data into an Exam object
  84. var exam models.Exam
  85. if err := json.Unmarshal(jsonData, &exam); err != nil {
  86. return nil, fmt.Errorf("failed to parse JSON data: %w", err)
  87. }
  88. return &exam, nil
  89. }