2022-08-31 03:21:18 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hay-kot/content/backend/internal/types"
|
|
|
|
"github.com/hay-kot/content/backend/pkgs/faker"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var fk = faker.NewFaker()
|
|
|
|
|
|
|
|
func locationFactory() types.LocationCreate {
|
|
|
|
return types.LocationCreate{
|
|
|
|
Name: fk.RandomString(10),
|
|
|
|
Description: fk.RandomString(100),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Locations_Get(t *testing.T) {
|
|
|
|
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Get by ID
|
|
|
|
foundLoc, err := testRepos.Locations.Get(context.Background(), loc.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, loc.ID, foundLoc.ID)
|
|
|
|
|
2022-09-03 09:52:05 +00:00
|
|
|
err = testRepos.Locations.Delete(context.Background(), loc.ID)
|
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 09:17:48 +00:00
|
|
|
func Test_LocationsGetAllWithCount(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
result, err := testRepos.Locations.Create(ctx, testGroup.ID, types.LocationCreate{
|
|
|
|
Name: fk.RandomString(10),
|
|
|
|
Description: fk.RandomString(100),
|
|
|
|
})
|
2022-09-03 09:52:05 +00:00
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
|
2022-09-03 09:52:05 +00:00
|
|
|
_, err = testRepos.Items.Create(ctx, testGroup.ID, types.ItemCreate{
|
2022-09-03 09:17:48 +00:00
|
|
|
Name: fk.RandomString(10),
|
|
|
|
Description: fk.RandomString(100),
|
|
|
|
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-03 09:17:48 +00:00
|
|
|
results, err := testRepos.Locations.GetAll(context.Background(), testGroup.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 Test_Locations_Create(t *testing.T) {
|
|
|
|
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Get by ID
|
|
|
|
foundLoc, err := testRepos.Locations.Get(context.Background(), loc.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, loc.ID, foundLoc.ID)
|
|
|
|
|
2022-09-03 09:52:05 +00:00
|
|
|
err = testRepos.Locations.Delete(context.Background(), loc.ID)
|
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Locations_Update(t *testing.T) {
|
|
|
|
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
updateData := types.LocationUpdate{
|
|
|
|
ID: loc.ID,
|
|
|
|
Name: fk.RandomString(10),
|
|
|
|
Description: fk.RandomString(100),
|
|
|
|
}
|
|
|
|
|
|
|
|
update, err := testRepos.Locations.Update(context.Background(), updateData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
foundLoc, err := testRepos.Locations.Get(context.Background(), loc.ID)
|
|
|
|
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-03 09:52:05 +00:00
|
|
|
err = testRepos.Locations.Delete(context.Background(), loc.ID)
|
|
|
|
assert.NoError(t, err)
|
2022-08-31 03:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Locations_Delete(t *testing.T) {
|
|
|
|
loc, err := testRepos.Locations.Create(context.Background(), testGroup.ID, locationFactory())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = testRepos.Locations.Delete(context.Background(), loc.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = testRepos.Locations.Get(context.Background(), loc.ID)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|