-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Implemented Select to retrieve search criteria by criteria id
- Loading branch information
1 parent
5ca635e
commit 2efcee7
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |