Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running scalafmt on the entire test directory #770

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ sealed abstract class InstanceType extends Product with Serializable

object InstanceType {
case object Node extends InstanceType

case object Edge extends InstanceType

implicit val instantTypeDecoder: Decoder[InstanceType] = Decoder[String].emap {
implicit val instanceTypeDecoder: Decoder[InstanceType] = Decoder[String].emap {
case "node" => Right(Node)
case "edge" => Right(Edge)
case other => Left(s"Invalid Instance Type: $other")
}

implicit val instantTypeEncoder: Encoder[InstanceType] =
implicit val instanceTypeEncoder: Encoder[InstanceType] =
Encoder.instance[InstanceType](p =>
io.circe.Json.fromString(p.productPrefix.toLowerCase(Locale.US))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ class CdpApiExceptionSpec extends AnyFlatSpec with Matchers {
code = 400,
message = "Bad Request",
missing = None,
duplicated = Some(Seq(
JsonObject("id" -> Json.fromInt(2)),
JsonObject("id" -> Json.fromInt(1)),
JsonObject("externalId" -> Json.fromString("externalId-2")),
JsonObject("externalId" -> Json.fromString("externalId-1"))
)),
duplicated = Some(
Seq(
JsonObject("id" -> Json.fromInt(2)),
JsonObject("id" -> Json.fromInt(1)),
JsonObject("externalId" -> Json.fromString("externalId-2")),
JsonObject("externalId" -> Json.fromString("externalId-1"))
)
),
missingFields = None,
requestId = Some("1234")
)
Expand All @@ -62,22 +64,26 @@ class CdpApiExceptionSpec extends AnyFlatSpec with Matchers {
url = uri"https://api.cognitedata.com",
code = 400,
message = "Bad Request",
missing = Some(Seq(
JsonObject("id" -> Json.fromInt(3))
)),
duplicated = Some(Seq(
JsonObject("id" -> Json.fromInt(2)),
JsonObject("externalId" -> Json.fromString("externalId-2")),
JsonObject("externalId" -> Json.fromString("externalId-1")),
JsonObject("id" -> Json.fromInt(1))
)),
missing = Some(
Seq(
JsonObject("id" -> Json.fromInt(3))
)
),
duplicated = Some(
Seq(
JsonObject("id" -> Json.fromInt(2)),
JsonObject("externalId" -> Json.fromString("externalId-2")),
JsonObject("externalId" -> Json.fromString("externalId-1")),
JsonObject("id" -> Json.fromInt(1))
)
),
missingFields = None,
requestId = Some("1234")
)

ex.getMessage shouldBe
s"Request with id 1234 to https://api.cognitedata.com failed with status 400: Bad Request. " +
"Duplicated externalIds: [externalId-1, externalId-2]. Duplicated ids: [1, 2]. Missing ids: [3]."
"Duplicated externalIds: [externalId-1, externalId-2]. Duplicated ids: [1, 2]. Missing ids: [3]."
}

it should "format messages with missing fields" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,34 @@ import cats.effect.IO
import cats.effect.unsafe.IORuntime

import java.time.Instant
import com.cognite.sdk.scala.v1.{CogniteExternalId, CogniteInternalId, DataPointsByExternalIdResponse, DataPointsByIdResponse, TimeSeries}
import com.cognite.sdk.scala.v1.{
CogniteExternalId,
CogniteInternalId,
DataPointsByExternalIdResponse,
DataPointsByIdResponse,
TimeSeries
}
import com.cognite.sdk.scala.v1.resources.DataPointsResource
import org.scalatest.OptionValues
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements"))
trait DataPointsResourceBehaviors extends Matchers with OptionValues with RetryWhile { this: AnyFlatSpec =>
trait DataPointsResourceBehaviors extends Matchers with OptionValues with RetryWhile {
this: AnyFlatSpec =>
private val startTime = System.currentTimeMillis()
private val start = Instant.ofEpochMilli(startTime)
private val endTime = startTime + 20*1000
private val endTime = startTime + 20 * 1000
private val end = Instant.ofEpochMilli(endTime)
private val testDataPoints = (startTime to endTime by 1000).map(t =>
DataPoint(Instant.ofEpochMilli(t), java.lang.Math.random()))
DataPoint(Instant.ofEpochMilli(t), java.lang.Math.random())
)

def withTimeSeries(testCode: TimeSeries => Any): Unit

def dataPointsResource(dataPoints: DataPointsResource[IO])(implicit IORuntime: IORuntime): Unit = {
def dataPointsResource(
dataPoints: DataPointsResource[IO]
)(implicit IORuntime: IORuntime): Unit = {
it should "be possible to insert and delete numerical data points" in withTimeSeries {
timeSeries =>
val timeSeriesId = timeSeries.id
Expand All @@ -49,17 +59,21 @@ trait DataPointsResourceBehaviors extends Matchers with OptionValues with RetryW
}
)

val latestStartDp = dataPoints.getLatestDataPoint(
CogniteInternalId(timeSeriesId),
start.plusMillis(1).toEpochMilli.toString
).unsafeRunSync()
val latestStartDp = dataPoints
.getLatestDataPoint(
CogniteInternalId(timeSeriesId),
start.plusMillis(1).toEpochMilli.toString
)
.unsafeRunSync()
latestStartDp.isDefined shouldBe true
testDataPoints.headOption.map(_.value) shouldBe latestStartDp.map(_.value)

val latestEndDp = dataPoints.getLatestDataPoint(
CogniteInternalId(timeSeriesId),
end.plusMillis(1).toEpochMilli.toString
).unsafeRunSync()
val latestEndDp = dataPoints
.getLatestDataPoint(
CogniteInternalId(timeSeriesId),
end.plusMillis(1).toEpochMilli.toString
)
.unsafeRunSync()
latestEndDp.isDefined shouldBe true
testDataPoints.lastOption.map(_.value) shouldBe latestEndDp.map(_.value)

Expand All @@ -71,12 +85,16 @@ trait DataPointsResourceBehaviors extends Matchers with OptionValues with RetryW

dataPoints.insert(CogniteExternalId(timeSeriesExternalId), testDataPoints).unsafeRunSync()
retryWithExpectedResult[DataPointsByExternalIdResponse](
dataPoints.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1)).unsafeRunSync(),
dataPoints
.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1))
.unsafeRunSync(),
p2 => p2.datapoints should have size testDataPoints.size.toLong
)

retryWithExpectedResult[DataPointsByExternalIdResponse](
dataPoints.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1), Some(5)).unsafeRunSync(),
dataPoints
.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1), Some(5))
.unsafeRunSync(),
p2 => p2.datapoints should have size 5
)

Expand All @@ -88,23 +106,31 @@ trait DataPointsResourceBehaviors extends Matchers with OptionValues with RetryW
}
)

val latestStartDataPoint = dataPoints.getLatestDataPoint(
CogniteExternalId(timeSeriesExternalId),
start.plusMillis(1).toEpochMilli.toString
).unsafeRunSync()
val latestStartDataPoint = dataPoints
.getLatestDataPoint(
CogniteExternalId(timeSeriesExternalId),
start.plusMillis(1).toEpochMilli.toString
)
.unsafeRunSync()
latestStartDataPoint.isDefined shouldBe true
testDataPoints.headOption.map(_.value) shouldBe latestStartDataPoint.map(_.value)

val latestEndDataPoint = dataPoints.getLatestDataPoint(
CogniteExternalId(timeSeriesExternalId),
end.plusMillis(1).toEpochMilli.toString
).unsafeRunSync()
val latestEndDataPoint = dataPoints
.getLatestDataPoint(
CogniteExternalId(timeSeriesExternalId),
end.plusMillis(1).toEpochMilli.toString
)
.unsafeRunSync()
latestEndDataPoint.isDefined shouldBe true
testDataPoints.lastOption.map(_.value) shouldBe latestEndDataPoint.map(_.value)

dataPoints.deleteRangeByExternalId(timeSeriesExternalId, start, end.plusMillis(1)).unsafeRunSync()
dataPoints
.deleteRangeByExternalId(timeSeriesExternalId, start, end.plusMillis(1))
.unsafeRunSync()
retryWithExpectedResult[DataPointsByExternalIdResponse](
dataPoints.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1)).unsafeRunSync(),
dataPoints
.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1))
.unsafeRunSync(),
pad => pad.datapoints should have size 0,
retriesRemaining = 20
)
Expand All @@ -129,25 +155,35 @@ trait DataPointsResourceBehaviors extends Matchers with OptionValues with RetryW

dataPoints.insertByExternalId(timeSeriesExternalId, testDataPoints).unsafeRunSync()
retryWithExpectedResult[DataPointsByExternalIdResponse](
dataPoints.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1)).unsafeRunSync(),
dataPoints
.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1))
.unsafeRunSync(),
p2 => p2.datapoints should have size testDataPoints.size.toLong
)

retryWithExpectedResult[DataPointsByExternalIdResponse](
dataPoints.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1), Some(5)).unsafeRunSync(),
dataPoints
.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1), Some(5))
.unsafeRunSync(),
p2 => p2.datapoints should have size 5
)

dataPoints.deleteRangeByExternalId(timeSeriesExternalId, start, end.plusMillis(1)).unsafeRunSync()
dataPoints
.deleteRangeByExternalId(timeSeriesExternalId, start, end.plusMillis(1))
.unsafeRunSync()
retryWithExpectedResult[DataPointsByExternalIdResponse](
dataPoints.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1)).unsafeRunSync(),
dataPoints
.queryByExternalId(timeSeriesExternalId, start, end.plusMillis(1))
.unsafeRunSync(),
pad => pad.datapoints should have size 0
)
}

it should "be an error to insert numerical data points for non-existing time series" in {
val unknownId = 991919L
val thrown = the[CdpApiException] thrownBy dataPoints.insert(CogniteInternalId(unknownId), testDataPoints).unsafeRunSync()
val thrown = the[CdpApiException] thrownBy dataPoints
.insert(CogniteInternalId(unknownId), testDataPoints)
.unsafeRunSync()

val itemsNotFound = thrown.missing.value
val notFoundIds =
Expand All @@ -158,7 +194,9 @@ trait DataPointsResourceBehaviors extends Matchers with OptionValues with RetryW

it should "be an error to delete numerical data points for non-existing time series" in {
val unknownId = 991999L
val thrown = the[CdpApiException] thrownBy dataPoints.deleteRangeById(unknownId, start, end).unsafeRunSync()
val thrown = the[CdpApiException] thrownBy dataPoints
.deleteRangeById(unknownId, start, end)
.unsafeRunSync()
val itemsNotFound = thrown.missing.value
val notFoundIds =
itemsNotFound.map(jsonObj => jsonObj("id").value.asNumber.value.toLong.value)
Expand Down
51 changes: 31 additions & 20 deletions src/test/scala/com/cognite/sdk/scala/common/FilterTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,47 @@ object DummyFilter {
}

class FilterTest extends SdkTestSpec with OptionValues {
private implicit val dummyFilterRequestEncoder: Encoder[FilterRequest[DummyFilter]] = deriveEncoder[FilterRequest[DummyFilter]]
private implicit val dummyFilterRequestDecoder: Decoder[FilterRequest[DummyFilter]] = deriveDecoder[FilterRequest[DummyFilter]]
private implicit val dummyItemsWithCursorDecoder: Decoder[ItemsWithCursor[Int]] = deriveDecoder[ItemsWithCursor[Int]]
private implicit val dummyFilterRequestEncoder: Encoder[FilterRequest[DummyFilter]] =
deriveEncoder[FilterRequest[DummyFilter]]
private implicit val dummyFilterRequestDecoder: Decoder[FilterRequest[DummyFilter]] =
deriveDecoder[FilterRequest[DummyFilter]]
private implicit val dummyItemsWithCursorDecoder: Decoder[ItemsWithCursor[Int]] =
deriveDecoder[ItemsWithCursor[Int]]

it should "set final limit to batchSize when less than limit" in filterWithCursor(10, Some(100)) { finalLimit =>
finalLimit should be(10)
it should "set final limit to batchSize when less than limit" in filterWithCursor(10, Some(100)) {
finalLimit =>
finalLimit should be(10)
}

it should "set final limit to limit when less than batchsize" in filterWithCursor(100, Some(20)) { finalLimit =>
finalLimit should be(20)
it should "set final limit to limit when less than batchsize" in filterWithCursor(100, Some(20)) {
finalLimit =>
finalLimit should be(20)
}

it should "set final limit to batchsize when no limit" in filterWithCursor(100, None) { finalLimit =>
finalLimit should be(100)
it should "set final limit to batchsize when no limit" in filterWithCursor(100, None) {
finalLimit =>
finalLimit should be(100)
}

@SuppressWarnings(Array("org.wartremover.warts.Null", "org.wartremover.warts.Var", "org.wartremover.warts.AsInstanceOf"))
@SuppressWarnings(
Array(
"org.wartremover.warts.Null",
"org.wartremover.warts.Var",
"org.wartremover.warts.AsInstanceOf"
)
)
def filterWithCursor(batchSize: Int, limit: Option[Int])(test: Int => Any): Any = {
var hijackedRequest: FilterRequest[DummyFilter] = null
val requestHijacker = SttpBackendStub.synchronous.whenAnyRequest.thenRespondF(req => {
hijackedRequest = decode[FilterRequest[DummyFilter]](req.body.asInstanceOf[StringBody].s) match {
case Right(x) => x
case Left(e) => throw e
}
val requestHijacker = SttpBackendStub.synchronous.whenAnyRequest.thenRespondF { req =>
hijackedRequest =
decode[FilterRequest[DummyFilter]](req.body.asInstanceOf[StringBody].s) match {
case Right(x) => x
case Left(e) => throw e
}
Response(ItemsWithCursor(Seq(0, 1, 2), None), StatusCode.Ok, "OK")
})
lazy val dummyClient = Client("foo",
projectName,
"https://api.cognitedata.com",
auth)(implicitly, requestHijacker)
}
lazy val dummyClient =
Client("foo", projectName, "https://api.cognitedata.com", auth)(implicitly, requestHijacker)
val dummyRequestSession = dummyClient.requestSession

val _ = Filter.filterWithCursor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ import scala.collection.immutable.Seq
import scala.concurrent.duration._

@SuppressWarnings(Array("org.wartremover.warts.Var", "org.wartremover.warts.NonUnitStatements"))
class OAuth2ClientCredentialsTest extends AnyFlatSpec with Matchers with OptionValues with RetryWhile {
class OAuth2ClientCredentialsTest
extends AnyFlatSpec
with Matchers
with OptionValues
with RetryWhile {
import natchez.Trace.Implicits.noop

val tokenUri: String = sys.env.get("TEST_TOKEN_URL")
val tokenUri: String = sys.env
.get("TEST_TOKEN_URL")
.orElse(
sys.env.get("TEST_AAD_TENANT")
.map(tenant => s"https://login.microsoftonline.com/$tenant/oauth2/v2.0/token"))
sys.env
.get("TEST_AAD_TENANT")
.map(tenant => s"https://login.microsoftonline.com/$tenant/oauth2/v2.0/token")
)
.getOrElse("https://sometokenurl")
val clientId: String = sys.env("TEST_CLIENT_ID")
val clientSecret: String = sys.env("TEST_CLIENT_SECRET")
Expand Down Expand Up @@ -121,11 +128,19 @@ class OAuth2ClientCredentialsTest extends AnyFlatSpec with Matchers with OptionV

val io = for {
_ <- numTokenRequests.update(_ => 0)
authProvider <- OAuth2.ClientCredentialsProvider[IO](credentials,
authProvider <- OAuth2.ClientCredentialsProvider[IO](
credentials,
refreshSecondsBeforeExpiration = 2,
Some(TokenState("firstToken", Clock[IO].realTime.map(_.toSeconds).unsafeRunSync() + 4, "irrelevant")))
Some(
TokenState(
"firstToken",
Clock[IO].realTime.map(_.toSeconds).unsafeRunSync() + 4,
"irrelevant"
)
)
)
_ <- List.fill(5)(authProvider.getAuth).parUnorderedSequence
noNewToken <- numTokenRequests.get // original token is still valid
noNewToken <- numTokenRequests.get // original token is still valid
_ <- IO.sleep(4.seconds)
_ <- List.fill(5)(authProvider.getAuth).parUnorderedSequence
oneRequestedToken <- numTokenRequests.get // original token is expired
Expand All @@ -134,7 +149,7 @@ class OAuth2ClientCredentialsTest extends AnyFlatSpec with Matchers with OptionV
twoRequestedToken <- numTokenRequests.get // first renew token is expired
} yield (noNewToken, oneRequestedToken, twoRequestedToken)

retryWithExpectedResult[(Int,Int,Int)](
retryWithExpectedResult[(Int, Int, Int)](
io.unsafeRunTimed(10.seconds).value,
r => r shouldBe ((0, 1, 2))
)
Expand Down
Loading