diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 0e065bb..df3a247 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -6,3 +6,6 @@ # Scala Steward: Reformat with scalafmt 3.8.2 1905008f11ffcbae185026dd4e6557dc298bbaf1 + +# Scala Steward: Reformat with scalafmt 3.8.6 +002feb71be8cd8d7e5853f14324285012ef976d9 diff --git a/.scalafmt.conf b/.scalafmt.conf index 7ac5d80..f129a4e 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,4 +1,4 @@ -version = "3.8.3" +version = "3.8.6" runner.dialect = "scala213" diff --git a/src/test/scala/io/renku/jsonld/DefaultGraphSpec.scala b/src/test/scala/io/renku/jsonld/DefaultGraphSpec.scala index 272a5ce..04d7e26 100644 --- a/src/test/scala/io/renku/jsonld/DefaultGraphSpec.scala +++ b/src/test/scala/io/renku/jsonld/DefaultGraphSpec.scala @@ -112,13 +112,12 @@ class DefaultGraphSpec extends AnyWordSpec with should.Matchers with ScalaCheckP "toJson" should { - "turn the given DefaultGraph into JSON" in { + "turn the given DefaultGraph into JSON" in forAll { (graph: DefaultGraph) => graph.toJson shouldBe Json.obj( "@graph" -> Json.fromValues(graph.entities.map(_.toJson)) ) } - } } "entityId" should { @@ -197,29 +196,27 @@ class DefaultGraphSpec extends AnyWordSpec with should.Matchers with ScalaCheckP } "return DefaultGraph with flattened entities in the sense of an JsonLDArray merge " + - "if there's more than one entity" in { - forAll { - (childlessGrandparent: JsonLDEntity, parentRelationProperty: Property, parentNotNested: JsonLDEntity) => - val childrenTuples = entityProperties.generateNonEmptyList().toList - val parentWithProperties = parentNotNested.add(childrenTuples) - val grandparentWithChild = childlessGrandparent.add(parentRelationProperty -> parentWithProperties) - - val flattenedGrandparent = - childlessGrandparent.add(parentRelationProperty -> JsonLD.fromEntityId(parentWithProperties.id)) - val flattenedParent = childrenTuples - .foldLeft(parentNotNested) { case (parent, (property, child)) => - parent.add(property -> child.id.asJsonLD) - } - val children = childrenTuples.map { case (_, entity) => entity: JsonLD } - - val otherEntity = jsonLDEntities.generateOne - val edge = jsonLDEdges.generateOne - - val Right(flattened) = DefaultGraph(grandparentWithChild, otherEntity, edge).flatten - - flattened.entities should contain theSameElementsAs - flattenedGrandparent :: flattenedParent :: children ::: otherEntity :: edge :: Nil - } + "if there's more than one entity" in + forAll { (childlessGrandparent: JsonLDEntity, parentRelationProperty: Property, parentNotNested: JsonLDEntity) => + val childrenTuples = entityProperties.generateNonEmptyList().toList + val parentWithProperties = parentNotNested.add(childrenTuples) + val grandparentWithChild = childlessGrandparent.add(parentRelationProperty -> parentWithProperties) + + val flattenedGrandparent = + childlessGrandparent.add(parentRelationProperty -> JsonLD.fromEntityId(parentWithProperties.id)) + val flattenedParent = childrenTuples + .foldLeft(parentNotNested) { case (parent, (property, child)) => + parent.add(property -> child.id.asJsonLD) + } + val children = childrenTuples.map { case (_, entity) => entity: JsonLD } + + val otherEntity = jsonLDEntities.generateOne + val edge = jsonLDEdges.generateOne + + val Right(flattened) = DefaultGraph(grandparentWithChild, otherEntity, edge).flatten + + flattened.entities should contain theSameElementsAs + flattenedGrandparent :: flattenedParent :: children ::: otherEntity :: edge :: Nil } } } diff --git a/src/test/scala/io/renku/jsonld/EntityIdSpec.scala b/src/test/scala/io/renku/jsonld/EntityIdSpec.scala index 4f98392..3bb1e89 100644 --- a/src/test/scala/io/renku/jsonld/EntityIdSpec.scala +++ b/src/test/scala/io/renku/jsonld/EntityIdSpec.scala @@ -41,17 +41,15 @@ class EntityIdSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should } } - "allow passing a Property" in { + "allow passing a Property" in forAll { (property: Property) => EntityId.of(property).value shouldBe property.url } - } - "allow passing a Schema" in { + "allow passing a Schema" in forAll { (schema: Schema) => EntityId.of(schema).value shouldBe schema.url } - } "return an EntityId with the URI created from the given value using the converter" in { case class Value(v: String) diff --git a/src/test/scala/io/renku/jsonld/EntityTypeSpec.scala b/src/test/scala/io/renku/jsonld/EntityTypeSpec.scala index 8cbfdb1..8410720 100644 --- a/src/test/scala/io/renku/jsonld/EntityTypeSpec.scala +++ b/src/test/scala/io/renku/jsonld/EntityTypeSpec.scala @@ -32,46 +32,40 @@ class EntityTypeSpec extends AnyWordSpec with ScalaCheckPropertyChecks with shou "show" should { - "return the value" in { + "return the value" in forAll { t: EntityType => t.show shouldBe t.value } - } } "EntityTypes.show" should { - "return string representations of all the types separated with `; `" in { + "return string representations of all the types separated with `; `" in forAll { t: EntityTypes => t.show shouldBe t.list.map(_.value).nonEmptyIntercalate("; ") } - } } "EntityTypes.equals" should { - "return true if the types are identical regardless of the order" in { + "return true if the types are identical regardless of the order" in forAll { t: EntityTypes => val equalTypes = EntityTypes(NonEmptyList.fromListUnsafe(Random.shuffle(t.list.toList))) t shouldBe equalTypes t.hashCode() shouldBe equalTypes.hashCode() } - } - "return false if the types are not identical" in { + "return false if the types are not identical" in forAll { (t1: EntityTypes, t2: EntityTypes) => t1 should not be t2 t1.hashCode() should not be t2.hashCode() } - } } "contains" should { - "return true for exact match although in different order" in { + "return true for exact match although in different order" in forAll { entityTypes: EntityTypes => val shuffledTypes = Random.shuffle(entityTypes.toList) (entityTypes contains EntityTypes.of(shuffledTypes.head, shuffledTypes.tail: _*)) shouldBe true } - } - "return true if there are at least the searched types" in { + "return true if there are at least the searched types" in forAll { (type1: EntityType, type2: EntityType, type3: EntityType) => EntityTypes.of(type1, type2, type3).contains(type2, type1) shouldBe true } - } } } diff --git a/src/test/scala/io/renku/jsonld/JsonLDDecoderSpec.scala b/src/test/scala/io/renku/jsonld/JsonLDDecoderSpec.scala index d8122ab..891090c 100644 --- a/src/test/scala/io/renku/jsonld/JsonLDDecoderSpec.scala +++ b/src/test/scala/io/renku/jsonld/JsonLDDecoderSpec.scala @@ -45,20 +45,18 @@ class JsonLDDecoderSpec "emap" should { - "use the given function to map the result" in { + "use the given function to map the result" in forAll { value: String => val decoder = decodeString.emap(_.length.asRight[String]) JsonLD.fromString(value).cursor.as[Int](decoder) shouldBe Right(value.length) } - } - "fail with the message given in the mapping function" in { + "fail with the message given in the mapping function" in forAll { value: String => val message = nonEmptyStrings().generateOne val decoder = decodeString.emap(_ => message.asLeft[Int]) JsonLD.fromString(value).cursor.as[Int](decoder).leftMap(_.message) shouldBe Left(message) } - } case class Person(name: String) @@ -71,7 +69,7 @@ class JsonLDDecoderSpec result.getClass shouldBe decoder.getClass } - "apply the function when called on JsonLDEntityDecoder" in { + "apply the function when called on JsonLDEntityDecoder" in forAll(Gen.alphaNumStr) { value: String => val decoder: JsonLDEntityDecoder[Person] = JsonLDDecoder.entity(EntityTypes.of(schema / "Person")) { cursor => cursor.downField(schema / "name").as[String].map(Person) @@ -92,7 +90,6 @@ class JsonLDDecoderSpec decoded shouldBe Person(value.reverse) } - } "fail if emap is returning a Left on JsonLDEntityDecoder" in { val decoder: JsonLDEntityDecoder[Person] = JsonLDDecoder.entity(EntityTypes.of(schema / "Person")) { cursor => @@ -116,12 +113,11 @@ class JsonLDDecoderSpec } "map" should { - "appy the given function" in { + "appy the given function" in forAll { value: String => val decoder = decodeString.map(_.length) JsonLD.fromString(value).cursor.as[Int](decoder) shouldBe Right(value.length) } - } } "implicit decoders" should { @@ -143,19 +139,17 @@ class JsonLDDecoderSpec forAll(scenarios) { case (typeName, decoder, generator, illegalType: JsonLDValue[_]) => - s"allow to successfully decode $typeName JsonLDValues to $typeName" in { + s"allow to successfully decode $typeName JsonLDValues to $typeName" in forAll(generator) { case (value, jsonLD) => jsonLD.cursor.as(decoder) shouldBe Right(value) } - } - s"fail to decode JsonLDValues of illegal type to $typeName" in { + s"fail to decode JsonLDValues of illegal type to $typeName" in forAll(generator) { case (_, _) => illegalType.cursor.as(decoder) shouldBe Left( DecodingFailure(s"Cannot decode ${illegalType.value} to $typeName", Nil) ) } - } case (_, _, _, illegalType) => fail(s"Missing tests for $illegalType") } @@ -175,13 +169,12 @@ class JsonLDDecoderSpec JsonLD.fromString(date.toString).cursor.as[LocalDate] shouldBe Right(date) } - "allow to successfully decode any JsonLD to JsonLD" in { + "allow to successfully decode any JsonLD to JsonLD" in forAll { json: JsonLD => json.cursor.as[JsonLD] shouldBe Right(json) } - } - "allow to decode to Some for optional property if it exists" in { + "allow to decode to Some for optional property if it exists" in forAll(entityIds, entityTypesObject, properties, Gen.oneOf(jsonLDValues, jsonLDEntities)) { (entityId, entityTypes, property, value) => JsonLD @@ -190,9 +183,8 @@ class JsonLDDecoderSpec .downField(property) .as[Option[JsonLD]] shouldBe Some(value).asRight } - } - "allow to decode to None for optional property if it does not exists" in { + "allow to decode to None for optional property if it does not exists" in forAll { (entityId: EntityId, entityTypes: EntityTypes) => JsonLD .entity(entityId, entityTypes, Map.empty[Property, JsonLD]) @@ -200,7 +192,6 @@ class JsonLDDecoderSpec .downField(schema / "non-existing") .as[Option[JsonLD]] shouldBe None.asRight } - } } "decode" should { diff --git a/src/test/scala/io/renku/jsonld/JsonLDEncoderSpec.scala b/src/test/scala/io/renku/jsonld/JsonLDEncoderSpec.scala index aa926a5..a76f7b6 100644 --- a/src/test/scala/io/renku/jsonld/JsonLDEncoderSpec.scala +++ b/src/test/scala/io/renku/jsonld/JsonLDEncoderSpec.scala @@ -34,7 +34,7 @@ class JsonLDEncoderSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s "instance" should { - "allow to create a JsonLDEncoder for the given type" in { + "allow to create a JsonLDEncoder for the given type" in forAll { (id: EntityId, types: EntityTypes, property: Property, value: String) => val encoder = JsonLDEncoder.instance[Object] { o => JsonLD.entity(id, types, property -> o.field.asJsonLD) @@ -42,12 +42,11 @@ class JsonLDEncoderSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s encoder(Object(value)) shouldBe JsonLD.entity(id, types, property -> value.asJsonLD) } - } } "entityId" should { - "allow to create a JsonLDEncoder producing EntityId for the given type" in { + "allow to create a JsonLDEncoder producing EntityId for the given type" in forAll { (value: String) => val encoder = JsonLDEncoder.entityId[Object] { o => EntityId.of(o.field) @@ -55,16 +54,14 @@ class JsonLDEncoderSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s encoder(Object(value)) shouldBe JsonLD.fromEntityId(EntityId.of(value)) } - } } "schema" should { - "allow to create a JsonLDEncoder producing EntityId for the given type" in { + "allow to create a JsonLDEncoder producing EntityId for the given type" in forAll { (schema: Schema) => schema.asJsonLD shouldBe JsonLDEntityId(EntityId.of(schema.toString)) } - } } "apply" should { diff --git a/src/test/scala/io/renku/jsonld/JsonLDFlattenSpec.scala b/src/test/scala/io/renku/jsonld/JsonLDFlattenSpec.scala index 1a34798..8ec88ae 100644 --- a/src/test/scala/io/renku/jsonld/JsonLDFlattenSpec.scala +++ b/src/test/scala/io/renku/jsonld/JsonLDFlattenSpec.scala @@ -35,33 +35,30 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s "flatten" should { - "do nothing for JsonLDValue" in { + "do nothing for JsonLDValue" in forAll(jsonLDValues) { json => json.flatten shouldBe Right(json) } - } - "do nothing for JsonLDEntityId" in { + "do nothing for JsonLDEntityId" in forAll { entityId: EntityId => val json = JsonLD.fromEntityId(entityId) json.flatten shouldBe Right(json) } - } "do nothing for JsonLDNull" in { JsonLD.Null.flatten shouldBe Right(JsonLD.Null) } - "do nothing if JsonLDEntity does not have nested object in its properties" in { + "do nothing if JsonLDEntity does not have nested object in its properties" in forAll { (id: EntityId, types: EntityTypes, property1: (Property, JsonLD), other: List[(Property, JsonLD)]) => val entityAsJsonLD = JsonLD.arr(JsonLD.entity(id, types, property1, other: _*)) entityAsJsonLD.flatten shouldBe Right(entityAsJsonLD) } - } "pull out all the nested JsonLDEntity entities into a flat JsonLDArray " + - "and replace the nested properties values with relevant EntityIds" in { - /* + "and replace the nested properties values with relevant EntityIds" in + /* grandparent | parent @@ -71,29 +68,26 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s | V (child, parent, grandparent) // order not guaranteed - */ - forAll { - (childlessGrandparent: JsonLDEntity, parentRelationProperty: Property, parentNotNested: JsonLDEntity) => - val childrenTuples = entityProperties.generateNonEmptyList().toList - val parentWithProperties = parentNotNested.add(childrenTuples) - val grandparentWithChild = childlessGrandparent.add(parentRelationProperty -> parentWithProperties) - - val flattenedGrandparent = - childlessGrandparent.add(parentRelationProperty -> JsonLD.fromEntityId(parentWithProperties.id)) - val flattenedParent = childrenTuples - .foldLeft(parentNotNested) { case (parent, (property, child)) => - parent.add(property -> child.id.asJsonLD) - } - val children = childrenTuples.map { case (_, entity) => entity: JsonLD } - - val Right(actual) = grandparentWithChild.flatten - actual.toJson.asArray.get should contain theSameElementsAs { - JsonLD.arr(flattenedGrandparent :: flattenedParent :: children: _*).toJson.asArray.get - } - } + */ + forAll { (childlessGrandparent: JsonLDEntity, parentRelationProperty: Property, parentNotNested: JsonLDEntity) => + val childrenTuples = entityProperties.generateNonEmptyList().toList + val parentWithProperties = parentNotNested.add(childrenTuples) + val grandparentWithChild = childlessGrandparent.add(parentRelationProperty -> parentWithProperties) + + val flattenedGrandparent = + childlessGrandparent.add(parentRelationProperty -> JsonLD.fromEntityId(parentWithProperties.id)) + val flattenedParent = childrenTuples + .foldLeft(parentNotNested) { case (parent, (property, child)) => + parent.add(property -> child.id.asJsonLD) + } + val children = childrenTuples.map { case (_, entity) => entity: JsonLD } + + val Right(actual) = grandparentWithChild.flatten + actual.toJson.asArray.get should contain theSameElementsAs + JsonLD.arr(flattenedGrandparent :: flattenedParent :: children: _*).toJson.asArray.get } - "should fail if there are two unequal child entities with the same EntityID in a single Entity" in { + "should fail if there are two unequal child entities with the same EntityID in a single Entity" in forAll { (grandParent: JsonLDEntity) => val newProperties = valuesProperties.generateNonEmptyMap() val parent0 = jsonLDEntities.generateOne @@ -109,9 +103,8 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s MalformedJsonLD(s"Not equal entity(ies) in json-ld: ${parent0.entityId.getOrElse("")}") ) } - } - "should fail if there are two unequal entities with the same EntityId in the nested structure" in { + "should fail if there are two unequal entities with the same EntityId in the nested structure" in forAll { (parent0: JsonLDEntity, parent1: JsonLDEntity) => val childrenTuples = entityProperties.generateNonEmptyList().toList val newProperties = valuesProperties.generateNonEmptyMap() @@ -127,17 +120,15 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s MalformedJsonLD(s"Not equal entity(ies) in json-ld: ${modifiedChildEntity.entityId.getOrElse("")}") ) } - } - "do nothing for JsonLDArray if none of its items have nested entities" in { + "do nothing for JsonLDArray if none of its items have nested entities" in forAll { (entity0: JsonLDEntity, entity1: JsonLDEntity) => val entity0WithEntity1IdAsProperty = entity0.add(properties.generateOne -> entity1.id.asJsonLD) val arrayOfEntities = JsonLD.arr(entity0WithEntity1IdAsProperty, entity1) arrayOfEntities.flatten shouldBe Right(arrayOfEntities) } - } - "pull out nested entities from JsonLDArray items and put them on the root level" in { + "pull out nested entities from JsonLDArray items and put them on the root level" in forAll { (entity: JsonLDEntity, child: JsonLDEntity) => val property = properties.generateOne val entityWithChildren = entity.add(property -> child) @@ -145,15 +136,13 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s JsonLD.arr(entityWithChildren).flatten shouldBe Right(JsonLD.arr(child, entityWithChildrenFlattened)) } - } - "pull out entities nested in a nested JsonLDArray and put them on the root level" in { + "pull out entities nested in a nested JsonLDArray and put them on the root level" in forAll { (rootLevelEntity: JsonLDEntity, nestedEntity1: JsonLDEntity, nestedEntity2: JsonLDEntity) => JsonLD.arr(rootLevelEntity, JsonLD.arr(nestedEntity1, nestedEntity2)).flatten shouldBe Right( JsonLD.arr(rootLevelEntity, nestedEntity1, nestedEntity2) ) } - } "de-duplicate same children found in multiple entities" in { /* @@ -197,7 +186,7 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s } } - "deduplicate pulled out entities which contain an array property with elements in a different order" in { + "deduplicate pulled out entities which contain an array property with elements in a different order" in /* JsonLDArray(entity0, entity1) | | @@ -218,9 +207,8 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s be(Right(JsonLD.arr(normalEntity))) or be(Right(JsonLD.arr(mixedUpEntity))) ) } - } - "pull out child entities from reversed property on children" in { + "pull out child entities from reversed property on children" in /* parent | @@ -245,9 +233,8 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s child1.copy(properties = child1.properties + (reverseProperty -> parent.id.asJsonLD)) ) } - } - "pull out child entities from reversed property on parent" in { + "pull out child entities from reversed property on parent" in /* (child0, child1) | @@ -278,9 +265,8 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s child1 ) } - } - "pull out child entities from reverse properties with two levels of nesting" in { + "pull out child entities from reverse properties with two levels of nesting" in /* parent0 | @@ -311,14 +297,12 @@ class JsonLDFlattenSpec extends AnyWordSpec with ScalaCheckPropertyChecks with s child2.copy(properties = child2.properties + (property1 -> parent1.id.asJsonLD)) ) } - } - "produce the same result the second time as the first time called on the previous result" in { + "produce the same result the second time as the first time called on the previous result" in forAll { (id: EntityId, types: EntityTypes, property1: (Property, JsonLD), other: List[(Property, JsonLD)]) => val entityAsJsonLD = JsonLD.arr(JsonLD.entity(id, types, property1, other: _*)) entityAsJsonLD.flatten.flatMap(_.flatten) shouldBe Right(entityAsJsonLD) } - } } "unsafeFlatten" should { diff --git a/src/test/scala/io/renku/jsonld/JsonLDMergeSpec.scala b/src/test/scala/io/renku/jsonld/JsonLDMergeSpec.scala index dfc309a..973acb2 100644 --- a/src/test/scala/io/renku/jsonld/JsonLDMergeSpec.scala +++ b/src/test/scala/io/renku/jsonld/JsonLDMergeSpec.scala @@ -31,46 +31,40 @@ class JsonLDMergeSpec extends AnyWordSpec with ScalaCheckPropertyChecks with sho "merge" should { - "do nothing for JsonLDValue" in { + "do nothing for JsonLDValue" in forAll(jsonLDValues) { json => json.merge shouldBe Right(json) } - } - "do nothing for JsonLDEntityId" in { + "do nothing for JsonLDEntityId" in forAll { entityId: EntityId => val json = JsonLD.fromEntityId(entityId) json.merge shouldBe Right(json) } - } "do nothing for JsonLDNull" in { JsonLD.Null.merge shouldBe Right(JsonLD.Null) } - "do nothing for JsonLDEntity" in { + "do nothing for JsonLDEntity" in forAll { entity: JsonLDEntity => entity.merge shouldBe Right(entity) } - } - "do nothing for JsonLDArray of JsonLDEntities if there are not Edges to merge" in { + "do nothing for JsonLDArray of JsonLDEntities if there are not Edges to merge" in forAll { (entity1: JsonLDEntity, entity2: JsonLDEntity) => JsonLD.arr(entity1, entity2).merge shouldBe Right(JsonLD.arr(entity1, entity2)) } - } - "do nothing for JsonLDArray of JsonLDEntityIds" in { + "do nothing for JsonLDArray of JsonLDEntityIds" in forAll { (id1: EntityId, id2: EntityId) => JsonLD.arr(id1.asJsonLD, id2.asJsonLD).merge shouldBe Right(JsonLD.arr(id1.asJsonLD, id2.asJsonLD)) } - } - "do nothing for JsonLDArray of JsonLDValues" in { + "do nothing for JsonLDArray of JsonLDValues" in forAll(jsonLDValues, jsonLDValues) { (value1, value2) => JsonLD.arr(value1, value2).merge shouldBe Right(JsonLD.arr(value1, value2)) } - } "move targets from relevant edges to JsonLDEntities defined in the JsonLDArray" in { val entity1 = JsonLD.entity( diff --git a/src/test/scala/io/renku/jsonld/JsonLDSpec.scala b/src/test/scala/io/renku/jsonld/JsonLDSpec.scala index 5f19096..2d2c02f 100644 --- a/src/test/scala/io/renku/jsonld/JsonLDSpec.scala +++ b/src/test/scala/io/renku/jsonld/JsonLDSpec.scala @@ -38,79 +38,70 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "JsonLD.fromString" should { - "allow to construct JsonLD String value" in { + "allow to construct JsonLD String value" in forAll { value: String => JsonLD.fromString(value).toJson shouldBe json"""{ "@value": $value }""" } - } - "have no entityId" in { + "have no entityId" in forAll { value: String => JsonLD.fromString(value).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { value: String => JsonLD.fromString(value).entityTypes shouldBe None } - } } "JsonLD.fromInt" should { - "allow to construct JsonLD Int value" in { + "allow to construct JsonLD Int value" in forAll { value: Int => JsonLD.fromInt(value).toJson shouldBe json"""{ "@value": $value }""" } - } - "have no entityId" in { + "have no entityId" in forAll { value: Int => JsonLD.fromInt(value).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { value: Int => JsonLD.fromInt(value).entityTypes shouldBe None } - } } "JsonLD.fromLong" should { - "allow to construct JsonLD Long value" in { + "allow to construct JsonLD Long value" in forAll { value: Long => JsonLD.fromLong(value).toJson shouldBe json"""{ "@value": $value }""" } - } - "have no entityId" in { + "have no entityId" in forAll { value: Long => JsonLD.fromLong(value).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { value: Long => JsonLD.fromLong(value).entityTypes shouldBe None } - } } "JsonLD.fromInstant" should { - "allow to construct JsonLD xsd:dateTime value" in { + "allow to construct JsonLD xsd:dateTime value" in forAll { value: Instant => JsonLD.fromInstant(value).toJson shouldBe json"""{ @@ -118,24 +109,21 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "@value": $value }""" } - } - "have no entityId" in { + "have no entityId" in forAll { value: Instant => JsonLD.fromInstant(value).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { value: Instant => JsonLD.fromInstant(value).entityTypes shouldBe None } - } } "JsonLD.fromLocalDate" should { - "allow to construct JsonLD xsd:dateTime value" in { + "allow to construct JsonLD xsd:dateTime value" in forAll { value: LocalDate => JsonLD.fromLocalDate(value).toJson shouldBe json"""{ @@ -143,50 +131,43 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "@value": $value }""" } - } - "have no entityId" in { + "have no entityId" in forAll { value: LocalDate => JsonLD.fromLocalDate(value).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { value: LocalDate => JsonLD.fromLocalDate(value).entityTypes shouldBe None } - } } "JsonLD.fromBoolean" should { - "allow to construct JsonLD Boolean value" in { + "allow to construct JsonLD Boolean value" in forAll { value: Boolean => JsonLD.fromBoolean(value).toJson shouldBe json"""{ "@value": $value }""" } - } - "have no entityId" in { + "have no entityId" in forAll { value: Boolean => JsonLD.fromBoolean(value).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { value: Boolean => JsonLD.fromBoolean(value).entityTypes shouldBe None } - } } "JsonLD.Null" should { - "be Json.Null" in { + "be Json.Null" in JsonLD.JsonLDNull.toJson == null - } "have no entityId" in { JsonLD.JsonLDNull.entityId shouldBe None @@ -199,27 +180,25 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "JsonLD.fromOption" should { - "allow to construct JsonLD value when the value is present" in { + "allow to construct JsonLD value when the value is present" in forAll { value: Long => JsonLD.fromOption(Some(value)).toJson shouldBe json"""{ "@value": $value }""" } - } - "allow to construct JsonLD value when the value is present and has some type" in { + "allow to construct JsonLD value when the value is present and has some type" in forAll { value: Instant => implicit val jsonLDEncoder: JsonLDEncoder[Instant] = JsonLDEncoder.instance[Instant](JsonLD.fromInstant) JsonLD.fromOption(Some(value)).toJson shouldBe JsonLD.fromInstant(value).toJson } - } "allow to construct JsonLD value when the value is absent" in { JsonLD.fromOption(Option.empty[String]) shouldBe JsonLD.JsonLDNull } - "have entityId for Some entity having one" in { + "have entityId for Some entity having one" in forAll { (id: EntityId, types: EntityTypes, property: Property, value: String) => implicit val encoder: JsonLDEncoder[Object] = JsonLDEncoder.instance { o: Object => JsonLD.entity(id, types, property -> JsonLD.fromString(o.value)) @@ -227,13 +206,12 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M JsonLD.fromOption(Some(Object(value))).entityId shouldBe Some(id) } - } "have no entityId for None" in { JsonLD.fromOption(Option.empty[String]).entityId shouldBe None } - "have entityTypes for Some entity having one" in { + "have entityTypes for Some entity having one" in forAll { (id: EntityId, types: EntityTypes, property: Property, value: String) => implicit val encoder: JsonLDEncoder[Object] = JsonLDEncoder.instance { o: Object => JsonLD.entity(id, types, property -> JsonLD.fromString(o.value)) @@ -241,7 +219,6 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M JsonLD.fromOption(Some(Object(value))).entityTypes shouldBe Some(types) } - } "have no entityTypes for None" in { JsonLD.fromOption(Option.empty[String]).entityTypes shouldBe None @@ -250,53 +227,47 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "JsonLD.fromEntityId" should { - "allow to construct JsonLD EntityId object" in { + "allow to construct JsonLD EntityId object" in forAll { value: EntityId => JsonLD.fromEntityId(value).toJson shouldBe json"""{ "@id": $value }""" } - } - "have no entityId" in { + "have no entityId" in forAll { value: EntityId => JsonLD.fromEntityId(value).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { value: EntityId => JsonLD.fromEntityId(value).entityTypes shouldBe None } - } } "JsonLD.arr" should { - "allow to construct an array of JsonLD objects" in { + "allow to construct an array of JsonLD objects" in forAll { values: List[String] => JsonLD.arr(values map JsonLD.fromString: _*).toJson shouldBe Json.arr(values map Json.fromString map (json => Json.obj("@value" -> json)): _*) } - } - "have no entityId" in { + "have no entityId" in forAll { values: List[String] => JsonLD.arr(values map JsonLD.fromString: _*).entityId shouldBe None } - } - "have no entityTypes" in { + "have no entityTypes" in forAll { values: List[String] => JsonLD.arr(values map JsonLD.fromString: _*).entityTypes shouldBe None } - } } "JsonLD.entity" should { - "allow to construct JsonLD entity with multiple types" in { + "allow to construct JsonLD entity with multiple types" in forAll(entityIds, nonEmptyList(entityTypes, minElements = 2), schemas) { (id, types, schema) => val (name1, values1) = listValueProperties(schema).generateOne val property1 @ (_, value1) = name1 -> JsonLD.arr(values1.toList: _*) @@ -310,9 +281,8 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "$name2": ${value2.toJson} }""").fold(throw _, identity) } - } - "allow to construct JsonLD entity with single type" in { + "allow to construct JsonLD entity with single type" in forAll(entityIds, entityTypes, schemas) { (id, entityType, schema) => val (name1, values1) = listValueProperties(schema).generateOne val property1 @ (_, value1) = name1 -> JsonLD.arr(values1.toList: _*) @@ -326,9 +296,8 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "$name2": ${value2.toJson} }""").fold(throw _, identity) } - } - "skip properties with properties having None values" in { + "skip properties with properties having None values" in forAll { (id: EntityId, entityType: EntityType, schema: Schema) => val property1 @ (name1, value1) = singleValueProperties(schema).generateOne val property2 = properties.generateOne -> JsonLD.fromOption(Option.empty[String]) @@ -340,9 +309,8 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "$name1": ${value1.toJson} }""").fold(throw _, identity) } - } - "be able to add a single reverse property" in { + "be able to add a single reverse property" in forAll { (parentId: EntityId, parentTypes: EntityTypes, @@ -369,9 +337,8 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M } }""").fold(throw _, identity) } - } - "be able to add multiple reverse properties" in { + "be able to add multiple reverse properties" in forAll { (parentId: EntityId, parentTypes: EntityTypes, @@ -408,9 +375,8 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M } }""").fold(throw _, identity) } - } - "be able to add reverse property with a list of entities" in { + "be able to add reverse property with a list of entities" in forAll { (parentId: EntityId, parentTypes: EntityTypes, parentProperty: (Property, JsonLD)) => val reverseProperty = properties.generateOne val reversePropertyEntities = jsonLDEntities.generateNonEmptyList().toList @@ -427,9 +393,8 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M } }""").fold(throw _, identity) } - } - "be able to skip reverse property when empty" in { + "be able to skip reverse property when empty" in forAll { (parentId: EntityId, parentTypes: EntityTypes, parentProperty: (Property, JsonLD)) => JsonLD .entity(parentId, parentTypes, Reverse.empty, parentProperty) @@ -440,66 +405,57 @@ class JsonLDSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "${parentProperty._1}": ${parentProperty._2.toJson} }""").fold(throw _, identity) } - } - "have some entityId" in { + "have some entityId" in forAll { (id: EntityId, types: EntityTypes, property: (Property, JsonLD)) => JsonLD.entity(id, types, property).entityId shouldBe Some(id) } - } - "have some entityTypes" in { + "have some entityTypes" in forAll { (id: EntityId, types: EntityTypes, property: (Property, JsonLD)) => JsonLD.entity(id, types, property).entityTypes shouldBe Some(types) } - } } "asArray" should { - "return vector with single JsonLDValue" in { + "return vector with single JsonLDValue" in forAll(jsonLDValues) { json => json.asArray shouldBe Some(Vector(json)) } - } - "return vector with single JsonLDEntityID" in { + "return vector with single JsonLDEntityID" in forAll { entityId: EntityId => val idAsJson = JsonLD.fromEntityId(entityId) idAsJson.asArray shouldBe Some(Vector(idAsJson)) } - } "return None on a JsonLDNull" in { JsonLD.Null.asArray shouldBe None } - "return vector containing the entity" in { + "return vector containing the entity" in forAll(jsonLDEntities) { entity => entity.asArray shouldBe Some(Vector(entity)) } - } - "return a vector with the same elements as the array" in { + "return a vector with the same elements as the array" in forAll(nonEmptyList(jsonLDEntities)) { entities => JsonLD.arr(entities.toList: _*).asArray shouldBe Some(entities.toList.toVector) } - } } "equals & hashCode" should { - "return true for two JsonLDArrays with the same elements in the same order" in { + "return true for two JsonLDArrays with the same elements in the same order" in forAll(nonEmptyList(jsonLDEntities)) { entities => JsonLD.arr(entities.toList: _*) shouldBe JsonLD.arr(entities.toList: _*) JsonLD.arr(entities.toList: _*).hashCode shouldBe JsonLD.arr(entities.toList: _*).hashCode } - } - "return true for two JsonLDArrays with the same elements in *different* order" in { + "return true for two JsonLDArrays with the same elements in *different* order" in forAll(nonEmptyList(jsonLDEntities)) { entities => JsonLD.arr(entities.toList: _*) shouldBe JsonLD.arr(Random.shuffle(entities.toList): _*) JsonLD.arr(entities.toList: _*).hashCode shouldBe JsonLD.arr(Random.shuffle(entities.toList): _*).hashCode } - } } diff --git a/src/test/scala/io/renku/jsonld/NamedGraphSpec.scala b/src/test/scala/io/renku/jsonld/NamedGraphSpec.scala index 60ce6a5..e201bc0 100644 --- a/src/test/scala/io/renku/jsonld/NamedGraphSpec.scala +++ b/src/test/scala/io/renku/jsonld/NamedGraphSpec.scala @@ -126,14 +126,13 @@ class NamedGraphSpec extends AnyWordSpec with should.Matchers with ScalaCheckPro "toJson" should { - "turn the given NamedGraph into JSON" in { + "turn the given NamedGraph into JSON" in forAll { (graph: NamedGraph) => graph.toJson shouldBe Json.obj( "@id" -> graph.id.asJson, "@graph" -> Json.fromValues(graph.entities.map(_.toJson)) ) } - } } "entityId" should { @@ -221,29 +220,27 @@ class NamedGraphSpec extends AnyWordSpec with should.Matchers with ScalaCheckPro } "return NamedGraph with flattened entities in the sense of an JsonLDArray merge " + - "if there's more than one entity" in { - forAll { - (childlessGrandparent: JsonLDEntity, parentRelationProperty: Property, parentNotNested: JsonLDEntity) => - val childrenTuples = entityProperties.generateNonEmptyList().toList - val parentWithProperties = parentNotNested.add(childrenTuples) - val grandparentWithChild = childlessGrandparent.add(parentRelationProperty -> parentWithProperties) - - val flattenedGrandparent = - childlessGrandparent.add(parentRelationProperty -> JsonLD.fromEntityId(parentWithProperties.id)) - val flattenedParent = childrenTuples - .foldLeft(parentNotNested) { case (parent, (property, child)) => - parent.add(property -> child.id.asJsonLD) - } - val children = childrenTuples.map { case (_, entity) => entity: JsonLD } - - val otherEntity = jsonLDEntities.generateOne - val edge = jsonLDEdges.generateOne - - val Right(flattened) = NamedGraph(entityIds.generateOne, grandparentWithChild, otherEntity, edge).flatten - - flattened.entities should contain theSameElementsAs - flattenedGrandparent :: flattenedParent :: children ::: otherEntity :: edge :: Nil - } + "if there's more than one entity" in + forAll { (childlessGrandparent: JsonLDEntity, parentRelationProperty: Property, parentNotNested: JsonLDEntity) => + val childrenTuples = entityProperties.generateNonEmptyList().toList + val parentWithProperties = parentNotNested.add(childrenTuples) + val grandparentWithChild = childlessGrandparent.add(parentRelationProperty -> parentWithProperties) + + val flattenedGrandparent = + childlessGrandparent.add(parentRelationProperty -> JsonLD.fromEntityId(parentWithProperties.id)) + val flattenedParent = childrenTuples + .foldLeft(parentNotNested) { case (parent, (property, child)) => + parent.add(property -> child.id.asJsonLD) + } + val children = childrenTuples.map { case (_, entity) => entity: JsonLD } + + val otherEntity = jsonLDEntities.generateOne + val edge = jsonLDEdges.generateOne + + val Right(flattened) = NamedGraph(entityIds.generateOne, grandparentWithChild, otherEntity, edge).flatten + + flattened.entities should contain theSameElementsAs + flattenedGrandparent :: flattenedParent :: children ::: otherEntity :: edge :: Nil } } } diff --git a/src/test/scala/io/renku/jsonld/ReverseSpec.scala b/src/test/scala/io/renku/jsonld/ReverseSpec.scala index 69e4838..d6ad5bb 100644 --- a/src/test/scala/io/renku/jsonld/ReverseSpec.scala +++ b/src/test/scala/io/renku/jsonld/ReverseSpec.scala @@ -33,7 +33,7 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. "of(Property -> List[JsonLD])" should { - "return right if all the given items are JsonLDEntities" in { + "return right if all the given items are JsonLDEntities" in forAll(properties, nonEmptyList(jsonLDEntities)) { (property, entities) => val Right(entity) = Reverse.of(property -> entities.toList) @@ -41,16 +41,14 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. ${property.url}: ${Json.arr(entities.map(_.toJson).toList: _*)} }""" } - } - "return left if at least one of the given items is not JsonLDEntity" in { + "return left if at least one of the given items is not JsonLDEntity" in forAll { (property: Property, entities: List[JsonLDEntity], nonEntity: String) => val Left(exception) = Reverse.of(property -> (entities :+ JsonLD.fromString(nonEntity))) exception shouldBe an[IllegalArgumentException] exception.getMessage shouldBe s""""@reverse" "$property" property can exist on entity only""" } - } "return Reverse.emtpy if an empty list is passed in" in { Reverse.of(properties.generateOne -> List.empty) shouldBe Right(Reverse.empty) @@ -59,7 +57,7 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. "of varargs of Property to JsonLD" should { - "return an instance of Reverse if the given properties' values are JsonLDEntities" in { + "return an instance of Reverse if the given properties' values are JsonLDEntities" in forAll(properties, jsonLDEntities, properties, jsonLDEntities) { (property1, entity1, property2, entity2) => val Right(reverse) = Reverse.of( property1 -> entity1, @@ -71,9 +69,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. ${property2.url}: ${entity2.toJson} }""" } - } - "return an instance of Reverse if the given properties' values are JsonLDArrays of JsonLDEntities" in { + "return an instance of Reverse if the given properties' values are JsonLDArrays of JsonLDEntities" in forAll(properties, nonEmptyList(jsonLDEntities)) { (property, entities) => val arrayValue = JsonLD.arr(entities.toList: _*) val Right(reverse) = Reverse.of( @@ -84,9 +81,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. ${property.url}: ${arrayValue.toJson} }""" } - } - "return left if there are properties with values neither JsonLDEntities nor arrays of JsonLDEntities" in { + "return left if there are properties with values neither JsonLDEntities nor arrays of JsonLDEntities" in forAll(properties, jsonLDEntities, properties, jsonLDValues, properties, jsonLDEntities) { (property1, entity1, property2, value2, property3, entity3) => val Left(exception) = Reverse.of( @@ -98,9 +94,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. exception shouldBe an[IllegalArgumentException] exception.getMessage shouldBe s""""@reverse" "$property2" property has to exist on an object""" } - } - "return left if there are properties with values which are arrays of not JsonLDEntities" in { + "return left if there are properties with values which are arrays of not JsonLDEntities" in forAll(properties, nonEmptyList(jsonLDEntities), properties, nonEmptyList(jsonLDValues)) { (property1, entities, property2, values) => val Left(exception) = Reverse.of( @@ -111,12 +106,11 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. exception shouldBe an[IllegalArgumentException] exception.getMessage shouldBe s""""@reverse" "$property2" property has to exist on each object of an array""" } - } } "ofJsonLDsUnsafe" should { - "return an instance of Reverse if the given properties' values are JsonLDEntities" in { + "return an instance of Reverse if the given properties' values are JsonLDEntities" in forAll(properties, jsonLDEntities, properties, jsonLDEntities) { (property1, entity1, property2, entity2) => Reverse .ofJsonLDsUnsafe( @@ -128,9 +122,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. ${property2.url}: ${entity2.toJson} }""" } - } - "fail if there are properties with values neither JsonLDEntities nor arrays of JsonLDEntities" in { + "fail if there are properties with values neither JsonLDEntities nor arrays of JsonLDEntities" in forAll(properties, jsonLDEntities, properties, jsonLDValues, properties, jsonLDEntities) { (property1, entity1, property2, value2, property3, entity3) => val exception = intercept[Exception] { @@ -144,7 +137,6 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. exception shouldBe an[IllegalArgumentException] exception.getMessage shouldBe s""""@reverse" "$property2" property has to exist on an object""" } - } } "fromList" should { @@ -153,7 +145,7 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. Reverse.fromList(Nil) shouldBe Right(Reverse.empty) } - "return an instance of Reverse if the given properties' values are JsonLDEntities" in { + "return an instance of Reverse if the given properties' values are JsonLDEntities" in forAll(properties, jsonLDEntities, properties, jsonLDEntities) { (property1, entity1, property2, entity2) => val Right(reverse) = Reverse.fromList( List( @@ -167,9 +159,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. ${property2.url}: ${entity2.toJson} }""" } - } - "return an instance of Reverse if the given properties' values are JsonLDArrays of JsonLDEntities" in { + "return an instance of Reverse if the given properties' values are JsonLDArrays of JsonLDEntities" in forAll(properties, nonEmptyList(jsonLDEntities)) { (property, entities) => val arrayValue = JsonLD.arr(entities.toList: _*) val Right(reverse) = Reverse.fromList( @@ -180,9 +171,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. ${property.url}: ${arrayValue.toJson} }""" } - } - "return an instance of Reverse if the given properties' values are JsonLDArrays of JsonLDEntityIds" in { + "return an instance of Reverse if the given properties' values are JsonLDArrays of JsonLDEntityIds" in forAll(properties, nonEmptyList(entityIds)) { (property, entityIds) => val arrayValue = JsonLD.arr(entityIds.map(id => JsonLDEntityId(id)).toList: _*) val Right(reverse) = Reverse.fromList( @@ -193,9 +183,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. ${property.url}: ${arrayValue.toJson} }""" } - } - "return left if there are properties with values neither JsonLDEntities nor arrays of JsonLDEntities nor JsonLDEntityIds" in { + "return left if there are properties with values neither JsonLDEntities nor arrays of JsonLDEntities nor JsonLDEntityIds" in forAll(properties, jsonLDEntities, properties, jsonLDValues, properties, entityIds) { (property1, entity1, property2, value2, property3, entityId) => val Left(exception) = Reverse.fromList( @@ -209,9 +198,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. exception shouldBe an[IllegalArgumentException] exception.getMessage shouldBe s""""@reverse" "$property2" property has to exist on an object""" } - } - "return left if there are properties with values which are arrays of not JsonLDEntities" in { + "return left if there are properties with values which are arrays of not JsonLDEntities" in forAll(properties, nonEmptyList(jsonLDEntities), properties, nonEmptyList(jsonLDValues)) { (property1, entities, property2, values) => val Left(exception) = Reverse.fromList( @@ -224,12 +212,11 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. exception shouldBe an[IllegalArgumentException] exception.getMessage shouldBe s""""@reverse" "$property2" property has to exist on each object of an array""" } - } } "fromListUnsafe" should { - "return an instance of Reverse if all the given items are JsonLDEntities or JsonLDIds" in { + "return an instance of Reverse if all the given items are JsonLDEntities or JsonLDIds" in forAll(properties, nonEmptyList(jsonLDEntities), properties, jsonLDEntities, nonEmptyList(entityIds)) { (property1, entities, property2, entity, entityIds) => val jsonLDEntityIds = entityIds.map(JsonLDEntityId(_)) @@ -240,9 +227,8 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. Reverse.fromListUnsafe(properties) shouldBe Reverse.fromList(properties).fold(throw _, identity) } - } - "throw an exception if at least one of the properties' values is not JsonLDEntity nor an JsonLDEntityId" in { + "throw an exception if at least one of the properties' values is not JsonLDEntity nor an JsonLDEntityId" in forAll(properties, nonEmptyList(jsonLDEntities), properties, jsonLDValues, properties, entityIds) { (property1, entities, property2, value, property3, entityId) => val exception = intercept[Exception] { @@ -258,27 +244,23 @@ class ReverseSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should. exception shouldBe an[IllegalArgumentException] exception.getMessage shouldBe s""""@reverse" "$property2" property has to exist on an object""" } - } } "equals" should { - "return true if there are two Reverse instances with the same properties" in { + "return true if there are two Reverse instances with the same properties" in forAll(properties, nonEmptyList(jsonLDEntities)) { (property, entities) => Reverse.of(property -> entities.toList) shouldBe Reverse.of(property -> entities.toList) } - } - "return false if there are two Reverse instances with different properties" in { + "return false if there are two Reverse instances with different properties" in forAll(properties, nonEmptyList(jsonLDEntities)) { (property, entities) => Reverse.of(property -> entities.toList) should not be Reverse.empty } - } - "return false if compared with non Reverse object" in { + "return false if compared with non Reverse object" in forAll(properties, nonEmptyList(jsonLDEntities)) { (property, entities) => Reverse.of(property -> entities.toList) should not be 1 } - } } } diff --git a/src/test/scala/io/renku/jsonld/ontology/OntologySpec.scala b/src/test/scala/io/renku/jsonld/ontology/OntologySpec.scala index 6c21363..333d63f 100644 --- a/src/test/scala/io/renku/jsonld/ontology/OntologySpec.scala +++ b/src/test/scala/io/renku/jsonld/ontology/OntologySpec.scala @@ -41,10 +41,8 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(leaf, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(leaf.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "name", @@ -73,12 +71,10 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(rootType, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(rootType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD .entity(leaf.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "leaf", @@ -86,7 +82,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "domain" -> List(rootType.clazz.id).asJsonLD, rdfs / "range" -> List(ObjectPropertyRange(leaf)).asJsonLD ) :: - JsonLD .entity( schema / "name", @@ -121,14 +116,12 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(rootType, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(rootType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD .entity(otherLeafType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD .entity(leaf.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "leaf", @@ -136,7 +129,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "domain" -> List(rootType.clazz.id).asJsonLD, rdfs / "range" -> List(ObjectPropertyRange(leaf), ObjectPropertyRange(otherLeafType)).asJsonLD ) :: - JsonLD .entity( schema / "name", @@ -176,7 +168,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(rootType, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(rootType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD @@ -185,7 +176,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { .entity(otherLeafType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD .entity(yetAnotherLeafType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "leaf", @@ -200,7 +190,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "domain" -> List(rootType.clazz.id).asJsonLD, rdfs / "range" -> List(ObjectPropertyRange(otherLeafType)).asJsonLD ) :: - JsonLD .entity( schema / "name", @@ -233,12 +222,10 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(rootType, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(rootType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD .entity(leaf.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "leaf", @@ -246,7 +233,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "domain" -> List(rootType.clazz.id).asJsonLD, rdfs / "range" -> List(ObjectPropertyRange(leaf)).asJsonLD ) :: - JsonLD .entity( schema / "name", @@ -283,12 +269,10 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(rootType, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(rootType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD .entity(otherLeafType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "leaf", @@ -297,7 +281,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "range" -> List(ObjectPropertyRange(otherLeafType)).asJsonLD, rdfs / "subPropertyOf" -> EntityId.of(owl / "topObjectProperty").asJsonLD ) :: - JsonLD .entity( schema / "name", @@ -324,7 +307,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(rootType, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(rootType.clazz.id, EntityTypes of owl / "Class", @@ -335,7 +317,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { EntityTypes of owl / "Class", rdfs / "subClassOf" -> List(EntityId.of(schema / "Top")).asJsonLD ) :: - JsonLD .entity( schema / "leaf", @@ -362,12 +343,10 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(rootType, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(rootType.clazz.id, EntityTypes of owl / "Class", rdfs / "comment" -> "Class comment".asJsonLD) :: JsonLD .entity(otherLeafType.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "leaf", @@ -376,7 +355,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "range" -> List(ObjectPropertyRange(otherLeafType)).asJsonLD, rdfs / "comment" -> "Object prop comment".asJsonLD ) :: - JsonLD .entity( schema / "name", @@ -385,7 +363,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "range" -> List(DataPropertyRange(xsd / "string")).asJsonLD, rdfs / "comment" -> "Data prop comment".asJsonLD ) :: - Nil: _* ) } @@ -444,10 +421,8 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(root, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(root.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "root", @@ -480,12 +455,10 @@ class OntologySpec extends AnyWordSpec with should.Matchers { generateOntology(root, ontologyId) shouldBe JsonLD.arr( JsonLD .entity(ontologyId, EntityTypes of owl / "Ontology", owl / "imports" -> JsonLD.arr(oa.asJsonLD)) :: - JsonLD .entity(root.clazz.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: JsonLD .entity(reverseClass.id, EntityTypes of owl / "Class", Map.empty[Property, JsonLD]) :: - JsonLD .entity( schema / "root", @@ -493,7 +466,6 @@ class OntologySpec extends AnyWordSpec with should.Matchers { rdfs / "domain" -> List(reverseClass.id).asJsonLD, rdfs / "range" -> List(ObjectPropertyRange(root)).asJsonLD ) :: - JsonLD .entity( schema / "name", diff --git a/src/test/scala/io/renku/jsonld/syntax/syntaxSpec.scala b/src/test/scala/io/renku/jsonld/syntax/syntaxSpec.scala index be14a04..ebcbd29 100644 --- a/src/test/scala/io/renku/jsonld/syntax/syntaxSpec.scala +++ b/src/test/scala/io/renku/jsonld/syntax/syntaxSpec.scala @@ -32,35 +32,30 @@ class syntaxSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M "asJsonLD" should { - "convert a String object into a JsonLD" in { + "convert a String object into a JsonLD" in forAll { value: String => value.asJsonLD shouldBe JsonLD.fromString(value) } - } - "convert an Int object into a JsonLD" in { + "convert an Int object into a JsonLD" in forAll { value: Int => value.asJsonLD shouldBe JsonLD.fromInt(value) } - } - "convert a Long object into a JsonLD" in { + "convert a Long object into a JsonLD" in forAll { value: Long => value.asJsonLD shouldBe JsonLD.fromLong(value) } - } - "convert a Instant into a JsonLD" in { + "convert a Instant into a JsonLD" in forAll { value: Instant => value.asJsonLD shouldBe JsonLD.fromInstant(value) } - } - "convert a LocalDate into a JsonLD" in { + "convert a LocalDate into a JsonLD" in forAll { value: LocalDate => value.asJsonLD shouldBe JsonLD.fromLocalDate(value) } - } "convert a custom value object into a JsonLD" in { case class ValueObject(value: String) @@ -71,31 +66,27 @@ class syntaxSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M } } - "convert an Option into a JsonLD" in { + "convert an Option into a JsonLD" in forAll { maybeValue: Option[Long] => maybeValue.asJsonLD shouldBe JsonLD.fromOption(maybeValue) } - } - "convert a sequence of objects into a JsonLD" in { + "convert a sequence of objects into a JsonLD" in forAll { seq: Seq[Long] => seq.asJsonLD shouldBe JsonLD.arr(seq map JsonLD.fromLong: _*) } - } - "convert a list of objects into a JsonLD" in { + "convert a list of objects into a JsonLD" in forAll { seq: List[Long] => seq.asJsonLD shouldBe JsonLD.arr(seq map JsonLD.fromLong: _*) } - } - "convert a set of objects into a JsonLD" in { + "convert a set of objects into a JsonLD" in forAll { set: Set[Long] => set.asJsonLD shouldBe JsonLD.arr(set.toList.sorted map JsonLD.fromLong: _*) } - } - "convert a custom object into a JsonLD" in { + "convert a custom object into a JsonLD" in forAll { (schema: Schema, id: EntityId, types: EntityTypes, string: String, int: Int) => implicit val encoder: JsonLDEncoder[Object] = JsonLDEncoder.instance { o => JsonLD.entity( @@ -113,7 +104,6 @@ class syntaxSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M schema / "int" -> int.asJsonLD ) } - } "convert a custom nested objects into a JsonLD" in { @@ -151,7 +141,7 @@ class syntaxSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M } } - "convert a custom nested objects defined by @id only into a JsonLD" in { + "convert a custom nested objects defined by @id only into a JsonLD" in forAll { (schema: Schema, parentId: EntityId, parentTypes: EntityTypes, parentField: String, childField: Int) => implicit val childEncoder: JsonLDEncoder[Child] = JsonLDEncoder.entityId { o => EntityId.of(schema / o.int) @@ -172,20 +162,18 @@ class syntaxSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M schema / "child" -> JsonLD.fromEntityId(EntityId.of(schema / childField)) ) } - } } "asEntityType" should { - "convert a Property to EntityType" in { + "convert a Property to EntityType" in forAll { property: Property => property.asEntityType shouldBe EntityType.of(property) } - } } "asEntityId" should { - "return EntityId for a custom object" in { + "return EntityId for a custom object" in forAll { (id: EntityId, string: String, int: Int) => implicit val encoder: EntityIdEncoder[Object] = EntityIdEncoder.instance { _ => id @@ -193,7 +181,6 @@ class syntaxSpec extends AnyWordSpec with ScalaCheckPropertyChecks with should.M Object(string, int).asEntityId shouldBe id } - } } private case class Object(string: String, int: Int)