Skip to content

Commit

Permalink
Remove AnyFunSpec from base trait (#673)
Browse files Browse the repository at this point in the history
* remove AnyFunSpec from base class

* remove AnyFunSpec from base class

* commit missed file
  • Loading branch information
paul-butcher authored Jul 11, 2023
1 parent 6dd0d95 commit 4fbbbb0
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 393 deletions.
3 changes: 2 additions & 1 deletion search/src/test/scala/weco/api/search/ApiErrorsTest.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package weco.api.search

import org.scalatest.funspec.AnyFunSpec
import weco.api.search.works.ApiWorksTestBase

class ApiErrorsTest extends ApiWorksTestBase {
class ApiErrorsTest extends AnyFunSpec with ApiWorksTestBase {
it("returns a Not Found error if you try to get an unrecognised path") {
withApi { route =>
assertNotFound(route)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package weco.api.search

import akka.http.scaladsl.model.ContentTypes
import io.circe.Json
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
import weco.api.search.works.ApiWorksTestBase

class ApiSearchTemplatesTest extends ApiWorksTestBase with Matchers {
class ApiSearchTemplatesTest extends AnyFunSpec with ApiWorksTestBase with Matchers {
it("renders a list of available search templates") {
checkJson { json =>
json.isObject shouldBe true
Expand Down
12 changes: 11 additions & 1 deletion search/src/test/scala/weco/api/search/ApiTestBase.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package weco.api.search

import akka.http.scaladsl.server.Route
import org.scalatest.Assertion
import org.scalatest.{Assertion, Suite}
import weco.api.search.fixtures.ApiFixture

import scala.math

trait ApiTestBase extends ApiFixture {
this: Suite =>
val publicRootUri = "https://api-testing.local/catalogue/v2"

// This is the path relative to which requests are made on the host,
Expand Down Expand Up @@ -41,6 +44,13 @@ trait ApiTestBase extends ApiFixture {
"description": "$description"
}"""

def resultListWithCalculatedPageCount(totalResults: Int, pageSize: Int = 10): String =
resultList(
pageSize,
totalPages = math.ceil(totalResults.toDouble / pageSize).toInt,
totalResults = totalResults
)

def resultList(
pageSize: Int = 10,
totalPages: Int = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import akka.http.scaladsl.server.Route
import com.sksamuel.elastic4s.Index
import io.circe.parser.parse
import io.circe.Json
import org.scalatest.Assertion
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.{Assertion, Suite}
import weco.api.search.SearchApi
import weco.fixtures.TestWith
import weco.api.search.models.{ApiConfig, ElasticConfig, QueryConfig}

trait ApiFixture extends AnyFunSpec with ScalatestRouteTest with IndexFixtures {

trait ApiFixture extends ScalatestRouteTest with IndexFixtures {
this: Suite =>
val Status = akka.http.scaladsl.model.StatusCodes

val publicRootUri: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package weco.api.search.images

import org.scalatest.Suite
import weco.api.search.ApiTestBase
import weco.api.search.fixtures.TestDocumentFixtures
import weco.api.search.json.CatalogueJsonUtil
Expand All @@ -9,7 +10,7 @@ trait ApiImagesTestBase
extends ApiTestBase
with CatalogueJsonUtil
with TestDocumentFixtures {

this: Suite =>
def imagesListResponse(
ids: Seq[String],
strictOrdering: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package weco.api.search.images

import org.scalatest.funspec.AnyFunSpec
import weco.api.search.models.request.SingleImageIncludes

class ImagesAggregationsTest extends ApiImagesTestBase {
class ImagesAggregationsTest extends AnyFunSpec
with ApiImagesTestBase {
it("aggregates by license") {
val images = (0 to 6).map(i => s"images.different-licenses.$i")
val displayImages = images
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package weco.api.search.images

import org.scalatest.funspec.AnyFunSpec
import org.scalatest.prop.TableDrivenPropertyChecks
import weco.api.search.models.ElasticConfig

class ImagesErrorsTest
extends ApiImagesTestBase
extends AnyFunSpec
with ApiImagesTestBase
with TableDrivenPropertyChecks {
describe("returns a 404 for missing resources") {
it("looking up an image that doesn't exist") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package weco.api.search.images

import org.scalatest.funspec.AnyFunSpec
import weco.api.search.models.request.SingleImageIncludes

class ImagesFilteredAggregationsTest extends ApiImagesTestBase {
class ImagesFilteredAggregationsTest extends AnyFunSpec with ApiImagesTestBase {
it("filters and aggregates by license") {
withImagesApi {
case (imagesIndex, routes) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package weco.api.search.images

import akka.http.scaladsl.server.Route
import org.scalatest.Assertion
import org.scalatest.funspec.AnyFunSpec
import weco.fixtures.TestWith

class ImagesFiltersTest extends ApiImagesTestBase {
class ImagesFiltersTest extends AnyFunSpec with ApiImagesTestBase {
describe("filtering images by license") {
it("filters by license") {
withImagesApi {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package weco.api.search.images

class ImagesIncludesTest extends ApiImagesTestBase {
import org.scalatest.funspec.AnyFunSpec

class ImagesIncludesTest extends AnyFunSpec with ApiImagesTestBase {
describe("images includes") {
it(
"includes the source contributors on results from the list endpoint if we pass ?include=source.contributors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package weco.api.search.images

import io.circe.Json
import org.scalatest.funspec.AnyFunSpec

class ImagesSimilarityTest extends ApiImagesTestBase {
class ImagesSimilarityTest extends AnyFunSpec with ApiImagesTestBase {

/**
* These tests treat the simple presence of the requested property as a proxy
Expand Down
15 changes: 9 additions & 6 deletions search/src/test/scala/weco/api/search/images/ImagesTest.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package weco.api.search.images

import org.scalatest.funspec.AnyFunSpec
import weco.api.search.models.request.SingleImageIncludes

class ImagesTest extends ApiImagesTestBase {
class ImagesTest extends AnyFunSpec with ApiImagesTestBase {
it("returns a list of images") {
withImagesApi {
case (imagesIndex, routes) =>
Expand Down Expand Up @@ -112,11 +113,11 @@ class ImagesTest extends ApiImagesTestBase {

assertJsonResponse(
routes,
path =
s"$rootPath/images?sort=source.production.dates"
path = s"$rootPath/images?sort=source.production.dates"
) {
Status.OK -> imagesListResponse(
ids = productionImages, strictOrdering = true
ids = productionImages,
strictOrdering = true
)
}
}
Expand All @@ -133,7 +134,8 @@ class ImagesTest extends ApiImagesTestBase {
s"$rootPath/images?sort=source.production.dates&sortOrder=asc"
) {
Status.OK -> imagesListResponse(
ids = productionImages, strictOrdering = true
ids = productionImages,
strictOrdering = true
)
}
}
Expand All @@ -150,7 +152,8 @@ class ImagesTest extends ApiImagesTestBase {
s"$rootPath/images?sort=source.production.dates&sortOrder=desc"
) {
Status.OK -> imagesListResponse(
ids = productionImages.reverse, strictOrdering = true
ids = productionImages.reverse,
strictOrdering = true
)
}
}
Expand Down
46 changes: 41 additions & 5 deletions search/src/test/scala/weco/api/search/works/ApiWorksTestBase.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package weco.api.search.works

import io.circe.Json
import org.scalatest.Suite
import weco.api.search.ApiTestBase
import weco.api.search.fixtures.TestDocumentFixtures
import weco.api.search.json.CatalogueJsonUtil
Expand All @@ -10,6 +11,7 @@ trait ApiWorksTestBase
extends ApiTestBase
with CatalogueJsonUtil
with TestDocumentFixtures {
this: Suite =>

def getMinimalDisplayWorks(ids: Seq[String]): Seq[Json] =
ids
Expand All @@ -21,25 +23,59 @@ trait ApiWorksTestBase
ids: Seq[String],
includes: WorksIncludes = WorksIncludes.none,
strictOrdering: Boolean = false
): String =
s"{${worksList(ids, includes, strictOrdering)}}"

private def worksList(
ids: Seq[String],
includes: WorksIncludes = WorksIncludes.none,
strictOrdering: Boolean = false
): String = {
val works =
ids
.map { getVisibleWork }
.map {
getVisibleWork
}
.map(_.display.withIncludes(includes))

val sortedWorks = if (strictOrdering) {
works
} else {
works.sortBy(w => getKey(w, "id").get.asString)
}

s"""
|{
| ${resultList(totalResults = ids.size)},
| ${resultListWithCalculatedPageCount(
totalResults = ids.size
)},
| "results": [
| ${sortedWorks.mkString(",")}
| ]
|}
""".stripMargin
}

def worksListResponseWithAggs(
ids: Seq[String],
aggs: Map[String, Seq[(Int, String)]]
) =
s"{${worksList(ids)}, ${aggregations(aggs)}}"

private def aggregations(aggs: Map[String, Seq[(Int, String)]]): String = {
val aggregationEntries = aggs map {
case (key, buckets) =>
val aggregationBuckets = buckets map {
case (count, bucketData) =>
s"""
|{
| "count" : $count,
| "data" : $bucketData,
| "type" : "AggregationBucket"
| }
|""".stripMargin

}
s""""$key": {"buckets": [${aggregationBuckets.mkString(",")}], "type" : "Aggregation"}"""
}
s""" "aggregations":{${aggregationEntries.mkString(",")}, "type" : "Aggregations"}"""

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package weco.api.search.works

class WorksAggregationsTest extends ApiWorksTestBase {
import org.scalatest.funspec.AnyFunSpec

class WorksAggregationsTest extends AnyFunSpec with ApiWorksTestBase {
it("aggregates by format") {
withWorksApi {
case (worksIndex, routes) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package weco.api.search.works

import com.sksamuel.elastic4s.Index
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.prop.TableDrivenPropertyChecks
import weco.api.search.models.ElasticConfig
import weco.elasticsearch.IndexConfig

class WorksErrorsTest extends ApiWorksTestBase with TableDrivenPropertyChecks {
class WorksErrorsTest extends AnyFunSpec with ApiWorksTestBase with TableDrivenPropertyChecks {

val includesString =
"['identifiers', 'items', 'holdings', 'subjects', 'genres', 'contributors', 'production', 'languages', 'notes', 'formerFrequency', 'designation', 'images', 'parts', 'partOf', 'precededBy', 'succeededBy']"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package weco.api.search.works

import org.scalatest.funspec.AnyFunSpec
import weco.api.search.models.request.WorksIncludes

class WorksFilteredAggregationsTest extends ApiWorksTestBase {
class WorksFilteredAggregationsTest extends AnyFunSpec with ApiWorksTestBase {
val aggregatedWorks =
(0 to 9).map(i => s"works.examples.filtered-aggregations-tests.$i")

Expand Down
Loading

0 comments on commit 4fbbbb0

Please sign in to comment.