oyvindberg / typo

Typed Postgresql integration for Scala. Hopes to avoid typos
https://oyvindberg.github.io/typo/
MIT License
99 stars 9 forks source link

Test code not generated? #118

Closed kubukoz closed 1 month ago

kubukoz commented 2 months ago

This might be a skill issue, but I'm struggling to see any generated test code. For example:

//
// remember to give the project a github star if you like it <3
//
//> using dep "com.olvind.typo::typo:0.22.2"
//> using dep "com.olvind.typo::typo-dsl-doobie:0.22.2"
//> using scala "3.4.2"

import typo.*

// adapt to your instance and credentials
implicit val c: java.sql.Connection = java
  .sql
  .DriverManager
  .getConnection("jdbc:postgresql://localhost:6432/postgres?user=postgres&password=postgres")

val _ =
  // setup table
  c.prepareStatement("drop table if exists todos; create table todos(id serial primary key)")
    .execute()

val options = Options(
  pkg = "org.foo.generated",
  dbLib = Some(DbLibName.Doobie),
  jsonLibs = Nil,
  generateMockRepos = Selector.All,
  enableDsl = true,
)

// current folder, where you run the script from
val location = java.nio.file.Path.of(sys.props("user.dir"))

// destination folder. All files in this dir will be overwritten!
val targetDir = location.resolve("generated/main")
val testTargetDir = location.resolve("generated/test")

// where Typo will look for sql files
val scriptsFolder = location.resolve("sql")

// you can use this to customize which relations you want to generate code for, see below
val selector = Selector.ExcludePostgresInternal

@main def run = generateFromDb(
  options,
  targetFolder = targetDir,
  testTargetFolder = Some(testTargetDir),
  selector = selector,
  scriptsPaths = List(scriptsFolder),
).overwriteFolder()

// add changed files to git, so you can keep them under control
//scala.sys.process.Process(List("git", "add", targetDir.toString)).!!

all the files that get generated are the main ones. There's nothing for tests, and/or mocks:

generated
└── main
    └── org
        └── foo
            └── generated
                ├── customtypes
                │   ├── Defaulted.scala
                │   └── TypoInstant.scala
                ├── information_schema
                │   ├── CardinalNumber.scala
                │   ├── CharacterData.scala
                │   ├── SqlIdentifier.scala
                │   ├── TimeStamp.scala
                │   └── YesOrNo.scala
                ├── package.scala
                └── public
                    └── todos
                        ├── TodosFields.scala
                        ├── TodosId.scala
                        ├── TodosRepo.scala
                        ├── TodosRepoImpl.scala
                        ├── TodosRow.scala
                        └── TodosRowUnsaved.scala

9 directories, 14 files

Is there something I forgot to configure?

oyvindberg commented 2 months ago

https://oyvindberg.github.io/typo/docs/customization/overview/

You need to configure it to generate mocks and testinsert. There's a bunch of things which can be configured there

kubukoz commented 2 months ago

sounds a bit counterintuitive that generateMockRepos isn't enough to generate mocks 😅 the mocks don't require anything from testinserts, do they?

oyvindberg commented 2 months ago

Oh, it should - I missed that you did set it. I'll have a look

oyvindberg commented 2 months ago

doh, I found it! generateFromDb has two overloads, and the "simple" version - the one you use - throws away the generated source code in test scope. I just added this and overlooked it.

Quick fix: Check the implementation and call the overload with ProjectGraph instead, and run .foreach(overwriteFolder()) instead in the script.

I'll patch it up soon