2022-09-05 08:26:21 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-04 17:55:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-05 08:26:21 +00:00
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func labelFactory() LabelCreate {
|
|
|
|
return LabelCreate{
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func useLabels(t *testing.T, len int) []LabelOut {
|
2022-09-05 08:26:21 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
labels := make([]LabelOut, len)
|
2022-09-05 08:26:21 +00:00
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
itm := labelFactory()
|
|
|
|
|
|
|
|
item, err := tRepos.Labels.Create(context.Background(), tGroup.ID, itm)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
labels[i] = item
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
t.Cleanup(func() {
|
2022-09-05 08:26:21 +00:00
|
|
|
for _, item := range labels {
|
2023-08-02 21:00:57 +00:00
|
|
|
_ = tRepos.Labels.delete(context.Background(), item.ID)
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return labels
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabelRepository_Get(t *testing.T) {
|
2022-09-12 22:47:27 +00:00
|
|
|
labels := useLabels(t, 1)
|
2022-09-05 08:26:21 +00:00
|
|
|
label := labels[0]
|
|
|
|
|
|
|
|
// Get by ID
|
2022-09-27 23:52:13 +00:00
|
|
|
foundLoc, err := tRepos.Labels.GetOne(context.Background(), label.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
assert.Equal(t, label.ID, foundLoc.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabelRepositoryGetAll(t *testing.T) {
|
2022-09-12 22:47:27 +00:00
|
|
|
useLabels(t, 10)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
all, err := tRepos.Labels.GetAll(context.Background(), tGroup.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
assert.Len(t, all, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabelRepository_Create(t *testing.T) {
|
|
|
|
loc, err := tRepos.Labels.Create(context.Background(), tGroup.ID, labelFactory())
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
// Get by ID
|
2022-09-27 23:52:13 +00:00
|
|
|
foundLoc, err := tRepos.Labels.GetOne(context.Background(), loc.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
assert.Equal(t, loc.ID, foundLoc.ID)
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
err = tRepos.Labels.delete(context.Background(), loc.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabelRepository_Update(t *testing.T) {
|
|
|
|
loc, err := tRepos.Labels.Create(context.Background(), tGroup.ID, labelFactory())
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
updateData := LabelUpdate{
|
2022-09-05 08:26:21 +00:00
|
|
|
ID: loc.ID,
|
2022-09-12 22:47:27 +00:00
|
|
|
Name: fk.Str(10),
|
|
|
|
Description: fk.Str(100),
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
update, err := tRepos.Labels.UpdateByGroup(context.Background(), tGroup.ID, updateData)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
foundLoc, err := tRepos.Labels.GetOne(context.Background(), loc.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
|
|
|
assert.Equal(t, update.ID, foundLoc.ID)
|
|
|
|
assert.Equal(t, update.Name, foundLoc.Name)
|
|
|
|
assert.Equal(t, update.Description, foundLoc.Description)
|
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
err = tRepos.Labels.delete(context.Background(), loc.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabelRepository_Delete(t *testing.T) {
|
|
|
|
loc, err := tRepos.Labels.Create(context.Background(), tGroup.ID, labelFactory())
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2023-08-02 21:00:57 +00:00
|
|
|
err = tRepos.Labels.delete(context.Background(), loc.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
_, err = tRepos.Labels.GetOne(context.Background(), loc.ID)
|
2024-01-04 17:55:26 +00:00
|
|
|
require.Error(t, err)
|
2022-09-05 08:26:21 +00:00
|
|
|
}
|