11package prime_test
22
33import (
4+ . "github.com/onsi/ginkgo/v2"
5+ . "github.com/onsi/gomega"
6+
47 dataNormalizer "github.com/tidepool-org/platform/data/normalizer"
5- "github.com/tidepool-org/platform/data/types/device"
6- "github.com/tidepool-org/platform/data/types/device/prime"
7- dataTypesDeviceTest "github.com/tidepool-org/platform/data/types/device/test"
8+ dataTypesDevice "github.com/tidepool-org/platform/data/types/device"
9+ dataTypesDevicePrime "github.com/tidepool-org/platform/data/types/device/prime"
10+ dataTypesDevicePrimeTest "github.com/tidepool-org/platform/data/types/device/prime /test"
811 dataTypesTest "github.com/tidepool-org/platform/data/types/test"
912 errorsTest "github.com/tidepool-org/platform/errors/test"
1013 logTest "github.com/tidepool-org/platform/log/test"
1114 "github.com/tidepool-org/platform/pointer"
1215 "github.com/tidepool-org/platform/structure"
1316 structureValidator "github.com/tidepool-org/platform/structure/validator"
14- "github.com/tidepool-org/platform/test"
15-
16- . "github.com/onsi/ginkgo/v2"
17- . "github.com/onsi/gomega"
1817)
1918
20- func NewMeta () interface {} {
21- return & device.Meta {
22- Type : "deviceEvent" ,
23- SubType : "prime" ,
24- }
25- }
26-
27- func NewPrime () * prime.Prime {
28- datum := prime .New ()
29- datum .Device = * dataTypesDeviceTest .RandomDevice ()
30- datum .SubType = "prime"
31- datum .Target = pointer .FromString (test .RandomStringFromArray (prime .Targets ()))
32- switch * datum .Target {
33- case "cannula" :
34- datum .Volume = pointer .FromFloat64 (test .RandomFloat64FromRange (prime .VolumeTargetCannulaMinimum , prime .VolumeTargetCannulaMaximum ))
35- case "tubing" :
36- datum .Volume = pointer .FromFloat64 (test .RandomFloat64FromRange (prime .VolumeTargetTubingMinimum , prime .VolumeTargetTubingMaximum ))
37- }
38- return datum
39- }
40-
41- func ClonePrime (datum * prime.Prime ) * prime.Prime {
42- if datum == nil {
43- return nil
19+ func NewMeta () any {
20+ return & dataTypesDevice.Meta {
21+ Type : dataTypesDevice .Type ,
22+ SubType : dataTypesDevicePrime .SubType ,
4423 }
45- clone := prime .New ()
46- clone .Device = * dataTypesDeviceTest .CloneDevice (& datum .Device )
47- clone .Target = pointer .CloneString (datum .Target )
48- clone .Volume = pointer .CloneFloat64 (datum .Volume )
49- return clone
5024}
5125
5226var _ = Describe ("Status" , func () {
5327 It ("SubType is expected" , func () {
54- Expect (prime .SubType ).To (Equal ("prime" ))
28+ Expect (dataTypesDevicePrime .SubType ).To (Equal ("prime" ))
5529 })
5630
5731 It ("TargetCannula is expected" , func () {
58- Expect (prime .TargetCannula ).To (Equal ("cannula" ))
32+ Expect (dataTypesDevicePrime .TargetCannula ).To (Equal ("cannula" ))
5933 })
6034
6135 It ("TargetTubing is expected" , func () {
62- Expect (prime .TargetTubing ).To (Equal ("tubing" ))
36+ Expect (dataTypesDevicePrime .TargetTubing ).To (Equal ("tubing" ))
6337 })
6438
6539 It ("VolumeTargetCannulaMaximum is expected" , func () {
66- Expect (prime .VolumeTargetCannulaMaximum ).To (Equal (10 .0 ))
40+ Expect (dataTypesDevicePrime .VolumeTargetCannulaMaximum ).To (Equal (1000 .0 ))
6741 })
6842
6943 It ("VolumeTargetCannulaMinimum is expected" , func () {
70- Expect (prime .VolumeTargetCannulaMinimum ).To (Equal (0.0 ))
44+ Expect (dataTypesDevicePrime .VolumeTargetCannulaMinimum ).To (Equal (0.0 ))
7145 })
7246
7347 It ("VolumeTargetTubingMaximum is expected" , func () {
74- Expect (prime .VolumeTargetTubingMaximum ).To (Equal (100 .0 ))
48+ Expect (dataTypesDevicePrime .VolumeTargetTubingMaximum ).To (Equal (1000 .0 ))
7549 })
7650
7751 It ("VolumeTargetTubingMinimum is expected" , func () {
78- Expect (prime .VolumeTargetTubingMinimum ).To (Equal (0.0 ))
52+ Expect (dataTypesDevicePrime .VolumeTargetTubingMinimum ).To (Equal (0.0 ))
7953 })
8054
8155 It ("Targets returns expected" , func () {
82- Expect (prime .Targets ()).To (Equal ([]string {"cannula" , "tubing" }))
56+ Expect (dataTypesDevicePrime .Targets ()).To (Equal ([]string {"cannula" , "tubing" }))
8357 })
8458
8559 Context ("New" , func () {
8660 It ("returns the expected datum with all values initialized" , func () {
87- datum := prime .New ()
61+ datum := dataTypesDevicePrime .New ()
8862 Expect (datum ).ToNot (BeNil ())
89- Expect (datum .Type ).To (Equal ("deviceEvent" ))
90- Expect (datum .SubType ).To (Equal ("prime" ))
63+ Expect (datum .Type ).To (Equal (dataTypesDevice . Type ))
64+ Expect (datum .SubType ).To (Equal (dataTypesDevicePrime . SubType ))
9165 Expect (datum .Target ).To (BeNil ())
9266 Expect (datum .Volume ).To (BeNil ())
9367 })
@@ -100,128 +74,128 @@ var _ = Describe("Status", func() {
10074
10175 Context ("Validate" , func () {
10276 DescribeTable ("validates the datum" ,
103- func (mutator func (datum * prime .Prime ), expectedErrors ... error ) {
104- datum := NewPrime ()
77+ func (mutator func (datum * dataTypesDevicePrime .Prime ), expectedErrors ... error ) {
78+ datum := dataTypesDevicePrimeTest . RandomPrime ()
10579 mutator (datum )
10680 dataTypesTest .ValidateWithExpectedOrigins (datum , structure .Origins (), expectedErrors ... )
10781 },
10882 Entry ("succeeds" ,
109- func (datum * prime .Prime ) {},
83+ func (datum * dataTypesDevicePrime .Prime ) {},
11084 ),
11185 Entry ("type missing" ,
112- func (datum * prime .Prime ) { datum .Type = "" },
113- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueEmpty (), "/type" , & device .Meta {SubType : "prime" }),
86+ func (datum * dataTypesDevicePrime .Prime ) { datum .Type = "" },
87+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueEmpty (), "/type" , & dataTypesDevice .Meta {SubType : dataTypesDevicePrime . SubType }),
11488 ),
11589 Entry ("type invalid" ,
116- func (datum * prime .Prime ) { datum .Type = "invalidType" },
117- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidType" , "deviceEvent" ), "/type" , & device .Meta {Type : "invalidType" , SubType : "prime" }),
90+ func (datum * dataTypesDevicePrime .Prime ) { datum .Type = "invalidType" },
91+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidType" , dataTypesDevice . Type ), "/type" , & dataTypesDevice .Meta {Type : "invalidType" , SubType : dataTypesDevicePrime . SubType }),
11892 ),
11993 Entry ("type device" ,
120- func (datum * prime .Prime ) { datum .Type = "deviceEvent" },
94+ func (datum * dataTypesDevicePrime .Prime ) { datum .Type = dataTypesDevice . Type },
12195 ),
12296 Entry ("sub type missing" ,
123- func (datum * prime .Prime ) { datum .SubType = "" },
124- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueEmpty (), "/subType" , & device .Meta {Type : "deviceEvent" }),
97+ func (datum * dataTypesDevicePrime .Prime ) { datum .SubType = "" },
98+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueEmpty (), "/subType" , & dataTypesDevice .Meta {Type : dataTypesDevice . Type }),
12599 ),
126100 Entry ("sub type invalid" ,
127- func (datum * prime .Prime ) { datum .SubType = "invalidSubType" },
128- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidSubType" , "prime" ), "/subType" , & device .Meta {Type : "deviceEvent" , SubType : "invalidSubType" }),
101+ func (datum * dataTypesDevicePrime .Prime ) { datum .SubType = "invalidSubType" },
102+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidSubType" , dataTypesDevicePrime . SubType ), "/subType" , & dataTypesDevice .Meta {Type : dataTypesDevice . Type , SubType : "invalidSubType" }),
129103 ),
130104 Entry ("sub type prime" ,
131- func (datum * prime .Prime ) { datum .SubType = "prime" },
105+ func (datum * dataTypesDevicePrime .Prime ) { datum .SubType = dataTypesDevicePrime . SubType },
132106 ),
133107 Entry ("target missing" ,
134- func (datum * prime .Prime ) { datum .Target = nil },
108+ func (datum * dataTypesDevicePrime .Prime ) { datum .Target = nil },
135109 errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotExists (), "/primeTarget" , NewMeta ()),
136110 ),
137111 Entry ("target invalid" ,
138- func (datum * prime .Prime ) { datum .Target = pointer .FromString ("invalid" ) },
139- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueStringNotOneOf ("invalid" , [] string { "cannula" , "tubing" } ), "/primeTarget" , NewMeta ()),
112+ func (datum * dataTypesDevicePrime .Prime ) { datum .Target = pointer .FromString ("invalid" ) },
113+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueStringNotOneOf ("invalid" , dataTypesDevicePrime . Targets () ), "/primeTarget" , NewMeta ()),
140114 ),
141115 Entry ("target cannula; volume missing" ,
142- func (datum * prime .Prime ) {
143- datum .Target = pointer .FromString ("cannula" )
116+ func (datum * dataTypesDevicePrime .Prime ) {
117+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetCannula )
144118 datum .Volume = nil
145119 },
146120 ),
147121 Entry ("target cannula; volume out of range (lower)" ,
148- func (datum * prime .Prime ) {
149- datum .Target = pointer .FromString ("cannula" )
150- datum .Volume = pointer .FromFloat64 (- 0.1 )
122+ func (datum * dataTypesDevicePrime .Prime ) {
123+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetCannula )
124+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetCannulaMinimum - 0.1 )
151125 },
152- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (- 0.1 , 0 , 10 ), "/volume" , NewMeta ()),
126+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (dataTypesDevicePrime . VolumeTargetCannulaMinimum - 0.1 , dataTypesDevicePrime . VolumeTargetCannulaMinimum , dataTypesDevicePrime . VolumeTargetCannulaMaximum ), "/volume" , NewMeta ()),
153127 ),
154128 Entry ("target cannula; volume in range (lower)" ,
155- func (datum * prime .Prime ) {
156- datum .Target = pointer .FromString ("cannula" )
157- datum .Volume = pointer .FromFloat64 (0.0 )
129+ func (datum * dataTypesDevicePrime .Prime ) {
130+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetCannula )
131+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetCannulaMinimum )
158132 },
159133 ),
160134 Entry ("target cannula; volume in range (upper)" ,
161- func (datum * prime .Prime ) {
162- datum .Target = pointer .FromString ("cannula" )
163- datum .Volume = pointer .FromFloat64 (10.0 )
135+ func (datum * dataTypesDevicePrime .Prime ) {
136+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetCannula )
137+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetCannulaMaximum )
164138 },
165139 ),
166140 Entry ("target cannula; volume out of range (upper)" ,
167- func (datum * prime .Prime ) {
168- datum .Target = pointer .FromString ("cannula" )
169- datum .Volume = pointer .FromFloat64 (10 .1 )
141+ func (datum * dataTypesDevicePrime .Prime ) {
142+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetCannula )
143+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetCannulaMaximum + 0 .1 )
170144 },
171- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (10. 1 , 0 , 10 ), "/volume" , NewMeta ()),
145+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (dataTypesDevicePrime . VolumeTargetCannulaMaximum + 0. 1 , dataTypesDevicePrime . VolumeTargetCannulaMinimum , dataTypesDevicePrime . VolumeTargetCannulaMaximum ), "/volume" , NewMeta ()),
172146 ),
173147 Entry ("target tubing; volume missing" ,
174- func (datum * prime .Prime ) {
175- datum .Target = pointer .FromString ("tubing" )
148+ func (datum * dataTypesDevicePrime .Prime ) {
149+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetTubing )
176150 datum .Volume = nil
177151 },
178152 ),
179153 Entry ("target tubing; volume out of range (lower)" ,
180- func (datum * prime .Prime ) {
181- datum .Target = pointer .FromString ("tubing" )
182- datum .Volume = pointer .FromFloat64 (- 0.1 )
154+ func (datum * dataTypesDevicePrime .Prime ) {
155+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetTubing )
156+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetTubingMinimum - 0.1 )
183157 },
184- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (- 0.1 , 0 , 100 ), "/volume" , NewMeta ()),
158+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (dataTypesDevicePrime . VolumeTargetTubingMinimum - 0.1 , dataTypesDevicePrime . VolumeTargetTubingMinimum , dataTypesDevicePrime . VolumeTargetTubingMaximum ), "/volume" , NewMeta ()),
185159 ),
186160 Entry ("target tubing; volume in range (lower)" ,
187- func (datum * prime .Prime ) {
188- datum .Target = pointer .FromString ("tubing" )
189- datum .Volume = pointer .FromFloat64 (0.0 )
161+ func (datum * dataTypesDevicePrime .Prime ) {
162+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetTubing )
163+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetTubingMinimum )
190164 },
191165 ),
192166 Entry ("target tubing; volume in range (upper)" ,
193- func (datum * prime .Prime ) {
194- datum .Target = pointer .FromString ("tubing" )
195- datum .Volume = pointer .FromFloat64 (100.0 )
167+ func (datum * dataTypesDevicePrime .Prime ) {
168+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetTubing )
169+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetTubingMaximum )
196170 },
197171 ),
198172 Entry ("target tubing; volume out of range (upper)" ,
199- func (datum * prime .Prime ) {
200- datum .Target = pointer .FromString ("tubing" )
201- datum .Volume = pointer .FromFloat64 (100 .1 )
173+ func (datum * dataTypesDevicePrime .Prime ) {
174+ datum .Target = pointer .FromString (dataTypesDevicePrime . TargetTubing )
175+ datum .Volume = pointer .FromFloat64 (dataTypesDevicePrime . VolumeTargetTubingMaximum + 0 .1 )
202176 },
203- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (100. 1 , 0 , 100 ), "/volume" , NewMeta ()),
177+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotInRange (dataTypesDevicePrime . VolumeTargetTubingMaximum + 0. 1 , dataTypesDevicePrime . VolumeTargetTubingMinimum , dataTypesDevicePrime . VolumeTargetTubingMaximum ), "/volume" , NewMeta ()),
204178 ),
205179 Entry ("multiple errors" ,
206- func (datum * prime .Prime ) {
180+ func (datum * dataTypesDevicePrime .Prime ) {
207181 datum .Type = "invalidType"
208182 datum .SubType = "invalidSubType"
209183 datum .Target = pointer .FromString ("invalid" )
210184 },
211- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidType" , "deviceEvent" ), "/type" , & device .Meta {Type : "invalidType" , SubType : "invalidSubType" }),
212- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidSubType" , "prime" ), "/subType" , & device .Meta {Type : "invalidType" , SubType : "invalidSubType" }),
213- errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueStringNotOneOf ("invalid" , [] string { "cannula" , "tubing" }) , "/primeTarget" , & device .Meta {Type : "invalidType" , SubType : "invalidSubType" }),
185+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidType" , dataTypesDevice . Type ), "/type" , & dataTypesDevice .Meta {Type : "invalidType" , SubType : "invalidSubType" }),
186+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueNotEqualTo ("invalidSubType" , dataTypesDevicePrime . SubType ), "/subType" , & dataTypesDevice .Meta {Type : "invalidType" , SubType : "invalidSubType" }),
187+ errorsTest .WithPointerSourceAndMeta (structureValidator .ErrorValueStringNotOneOf ("invalid" , dataTypesDevicePrime . Targets ()) , "/primeTarget" , & dataTypesDevice .Meta {Type : "invalidType" , SubType : "invalidSubType" }),
214188 ),
215189 )
216190 })
217191
218192 Context ("Normalize" , func () {
219193 DescribeTable ("normalizes the datum" ,
220- func (mutator func (datum * prime .Prime )) {
194+ func (mutator func (datum * dataTypesDevicePrime .Prime )) {
221195 for _ , origin := range structure .Origins () {
222- datum := NewPrime ()
196+ datum := dataTypesDevicePrimeTest . RandomPrime ()
223197 mutator (datum )
224- expectedDatum := ClonePrime (datum )
198+ expectedDatum := dataTypesDevicePrimeTest . ClonePrime (datum )
225199 normalizer := dataNormalizer .New (logTest .NewLogger ())
226200 Expect (normalizer ).ToNot (BeNil ())
227201 datum .Normalize (normalizer .WithOrigin (origin ))
@@ -231,13 +205,13 @@ var _ = Describe("Status", func() {
231205 }
232206 },
233207 Entry ("does not modify the datum" ,
234- func (datum * prime .Prime ) {},
208+ func (datum * dataTypesDevicePrime .Prime ) {},
235209 ),
236210 Entry ("does not modify the datum; target missing" ,
237- func (datum * prime .Prime ) { datum .Target = nil },
211+ func (datum * dataTypesDevicePrime .Prime ) { datum .Target = nil },
238212 ),
239213 Entry ("does not modify the datum; volume missing" ,
240- func (datum * prime .Prime ) { datum .Volume = nil },
214+ func (datum * dataTypesDevicePrime .Prime ) { datum .Volume = nil },
241215 ),
242216 )
243217 })
0 commit comments