-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2822529
commit 45eac35
Showing
11 changed files
with
262 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
magnum-mysql/src/main/scala/com/augustnagro/magnum/mysql/MySqlCodec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.augustnagro.magnum.mysql | ||
|
||
import com.augustnagro.magnum.DbCodec | ||
|
||
import java.sql.{PreparedStatement, ResultSet, Types} | ||
import java.util.UUID | ||
|
||
object MySqlCodec: | ||
|
||
given VarCharUUIDCodec: DbCodec[UUID] with | ||
def queryRepr: String = "?" | ||
val cols: IArray[Int] = IArray(Types.VARCHAR) | ||
def readSingle(rs: ResultSet, pos: Int): UUID = { | ||
val uuidString = rs.getString(pos) | ||
uuidString match | ||
case null => null | ||
case x => UUID.fromString(x) | ||
} | ||
def writeSingle(entity: UUID, ps: PreparedStatement, pos: Int): Unit = | ||
ps.setString(pos, entity.toString) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
drop table if exists mag_user; | ||
|
||
create table mag_user ( | ||
id bigint auto_increment primary key, | ||
social_id varchar(36) | ||
); | ||
|
||
insert into mag_user (social_id) values | ||
('d06443a6-3efb-46c4-a66a-a80a8a9a5388'), | ||
('529b6c6d-7228-4da5-81d7-13b706f78ddb'), | ||
(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import com.dimafeng.testcontainers.MySQLContainer | ||
import com.dimafeng.testcontainers.munit.fixtures.TestContainersFixtures | ||
import com.mysql.cj.jdbc.MysqlDataSource | ||
import munit.FunSuite | ||
import org.testcontainers.utility.DockerImageName | ||
|
||
import java.nio.file.{Files, Path} | ||
import javax.sql.DataSource | ||
import scala.util.Using.Manager | ||
import com.augustnagro.magnum.mysql.MySqlCodec.VarCharUUIDCodec | ||
import com.augustnagro.magnum.* | ||
|
||
import java.util.UUID | ||
|
||
class MySqlCodecTests extends FunSuite, TestContainersFixtures { | ||
|
||
case class MagUserCreator( | ||
socialId: Option[UUID] | ||
) derives DbCodec | ||
|
||
@Table(MySqlDbType, SqlNameMapper.CamelToSnakeCase) | ||
case class MagUser( | ||
@Id id: Long, | ||
socialId: Option[UUID] | ||
) derives DbCodec | ||
|
||
val userRepo = Repo[MagUserCreator, MagUser, Long] | ||
|
||
test("select nullable uuid"): | ||
connect(ds()): | ||
val users = userRepo.findAll | ||
assert(users.exists(_.socialId.isEmpty)) | ||
assert( | ||
users.exists( | ||
_.socialId.contains( | ||
UUID.fromString("529b6c6d-7228-4da5-81d7-13b706f78ddb") | ||
) | ||
) | ||
) | ||
|
||
test("insert uuid"): | ||
connect(ds()): | ||
val uuid = UUID.randomUUID() | ||
val uc = MagUserCreator(socialId = Some(uuid)) | ||
userRepo.insert(uc) | ||
val users = userRepo.findAll | ||
assert(users.exists(_.socialId.contains(uuid))) | ||
|
||
test("insert null uuid"): | ||
connect(ds()): | ||
val existingNullCount = userRepo.findAll.count(_.socialId.isEmpty) | ||
val uc = MagUserCreator(socialId = None) | ||
userRepo.insert(uc) | ||
val newNullCount = userRepo.findAll.count(_.socialId.isEmpty) | ||
assertEquals(newNullCount, existingNullCount + 1) | ||
|
||
val mySqlContainer = ForAllContainerFixture( | ||
MySQLContainer | ||
.Def(dockerImageName = DockerImageName.parse("mysql:8.0.32")) | ||
.createContainer() | ||
) | ||
|
||
override def munitFixtures: Seq[Fixture[_]] = | ||
super.munitFixtures :+ mySqlContainer | ||
|
||
def ds(): DataSource = | ||
val mySql = mySqlContainer() | ||
val ds = MysqlDataSource() | ||
ds.setURL(mySql.jdbcUrl) | ||
ds.setUser(mySql.username) | ||
ds.setPassword(mySql.password) | ||
ds.setAllowMultiQueries(true) | ||
val userSql = | ||
Files.readString(Path.of(getClass.getResource("/mysql-user.sql").toURI)) | ||
Manager(use => | ||
val con = use(ds.getConnection) | ||
use(con.prepareStatement(userSql)).execute() | ||
).get | ||
ds | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.