github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/service/application_metadata_test.go (about) 1 package service_test 2 3 import ( 4 "context" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 "github.com/pyroscope-io/pyroscope/pkg/model" 9 "github.com/pyroscope-io/pyroscope/pkg/model/appmetadata" 10 "github.com/pyroscope-io/pyroscope/pkg/service" 11 "github.com/pyroscope-io/pyroscope/pkg/storage/metadata" 12 ) 13 14 var _ = Describe("ApplicationMetadataService", func() { 15 s := new(testSuite) 16 BeforeEach(s.BeforeEach) 17 AfterEach(s.AfterEach) 18 19 var svc service.ApplicationMetadataService 20 BeforeEach(func() { 21 svc = service.NewApplicationMetadataService(s.DB()) 22 }) 23 24 app := appmetadata.ApplicationMetadata{ 25 FQName: "myapp", 26 SampleRate: 100, 27 SpyName: "gospy", 28 Units: metadata.SamplesUnits, 29 AggregationType: metadata.AverageAggregationType, 30 } 31 32 assertNumOfApps := func(num int) []appmetadata.ApplicationMetadata { 33 apps, err := svc.List(context.TODO()) 34 Expect(err).ToNot(HaveOccurred()) 35 Expect(len(apps)).To(Equal(num)) 36 return apps 37 } 38 39 It("validates input", func() { 40 ctx := context.TODO() 41 42 // Create 43 err := svc.CreateOrUpdate(ctx, appmetadata.ApplicationMetadata{FQName: ""}) 44 Expect(err).To(HaveOccurred()) 45 Expect(model.IsValidationError(err)).To(BeTrue()) 46 47 // Get 48 _, err = svc.Get(ctx, "") 49 Expect(err).To(HaveOccurred()) 50 Expect(model.IsValidationError(err)).To(BeTrue()) 51 52 // Delete 53 err = svc.Delete(ctx, "") 54 Expect(err).To(HaveOccurred()) 55 Expect(model.IsValidationError(err)).To(BeTrue()) 56 }) 57 58 Context("create/update", func() { 59 It("upserts", func() { 60 assertNumOfApps(0) 61 62 ctx := context.TODO() 63 err := svc.CreateOrUpdate(ctx, app) 64 Expect(err).ToNot(HaveOccurred()) 65 66 err = svc.CreateOrUpdate(ctx, app) 67 Expect(err).ToNot(HaveOccurred()) 68 assertNumOfApps(1) 69 }) 70 71 It("handle partial updates", func() { 72 ctx := context.TODO() 73 err := svc.CreateOrUpdate(ctx, app) 74 Expect(err).ToNot(HaveOccurred()) 75 76 err = svc.CreateOrUpdate(ctx, appmetadata.ApplicationMetadata{ 77 FQName: app.FQName, 78 SampleRate: 101, 79 }) 80 Expect(err).ToNot(HaveOccurred()) 81 82 a, err := svc.Get(ctx, app.FQName) 83 Expect(err).ToNot(HaveOccurred()) 84 85 // Other fields should not be touched 86 app2 := app 87 app2.SampleRate = 101 88 Expect(a).To(Equal(app2)) 89 }) 90 91 }) 92 93 Context("get", func() { 94 It("fetches correctly", func() { 95 ctx := context.TODO() 96 err := svc.CreateOrUpdate(ctx, app) 97 Expect(err).ToNot(HaveOccurred()) 98 99 res, err := svc.Get(ctx, app.FQName) 100 Expect(err).ToNot(HaveOccurred()) 101 Expect(res).To(Equal(app)) 102 }) 103 It("fails when app doesn't exist", func() { 104 ctx := context.TODO() 105 _, err := svc.Get(ctx, "non_existing_app") 106 Expect(err).To(HaveOccurred()) 107 Expect(err).To(MatchError(model.ErrApplicationNotFound)) 108 }) 109 }) 110 111 Context("delete", func() { 112 It("deletes correctly", func() { 113 ctx := context.TODO() 114 err := svc.CreateOrUpdate(ctx, app) 115 Expect(err).ToNot(HaveOccurred()) 116 assertNumOfApps(1) 117 118 err = svc.Delete(ctx, app.FQName) 119 120 Expect(err).ToNot(HaveOccurred()) 121 assertNumOfApps(0) 122 }) 123 124 It("doesn't fail when app doesn't exist", func() { 125 ctx := context.TODO() 126 err := svc.Delete(ctx, "non_existing_app") 127 Expect(err).ToNot(HaveOccurred()) 128 }) 129 }) 130 131 })