Skip to content

Commit

Permalink
⚡️ Added criteria.SelectAll function to retrieve all the criteria fro…
Browse files Browse the repository at this point in the history
…m the database
  • Loading branch information
lhbelfanti committed Sep 16, 2024
1 parent 0014ea4 commit 8a81669
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 14 deletions.
18 changes: 18 additions & 0 deletions cmd/api/search/criteria/daos.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package criteria

import "ahbcc/internal/scrapper"

type (
// DAO represents a search criteria
DAO struct {
Expand All @@ -22,3 +24,19 @@ type (
SearchCriteriaID int `json:"search_criteria_id"`
}
)

// toCriteriaDTO converts a criteria.DAO into a scrapper.CriteriaDTO
func (dao DAO) toCriteriaDTO() scrapper.CriteriaDTO {
return scrapper.CriteriaDTO{
ID: dao.ID,
Name: dao.Name,
AllOfTheseWords: dao.AllOfTheseWords,
ThisExactPhrase: dao.ThisExactPhrase,
AnyOfTheseWords: dao.AllOfTheseWords,
NoneOfTheseWords: dao.NoneOfTheseWords,
TheseHashtags: dao.TheseHashtags,
Language: dao.Language,
Since: dao.Since,
Until: dao.Until,
}
}
15 changes: 1 addition & 14 deletions cmd/api/search/criteria/enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,7 @@ func MakeEnqueue(selectCriteriaByID SelectByID, selectExecutionsByStatuses Selec
}
}

body := scrapper.CriteriaDTO{
ID: criteriaDAO.ID,
Name: criteriaDAO.Name,
AllOfTheseWords: criteriaDAO.AllOfTheseWords,
ThisExactPhrase: criteriaDAO.ThisExactPhrase,
AnyOfTheseWords: criteriaDAO.AllOfTheseWords,
NoneOfTheseWords: criteriaDAO.NoneOfTheseWords,
TheseHashtags: criteriaDAO.TheseHashtags,
Language: criteriaDAO.Language,
Since: criteriaDAO.Since,
Until: criteriaDAO.Until,
}

err = enqueueCriteria(ctx, body)
err = enqueueCriteria(ctx, criteriaDAO.toCriteriaDTO())
if err != nil {
log.Error(ctx, err.Error())
return FailedToExecuteEnqueueCriteria
Expand Down
3 changes: 3 additions & 0 deletions cmd/api/search/criteria/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const (
var (
FailedToRetrieveCriteriaData = errors.New("failed to retrieve criteria data")

FailedToRetrieveAllCriteriaData = errors.New("failed to retrieve all criteria data")
FailedToExecuteSelectCollectRowsInSelectAll = errors.New("failed to execute select collect rows in select all")

FailedToExecuteSelectCriteriaByID = errors.New("failed to execute select criteria by id")
FailedToExecuteSelectExecutionsByStatuses = errors.New("failed to execute select executions by statuses")
AnExecutionOfThisCriteriaIDIsAlreadyEnqueued = errors.New("an execution of this criteria is already enqueued")
Expand Down
30 changes: 30 additions & 0 deletions cmd/api/search/criteria/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ func MockCriteriaDAO() DAO {
}
}

// MockCriteriaDAOSlice mocks a []criteria.DAO
func MockCriteriaDAOSlice() []DAO {
return []DAO{
{
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",
},
{
ID: 2,
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",
},
}
}

// MockExecutionDayDTO mocks an ExecutionDayDTO
func MockExecutionDayDTO(errorReason *string) ExecutionDayDTO {
return ExecutionDayDTO{
Expand Down
28 changes: 28 additions & 0 deletions cmd/api/search/criteria/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type (
// SelectByID returns a criteria seeking by criteria ID
SelectByID func(ctx context.Context, id int) (DAO, error)

// SelectAll returns all the criteria of the 'search_criteria' table
SelectAll func(ctx context.Context) ([]DAO, error)

// SelectExecutionsByStatuses returns all the search criteria executions in certain state
SelectExecutionsByStatuses func(ctx context.Context, statuses []string) ([]ExecutionDAO, error)
)
Expand All @@ -40,6 +43,31 @@ func MakeSelectByID(db database.Connection) SelectByID {
}
}

// MakeSelectAll creates a new SelectAll
func MakeSelectAll(db database.Connection, collectRows database.CollectRows[DAO]) SelectAll {
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
`

return func(ctx context.Context) ([]DAO, error) {
rows, err := db.Query(ctx, query)
if err != nil {
log.Error(ctx, err.Error())
return nil, FailedToRetrieveAllCriteriaData
}
defer rows.Close()

searchCriteria, err := collectRows(rows)
if err != nil {
log.Error(ctx, err.Error())
return nil, FailedToExecuteSelectCollectRowsInSelectAll
}

return searchCriteria, nil
}
}

// MakeSelectExecutionsByStatuses creates a new SelectExecutionsByStatuses
func MakeSelectExecutionsByStatuses(db database.Connection, collectRows database.CollectRows[ExecutionDAO]) SelectExecutionsByStatuses {
const query string = `
Expand Down
54 changes: 54 additions & 0 deletions cmd/api/search/criteria/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,60 @@ func TestSelectByID_failsWhenSelectOperationFails(t *testing.T) {
mockPgxRow.AssertExpectations(t)
}

func TestSelectAll_success(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRows := new(database.MockPgxRows)
mockPgxRows.On("Close").Return()
mockPostgresConnection.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(mockPgxRows, nil)
mockExecutionDAOSlice := criteria.MockCriteriaDAOSlice()
mockCollectRows := database.MockCollectRows[criteria.DAO](mockExecutionDAOSlice, nil)

selectAllCriteria := criteria.MakeSelectAll(mockPostgresConnection, mockCollectRows)

want := mockExecutionDAOSlice
got, err := selectAllCriteria(context.Background())

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

func TestSelectAll_failsWhenSelectOperationThrowsError(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRows := new(database.MockPgxRows)
mockPostgresConnection.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(mockPgxRows, errors.New("failed to select all criteria"))
mockExecutionDAOSlice := criteria.MockCriteriaDAOSlice()
mockCollectRows := database.MockCollectRows[criteria.DAO](mockExecutionDAOSlice, nil)

selectAllCriteria := criteria.MakeSelectAll(mockPostgresConnection, mockCollectRows)

want := criteria.FailedToRetrieveAllCriteriaData
_, got := selectAllCriteria(context.Background())

assert.Equal(t, want, got)
mockPostgresConnection.AssertExpectations(t)
mockPgxRows.AssertExpectations(t)
}

func TestSelectAll_failsWhenCollectRowsThrowsError(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRows := new(database.MockPgxRows)
mockPgxRows.On("Close").Return()
mockPostgresConnection.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(mockPgxRows, nil)
mockExecutionDAOSlice := criteria.MockCriteriaDAOSlice()
mockCollectRows := database.MockCollectRows[criteria.DAO](mockExecutionDAOSlice, errors.New("failed to collect rows"))

selectAllCriteria := criteria.MakeSelectAll(mockPostgresConnection, mockCollectRows)

want := criteria.FailedToExecuteSelectCollectRowsInSelectAll
_, got := selectAllCriteria(context.Background())

assert.Equal(t, want, got)
mockPostgresConnection.AssertExpectations(t)
mockPgxRows.AssertExpectations(t)
}

func TestSelectExecutionsByState_success(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPgxRows := new(database.MockPgxRows)
Expand Down

0 comments on commit 8a81669

Please sign in to comment.