2022-09-06 19:15:07 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
"github.com/google/uuid"
|
2022-09-06 19:15:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestItemService_CsvImport(t *testing.T) {
|
|
|
|
data := loadcsv()
|
|
|
|
svc := &ItemService{
|
|
|
|
repo: tRepos,
|
|
|
|
}
|
2022-10-16 01:46:57 +00:00
|
|
|
count, err := svc.CsvImport(context.Background(), tGroup.ID, data)
|
|
|
|
assert.Equal(t, 6, count)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check import refs are deduplicated
|
|
|
|
count, err = svc.CsvImport(context.Background(), tGroup.ID, data)
|
|
|
|
assert.Equal(t, 0, count)
|
2022-09-06 19:15:07 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
items, err := svc.GetAll(context.Background(), tGroup.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
for _, item := range items {
|
|
|
|
err := svc.repo.Items.Delete(context.Background(), item.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(t, len(items), 6)
|
|
|
|
|
|
|
|
dataCsv := []csvRow{}
|
|
|
|
for _, item := range data {
|
|
|
|
dataCsv = append(dataCsv, newCsvRow(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
locationService := &LocationService{
|
|
|
|
repos: tRepos,
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelService := &LabelService{
|
|
|
|
repos: tRepos,
|
|
|
|
}
|
|
|
|
|
|
|
|
allLocation, err := locationService.GetAll(context.Background(), tGroup.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
locNames := []string{}
|
|
|
|
for _, loc := range allLocation {
|
|
|
|
locNames = append(locNames, loc.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
allLabels, err := LabelService.GetAll(context.Background(), tGroup.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
labelNames := []string{}
|
|
|
|
for _, label := range allLabels {
|
|
|
|
labelNames = append(labelNames, label.Name)
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
ids := []uuid.UUID{}
|
|
|
|
t.Cleanup((func() {
|
|
|
|
for _, id := range ids {
|
|
|
|
err := svc.repo.Items.Delete(context.Background(), id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2022-09-06 19:15:07 +00:00
|
|
|
for _, item := range items {
|
|
|
|
assert.Contains(t, locNames, item.Location.Name)
|
|
|
|
for _, label := range item.Labels {
|
|
|
|
assert.Contains(t, labelNames, label.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, csvRow := range dataCsv {
|
2022-09-13 04:54:30 +00:00
|
|
|
if csvRow.Item.Name == item.Name {
|
|
|
|
assert.Equal(t, csvRow.Item.Description, item.Description)
|
2022-09-27 23:52:13 +00:00
|
|
|
assert.Equal(t, csvRow.Item.Quantity, item.Quantity)
|
|
|
|
assert.Equal(t, csvRow.Item.Insured, item.Insured)
|
2022-09-06 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
}
|