github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/agent/mcorpc/aggregate/chart_test.go (about)

     1  // Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package aggregate
     6  
     7  import (
     8  	"encoding/json"
     9  
    10  	"github.com/guptarohit/asciigraph"
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("ChartAggregator", func() {
    16  	var (
    17  		err error
    18  		agg *ChartAggregator
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		agg, err = NewChartAggregator([]any{})
    23  		Expect(err).ToNot(HaveOccurred())
    24  	})
    25  
    26  	Describe("ProcessValue", func() {
    27  		It("Should process various values", func() {
    28  			Expect(agg.ProcessValue(1)).ToNot(HaveOccurred())
    29  			Expect(agg.ProcessValue(7)).ToNot(HaveOccurred())
    30  			Expect(agg.ProcessValue("50")).ToNot(HaveOccurred())
    31  
    32  			Expect(agg.items).To(Equal([]float64{1, 7, 50}))
    33  		})
    34  	})
    35  
    36  	Describe("Results", func() {
    37  		var expected string
    38  
    39  		BeforeEach(func() {
    40  			Expect(agg.ProcessValue(1)).ToNot(HaveOccurred())
    41  			Expect(agg.ProcessValue(7)).ToNot(HaveOccurred())
    42  			Expect(agg.ProcessValue("50")).ToNot(HaveOccurred())
    43  
    44  			Expect(agg.items).To(Equal([]float64{1, 7, 50}))
    45  			expected = asciigraph.Plot([]float64{1, 7, 50}, asciigraph.Height(15), asciigraph.Width(60), asciigraph.Offset(5))
    46  		})
    47  
    48  		Describe("ResultStrings", func() {
    49  			It("Should produce the correct result", func() {
    50  				results, err := agg.ResultStrings()
    51  				Expect(err).ToNot(HaveOccurred())
    52  				Expect(results["Chart"]).To(Equal(expected))
    53  			})
    54  		})
    55  
    56  		Describe("ResultJSON", func() {
    57  			It("Should produce the correct result", func() {
    58  				results, err := agg.ResultJSON()
    59  				Expect(err).ToNot(HaveOccurred())
    60  
    61  				jexpected, err := json.Marshal(map[string]string{
    62  					"chart": expected,
    63  				})
    64  				Expect(err).ToNot(HaveOccurred())
    65  				Expect(results).To(Equal(jexpected))
    66  			})
    67  		})
    68  
    69  		Describe("ResultFormattedStrings", func() {
    70  			It("Should produce the correct result", func() {
    71  				results, err := agg.ResultFormattedStrings("")
    72  				Expect(err).ToNot(HaveOccurred())
    73  				Expect(results[0]).To(Equal(expected))
    74  			})
    75  		})
    76  	})
    77  })