Skip to content

[feature]: kyo-zio-test: Support ZLayer interop with Kyo layer #1246

@calvinlfer

Description

@calvinlfer

Problem Statement

kyo-zio-test could benefit from two improvements:

  1. The ability to use the Environment in Kyo-zio-test
  2. The ability to provide (and provideX variants) Kyo Layers to specs & tests to eliminate the Environment dependency

Proposed Solution

The ideal API could look like this

object PgKyoSpec extends KyoSpecDefault:
  def spec = suite("Postgres Magnum Kyo tests")(
    test("count") {
      Env.use[TransactorKyo] {
        _.connect:
          carRepo.count
        .map(result => assertTrue(result == 3L))
      }
    },
    test("another one")(...)
  ).provideShared(transactorKyoLayer) // transactorKyoLayer is Layer[TransactorKyo, Resource & IO]

The above layer would instantiate the transactor kyo layer once and reuse it across all tests

Alternative Solutions

No response

Current Workaround

One could extend KyoSpecAbstract and provide layers manually but you don't get the benefits of memoization:

import kyo.*
import zio.ZIO
import kyo.test.*
import com.augustnagro.magnum.magkyo.TransactorKyo
import zio.test.Spec
import com.dimafeng.testcontainers.PostgreSQLContainer
import org.testcontainers.utility.DockerImageName
import org.postgresql.ds.PGSimpleDataSource
import java.nio.file.{Files, Path}
import com.augustnagro.magnum.SqlLogger

abstract class KyoSpecMagnum
    extends KyoSpecAbstract[
      Async & Resource & Abort[Throwable] & Env[TransactorKyo]
    ]:
  final override def run[In](
      v: => In < (Async & Resource & Abort[Throwable] & Env[TransactorKyo])
  )(using
      Frame
  ): ZIO[Environment, Throwable, In] =
    ZIOs.run {
      Memo.run {
        Resource.run {
          Env.runLayer( Layer(transactorKyo)) {
            v
          }
        }
      }
    }

  private def transactorKyo: TransactorKyo < (Resource & IO) = ...

Additional Context

This ask came when trying to write tests for the Magnum Kyo integration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions