Skip to content

Commit f8abd6d

Browse files
committed
Improve test coverage
1 parent c45c0fe commit f8abd6d

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/tech.ant8e/uuid4cats-effect_2.13/badge.svg)](https://maven-badges.herokuapp.com/maven-central/tech.ant8e/uuid4cats-effect_2.13)
33
[![Code of Conduct](https://img.shields.io/badge/Code%20of%20Conduct-Scala-blue.svg)](CODE_OF_CONDUCT.md)
44
![](https://github.com/ant8e/uuid4cats-effect/actions/workflows/ci.yml/badge.svg)
5+
[![codecov](https://codecov.io/gh/ant8e/uuid4cats-effect/branch/main/graph/badge.svg?token=QEUSQ3T053)](https://codecov.io/gh/ant8e/uuid4cats-effect)
56
# uuid4cats-effect - UUID and TypeID Generation for cats effect
67

78

codecov.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
target: auto # auto compares coverage to the previous base commit
6+
patch:
7+
default:
8+
informational: true

core/src/main/scala/tech/ant8e/uuid4cats/TypeID.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ object TypeID {
5555
.map(uuidGenerator =>
5656
(prefix: String) =>
5757
uuidGenerator.uuid.flatMap { uuid =>
58-
val typeID = build(prefix, uuid).leftMap(errors =>
59-
new IllegalArgumentException(errors.mkString_(", "))
60-
)
58+
val typeID = build(prefix, uuid)
59+
.leftMap(errors => new IllegalArgumentException(errors.show))
6160
ApplicativeError[F, Throwable].fromValidated(typeID)
6261
}
6362
)
@@ -198,9 +197,8 @@ object TypeID {
198197
override def toString: String = s"TypeID:$repr($uuid_)"
199198

200199
override def equals(obj: Any): Boolean = obj match {
201-
case other: TypeID =>
202-
this.uuid.equals(other.uuid) && this.prefix.equals(other.prefix)
203-
case _ => false
200+
case other: TypeID => TypeID.typeIDEq.eqv(this, other)
201+
case _ => false
204202
}
205203

206204
}

core/src/test/scala/tech/ant8e/uuid4cats/GeneratorSuite.scala

+17
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ class GeneratorSuite extends CatsEffectSuite {
6565
_ <- IO(typeids.distinct.size === typeids.size).assert
6666
_ <- IO(isSeqSorted(typeids)).assert
6767
} yield ()
68+
69+
for {
70+
typeids <- TypeID
71+
.generator[IO]("prefix")
72+
.flatMap(generator => List.tabulate(n)(_ => generator()).sequence)
73+
_ <- IO(typeids.distinct.size === typeids.size).assert
74+
_ <- IO(isSeqSorted(typeids)).assert
75+
} yield ()
76+
}
77+
78+
test("TypeID generator should not accept illegal prefix") {
79+
TypeID
80+
.generator[IO]
81+
.flatMap(generator => generator.typeid("WRONG"))
82+
.intercept[IllegalArgumentException]
83+
84+
TypeID.generator[IO]("WRONG").intercept[IllegalArgumentException]
6885
}
6986

7087
private def genN(generator: UUIDGenerator[IO], n: Int): IO[List[UUID]] =

core/src/test/scala/tech/ant8e/uuid4cats/TypeIDSuite.scala

+61-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package tech.ant8e.uuid4cats
1818

19+
import cats.Eq
1920
import cats.data.NonEmptyChain
2021
import cats.syntax.all._
2122
import munit.FunSuite
@@ -82,6 +83,20 @@ class TypeIDSuite extends FunSuite {
8283
uuid = uuid"01890a5d-ac96-774b-bcce-b302099a8057"
8384
)
8485
}
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+
}
85100

86101
test("TypeID should decode valid typeIDs") {
87102
// nil
@@ -198,7 +213,36 @@ class TypeIDSuite extends FunSuite {
198213

199214
// suffix-overflow
200215
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+
)
201237
}
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+
202246
private def assertValidEncoding(
203247
typeID: String,
204248
prefix: String,
@@ -211,6 +255,17 @@ class TypeIDSuite extends FunSuite {
211255
typeID.valid[TypeID.BuildError].toValidatedNec
212256
)
213257
}
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+
}
214269
private def assertValidDecoding(
215270
typeID: String,
216271
prefix: String,
@@ -228,8 +283,12 @@ class TypeIDSuite extends FunSuite {
228283

229284
assertEquals(obtained, expected)
230285
}
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)
233292
assert(obtained.isInvalid, s"$typeID should not decode as a valid TypeID")
234293
}
235294

0 commit comments

Comments
 (0)