16
16
17
17
package tech .ant8e .uuid4cats
18
18
19
+ import cats .Eq
19
20
import cats .data .NonEmptyChain
20
21
import cats .syntax .all ._
21
22
import munit .FunSuite
@@ -82,6 +83,20 @@ class TypeIDSuite extends FunSuite {
82
83
uuid = uuid " 01890a5d-ac96-774b-bcce-b302099a8057 "
83
84
)
84
85
}
86
+ test(" TypeID should not build invalid typeIDs" ) {
87
+ val tooLongPrefix =
88
+ " 0123456789012345678901234567890123456789012345678901234567890123456789"
89
+ assertInvalidEncoding(
90
+ tooLongPrefix,
91
+ uuid " 01890a5d-ac96-774b-bcce-b302099a8057 "
92
+ )
93
+
94
+ val v4UUID = uuid " 3054b437-160c-42ac-9b68-f814f93bfc28 "
95
+ assertInvalidEncoding(" prefix" , v4UUID)
96
+
97
+ assertInvalidEncoding(" prefix" , null )
98
+ assertInvalidEncoding(null , uuid " 01890a5d-ac96-774b-bcce-b302099a8057 " )
99
+ }
85
100
86
101
test(" TypeID should decode valid typeIDs" ) {
87
102
// nil
@@ -198,7 +213,36 @@ class TypeIDSuite extends FunSuite {
198
213
199
214
// suffix-overflow
200
215
assertInvalidDecoding(typeID = " prefix_8zzzzzzzzzzzzzzzzzzzzzzzzz" )
216
+
217
+ // sixteen with mandatory UUIDV7
218
+ assertInvalidDecoding(
219
+ typeID = " 0000000000000000000000000g" ,
220
+ enforceUUIDV7 = true
221
+ )
222
+
223
+ // missing separator
224
+ assertInvalidDecoding(
225
+ typeID = " prefix0000000000000000000000000g"
226
+ )
227
+ }
228
+
229
+ test(" TypeID should have an Eq instance" ) {
230
+ val eq = implicitly[Eq [TypeID ]]
231
+ assert(
232
+ eq.eqv(
233
+ TypeID .decode(" prefix_01h455vb4pex5vsknk084sn02q" ).toOption.get,
234
+ TypeID .decode(" prefix_01h455vb4pex5vsknk084sn02q" ).toOption.get
235
+ )
236
+ )
201
237
}
238
+
239
+ test(" TypeID should have a Show instance" ) {
240
+ assertEquals(
241
+ TypeID .decode(" prefix_01h455vb4pex5vsknk084sn02q" ).toOption.get.show,
242
+ " TypeID:prefix_01h455vb4pex5vsknk084sn02q(01890a5d-ac96-774b-bcce-b302099a8057)"
243
+ )
244
+ }
245
+
202
246
private def assertValidEncoding (
203
247
typeID : String ,
204
248
prefix : String ,
@@ -211,6 +255,17 @@ class TypeIDSuite extends FunSuite {
211
255
typeID.valid[TypeID .BuildError ].toValidatedNec
212
256
)
213
257
}
258
+
259
+ private def assertInvalidEncoding (
260
+ prefix : String ,
261
+ uuid : UUID
262
+ ): Unit = {
263
+ assert(
264
+ TypeID
265
+ .build(prefix = prefix, uuid = uuid)
266
+ .isInvalid
267
+ )
268
+ }
214
269
private def assertValidDecoding (
215
270
typeID : String ,
216
271
prefix : String ,
@@ -228,8 +283,12 @@ class TypeIDSuite extends FunSuite {
228
283
229
284
assertEquals(obtained, expected)
230
285
}
231
- private def assertInvalidDecoding (typeID : String ): Unit = {
232
- val obtained = TypeID .decode(typeID)
286
+
287
+ private def assertInvalidDecoding (
288
+ typeID : String ,
289
+ enforceUUIDV7 : Boolean = false
290
+ ): Unit = {
291
+ val obtained = TypeID .decode(typeID, enforceUUIDV7)
233
292
assert(obtained.isInvalid, s " $typeID should not decode as a valid TypeID " )
234
293
}
235
294
0 commit comments