Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Add missing postgres cache init config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bsardo authored Oct 22, 2020
1 parent b0f8d71 commit 445c207
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config/stored_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ func (cfg *PostgresConfig) validate(dataType DataType, errs configErrors) config
return errs
}

return cfg.PollUpdates.validate(dataType, errs)
errs = cfg.CacheInitialization.validate(dataType, errs)
errs = cfg.PollUpdates.validate(dataType, errs)
return errs
}

// PostgresConnection has options which put types to the Postgres Connection string. See:
Expand Down
105 changes: 105 additions & 0 deletions config/stored_requests_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package config

import (
"errors"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

const sampleQueryTemplate = "SELECT id, requestData, 'request' as type FROM stored_requests WHERE id in %REQUEST_ID_LIST% UNION ALL SELECT id, impData, 'imp' as type FROM stored_requests WHERE id in %IMP_ID_LIST%"
Expand Down Expand Up @@ -154,6 +157,108 @@ func TestInMemoryCacheValidationSingleCache(t *testing.T) {
}).validate(AccountDataType, nil))
}

func TestPostgresConfigValidation(t *testing.T) {
tests := []struct {
description string
connectionStr string
cacheInitQuery string
cacheInitTimeout int
cacheUpdateQuery string
cacheUpdateRefreshRate int
cacheUpdateTimeout int
existingErrors []error
wantErrorCount int
}{
{
description: "No connection string",
connectionStr: "",
},
{
description: "Connection string but no queries",
connectionStr: "some-connection-string",
},
{
description: "Valid cache init query with non-zero timeout",
connectionStr: "some-connection-string",
cacheInitQuery: "SELECT * FROM table;",
cacheInitTimeout: 1,
},
{
description: "Valid cache init query with zero timeout",
connectionStr: "some-connection-string",
cacheInitQuery: "SELECT * FROM table;",
cacheInitTimeout: 0,
wantErrorCount: 1,
},
{
description: "Invalid cache init query contains wildcard",
connectionStr: "some-connection-string",
cacheInitQuery: "SELECT * FROM table WHERE $1",
cacheInitTimeout: 1,
wantErrorCount: 1,
},
{
description: "Valid cache update query with non-zero timeout and refresh rate",
connectionStr: "some-connection-string",
cacheUpdateQuery: "SELECT * FROM table WHERE $1",
cacheUpdateRefreshRate: 1,
cacheUpdateTimeout: 1,
},
{
description: "Valid cache update query with zero timeout and non-zero refresh rate",
connectionStr: "some-connection-string",
cacheUpdateQuery: "SELECT * FROM table WHERE $1",
cacheUpdateRefreshRate: 1,
cacheUpdateTimeout: 0,
wantErrorCount: 1,
},
{
description: "Valid cache update query with non-zero timeout and zero refresh rate",
connectionStr: "some-connection-string",
cacheUpdateQuery: "SELECT * FROM table WHERE $1",
cacheUpdateRefreshRate: 0,
cacheUpdateTimeout: 1,
wantErrorCount: 1,
},
{
description: "Invalid cache update query missing wildcard",
connectionStr: "some-connection-string",
cacheUpdateQuery: "SELECT * FROM table",
cacheUpdateRefreshRate: 1,
cacheUpdateTimeout: 1,
wantErrorCount: 1,
},
{
description: "Multiple errors: valid queries missing timeouts and refresh rates plus existing error",
connectionStr: "some-connection-string",
cacheInitQuery: "SELECT * FROM table;",
cacheUpdateQuery: "SELECT * FROM table WHERE $1",
existingErrors: []error{errors.New("existing error before calling validate")},
wantErrorCount: 4,
},
}

for _, tt := range tests {
pgConfig := &PostgresConfig{
ConnectionInfo: PostgresConnection{
Database: tt.connectionStr,
},
CacheInitialization: PostgresCacheInitializer{
Query: tt.cacheInitQuery,
Timeout: tt.cacheInitTimeout,
},
PollUpdates: PostgresUpdatePolling{
Query: tt.cacheUpdateQuery,
RefreshRate: tt.cacheUpdateRefreshRate,
Timeout: tt.cacheUpdateTimeout,
},
}

errs := pgConfig.validate(RequestDataType, tt.existingErrors)
assert.Equal(t, tt.wantErrorCount, len(errs), tt.description)
}
}

func assertErrsExist(t *testing.T, err configErrors) {
t.Helper()
if len(err) == 0 {
Expand Down

0 comments on commit 445c207

Please sign in to comment.