fix(sql storage): Query() should return ErrReleaseNotFound immediately when no records are found

Signed-off-by: Mike Ng <ming@redhat.com>
pull/8470/head
Mike Ng 4 years ago
parent ae8fcf445d
commit b86105aebc

@ -310,6 +310,10 @@ func (s *SQL) Query(labels map[string]string) ([]*rspb.Release, error) {
return nil, err return nil, err
} }
if len(records) == 0 {
return nil, ErrReleaseNotFound
}
var releases []*rspb.Release var releases []*rspb.Release
for _, record := range records { for _, record := range records {
release, err := decodeRelease(record.Body) release, err := decodeRelease(record.Body)
@ -320,10 +324,6 @@ func (s *SQL) Query(labels map[string]string) ([]*rspb.Release, error) {
releases = append(releases, release) releases = append(releases, release)
} }
if len(releases) == 0 {
return nil, ErrReleaseNotFound
}
return releases, nil return releases, nil
} }

@ -292,6 +292,11 @@ func TestSqlUpdate(t *testing.T) {
func TestSqlQuery(t *testing.T) { func TestSqlQuery(t *testing.T) {
// Reflect actual use cases in ../storage.go // Reflect actual use cases in ../storage.go
labelSetUnknown := map[string]string{
"name": "smug-pigeon",
"owner": sqlReleaseDefaultOwner,
"status": "unknown",
}
labelSetDeployed := map[string]string{ labelSetDeployed := map[string]string{
"name": "smug-pigeon", "name": "smug-pigeon",
"owner": sqlReleaseDefaultOwner, "owner": sqlReleaseDefaultOwner,
@ -320,6 +325,15 @@ func TestSqlQuery(t *testing.T) {
sqlReleaseTableNamespaceColumn, sqlReleaseTableNamespaceColumn,
) )
mock.
ExpectQuery(regexp.QuoteMeta(query)).
WithArgs("smug-pigeon", sqlReleaseDefaultOwner, "unknown", "default").
WillReturnRows(
mock.NewRows([]string{
sqlReleaseTableBodyColumn,
}),
).RowsWillBeClosed()
mock. mock.
ExpectQuery(regexp.QuoteMeta(query)). ExpectQuery(regexp.QuoteMeta(query)).
WithArgs("smug-pigeon", sqlReleaseDefaultOwner, "deployed", "default"). WithArgs("smug-pigeon", sqlReleaseDefaultOwner, "deployed", "default").
@ -353,6 +367,13 @@ func TestSqlQuery(t *testing.T) {
), ),
).RowsWillBeClosed() ).RowsWillBeClosed()
_, err := sqlDriver.Query(labelSetUnknown)
if err == nil {
t.Errorf("Expected error {%v}, got nil", ErrReleaseNotFound)
} else if err != ErrReleaseNotFound {
t.Fatalf("failed to query for unknown smug-pigeon release: %v", err)
}
results, err := sqlDriver.Query(labelSetDeployed) results, err := sqlDriver.Query(labelSetDeployed)
if err != nil { if err != nil {
t.Fatalf("failed to query for deployed smug-pigeon release: %v", err) t.Fatalf("failed to query for deployed smug-pigeon release: %v", err)

Loading…
Cancel
Save