Skip to content

Commit

Permalink
organize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Aug 29, 2024
1 parent 19bfaef commit a23046c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 46 deletions.
1 change: 1 addition & 0 deletions app/org/maproulette/provider/ChallengeProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ class ChallengeProvider @Inject() (
}
}
}

// Helper function to retrieve a non-null, non-empty string from a field
// Supports both string and numeric IDs.
def getNonNullString(value: JsValue, fieldName: String): Option[String] = {
Expand Down
85 changes: 39 additions & 46 deletions test/org/maproulette/provider/ChallengeProviderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,27 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
repository.featureOSMId(json, challengeWithOsmId) mustEqual Some("9999999")
}

"return None if OSM ID not found in root or properties" in {
val json = Json.obj("otherField" -> "value")
repository.featureOSMId(json, challengeWithOsmId) mustEqual None
}

"return OSM ID from first feature in list if specified in challenge" in {
val json = Json.obj("features" -> Json.arr(Json.obj("custom_osm_id" -> "featureId1")))
repository.featureOSMId(json, challengeWithOsmId) mustEqual Some("featureId1")
}

"return OSM ID from nested features if specified in challenge" in {
val json = Json.obj(
"features" -> Json.arr(
Json.obj(
"properties" -> Json.obj("custom_osm_id" -> "nestedFeatureId1")
)
)
)
repository.featureOSMId(json, challengeWithOsmId) mustEqual Some("nestedFeatureId1")
}

"return None if OSM ID not found in root or properties" in {
val json = Json.obj("otherField" -> "value")
repository.featureOSMId(json, challengeWithOsmId) mustEqual None
}

"return None if features do not contain specified OSM ID field" in {
val json = Json.obj("features" -> Json.arr(Json.obj("otherField" -> "value1")))
repository.featureOSMId(json, challengeWithOsmId) mustEqual None
Expand All @@ -147,17 +158,6 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
val json = Json.obj("features" -> Json.arr())
repository.featureOSMId(json, challengeWithOsmId) mustEqual None
}

"return OSM ID from nested features if specified in challenge" in {
val json = Json.obj(
"features" -> Json.arr(
Json.obj(
"properties" -> Json.obj("custom_osm_id" -> "nestedFeatureId1")
)
)
)
repository.featureOSMId(json, challengeWithOsmId) mustEqual Some("nestedFeatureId1")
}
}

"taskNameFromJsValue" should {
Expand All @@ -166,31 +166,7 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
repository.taskNameFromJsValue(json, challengeWithOsmId) mustEqual "12345"
}

"return random UUID if OSM ID field is specified but not found" in {
val json = Json.obj("otherField" -> "value")
val result = repository.taskNameFromJsValue(json, challengeWithOsmId)
assert(UUID.fromString(result).toString == result)
}

"return random UUID if no valid feature ID is found and no challenge-specific ID is available" in {
val json = Json.obj("features" -> Json.arr(Json.obj("otherField" -> "value")))
val result = repository.taskNameFromJsValue(json, challengeWithoutOsmId)
assert(UUID.fromString(result).toString == result)
}

"return random UUID if no valid ID fields are found" in {
val json = Json.obj()
val result = repository.taskNameFromJsValue(json, challengeWithoutOsmId)
assert(UUID.fromString(result).toString == result)
}

"return random UUID if features array is empty" in {
val json = Json.obj("features" -> Json.arr())
val result = repository.taskNameFromJsValue(json, challengeWithoutOsmId)
assert(UUID.fromString(result).toString == result)
}

"return field from properties if ID field is 'null' and other fields are valid" in {
"return feature id from properties if ID field is null and other fields are valid" in {
val json = Json.obj(
"id" -> null,
"properties" -> Json
Expand All @@ -199,15 +175,14 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
repository.taskNameFromJsValue(json, challengeWithoutOsmId) mustEqual "idstring"
}

"return field from properties if ID field is null and properties contain valid IDs" in {
"return feature id from properties if ID field is null and properties contain valid IDs" in {
val json = Json.obj(
"fillerProperty" -> "string",
"properties" -> Json.obj("fillerProperty" -> "string", "id" -> null, "name" -> "testName")
"properties" -> Json.obj("fillerProperty" -> "string", "id" -> null, "name" -> "testName")
)
repository.taskNameFromJsValue(json, challengeWithoutOsmId) mustEqual "testName"
}

"return field from the first feature if the ID field is 'null' and challenge specifies valid feature IDs" in {
"return feature id from the first feature if the ID field is null and challenge specifies valid feature IDs" in {
val json = Json.obj(
"features" -> Json.arr(
Json.obj("id" -> null, "properties" -> Json.obj("name" -> "featureName")),
Expand All @@ -217,7 +192,25 @@ class ChallengeProviderSpec extends PlaySpec with MockitoSugar {
repository.taskNameFromJsValue(json, challengeWithoutOsmId) mustEqual "featureName"
}

"return a UUID if JSON features array has objects with null or empty names" in {
"return random UUID if OSM ID field is specified but not found" in {
val json = Json.obj("otherField" -> "value")
val result = repository.taskNameFromJsValue(json, challengeWithOsmId)
assert(UUID.fromString(result).toString == result)
}

"return random UUID if no valid ID fields are found" in {
val json = Json.obj()
val result = repository.taskNameFromJsValue(json, challengeWithoutOsmId)
assert(UUID.fromString(result).toString == result)
}

"return random UUID if features array is empty" in {
val json = Json.obj("features" -> Json.arr())
val result = repository.taskNameFromJsValue(json, challengeWithoutOsmId)
assert(UUID.fromString(result).toString == result)
}

"return random UUID if JSON features array has objects with null or empty names" in {
val json = Json.obj("features" -> Json.arr(Json.obj("name" -> ""), Json.obj("name" -> null)))
val result = repository.taskNameFromJsValue(json, challengeWithoutOsmId)
assert(UUID.fromString(result).toString == result)
Expand Down

0 comments on commit a23046c

Please sign in to comment.