Skip to content

Commit

Permalink
feat: Adding frequency in the query for notifying
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed May 5, 2021
1 parent 4944ba8 commit fc3cc6c
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/ketchup/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkg/service/ketchup/ketchuptest/ketchuptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/store/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 = `
Expand Down
10 changes: 6 additions & 4 deletions pkg/store/ketchup/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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":
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/store/ketchup/ketchuptest/ketchuptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit fc3cc6c

Please sign in to comment.