2022-08-31 03:21:18 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func locationFactory() LocationCreate {
|
|
|
|
return LocationCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Get(t *testing.T) {
|
|
|
|
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Get by ID
|
2022-09-05 08:26:21 +00:00
|
|
|
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, loc.ID, foundLoc.ID)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepositoryGetAllWithCount(t *testing.T) {
|
2022-09-03 09:17:48 +00:00
|
|
|
ctx := context.Background()
|
2022-09-27 23:52:13 +00:00
|
|
|
result, err := tRepos.Locations.Create(ctx, tGroup.ID, LocationCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-09-03 09:17:48 +00:00
|
|
|
})
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
_, err = tRepos.Items.Create(ctx, tGroup.ID, ItemCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-09-03 09:17:48 +00:00
|
|
|
LocationID: result.ID,
|
|
|
|
})
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
results, err := tRepos.Locations.GetAll(context.Background(), tGroup.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
for _, loc := range results {
|
|
|
|
if loc.ID == result.ID {
|
|
|
|
assert.Equal(t, 1, loc.ItemCount)
|
|
|
|
}
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
2022-09-03 09:17:48 +00:00
|
|
|
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Create(t *testing.T) {
|
|
|
|
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Get by ID
|
2022-09-05 08:26:21 +00:00
|
|
|
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, loc.ID, foundLoc.ID)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Update(t *testing.T) {
|
|
|
|
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
updateData := LocationUpdate{
|
2022-08-31 03:21:18 +00:00
|
|
|
ID: loc.ID,
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
update, err := tRepos.Locations.Update(context.Background(), updateData)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, update.ID, foundLoc.ID)
|
|
|
|
assert.Equal(t, update.Name, foundLoc.Name)
|
|
|
|
assert.Equal(t, update.Description, foundLoc.Description)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
func TestLocationRepository_Delete(t *testing.T) {
|
|
|
|
loc, err := tRepos.Locations.Create(context.Background(), tGroup.ID, locationFactory())
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
err = tRepos.Locations.Delete(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-05 08:26:21 +00:00
|
|
|
_, err = tRepos.Locations.Get(context.Background(), loc.ID)
|
2022-08-31 03:21:18 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|