grafana/pkg/services/ngalert/state/historian/dashboard_test.go
idafurjes 7c2522c477
Chore: Move dashboard models to dashboard pkg (#61458)
* Copy dashboard models to dashboard pkg

* Use some models from current pkg instead of models

* Adjust api pkg

* Adjust pkg services

* Fix lint
2023-01-16 16:33:55 +01:00

45 lines
1.4 KiB
Go

package historian
import (
"context"
"testing"
"time"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestDashboardResolver(t *testing.T) {
t.Run("fetches dashboards from dashboard service", func(t *testing.T) {
dbs := &dashboards.FakeDashboardService{}
exp := int64(14)
dbs.On("GetDashboard", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
args.Get(1).(*dashboards.GetDashboardQuery).Result = &dashboards.Dashboard{ID: exp}
}).Return(nil)
sut := createDashboardResolverSut(dbs)
id, err := sut.getID(context.Background(), 1, "dashboard-uid")
require.NoError(t, err)
require.Equal(t, exp, id)
})
t.Run("fetches dashboardNotFound if underlying dashboard does not exist", func(t *testing.T) {
dbs := &dashboards.FakeDashboardService{}
dbs.On("GetDashboard", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
args.Get(1).(*dashboards.GetDashboardQuery).Result = nil
}).Return(dashboards.ErrDashboardNotFound)
sut := createDashboardResolverSut(dbs)
_, err := sut.getID(context.Background(), 1, "not-exist")
require.Error(t, err)
require.ErrorIs(t, err, dashboards.ErrDashboardNotFound)
})
}
func createDashboardResolverSut(dbs *dashboards.FakeDashboardService) *dashboardResolver {
return newDashboardResolver(dbs, 1*time.Nanosecond)
}