exam_test.go 2.6 KB

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