exam_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package file
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "git.andreafazzi.eu/andrea/probo/lib/models"
  8. "git.andreafazzi.eu/andrea/probo/lib/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, map[string]string{"class": "1 D LIN"})
  42. quizzes := quizStore.Storer.FilterInCollection(c, map[string]string{"tags": "#tag1"})
  43. for _, p := range participants {
  44. e := new(models.Exam)
  45. e.Participant = p
  46. e.Quizzes = quizzes
  47. _, err = examStore.Create(e)
  48. t.Nil(err)
  49. defer os.Remove(examStore.GetPath(e))
  50. examFromDisk, err := readExamFromJSON(e.GetID())
  51. t.Nil(err)
  52. if !t.Failed() {
  53. t.Not(t.Nil(examFromDisk.Participant))
  54. if !t.Failed() {
  55. t.Equal("Smith", examFromDisk.Participant.Lastname)
  56. t.Equal(2, len(examFromDisk.Quizzes))
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. func readExamFromJSON(examID string) (*models.Exam, error) {
  64. // Build the path to the JSON file
  65. jsonPath := fmt.Sprintf("testdata/exams/exam_%s.json", examID)
  66. // Open the JSON file
  67. file, err := os.Open(jsonPath)
  68. if err != nil {
  69. return nil, fmt.Errorf("failed to open JSON file: %w", err)
  70. }
  71. defer file.Close()
  72. // Read the JSON data from the file
  73. jsonData, err := io.ReadAll(file)
  74. if err != nil {
  75. return nil, fmt.Errorf("failed to read JSON data: %w", err)
  76. }
  77. // Unmarshal the JSON data into an Exam object
  78. var exam models.Exam
  79. if err := json.Unmarshal(jsonData, &exam); err != nil {
  80. return nil, fmt.Errorf("failed to parse JSON data: %w", err)
  81. }
  82. return &exam, nil
  83. }