2022-10-23 06:20:48 +00:00
|
|
|
package bundb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/bundb"
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMonitor_Create(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db := newDB(t)
|
|
|
|
fixture := loadFixtures(t, db)
|
|
|
|
repo := bundb.NewMonitor(db)
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-12-25 11:00:43 +00:00
|
|
|
group := fixture.group(t, "group-1-server-1")
|
2022-10-23 06:20:48 +00:00
|
|
|
|
2022-12-25 11:00:43 +00:00
|
|
|
params, err := domain.NewCreateMonitorParams(group.ID, 1234)
|
2022-10-23 06:20:48 +00:00
|
|
|
require.Zero(t, err)
|
|
|
|
|
|
|
|
monitor, err := repo.Create(context.Background(), params)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = uuid.Parse(monitor.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, params.GroupID(), monitor.GroupID)
|
|
|
|
assert.Equal(t, params.TribeID(), monitor.TribeID)
|
|
|
|
assert.WithinDuration(t, time.Now(), monitor.CreatedAt, 1*time.Second)
|
|
|
|
|
|
|
|
// check unique index
|
|
|
|
monitor, err = repo.Create(context.Background(), params)
|
|
|
|
assert.ErrorIs(t, err, domain.MonitorAlreadyExistsError{
|
|
|
|
TribeID: params.TribeID(),
|
|
|
|
GroupID: params.GroupID(),
|
|
|
|
})
|
|
|
|
assert.Zero(t, monitor)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ERR: group doesn't exist", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("foreign key violation", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
groupID := uuid.NewString()
|
|
|
|
params, err := domain.NewCreateMonitorParams(groupID, 1234)
|
|
|
|
require.Zero(t, err)
|
|
|
|
|
|
|
|
monitor, err := repo.Create(context.Background(), params)
|
|
|
|
assert.ErrorIs(t, err, domain.GroupDoesNotExistError{
|
|
|
|
ID: groupID,
|
|
|
|
})
|
|
|
|
assert.Zero(t, monitor)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("not UUID", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
groupID := "592292203234328587"
|
|
|
|
params, err := domain.NewCreateMonitorParams(groupID, 1234)
|
|
|
|
require.Zero(t, err)
|
|
|
|
|
|
|
|
monitor, err := repo.Create(context.Background(), params)
|
|
|
|
assert.ErrorIs(t, err, domain.GroupDoesNotExistError{
|
|
|
|
ID: groupID,
|
|
|
|
})
|
|
|
|
assert.Zero(t, monitor)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMonitor_List(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db := newDB(t)
|
|
|
|
fixture := loadFixtures(t, db)
|
|
|
|
repo := bundb.NewMonitor(db)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
groupID string
|
|
|
|
expectedMonitors []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "group-1-server-1",
|
2022-12-25 11:00:43 +00:00
|
|
|
groupID: fixture.group(t, "group-1-server-1").ID,
|
2022-10-23 06:20:48 +00:00
|
|
|
expectedMonitors: []string{
|
|
|
|
"e3017ba8-4fba-4bb1-ac8d-e4e9477a04d2",
|
|
|
|
"89719460-58ab-46b8-8682-46f161546949",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group-2-server-2",
|
2022-12-25 11:00:43 +00:00
|
|
|
groupID: fixture.group(t, "group-2-server-2").ID,
|
2022-10-23 06:20:48 +00:00
|
|
|
expectedMonitors: []string{
|
|
|
|
"c7d63c3d-55f6-432e-b9d8-006f8c5ab407",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "random UUID",
|
|
|
|
groupID: uuid.NewString(),
|
|
|
|
expectedMonitors: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not UUID",
|
|
|
|
groupID: "test",
|
|
|
|
expectedMonitors: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
res, err := repo.List(context.Background(), tt.groupID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, res, len(tt.expectedMonitors))
|
|
|
|
for _, id := range tt.expectedMonitors {
|
|
|
|
found := false
|
|
|
|
for _, m := range res {
|
|
|
|
if m.ID == id {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.True(t, found, "monitor (id=%s) not found", id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 07:44:20 +00:00
|
|
|
|
|
|
|
func TestMonitor_Get(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db := newDB(t)
|
|
|
|
fixture := loadFixtures(t, db)
|
|
|
|
repo := bundb.NewMonitor(db)
|
2022-12-25 11:00:43 +00:00
|
|
|
group := fixture.group(t, "group-1-server-1")
|
2022-10-23 07:44:20 +00:00
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-12-25 11:00:43 +00:00
|
|
|
monitors, err := repo.List(context.Background(), group.ID)
|
2022-10-23 07:44:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Greater(t, len(monitors), 0)
|
|
|
|
|
|
|
|
m, err := repo.Get(context.Background(), monitors[0].ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, monitors[0], m)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ERR: monitor not found", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ID - not UUID",
|
|
|
|
id: "test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ID - random UUID",
|
|
|
|
id: uuid.NewString(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-10-27 09:56:40 +00:00
|
|
|
monitor, err := repo.Get(context.Background(), tt.id)
|
|
|
|
assert.ErrorIs(t, err, domain.MonitorNotFoundError{ID: tt.id})
|
|
|
|
assert.Zero(t, monitor)
|
2022-10-23 07:44:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMonitor_Delete(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
db := newDB(t)
|
|
|
|
fixture := loadFixtures(t, db)
|
|
|
|
repo := bundb.NewMonitor(db)
|
2022-12-25 11:00:43 +00:00
|
|
|
group := fixture.group(t, "group-1-server-1")
|
2022-10-23 07:44:20 +00:00
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-12-25 11:00:43 +00:00
|
|
|
beforeDelete, err := repo.List(context.Background(), group.ID)
|
2022-10-23 07:44:20 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, len(beforeDelete), 0)
|
|
|
|
|
|
|
|
assert.NoError(t, repo.Delete(context.Background(), beforeDelete[0].ID))
|
|
|
|
|
2022-12-25 11:00:43 +00:00
|
|
|
afterDelete, err := repo.List(context.Background(), group.ID)
|
2022-10-23 07:44:20 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, afterDelete, len(beforeDelete)-1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ERR: monitor not found", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ID - not UUID",
|
|
|
|
id: "test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ID - random UUID",
|
|
|
|
id: uuid.NewString(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert.ErrorIs(
|
|
|
|
t,
|
|
|
|
repo.Delete(context.Background(), tt.id),
|
|
|
|
domain.MonitorNotFoundError{ID: tt.id},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|