Skip to content

Commit 4c47cc2

Browse files
author
Stanislav Lukeš
committed
Transformations: Added query and update tests
1 parent 99ba8a8 commit 4c47cc2

File tree

4 files changed

+48
-56
lines changed

4 files changed

+48
-56
lines changed

src/main/scala/com/cognite/sdk/scala/v1/resources/transformations.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ class Transformations[F[_]: Monad](val requestSession: RequestSession[F])
137137

138138
def queryOne[I](
139139
q: String
140-
)(implicit itemDecoder: Decoder[I]): F[I] = {
140+
)(implicit itemDecoder: Decoder[I]): F[I] =
141141
query[I](q, limit = 1).map(_.results.items.head)
142-
}
143142
}
144143

145144
object Transformations {

src/main/scala/com/cognite/sdk/scala/v1/transformations.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ final case class StandardTransformConfigUpdate(
103103
destination: Option[NonNullableSetter[Json]] = None,
104104
conflictMode: Option[NonNullableSetter[String]] = None,
105105
query: Option[NonNullableSetter[String]] = None,
106-
//sourceOidcCredentials: Option[Setter[FlatOidcCredentialsUpdate]] = None,
107-
//destinationOidcCredentials: Option[Setter[FlatOidcCredentialsUpdate]] = None,
106+
sourceOidcCredentials: Option[Setter[FlatOidcCredentialsUpdate]] = None,
107+
destinationOidcCredentials: Option[Setter[FlatOidcCredentialsUpdate]] = None,
108108
sourceApiKey: Option[Setter[String]] = None,
109109
destinationApiKey: Option[Setter[String]] = None,
110110
isPublic: Option[NonNullableSetter[Boolean]] = None
@@ -118,6 +118,10 @@ final case class FlatOidcCredentialsUpdate(
118118
cdfProjectName: Option[String] = None
119119
)
120120

121+
object FlatOidcCredentialsUpdate {
122+
implicit val encoder: Encoder[FlatOidcCredentialsUpdate] = deriveEncoder[FlatOidcCredentialsUpdate]
123+
}
124+
121125
final case class QueryQuery(query: String)
122126

123127
final case class QueryResponse[T](

src/test/scala/com/cognite/sdk/scala/common/WritableBehaviors.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ trait WritableBehaviors extends Matchers with OptionValues { this: AnyFlatSpec =
279279
maybeDeletable.map(_.deleteByIds(updatedItems.map(_.id)))
280280
}
281281

282+
def updatableByBothIds[R <: ToCreate[W] with ToUpdate[U] with WithId[Long] with WithExternalId, W, U](
283+
resource: Create[R, W, Id] with UpdateById[R, U, Id] with UpdateByExternalId[R, U, Id] with RetrieveByIds[R, Id],
284+
maybeDeletable: Option[DeleteByIds[Id, Long] with DeleteByExternalIds[Id]],
285+
itemsToCreate: Seq[R],
286+
updatesToMake: Seq[U],
287+
expectedBehaviors: (Seq[R], Seq[R]) => Unit
288+
): Unit = {
289+
val externalIdUpdates = updatesToMake.zip(itemsToCreate).map {
290+
case (u, c) => c.externalId.get -> u
291+
}.toMap
292+
updatableById(resource, maybeDeletable, itemsToCreate, updatesToMake, expectedBehaviors)
293+
updatableByExternalId(resource, maybeDeletable, itemsToCreate, externalIdUpdates, expectedBehaviors)
294+
}
295+
296+
282297
def deletableWithIgnoreUnknownIds[R <: ToCreate[W] with WithId[Long], W, PrimitiveId](
283298
writable: Create[R, W, Id]
284299
with DeleteByIdsWithIgnoreUnknownIds[Id, Long]

src/test/scala/com/cognite/sdk/scala/v1/TransformationsTest.scala

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,36 @@ class TransformationsTest extends SdkTestSpec with ReadBehaviours with WritableB
115115
}
116116
)
117117

118-
119-
120-
121-
122-
123-
124-
125-
126-
127-
128-
129-
130-
131-
118+
it should behave like updatableByBothIds(
119+
greenfieldClient.transformations,
120+
Some(greenfieldClient.transformations),
121+
transformsToCreate,
122+
Seq(
123+
StandardTransformConfigUpdate(name = Some(SetValue("scala-sdk-update-1-1"))),
124+
StandardTransformConfigUpdate(
125+
destination = Some(SetValue(Json.obj("type" -> Json.fromString("datapoints")))),
126+
query = Some(SetValue("select 2")),
127+
sourceApiKey = Some(SetValue(greenfieldAuth.asInstanceOf[ApiKeyAuth].apiKey)),
128+
isPublic = Some(SetValue(true))
129+
)
130+
),
131+
(read: Seq[TransformConfigRead], updated: Seq[TransformConfigRead]) => {
132+
assert(read.size == updated.size)
133+
assert(read.size == transformsToCreate.size)
134+
assert(read.size == transformUpdates.size)
135+
assert(updated.map(_.name) == List("scala-sdk-update-1-1", "scala-sdk-read-example-2"))
136+
assert(updated(1).isPublic)
137+
assert(!read(1).isPublic)
138+
assert(updated(1).hasSourceApiKey)
139+
assert(updated(1).query == "select 2")
140+
()
141+
}
142+
)
132143

133144
case class RawAggregationResponse(average: Double)
134145

135146
it should "query average" in {
147+
greenfieldClient.transformations.list().compile.toList
136148
val response = greenfieldClient.transformations.queryOne[RawAggregationResponse](
137149
"select avg(` V1 vcross (m/s)`) as average from ORCA.VAN_net"
138150
)
@@ -153,42 +165,4 @@ class TransformationsTest extends SdkTestSpec with ReadBehaviours with WritableB
153165
println(response)
154166
assert(response.nonEmpty)
155167
}
156-
157-
// it should behave like updatableById(
158-
// client.assets,
159-
// Some(client.assets),
160-
// assetsToCreate,
161-
// Seq(
162-
// AssetUpdate(name = Some(SetValue("scala-sdk-update-1-1"))),
163-
// AssetUpdate(name = Some(SetValue("scala-sdk-update-2-1")), dataSetId = Some(SetNull()))
164-
// ),
165-
// (readAssets: Seq[Asset], updatedAssets: Seq[Asset]) => {
166-
// assert(assetsToCreate.size == assetUpdates.size)
167-
// assert(readAssets.size == assetsToCreate.size)
168-
// assert(updatedAssets.size == assetUpdates.size)
169-
// assert(updatedAssets.zip(readAssets).forall { case (updated, read) => updated.name === s"${read.name}-1" })
170-
// assert(readAssets)
171-
// assert(List(None, None) === dataSets)
172-
// ()
173-
// }
174-
// )
175-
//
176-
// it should behave like updatableByExternalId(
177-
// client.assets,
178-
// Some(client.assets),
179-
// Seq(
180-
// Asset(name = "update-1", externalId = Some("update-1-externalId")),
181-
// Asset(name = "update-2", externalId = Some("update-2-externalId"))),
182-
// Map("update-1-externalId" -> AssetUpdate(name = Some(SetValue("update-1-1"))),
183-
// "update-2-externalId" -> AssetUpdate(name = Some(SetValue("update-2-1")))),
184-
// (readAssets: Seq[Asset], updatedAssets: Seq[Asset]) => {
185-
// assert(assetsToCreate.size == assetUpdates.size)
186-
// assert(readAssets.size == assetsToCreate.size)
187-
// assert(updatedAssets.size == assetUpdates.size)
188-
// assert(updatedAssets.zip(readAssets).forall { case (updated, read) => updated.name === s"${read.name}-1" })
189-
// assert(updatedAssets.zip(readAssets).forall { case (updated, read) => updated.externalId === read.externalId })
190-
// ()
191-
// }
192-
// )
193-
194168
}

0 commit comments

Comments
 (0)