package historian import ( "context" "testing" "time" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/services/annotations" "github.com/grafana/grafana/pkg/services/annotations/annotationstest" "github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/services/ngalert/models" "github.com/grafana/grafana/pkg/services/ngalert/tests/fakes" "github.com/stretchr/testify/require" ) func TestAnnotationHistorian_Integration(t *testing.T) { t.Run("alert annotations are queryable", func(t *testing.T) { anns := createTestAnnotationBackendSut(t) items := []annotations.Item{createAnnotation()} require.NoError(t, anns.recordAnnotationsSync(context.Background(), nil, items, log.NewNopLogger())) q := models.HistoryQuery{ RuleUID: "my-rule", OrgID: 1, } frame, err := anns.QueryStates(context.Background(), q) require.NoError(t, err) require.NotNil(t, frame) require.Len(t, frame.Fields, 5) for i := 0; i < 5; i++ { require.Equal(t, frame.Fields[i].Len(), 1) } }) } func createTestAnnotationBackendSut(t *testing.T) *AnnotationBackend { t.Helper() fakeAnnoRepo := annotationstest.NewFakeAnnotationsRepo() rules := fakes.NewRuleStore(t) rules.Rules[1] = []*models.AlertRule{ models.AlertRuleGen(withOrgID(1), withUID("my-rule"))(), } return NewAnnotationBackend(fakeAnnoRepo, &dashboards.FakeDashboardService{}, rules) } func createAnnotation() annotations.Item { return annotations.Item{ Id: 1, OrgId: 1, AlertId: 1, Text: "MyAlert {a=b} - No data", Data: simplejson.New(), Epoch: time.Now().UnixNano() / int64(time.Millisecond), } } func withOrgID(orgId int64) func(rule *models.AlertRule) { return func(rule *models.AlertRule) { rule.OrgID = orgId } } func withUID(uid string) func(rule *models.AlertRule) { return func(rule *models.AlertRule) { rule.UID = uid } }