Skip to content

Commit

Permalink
Expect sql.ErrNoRows when querying read_errors table
Browse files Browse the repository at this point in the history
  • Loading branch information
epapbak committed Aug 7, 2024
1 parent 846f7af commit 580a8bc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion differ/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,9 @@ func (d *Differ) produceEntriesToKafka(cluster types.ClusterEntry, ruleContent t
}

// checkReadError function checks whether reading from read_errors table was
// successful
// successful. This log gives important context as the insertion into the
// read_error will fail if there's already a row but we couldn't read it for
// whatever reason.
func checkReadError(err error) {
if err != nil {
log.Err(err).Msg("read_errors read access error")
Expand Down
4 changes: 4 additions & 0 deletions differ/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ func (storage DBStorage) ReadErrorExists(

// check for any error during query
if err != nil {
// errNoRows is expected
if err == sql.ErrNoRows {
return false, nil
}
return false, err
}

Expand Down
32 changes: 32 additions & 0 deletions differ/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,38 @@ func TestReadErrorOnError(t *testing.T) {
checkAllExpectations(t, mock)
}

// TestReadErrorOnErrorNoRows checks if Storage.ReadErrorExists returns
// expected results when no rows are found
func TestReadErrorOnErrorNoRows(t *testing.T) {
// error to be thrown
mockedError := sql.ErrNoRows

// prepare new mocked connection to database
connection, mock := mustCreateMockConnection(t)

// expected query performed by tested function
expectedQuery := "SELECT exists\\(SELECT 1 FROM read_errors WHERE org_id=\\$1 and cluster=\\$2 and updated_at=\\$3\\);"

// let's raise an error!
mock.ExpectQuery(expectedQuery).WillReturnError(mockedError)
mock.ExpectClose()

// prepare connection to mocked database
storage := differ.NewFromConnection(connection, 1)

// call the tested method
_, err := storage.ReadErrorExists(1, "123", types.Timestamp(time.Now()))

// error is expected to be returned from called method
assert.Nil(t, err, "no error is expected if no row is found in read_errors table")

// connection to mocked DB needs to be closed properly
checkConnectionClose(t, connection)

// check if all expectations were met
checkAllExpectations(t, mock)
}

// TestWriteReadError function checks the method
// Storage.WriteReadError.
func TestWriteReadError(t *testing.T) {
Expand Down

0 comments on commit 580a8bc

Please sign in to comment.