Skip to content

Commit

Permalink
Reformat some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmior committed Mar 7, 2024
1 parent a8eb619 commit 56bb6ee
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

import UnitSpec._

class DiscoverSchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
behavior of "DiscoverSchema"

Expand Down Expand Up @@ -84,10 +82,11 @@ class DiscoverSchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
) shouldBe None
}

it should "produce minimal properties when requested" in {
val stringSchema = DiscoverSchema.discover(
Seq(JString("foo")).iterator
)(JsonoidParams().withPropertySet(PropertySets.MinProperties))
it should "produce minimal properties when requested" in withParams(propSet =
PropertySets.MinProperties
) { implicit params =>
val stringSchema =
DiscoverSchema.discover(Seq(JString("foo")).iterator)(params)
stringSchema.properties shouldBe empty
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package io.github.dataunitylab.jsonoid.discovery
import schemas._

class EquivalenceRelationSpec extends UnitSpec {

implicit val propSet: PropertySet = PropertySets.MinProperties

val objSchema1: ObjectSchema = ObjectSchema(Map("foo" -> BooleanSchema()))
val objSchema2: ObjectSchema = ObjectSchema(Map("bar" -> BooleanSchema()))
val objSchema3: ObjectSchema = ObjectSchema(
Expand All @@ -17,21 +14,17 @@ class EquivalenceRelationSpec extends UnitSpec {

behavior of "KindEquivalenceRelation"

it should "keep schemas separate when merging by label" in {
val merged = objSchema1.merge(objSchema2)(
JsonoidParams().withER(
EquivalenceRelations.LabelEquivalenceRelation
)
)
merged shouldBe a[ProductSchema]
it should "keep schemas separate when merging by label" in withParams(er =
EquivalenceRelations.LabelEquivalenceRelation
) { implicit params =>
objSchema1.merge(objSchema2) shouldBe a[ProductSchema]
}

it should "merge object schemas by kind" in {
it should "merge object schemas by kind" in withParams(er =
EquivalenceRelations.KindEquivalenceRelation
) { implicit params =>
val merged = objSchema1
.merge(objSchema2)(
JsonoidParams()
.withER(EquivalenceRelations.KindEquivalenceRelation)
)
.merge(objSchema2)
.asInstanceOf[ObjectSchema]
merged.properties.get[ObjectTypesProperty].objectTypes shouldBe Map(
"foo" -> BooleanSchema(),
Expand All @@ -41,45 +34,33 @@ class EquivalenceRelationSpec extends UnitSpec {

behavior of "LabelEquivalenceRelation"

it should "create ProductSchemas when merging non-objects" in {
val merged = objSchema1
.merge(IntegerSchema(0))(
JsonoidParams().withER(
EquivalenceRelations.LabelEquivalenceRelation
)
)
merged shouldBe a[ProductSchema]
it should "create ProductSchemas when merging non-objects" in withParams(er =
EquivalenceRelations.LabelEquivalenceRelation
) { implicit params =>
objSchema1.merge(IntegerSchema(0)) shouldBe a[ProductSchema]
}

behavior of "NonEquivalenceRelation"

it should "not merge when using non-equivalence" in {
val merged =
objSchema1.merge(objSchema1)(
JsonoidParams().withER(
EquivalenceRelations.NonEquivalenceRelation
)
)
merged shouldBe a[ProductSchema]
it should "not merge when using non-equivalence" in withParams(er =
EquivalenceRelations.NonEquivalenceRelation
) { implicit params =>
objSchema1.merge(objSchema1) shouldBe a[ProductSchema]
}

behavior of "IntersectingLabelEquivalenceRelation"

it should "separate schemas which have no common labels" in {
val merged = objSchema1.merge(objSchema2)(
JsonoidParams().withER(
EquivalenceRelations.IntersectingLabelEquivalenceRelation
)
)
merged shouldBe a[ProductSchema]
it should "separate schemas which have no common labels" in withParams(er =
EquivalenceRelations.IntersectingLabelEquivalenceRelation
) { implicit params =>
objSchema1.merge(objSchema2) shouldBe a[ProductSchema]
}

it should "keep schemas together which have common labels" in {
it should "keep schemas together which have common labels" in withParams(er =
EquivalenceRelations.IntersectingLabelEquivalenceRelation
) { implicit params =>
val merged = objSchema1
.merge(objSchema3)(
JsonoidParams()
.withER(EquivalenceRelations.IntersectingLabelEquivalenceRelation)
)
.merge(objSchema3)
.asInstanceOf[ObjectSchema]
merged.properties.get[ObjectTypesProperty].objectTypes shouldBe Map(
"foo" -> BooleanSchema(),
Expand All @@ -89,25 +70,21 @@ class EquivalenceRelationSpec extends UnitSpec {

behavior of "TypeMatchEquivalenceRelation"

it should "combine schemas with the same type" in {
it should "combine schemas with the same type" in withParams(er =
EquivalenceRelations.TypeMatchEquivalenceRelation
) { implicit params =>
val merged = objSchema1
.merge(objSchema3)(
JsonoidParams()
.withER(EquivalenceRelations.TypeMatchEquivalenceRelation)
)
.merge(objSchema3)
.asInstanceOf[ObjectSchema]
merged.properties.get[ObjectTypesProperty].objectTypes shouldBe Map(
"foo" -> BooleanSchema(),
"bar" -> BooleanSchema()
)
}

it should "separate schemas which different types" in {
val merged = objSchema1.merge(objSchema4)(
JsonoidParams().withER(
EquivalenceRelations.TypeMatchEquivalenceRelation
)
)
merged shouldBe a[ProductSchema]
it should "separate schemas which different types" in withParams(er =
EquivalenceRelations.TypeMatchEquivalenceRelation
) { implicit params =>
objSchema1.merge(objSchema4) shouldBe a[ProductSchema]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package io.github.dataunitylab.jsonoid.discovery
import org.json4s.JsonDSL._
import org.json4s._

import UnitSpec._

class ForeignKeyFinderSpec extends UnitSpec {
behavior of "ForeignKeyFinder"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class HelpersSpec extends UnitSpec {
}

it should "give a value when finding the max with None" in {
maxOrNone(none, Some("foo")) should be(Some("foo"))
maxOrNone(none, Some("foo")).value shouldBe "foo"
}

it should "give None with the max of two None values" in {
maxOrNone(none, none) should be(None)
maxOrNone(none, none) shouldBe None
}

behavior of "minOrNone"
Expand All @@ -99,11 +99,11 @@ class HelpersSpec extends UnitSpec {
}

it should "give a value when finding the min with None" in {
minOrNone(none, Some("foo")) should be(Some("foo"))
minOrNone(none, Some("foo")).value shouldBe "foo"
}

it should "give None with the min of two None values" in {
minOrNone(none, none) should be(None)
minOrNone(none, none) shouldBe None
}

behavior of "intersectOrNone"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import schemas.NullSchema
import org.json4s.JsonDSL._
import org.json4s._

import UnitSpec._

class PrimaryKeyFinderSpec extends UnitSpec {
behavior of "PrimaryKeyFinder"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ class TypeDetectorSpec extends UnitSpec {
behavior of "TypeDetectorSchema"

it should "detect an array type" in {
TypeDetector.detectType(Map("maxContains" -> 3)) shouldBe Some("array")
TypeDetector.detectType(Map("maxContains" -> 3)).value shouldBe "array"
}

it should "detect a number type" in {
TypeDetector.detectType(Map("maximum" -> 3)) shouldBe Some("number")
TypeDetector.detectType(Map("maximum" -> 3)).value shouldBe "number"
}

it should "detect an object type" in {
TypeDetector.detectType(Map("minProperties" -> 3)) shouldBe Some("object")
TypeDetector.detectType(Map("minProperties" -> 3)).value shouldBe "object"
}

it should "detect a string type" in {
TypeDetector.detectType(Map("format" -> "uri")) shouldBe Some("string")
TypeDetector.detectType(Map("format" -> "uri")).value shouldBe "string"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ abstract class UnitSpec
before {
Logger.getLogger("org.apache.spark").setLevel(Level.OFF)
}

def withParams(
additionalProperties: Boolean = false,
propSet: PropertySet = PropertySets.AllProperties,
er: EquivalenceRelation = EquivalenceRelations.KindEquivalenceRelation
)(testCode: JsonoidParams => Any): Unit = {
implicit val params =
JsonoidParams()
.withAdditionalProperties(additionalProperties)
.withER(er)
.withPropertySet(propSet)
testCode(params)
}
}

object UnitSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ class ArraySchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
}

it should "calculate entropy for a tuple schema" in {
tupleSchema.entropy shouldBe Some(2)
tupleSchema.entropy.value shouldBe 2
}

it should "calculate entropy for an array schema" in {
// This is 2 since the array length can be either 1 or 2
arraySchema.entropy shouldBe Some(2)
arraySchema.entropy.value shouldBe 2
}

behavior of "MinItemsProperty"
Expand All @@ -105,7 +105,8 @@ class ArraySchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
.expandTo(
Some(MinItemsProperty(Some(2)))
)
.minItems shouldBe Some(2)
.minItems
.value shouldBe 2
}

behavior of "MaxItemsProperty"
Expand All @@ -119,7 +120,8 @@ class ArraySchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
.expandTo(
Some(MaxItemsProperty(Some(3)))
)
.maxItems shouldBe Some(3)
.maxItems
.value shouldBe 3
}

behavior of "UniqueProperty"
Expand Down Expand Up @@ -185,14 +187,14 @@ class ArraySchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
}

it should "be able to find subschemas by pointer" in {
tupleSchema.findByPointer("/1") shouldBe Some(BooleanSchema())
tupleSchema.findByPointer("/1").value shouldBe BooleanSchema()
}

it should "be able to find nested subschemas by pointer" in {
val nestedList = List(tupleSchema, tupleSchema)
val nestedSchema =
ArraySchema(ArraySchema(nestedList).properties.mergeValue(nestedList))
nestedSchema.findByPointer("/0/1") shouldBe Some(BooleanSchema())
nestedSchema.findByPointer("/0/1").value shouldBe BooleanSchema()
}

it should "transform array schemas" in {
Expand All @@ -219,13 +221,13 @@ class ArraySchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
.shouldEqual(Right(List(NullSchema(), NullSchema())))
}

it should "have no properties in the minimal property set" in {
it should "have no properties in the minimal property set" in withParams(
propSet = PropertySets.MinProperties
) { implicit params =>
val cp = new Checkpoint()

val arrayProperties =
ArraySchema(List(BooleanSchema()))(
JsonoidParams().withPropertySet(PropertySets.MinProperties)
).properties
ArraySchema(List(BooleanSchema()))(params).properties

cp { arrayProperties should have size 1 }
cp { arrayProperties.get[ItemTypeProperty] }
Expand Down Expand Up @@ -288,23 +290,23 @@ class ArraySchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
}

it should "find the single type in an array schema" in {
arraySchema.findByPointer("/*").shouldEqual(Some(BooleanSchema()))
arraySchema.findByPointer("/*").value shouldBe BooleanSchema()
}

it should "find the array schema itself" in {
arraySchema.findByPointer("/").shouldEqual(Some(arraySchema))
arraySchema.findByPointer("/").value shouldBe arraySchema
}

it should "find nothing in a tuple schema with an empty pointer" in {
tupleSchema.findByPointer(JsonPointer(List(), false)).shouldBe(None)
}

it should "find a type in a tuple schema" in {
tupleSchema.findByPointer("/1").shouldEqual(Some(BooleanSchema()))
tupleSchema.findByPointer("/1").value shouldBe BooleanSchema()
}

it should "find the tuple schema itself" in {
tupleSchema.findByPointer("/").shouldEqual(Some(tupleSchema))
tupleSchema.findByPointer("/").value shouldBe tupleSchema
}

it should "not show anomalies in for non-array values" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class IntegerSchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
it should "expand by incrementing values" in {
MaxIntValueProperty(Some(10))
.expandTo(Some(MaxIntValueProperty((Some(11)))))
.maxIntValue shouldBe Some(11)
.maxIntValue
.value shouldBe 11
}

behavior of "MinIntValueProperty"
Expand All @@ -98,7 +99,8 @@ class IntegerSchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
it should "expand by decrementing values" in {
MinIntValueProperty(Some(14))
.expandTo(Some(MinIntValueProperty((Some(13)))))
.minIntValue shouldBe Some(13)
.minIntValue
.value shouldBe 13
}

behavior of "IntHyperLogLogProperty"
Expand Down Expand Up @@ -194,7 +196,8 @@ class IntegerSchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {
it should "not expand if already covered" in {
IntMultipleOfProperty(Some(14))
.expandTo(Some(IntMultipleOfProperty((Some(28)))))
.multiple shouldBe Some(14)
.multiple
.value shouldBe 14
}

behavior of "IntHistogramProperty"
Expand Down Expand Up @@ -223,10 +226,10 @@ class IntegerSchemaSpec extends UnitSpec with ScalaCheckPropertyChecks {

behavior of "IntegerSchema"

it should "have no properties in the minimal property set" in {
IntegerSchema(0)(
JsonoidParams().withPropertySet(PropertySets.MinProperties)
).properties shouldBe empty
it should "have no properties in the minimal property set" in withParams(
propSet = PropertySets.MinProperties
) { implicit params =>
IntegerSchema(0)(params).properties shouldBe empty
}

it should "show integers as a valid type" in {
Expand Down
Loading

0 comments on commit 56bb6ee

Please sign in to comment.