homebox/backend/internal/repo/repo_locations_test.go

105 lines
2.7 KiB
Go
Raw Normal View History

2022-08-31 03:21:18 +00:00
package repo
import (
"context"
"testing"
"github.com/hay-kot/homebox/backend/internal/types"
2022-08-31 03:21:18 +00:00
"github.com/stretchr/testify/assert"
)
func locationFactory() types.LocationCreate {
return types.LocationCreate{
Name: fk.Str(10),
Description: fk.Str(100),
2022-08-31 03:21:18 +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
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)
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
}
func TestLocationRepositoryGetAllWithCount(t *testing.T) {
2022-09-03 09:17:48 +00:00
ctx := context.Background()
result, err := tRepos.Locations.Create(ctx, tGroup.ID, types.LocationCreate{
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
_, err = tRepos.Items.Create(ctx, tGroup.ID, types.ItemCreate{
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
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
}
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
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)
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
}
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)
updateData := types.LocationUpdate{
ID: loc.ID,
Name: fk.Str(10),
Description: fk.Str(100),
2022-08-31 03:21:18 +00:00
}
update, err := tRepos.Locations.Update(context.Background(), updateData)
2022-08-31 03:21:18 +00:00
assert.NoError(t, err)
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)
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
}
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)
err = tRepos.Locations.Delete(context.Background(), loc.ID)
2022-08-31 03:21:18 +00:00
assert.NoError(t, err)
_, err = tRepos.Locations.Get(context.Background(), loc.ID)
2022-08-31 03:21:18 +00:00
assert.Error(t, err)
}