From fc3cc6c140dd3c77bb6523c92f8cb4ba840e4a2b Mon Sep 17 00:00:00 2001 From: Vincent Boutour Date: Wed, 5 May 2021 10:03:39 +0200 Subject: [PATCH] feat: Adding frequency in the query for notifying Signed-off-by: Vincent Boutour --- pkg/notifier/notifier.go | 2 +- pkg/service/ketchup/ketchup.go | 6 +++--- pkg/service/ketchup/ketchup_test.go | 2 +- pkg/service/ketchup/ketchuptest/ketchuptest.go | 2 +- pkg/store/ketchup/ketchup.go | 7 ++++--- pkg/store/ketchup/ketchup_test.go | 10 ++++++---- pkg/store/ketchup/ketchuptest/ketchuptest.go | 4 ++-- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/notifier/notifier.go b/pkg/notifier/notifier.go index 9f5f7325..ebce7381 100644 --- a/pkg/notifier/notifier.go +++ b/pkg/notifier/notifier.go @@ -138,7 +138,7 @@ func (a app) getKetchupToNotify(ctx context.Context, releases []model.Release) ( repositories[index] = release.Repository } - ketchups, err := a.ketchupService.ListForRepositories(ctx, repositories) + ketchups, err := a.ketchupService.ListForRepositories(ctx, repositories, model.Daily) if err != nil { return nil, fmt.Errorf("unable to get ketchups for repositories: %w", err) } diff --git a/pkg/service/ketchup/ketchup.go b/pkg/service/ketchup/ketchup.go index 7b4f8a5a..7e9052dc 100644 --- a/pkg/service/ketchup/ketchup.go +++ b/pkg/service/ketchup/ketchup.go @@ -17,7 +17,7 @@ import ( // App of package type App interface { List(ctx context.Context, page, pageSize uint) ([]model.Ketchup, uint64, error) - ListForRepositories(ctx context.Context, repositories []model.Repository) ([]model.Ketchup, error) + ListForRepositories(ctx context.Context, repositories []model.Repository, frequency model.KetchupFrequency) ([]model.Ketchup, error) Create(ctx context.Context, item model.Ketchup) (model.Ketchup, error) Update(ctx context.Context, item model.Ketchup) (model.Ketchup, error) Delete(ctx context.Context, item model.Ketchup) error @@ -48,13 +48,13 @@ func (a app) List(ctx context.Context, page, pageSize uint) ([]model.Ketchup, ui return enrichedList, total, nil } -func (a app) ListForRepositories(ctx context.Context, repositories []model.Repository) ([]model.Ketchup, error) { +func (a app) ListForRepositories(ctx context.Context, repositories []model.Repository, frequency model.KetchupFrequency) ([]model.Ketchup, error) { ids := make([]uint64, len(repositories)) for index, repo := range repositories { ids[index] = repo.ID } - list, err := a.ketchupStore.ListByRepositoriesID(ctx, ids) + list, err := a.ketchupStore.ListByRepositoriesID(ctx, ids, frequency) if err != nil { return nil, httpModel.WrapInternal(fmt.Errorf("unable to list by ids: %w", err)) } diff --git a/pkg/service/ketchup/ketchup_test.go b/pkg/service/ketchup/ketchup_test.go index 4e522518..405151ce 100644 --- a/pkg/service/ketchup/ketchup_test.go +++ b/pkg/service/ketchup/ketchup_test.go @@ -125,7 +125,7 @@ func TestListForRepositories(t *testing.T) { for _, tc := range cases { t.Run(tc.intention, func(t *testing.T) { - got, gotErr := tc.instance.ListForRepositories(context.Background(), tc.args.repositories) + got, gotErr := tc.instance.ListForRepositories(context.Background(), tc.args.repositories, model.Daily) failed := false diff --git a/pkg/service/ketchup/ketchuptest/ketchuptest.go b/pkg/service/ketchup/ketchuptest/ketchuptest.go index 70418ab3..fdcf30a6 100644 --- a/pkg/service/ketchup/ketchuptest/ketchuptest.go +++ b/pkg/service/ketchup/ketchuptest/ketchuptest.go @@ -47,7 +47,7 @@ func (a App) List(_ context.Context, _, _ uint) ([]model.Ketchup, uint64, error) } // ListForRepositories mocks -func (a App) ListForRepositories(_ context.Context, _ []model.Repository) ([]model.Ketchup, error) { +func (a App) ListForRepositories(_ context.Context, _ []model.Repository, _ model.KetchupFrequency) ([]model.Ketchup, error) { return a.listForRepositoriesKetchups, a.listForRepositoriesErr } diff --git a/pkg/store/ketchup/ketchup.go b/pkg/store/ketchup/ketchup.go index 0e77481c..f5adbc90 100644 --- a/pkg/store/ketchup/ketchup.go +++ b/pkg/store/ketchup/ketchup.go @@ -16,7 +16,7 @@ import ( type App interface { DoAtomic(ctx context.Context, action func(context.Context) error) error List(ctx context.Context, page, pageSize uint) ([]model.Ketchup, uint64, error) - ListByRepositoriesID(ctx context.Context, ids []uint64) ([]model.Ketchup, error) + ListByRepositoriesID(ctx context.Context, ids []uint64, frequency model.KetchupFrequency) ([]model.Ketchup, error) GetByRepositoryID(ctx context.Context, id uint64, forUpdate bool) (model.Ketchup, error) Create(ctx context.Context, o model.Ketchup) (uint64, error) Update(ctx context.Context, o model.Ketchup) error @@ -120,9 +120,10 @@ FROM WHERE repository_id = ANY ($1) AND k.user_id = u.id + AND k.frequency = $2 ` -func (a app) ListByRepositoriesID(ctx context.Context, ids []uint64) ([]model.Ketchup, error) { +func (a app) ListByRepositoriesID(ctx context.Context, ids []uint64, frequency model.KetchupFrequency) ([]model.Ketchup, error) { list := make([]model.Ketchup, 0) scanner := func(rows *sql.Rows) error { @@ -144,7 +145,7 @@ func (a app) ListByRepositoriesID(ctx context.Context, ids []uint64) ([]model.Ke return nil } - return list, db.List(ctx, a.db, scanner, listByRepositoriesIDQuery, pq.Array(ids)) + return list, db.List(ctx, a.db, scanner, listByRepositoriesIDQuery, pq.Array(ids), strings.ToLower(frequency.String())) } const getQuery = ` diff --git a/pkg/store/ketchup/ketchup_test.go b/pkg/store/ketchup/ketchup_test.go index 01004b45..c398762f 100644 --- a/pkg/store/ketchup/ketchup_test.go +++ b/pkg/store/ketchup/ketchup_test.go @@ -171,7 +171,8 @@ func TestList(t *testing.T) { func TestListByRepositoriesID(t *testing.T) { type args struct { - ids []uint64 + ids []uint64 + frequency model.KetchupFrequency } var cases = []struct { @@ -206,7 +207,8 @@ func TestListByRepositoriesID(t *testing.T) { { "timeout", args{ - ids: []uint64{1, 2}, + ids: []uint64{1, 2}, + frequency: model.Daily, }, make([]model.Ketchup, 0), sqlmock.ErrCancelled, @@ -225,7 +227,7 @@ func TestListByRepositoriesID(t *testing.T) { t.Run(tc.intention, func(t *testing.T) { testWithMock(t, func(mockDb *sql.DB, mock sqlmock.Sqlmock) { rows := sqlmock.NewRows([]string{"pattern", "version", "frequency", "repository_id", "user_id", "email"}) - expectedQuery := mock.ExpectQuery("SELECT k.pattern, k.version, k.frequency, k.repository_id, k.user_id, u.email FROM ketchup.ketchup k, ketchup.user u WHERE repository_id = ANY .+ AND k.user_id = u.id").WithArgs(pq.Array(tc.args.ids)).WillReturnRows(rows) + expectedQuery := mock.ExpectQuery("SELECT k.pattern, k.version, k.frequency, k.repository_id, k.user_id, u.email FROM ketchup.ketchup k, ketchup.user u WHERE repository_id = ANY .+ AND k.user_id = u.id AND k.frequency = .+").WithArgs(pq.Array(tc.args.ids), strings.ToLower(tc.args.frequency.String())).WillReturnRows(rows) switch tc.intention { case "simple": @@ -243,7 +245,7 @@ func TestListByRepositoriesID(t *testing.T) { rows.AddRow(model.DefaultPattern, "0.9.0", "Daily", "a", 1, testEmail) } - got, gotErr := New(mockDb).ListByRepositoriesID(testCtx, tc.args.ids) + got, gotErr := New(mockDb).ListByRepositoriesID(testCtx, tc.args.ids, tc.args.frequency) failed := false if tc.wantErr == nil && gotErr != nil { diff --git a/pkg/store/ketchup/ketchuptest/ketchuptest.go b/pkg/store/ketchup/ketchuptest/ketchuptest.go index 395a26eb..6281c900 100644 --- a/pkg/store/ketchup/ketchuptest/ketchuptest.go +++ b/pkg/store/ketchup/ketchuptest/ketchuptest.go @@ -105,12 +105,12 @@ func (a *App) List(_ context.Context, page, pageSize uint) ([]model.Ketchup, uin } // ListByRepositoriesID mocks -func (a *App) ListByRepositoriesID(_ context.Context, ids []uint64) ([]model.Ketchup, error) { +func (a *App) ListByRepositoriesID(_ context.Context, _ []uint64, _ model.KetchupFrequency) ([]model.Ketchup, error) { return a.listByRepositoriesIDKetchups, a.listByRepositoriesIDErr } // GetByRepositoryID mocks -func (a *App) GetByRepositoryID(_ context.Context, id uint64, forUpdate bool) (model.Ketchup, error) { +func (a *App) GetByRepositoryID(_ context.Context, _ uint64, _ bool) (model.Ketchup, error) { return a.getByRepositoryKetchup, a.getByRepositoryErr }