probo/store/file/group_test.go

81 lines
1.7 KiB
Go
Raw Normal View History

2023-11-21 15:12:13 +01:00
package file
import (
"fmt"
"os"
"git.andreafazzi.eu/andrea/probo/models"
"git.andreafazzi.eu/andrea/probo/store"
"github.com/gocarina/gocsv"
"github.com/remogatto/prettytest"
)
type groupTestSuite struct {
prettytest.Suite
}
func (t *groupTestSuite) TestCreate() {
participantStore := store.NewParticipantStore()
participantStore.Create(
&models.Participant{
ID: "1234",
Firstname: "John",
Lastname: "Smith",
Token: 111222,
Attributes: models.AttributeList{"class": "1 D LIN"},
})
participantStore.Create(
&models.Participant{
ID: "5678",
Firstname: "Jack",
Lastname: "Sparrow",
Token: 222333,
Attributes: models.AttributeList{"class": "2 D LIN"},
})
groupStore, err := NewDefaultGroupFileStore()
t.Nil(err)
if !t.Failed() {
g := new(models.Group)
g.Name = "Test Group"
participantStore.FilterInGroup(g, &models.ParticipantFilter{
Attributes: map[string]string{"class": "1 D LIN"},
})
_, err = groupStore.Create(g)
t.Nil(err)
defer os.Remove(groupStore.GetPath(g))
participantsFromDisk, err := readGroupFromCSV(g.GetID())
t.Nil(err)
if !t.Failed() {
t.Equal("Smith", participantsFromDisk[0].Lastname)
}
}
}
func readGroupFromCSV(groupID string) ([]*models.Participant, error) {
// Build the path to the CSV file
csvPath := fmt.Sprintf("testdata/groups/group_%s.csv", groupID)
// Open the CSV file
file, err := os.Open(csvPath)
if err != nil {
return nil, fmt.Errorf("failed to open CSV file: %w", err)
}
defer file.Close()
// Parse the CSV file
var participants []*models.Participant
if err := gocsv.UnmarshalFile(file, &participants); err != nil {
return nil, fmt.Errorf("failed to parse CSV file: %w", err)
}
return participants, nil
}