Skip to content

Commit

Permalink
tests for doobie/zio, bugfix for zio upsert, don't use streaming unsa…
Browse files Browse the repository at this point in the history
…ved in test as it doesnt work for pg 12
  • Loading branch information
oyvindberg committed Aug 9, 2024
1 parent 1292062 commit 694f33e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class PersonRepoImpl extends PersonRepo {
}
}
override def upsert(unsaved: PersonRow): ZIO[ZConnection, Throwable, UpdateResult[PersonRow]] = {
sql"""insert into "myschema"."person"("id", "favourite_football_club_id", "name", "nick_name", "blog_url", "email", "phone", "likes_pizza", "marital_status_id", "work_email", "sector", "favorite_number")
sql"""insert into "myschema"."person"("id", "favourite_football_club_id", "name", "nick_name", "blog_url", "email", "phone", "likes_pizza", "marital_status_id", "work_email", "favorite_number")
values (
${Segment.paramSegment(unsaved.id)(PersonId.setter)}::int8,
${Segment.paramSegment(unsaved.favouriteFootballClubId)(FootballClubId.setter)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class PersonRepoImpl extends PersonRepo {
}
}
override def upsert(unsaved: PersonRow): ZIO[ZConnection, Throwable, UpdateResult[PersonRow]] = {
sql"""insert into "myschema"."person"("id", "favourite_football_club_id", "name", "nick_name", "blog_url", "email", "phone", "likes_pizza", "marital_status_id", "work_email", "sector", "favorite_number")
sql"""insert into "myschema"."person"("id", "favourite_football_club_id", "name", "nick_name", "blog_url", "email", "phone", "likes_pizza", "marital_status_id", "work_email", "favorite_number")
values (
${Segment.paramSegment(unsaved.id)(PersonId.setter)}::int8,
${Segment.paramSegment(unsaved.favouriteFootballClubId)(FootballClubId.setter)},
Expand Down
12 changes: 3 additions & 9 deletions typo-tester-anorm/src/scala/adventureworks/IdentityTest.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package adventureworks

import adventureworks.customtypes.*
import adventureworks.public.identity_test.{IdentityTestId, IdentityTestRepoImpl, IdentityTestRow, IdentityTestRowUnsaved}
import adventureworks.public.identity_test.*
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.funsuite.AnyFunSuite

Expand All @@ -10,20 +10,14 @@ import scala.annotation.nowarn
class IdentityTest extends AnyFunSuite with TypeCheckedTripleEquals {
val repo = new IdentityTestRepoImpl()

def row(str: String) =
IdentityTestRowUnsaved(IdentityTestId(str), Defaulted.UseDefault)

test("works") {
withConnection { implicit c =>
val unsaved = row("a")
val unsaved = IdentityTestRowUnsaved(IdentityTestId("a"), Defaulted.UseDefault)
val inserted = repo.insert(unsaved)
val upserted = repo.upsert(inserted)
assert(inserted === upserted): @nowarn
assert(repo.insertUnsavedStreaming(Iterator(row("b"), row("c"))) === 2L): @nowarn
repo.select.orderBy(_.name.asc).toList === List(
IdentityTestRow(1, 1, IdentityTestId("a")),
IdentityTestRow(2, 2, IdentityTestId("b")),
IdentityTestRow(3, 3, IdentityTestId("c"))
IdentityTestRow(1, 1, IdentityTestId("a"))
)
}
}
Expand Down
24 changes: 24 additions & 0 deletions typo-tester-doobie/src/scala/adventureworks/IdentityTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package adventureworks

import adventureworks.customtypes.*
import adventureworks.public.identity_test.*
import doobie.free.connection.delay
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.funsuite.AnyFunSuite

class IdentityTest extends AnyFunSuite with TypeCheckedTripleEquals {
val repo = new IdentityTestRepoImpl()

test("works") {
withConnection {
val unsaved = IdentityTestRowUnsaved(IdentityTestId("a"), Defaulted.UseDefault)
for {
inserted <- repo.insert(unsaved)
upserted <- repo.upsert(inserted)
_ <- delay(assert(inserted === upserted))
rows <- repo.select.orderBy(_.name.asc).toList
_ <- delay(rows === List(IdentityTestRow(1, 1, IdentityTestId("a"))))
} yield ()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class IdentityTestRepoImpl extends IdentityTestRepo {
where "name" = ${Segment.paramSegment(name)(IdentityTestId.setter)}""".update.map(_ > 0)
}
override def upsert(unsaved: IdentityTestRow): ZIO[ZConnection, Throwable, UpdateResult[IdentityTestRow]] = {
sql"""insert into "public"."identity-test"("always_generated", "default_generated", "name")
sql"""insert into "public"."identity-test"("default_generated", "name")
values (
${Segment.paramSegment(unsaved.defaultGenerated)(Setter.intSetter)}::int4,
${Segment.paramSegment(unsaved.name)(IdentityTestId.setter)}
Expand Down
24 changes: 24 additions & 0 deletions typo-tester-zio-jdbc/src/scala/adventureworks/IdentityTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package adventureworks

import adventureworks.customtypes.*
import adventureworks.public.identity_test.*
import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.funsuite.AnyFunSuite
import zio.{Chunk, ZIO}

class IdentityTest extends AnyFunSuite with TypeCheckedTripleEquals {
val repo = new IdentityTestRepoImpl()

test("works") {
withConnection {
val unsaved = IdentityTestRowUnsaved(IdentityTestId("a"), Defaulted.UseDefault)
for {
inserted <- repo.insert(unsaved)
upserted <- repo.upsert(inserted)
_ <- ZIO.succeed(assert(inserted === upserted.updatedKeys.head))
rows <- repo.select.orderBy(_.name.asc).toChunk
_ <- ZIO.succeed(rows === Chunk(IdentityTestRow(1, 1, IdentityTestId("a"))))
} yield ()
}
}
}
2 changes: 1 addition & 1 deletion typo/src/scala/typo/internal/codegen/DbLibZioJdbc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class DbLibZioJdbc(pkg: sc.QIdent, inlineImplicits: Boolean, dslEnabled: Boolean
}

val sql = SQL {
code"""|insert into $relName(${dbNames(cols, isRead = false)})
code"""|insert into $relName(${dbNames(writeableColumnsWithId, isRead = false)})
|values (
| ${values.mkCode(",\n")}
|)
Expand Down

0 comments on commit 694f33e

Please sign in to comment.