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() group := getGroupFromFixture(t, fixture, "group-1-server-1") params, err := domain.NewCreateMonitorParams(group.ID.String(), 1234) 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", groupID: getGroupFromFixture(t, fixture, "group-1-server-1").ID.String(), expectedMonitors: []string{ "e3017ba8-4fba-4bb1-ac8d-e4e9477a04d2", "89719460-58ab-46b8-8682-46f161546949", }, }, { name: "group-2-server-2", groupID: getGroupFromFixture(t, fixture, "group-2-server-2").ID.String(), 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) } }) } } func TestMonitor_Get(t *testing.T) { t.Parallel() db := newDB(t) fixture := loadFixtures(t, db) repo := bundb.NewMonitor(db) group := getGroupFromFixture(t, fixture, "group-1-server-1") t.Run("OK", func(t *testing.T) { t.Parallel() monitors, err := repo.List(context.Background(), group.ID.String()) 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() monitor, err := repo.Get(context.Background(), tt.id) assert.ErrorIs(t, err, domain.MonitorNotFoundError{ID: tt.id}) assert.Zero(t, monitor) }) } }) } func TestMonitor_Delete(t *testing.T) { t.Parallel() db := newDB(t) fixture := loadFixtures(t, db) repo := bundb.NewMonitor(db) group := getGroupFromFixture(t, fixture, "group-1-server-1") t.Run("OK", func(t *testing.T) { t.Parallel() beforeDelete, err := repo.List(context.Background(), group.ID.String()) assert.NoError(t, err) assert.Greater(t, len(beforeDelete), 0) assert.NoError(t, repo.Delete(context.Background(), beforeDelete[0].ID)) afterDelete, err := repo.List(context.Background(), group.ID.String()) 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}, ) }) } }) }