github.com/influxdata/influxdb/v2@v2.7.6/dashboards/service_test.go (about)

     1  package dashboards
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/influxdata/influxdb/v2"
     8  	dashboardtesting "github.com/influxdata/influxdb/v2/dashboards/testing"
     9  	"github.com/influxdata/influxdb/v2/kv"
    10  	"github.com/influxdata/influxdb/v2/mock"
    11  	itesting "github.com/influxdata/influxdb/v2/testing"
    12  	"go.uber.org/zap/zaptest"
    13  )
    14  
    15  func TestBoltDashboardService(t *testing.T) {
    16  	dashboardtesting.DashboardService(initBoltDashboardService, t)
    17  }
    18  
    19  func initBoltDashboardService(f dashboardtesting.DashboardFields, t *testing.T) (influxdb.DashboardService, string, func()) {
    20  	s, closeBolt := itesting.NewTestBoltStore(t)
    21  	svc, op, closeSvc := initDashboardService(s, f, t)
    22  	return svc, op, func() {
    23  		closeSvc()
    24  		closeBolt()
    25  	}
    26  }
    27  
    28  func initDashboardService(s kv.SchemaStore, f dashboardtesting.DashboardFields, t *testing.T) (influxdb.DashboardService, string, func()) {
    29  	if f.TimeGenerator == nil {
    30  		f.TimeGenerator = influxdb.RealTimeGenerator{}
    31  	}
    32  
    33  	ctx := context.Background()
    34  	kvSvc := kv.NewService(zaptest.NewLogger(t), s, &mock.OrganizationService{})
    35  	kvSvc.IDGenerator = f.IDGenerator
    36  	kvSvc.TimeGenerator = f.TimeGenerator
    37  
    38  	svc := NewService(s, kvSvc)
    39  	svc.IDGenerator = f.IDGenerator
    40  	svc.TimeGenerator = f.TimeGenerator
    41  
    42  	for _, b := range f.Dashboards {
    43  		if err := svc.PutDashboard(ctx, b); err != nil {
    44  			t.Fatalf("failed to populate dashboards")
    45  		}
    46  	}
    47  	return svc, kv.OpPrefix, func() {
    48  		for _, b := range f.Dashboards {
    49  			if err := svc.DeleteDashboard(ctx, b.ID); err != nil {
    50  				t.Logf("failed to remove dashboard: %v", err)
    51  			}
    52  		}
    53  	}
    54  }