cloud-bulldozer / go-commons

Code repository with all go common packages and libraries
Apache License 2.0
4 stars 9 forks source link

Unit Tests comparison and prometheus package #17

Closed shashank-boyapally closed 1 year ago

shashank-boyapally commented 1 year ago

Unit Tested go-commons comparison package

Description

Unit Tests for go-commons comparison were written using gomega and ginkgo packages, 100% coverage was achieved using mock implementation of functions. Similar approach for prometheus package resulted in 98% coverage

Testing

Each Test case is enclosed along with its assertions in the _test.go file. Below is a sample test case. for comparison.go

It("Test1 error 404", func() {
            client.Search = func(o ...func(*esapi.SearchRequest)) (*esapi.Response, error) {
                var res *esapi.Response
                res = &esapi.Response{}
                res.StatusCode = 404
                body := ioutil.NopCloser(bytes.NewReader([]byte(`{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [placeholder]","index":"placeholder","resource.id":"placeholder","resource.type":"index_or_alias","index_uuid":"_na_"}],"type":"index_not_found_exception","reason":"no such index [placeholder]","index":"placeholder","resource.id":"placeholder","resource.type":"index_or_alias","index_uuid":"_na_"},"status":404}`)))
                res.Body = body
                defer res.Body.Close()
                return res, nil
            }
            comparator = NewComparator(*client, "placeholder")
            _, err := comparator.queryStringStats(query, field)
            Expect(err).To(BeEquivalentTo(errors.New("404 Not Found index_not_found_exception no such index [placeholder]")))
        })

for prometheus.go

It("Test2 mock error to nil", func() {
            mockAPI := new(MockAPI)
            query := "your_query"
            start := time.Now()
            p := Prometheus{api: mockAPI}
            _, err := p.Query(query, start)
            Expect(err).To(BeNil())
        })

The below script can be used to test the package.

cd comparison 
go get golang.org/x/tools/cmd/cover
go get github.com/onsi/ginkgo/v2
go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo@latest
ginkgo -v -cover --coverprofile cover.out
go tool cover -html=cover.out

Output when Test suites are Run

for comparison.go

[sboyapal@sboyapal comparison]$ ginkgo -v -cover
Running Suite: Comparison Suite - /home/sboyapal/Documents/forkedCommons/go-commons/comparison
==============================================================================================
Random Seed: 1688391156

Will run 11 of 11 specs
------------------------------
Tests for elastic.go Tests for NewComparator Test 1: Returns the expected comparator
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:39
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests for queryStringStats() Test1 error 404
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:62
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests for queryStringStats() Test2 no error
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:77
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests for queryStringStats() Test3 not a valid link
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:84
• [0.224 seconds]
------------------------------
Tests for elastic.go Tests for queryStringStats() Test4 non parsable json
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:90
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests for queryStringStats() Test5 non parsable json
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:105
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests on Compare() Test1 no error
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:133
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests on Compare() Test2 negative value
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:140
2023/07/03 09:32:37 with a tolerancy of 1%: -1.00 is +Inf% lower than baseline: 0.00
• [0.001 seconds]
------------------------------
Tests for elastic.go Tests on Compare() Test3 negative tolerance
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:148
2023/07/03 09:32:37 with a tolerancy of -1%: 1.00 is +Inf% higher than baseline: 0.00
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests on Compare() Test4 negative tolerance
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:156
• [0.000 seconds]
------------------------------
Tests for elastic.go Tests on Compare() Test5 no error with sum stat
/home/sboyapal/Documents/forkedCommons/go-commons/comparison/elastic_test.go:163
• [0.199 seconds]
------------------------------

Ran 11 of 11 Specs in 0.426 seconds
SUCCESS! -- 11 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
        github.com/cloud-bulldozer/go-commons/comparison        coverage: 100.0% of statements
composite coverage: 100.0% of statements

Ginkgo ran 1 suite in 889.053038ms
Test Suite Passed

for prometheus.go

[sboyapal@sboyapal prometheus]$ ginkgo -v -cover
Running Suite: Prometheus Suite - /home/sboyapal/Documents/forkedCommons/go-commons/prometheus
==============================================================================================
Random Seed: 1688394242

Will run 15 of 15 specs
------------------------------
Tests for Prometheus Tests for RoundTrip() Test1 for default behaviour
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:24
• [0.145 seconds]
------------------------------
Tests for Prometheus Tests for NewClient() Test1 empty parameters
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:39
• [0.000 seconds]
------------------------------
Tests for Prometheus Tests for NewClient() Test2 passing not valid url
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:44
• [0.000 seconds]
------------------------------
Tests for Prometheus Tests for Query() Test1 empty url
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:60
• [0.000 seconds]
------------------------------
Tests for Prometheus Tests for Query() Test2 mock error to nil
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:65
• [0.000 seconds]
------------------------------
Tests for Prometheus Tests for QueryRange() Test1 empty url
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:83
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test1 queryRange error
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:107
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test2 for Avg
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:113
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test3 error when v not in matrix
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:118
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test4 for Min
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:124
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test5 for Max
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:129
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test6 for Stdev
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:134
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test7 for P50 etc.
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:139
• [0.000 seconds]
------------------------------
Tests for Prometheus Test for QueryRangeAggregatedTS Test6 no standard aggregator
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:144
• [0.000 seconds]
------------------------------
Tests for Prometheus Tests for verifyConnection() Test1 mock to no nil
/home/sboyapal/Documents/forkedCommons/go-commons/prometheus/prometheus_test.go:157
• [0.000 seconds]
------------------------------

Ran 15 of 15 Specs in 0.147 seconds
SUCCESS! -- 15 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
        github.com/cloud-bulldozer/go-commons/prometheus        coverage: 98.0% of statements
composite coverage: 98.0% of statements

Ginkgo ran 1 suite in 550.736551ms
Test Suite Passed
shashank-boyapally commented 1 year ago

Similar behaviour of local.go where previous test had some effect on next test was seen in prometheus package. This is fixed ad hope it wont raise any issues.