github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/service/annotation_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/pyroscope-io/pyroscope/pkg/model"
    10  	"github.com/pyroscope-io/pyroscope/pkg/service"
    11  )
    12  
    13  var _ = Describe("AnnotationsService", func() {
    14  	s := new(testSuite)
    15  	BeforeEach(s.BeforeEach)
    16  	AfterEach(s.AfterEach)
    17  
    18  	var svc service.AnnotationsService
    19  	BeforeEach(func() {
    20  		svc = service.NewAnnotationsService(s.DB())
    21  	})
    22  
    23  	Describe("create annotation", func() {
    24  		It("works", func() {
    25  			now := time.Now()
    26  
    27  			annotation, err := svc.CreateAnnotation(context.Background(), model.CreateAnnotation{
    28  				AppName:   "myapp",
    29  				Content:   "mycontent",
    30  				Timestamp: now,
    31  			})
    32  
    33  			Expect(err).ToNot(HaveOccurred())
    34  			Expect(annotation).ToNot(BeNil())
    35  			Expect(annotation.AppName).To(Equal("myapp"))
    36  			Expect(annotation.Content).To(Equal("mycontent"))
    37  			Expect(annotation.Timestamp.Unix()).To(Equal(now.Unix()))
    38  
    39  			Expect(annotation.CreatedAt).ToNot(BeZero())
    40  			Expect(annotation.UpdatedAt).ToNot(BeZero())
    41  		})
    42  
    43  		It("validates parameters", func() {
    44  			annotation, err := svc.CreateAnnotation(context.Background(), model.CreateAnnotation{})
    45  
    46  			Expect(err).To(HaveOccurred())
    47  			Expect(annotation).To(BeNil())
    48  		})
    49  
    50  		When("an annotation already exists", func() {
    51  			p := model.CreateAnnotation{
    52  				AppName:   "myapp",
    53  				Content:   "mycontent",
    54  				Timestamp: time.Now(),
    55  			}
    56  
    57  			BeforeEach(func() {
    58  				annotation, err := svc.CreateAnnotation(context.Background(), p)
    59  				Expect(err).ToNot(HaveOccurred())
    60  				Expect(annotation).ToNot(BeNil())
    61  			})
    62  
    63  			It("upserts", func() {
    64  				annotation, err := svc.CreateAnnotation(context.Background(), model.CreateAnnotation{
    65  					AppName:   p.AppName,
    66  					Timestamp: p.Timestamp,
    67  					Content:   "mycontent updated",
    68  				})
    69  				Expect(err).ToNot(HaveOccurred())
    70  				Expect(annotation).ToNot(BeNil())
    71  
    72  				annotations, err := svc.FindAnnotationsByTimeRange(
    73  					context.Background(), "myapp",
    74  					time.Now().Add(-time.Hour),
    75  					time.Now())
    76  
    77  				Expect(annotations[0].Content).To(Equal("mycontent updated"))
    78  				Expect(err).ToNot(HaveOccurred())
    79  				Expect(annotations).ToNot(BeEmpty())
    80  				Expect(len(annotations)).To(Equal(1))
    81  			})
    82  		})
    83  
    84  	})
    85  
    86  	Describe("find annotations", func() {
    87  		When("there's no annotation", func() {
    88  			It("returns empty array", func() {
    89  				now := time.Now()
    90  
    91  				annotations, err := svc.FindAnnotationsByTimeRange(context.Background(), "myapp", now, now)
    92  				Expect(err).ToNot(HaveOccurred())
    93  				Expect(annotations).To(BeEmpty())
    94  			})
    95  		})
    96  
    97  		When("annotation exists", func() {
    98  			var now time.Time
    99  
   100  			BeforeEach(func() {
   101  				now = time.Now()
   102  				_, err := svc.CreateAnnotation(context.Background(), model.CreateAnnotation{
   103  					AppName:   "myapp",
   104  					Content:   "mycontent",
   105  					Timestamp: now,
   106  				})
   107  				Expect(err).ToNot(HaveOccurred())
   108  			})
   109  
   110  			When("finding within interval", func() {
   111  				It("finds correctly", func() {
   112  					annotations, err := svc.FindAnnotationsByTimeRange(context.Background(), "myapp", now, now)
   113  					Expect(err).ToNot(HaveOccurred())
   114  					Expect(annotations).ToNot(BeEmpty())
   115  					Expect(len(annotations)).To(Equal(1))
   116  				})
   117  			})
   118  
   119  			When("finding outside the interval", func() {
   120  				It("returns empty array", func() {
   121  					annotations, err := svc.FindAnnotationsByTimeRange(context.Background(), "myapp",
   122  						time.Now().Add(time.Hour),
   123  						time.Now().Add(time.Hour*2),
   124  					)
   125  
   126  					Expect(err).ToNot(HaveOccurred())
   127  					Expect(annotations).To(BeEmpty())
   128  				})
   129  			})
   130  		})
   131  	})
   132  })