From 2efcee7bc48c0d609d84f3a2d773cd531b0280b1 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 6 Sep 2024 01:14:50 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Implemented=20Select=20to?= =?UTF-8?q?=20retrieve=20search=20criteria=20by=20criteria=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/api/search/criteria/errors.go | 5 +++ cmd/api/search/criteria/mocks.go | 17 +++++++++ cmd/api/search/criteria/select.go | 37 ++++++++++++++++++++ cmd/api/search/criteria/select_test.go | 48 ++++++++++++++++++++++++++ cmd/api/search/criteria/types.go | 15 ++++++++ 5 files changed, 122 insertions(+) create mode 100644 cmd/api/search/criteria/errors.go create mode 100644 cmd/api/search/criteria/mocks.go create mode 100644 cmd/api/search/criteria/select.go create mode 100644 cmd/api/search/criteria/select_test.go create mode 100644 cmd/api/search/criteria/types.go diff --git a/cmd/api/search/criteria/errors.go b/cmd/api/search/criteria/errors.go new file mode 100644 index 0000000..fbaa0dd --- /dev/null +++ b/cmd/api/search/criteria/errors.go @@ -0,0 +1,5 @@ +package criteria + +import "errors" + +var FailedToRetrieveCriteriaData = errors.New("failed to retrieve criteria data") diff --git a/cmd/api/search/criteria/mocks.go b/cmd/api/search/criteria/mocks.go new file mode 100644 index 0000000..463d159 --- /dev/null +++ b/cmd/api/search/criteria/mocks.go @@ -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", + } +} diff --git a/cmd/api/search/criteria/select.go b/cmd/api/search/criteria/select.go new file mode 100644 index 0000000..640358c --- /dev/null +++ b/cmd/api/search/criteria/select.go @@ -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 + } +} diff --git a/cmd/api/search/criteria/select_test.go b/cmd/api/search/criteria/select_test.go new file mode 100644 index 0000000..4c8d912 --- /dev/null +++ b/cmd/api/search/criteria/select_test.go @@ -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) +} diff --git a/cmd/api/search/criteria/types.go b/cmd/api/search/criteria/types.go new file mode 100644 index 0000000..5519721 --- /dev/null +++ b/cmd/api/search/criteria/types.go @@ -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 +}