group_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package file
  2. import (
  3. "fmt"
  4. "os"
  5. "git.andreafazzi.eu/andrea/probo/models"
  6. "git.andreafazzi.eu/andrea/probo/store"
  7. "github.com/gocarina/gocsv"
  8. "github.com/remogatto/prettytest"
  9. )
  10. type groupTestSuite struct {
  11. prettytest.Suite
  12. }
  13. func (t *groupTestSuite) TestCreate() {
  14. participantStore := store.NewParticipantStore()
  15. participantStore.Create(
  16. &models.Participant{
  17. ID: "1234",
  18. Firstname: "John",
  19. Lastname: "Smith",
  20. Token: 111222,
  21. Attributes: models.AttributeList{"class": "1 D LIN"},
  22. })
  23. participantStore.Create(
  24. &models.Participant{
  25. ID: "5678",
  26. Firstname: "Jack",
  27. Lastname: "Sparrow",
  28. Token: 222333,
  29. Attributes: models.AttributeList{"class": "2 D LIN"},
  30. })
  31. groupStore, err := NewDefaultGroupFileStore()
  32. t.Nil(err)
  33. if !t.Failed() {
  34. g := new(models.Group)
  35. g.Name = "Test Group"
  36. participantStore.FilterInGroup(g, &models.ParticipantFilter{
  37. Attributes: map[string]string{"class": "1 D LIN"},
  38. })
  39. _, err = groupStore.Create(g)
  40. t.Nil(err)
  41. defer os.Remove(groupStore.GetPath(g))
  42. participantsFromDisk, err := readGroupFromCSV(g.GetID())
  43. t.Nil(err)
  44. if !t.Failed() {
  45. t.Equal("Smith", participantsFromDisk[0].Lastname)
  46. }
  47. }
  48. }
  49. func readGroupFromCSV(groupID string) ([]*models.Participant, error) {
  50. // Build the path to the CSV file
  51. csvPath := fmt.Sprintf("testdata/groups/group_%s.csv", groupID)
  52. // Open the CSV file
  53. file, err := os.Open(csvPath)
  54. if err != nil {
  55. return nil, fmt.Errorf("failed to open CSV file: %w", err)
  56. }
  57. defer file.Close()
  58. // Parse the CSV file
  59. var participants []*models.Participant
  60. if err := gocsv.UnmarshalFile(file, &participants); err != nil {
  61. return nil, fmt.Errorf("failed to parse CSV file: %w", err)
  62. }
  63. return participants, nil
  64. }