Skip to content

Commit

Permalink
⚡️ Implemented Select to retrieve search criteria by criteria id
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Sep 6, 2024
1 parent 5ca635e commit 2efcee7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/api/search/criteria/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package criteria

import "errors"

var FailedToRetrieveCriteriaData = errors.New("failed to retrieve criteria data")
17 changes: 17 additions & 0 deletions cmd/api/search/criteria/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package criteria

// MockCriteria mocks a criteria.Type
func MockCriteria() Type {
return Type{
ID: 1,
Name: "Example",
AllOfTheseWords: []string{"word1", "word2"},
ThisExactPhrase: "exact phrase",
AnyOfTheseWords: []string{"any1", "any2"},
NoneOfTheseWords: []string{"none1", "none2"},
TheseHashtags: []string{"#hashtag1", "#hashtag2"},
Language: "es",
Since: "2006-01-01",
Until: "2024-01-01",
}
}
37 changes: 37 additions & 0 deletions cmd/api/search/criteria/select.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package criteria

import (
"context"
"errors"
"fmt"

"github.com/jackc/pgx/v5"

"ahbcc/internal/database"
"ahbcc/internal/log"
)

// SelectByID returns a criteria seeking by criteria ID
type SelectByID func(ctx context.Context, id int) (Type, error)

// MakeSelectByID creates a new SelectByID
func MakeSelectByID(db database.Connection) SelectByID {
const query string = `
SELECT id, name, all_of_these_words, this_exact_phrase, any_of_these_words, none_of_these_words, these_hashtags, language, since_date, until_date
FROM search_criteria
WHERE id = %d
`

return func(ctx context.Context, id int) (Type, error) {
queryToExecute := fmt.Sprintf(query, id)

var criteria Type
err := db.QueryRow(ctx, queryToExecute).Scan(&criteria)
if errors.Is(err, pgx.ErrNoRows) {
log.Error(ctx, err.Error())
return Type{}, FailedToRetrieveCriteriaData
}

return criteria, nil
}
}
48 changes: 48 additions & 0 deletions cmd/api/search/criteria/select_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package criteria_test

import (
"context"
"testing"

"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"ahbcc/cmd/api/search/criteria"
"ahbcc/internal/database"
)

func TestSelectByID_success(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRow := new(database.MockPgxRow)
mockCriteria := criteria.MockCriteria()
database.MockScan[criteria.Type](mockPgxRow, mockCriteria, t)
mockPostgresConnection.On("QueryRow", mock.Anything, mock.Anything, mock.Anything).Return(mockPgxRow)

selectCriteriaByID := criteria.MakeSelectByID(mockPostgresConnection)

want := mockCriteria
got, err := selectCriteriaByID(context.Background(), 1)

assert.Nil(t, err)
assert.Equal(t, want, got)
mockPostgresConnection.AssertExpectations(t)
mockPgxRow.AssertExpectations(t)

}

func TestSelectByID_failsWhenSelectOperationFails(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRow := new(database.MockPgxRow)
mockPgxRow.On("Scan", mock.Anything).Return(pgx.ErrNoRows)
mockPostgresConnection.On("QueryRow", mock.Anything, mock.Anything, mock.Anything).Return(mockPgxRow)

selectCriteriaByID := criteria.MakeSelectByID(mockPostgresConnection)

want := criteria.FailedToRetrieveCriteriaData
_, got := selectCriteriaByID(context.Background(), 1)

assert.Equal(t, want, got)
mockPostgresConnection.AssertExpectations(t)
mockPgxRow.AssertExpectations(t)
}
15 changes: 15 additions & 0 deletions cmd/api/search/criteria/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package criteria

// Type represents a search criteria
type Type struct {
ID int
Name string
AllOfTheseWords []string
ThisExactPhrase string
AnyOfTheseWords []string
NoneOfTheseWords []string
TheseHashtags []string
Language string
Since string
Until string
}

0 comments on commit 2efcee7

Please sign in to comment.