forked from slick/slick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateMainSources.scala
272 lines (266 loc) · 12.1 KB
/
GenerateMainSources.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package slick.test.codegen
import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.io.Codec
import scala.io.Source
import java.io.File
import java.sql.Blob
import com.typesafe.slick.testkit.util.InternalJdbcTestDB
import com.typesafe.slick.testkit.util.JdbcTestDB
import com.typesafe.slick.testkit.util.StandardTestDBs
import com.typesafe.slick.testkit.util.TestCodeGenerator
import slick.codegen.OutputHelpers
import slick.codegen.SourceCodeGenerator
import slick.dbio.DBIO
import slick.jdbc.JdbcBackend
import slick.jdbc.meta.MTable
import slick.model.Model
/** Generates files for GeneratedCodeTest */
object GenerateMainSources extends TestCodeGenerator {
def packageName = "slick.test.codegen.generated"
def defaultTestCode(c: Config): String = "slick.test.codegen.GeneratedCodeTest.test" + c.objectName
lazy val configurations = Seq(
new Config("CG1", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/h2.sql")),
new Config("CG2", StandardTestDBs.HsqldbMem, "HsqldbMem", Seq("/dbs/hsqldb.sql")),
new Config("CG3", StandardTestDBs.SQLiteMem, "SQLiteMem", Seq("/dbs/sqlite.sql")),
new Config("CG7", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/h2.sql")) {
override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
override def entityName = {
case "COFFEES" => "Coff"
case other => super.entityName(other)
}
override def tableName = {
case "COFFEES" => "Coffs"
case "SUPPLIERS" => "Supps"
case other => super.tableName(other)
}
override def code = "trait AA; trait BB\n" + super.code
override def Table = new Table(_){
override def EntityType = new EntityType{
override def parents = Seq("AA","BB")
}
override def TableClass = new TableClass{
override def parents = Seq("AA","BB")
}
}
})
},
new Config("CG8", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/h2-simple.sql")) {
override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
override def Table = new Table(_){
override def EntityType = new EntityType{
override def enabled = false
}
override def mappingEnabled = true
override def code = {
if(model.name.table == "SIMPLE_AS"){
Seq("""
import slick.test.codegen.CustomTyping._
import slick.test.codegen.CustomTyping
type SimpleA = CustomTyping.SimpleA
val SimpleA = CustomTyping.SimpleA
""".trim) ++ super.code
} else super.code
}
override def Column = new Column(_){
override def rawType = model.name match {
case "A1" => "Bool"
case _ => super.rawType
}
}
}
})
},
new Config("CG9", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/h2.sql")) {
override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
override def Table = new Table(_){
override def autoIncLastAsOption = true
override def Column = new Column(_){
override def asOption = autoInc
}
}
})
},
new UUIDConfig("CG10", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/uuid-h2.sql")),
new Config("CG11", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/h2-simple.sql")) {
override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
override def Table = new Table(_){
override def Column = new Column(_){
override def asOption = true
}
}
})
},
new Config("Postgres1", StandardTestDBs.Postgres, "Postgres", Nil) {
import tdb.profile.api._
class A(tag: Tag) extends Table[(Int, Array[Byte], Blob)](tag, "a") {
def id = column[Int]("id")
def ba = column[Array[Byte]]("ba")
def blob = column[Blob]("blob")
def * = (id, ba, blob)
}
override def generator =
TableQuery[A].schema.create >>
tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_))
override def testCode =
"""
| import java.sql.Blob
| import javax.sql.rowset.serial.SerialBlob
| val a1 = ARow(1, Array[Byte](1,2,3), new SerialBlob(Array[Byte](4,5,6)))
| DBIO.seq(
| schema.create,
| A += a1,
| A.result.map { case Seq(ARow(id, ba, blob)) => assertEquals("1123", ""+id+ba.mkString) }
| ).transactionally
""".stripMargin
},
new UUIDConfig("Postgres2", StandardTestDBs.Postgres, "Postgres", Seq("/dbs/uuid-postgres.sql")),
new Config("Postgres3", StandardTestDBs.Postgres, "Postgres", Seq("/dbs/postgres.sql")) {
override def testCode: String =
"""import slick.ast.{FieldSymbol, Select}
|import slick.jdbc.meta.MTable
|import slick.relational.RelationalProfile
|DBIO.seq(
| schema.create,
| MTable.getTables(Some(""), Some("public"), None, None).map { tables =>
| def optionsOfColumn(c: slick.lifted.Rep[_]) =
| c.toNode.asInstanceOf[Select].field.asInstanceOf[FieldSymbol].options.toList
| //val smallserialOptions = optionsOfColumn(TestDefault.baseTableRow.smallintAutoInc)
| val serialOptions = optionsOfColumn(TestDefault.baseTableRow.intAutoInc)
| val bigserialOptions = optionsOfColumn(TestDefault.baseTableRow.bigintAutoInc)
| val char1EmptyOptions = optionsOfColumn(TestDefault.baseTableRow.char1DefaultEmpty)
| val char1ValidOptions = optionsOfColumn(TestDefault.baseTableRow.char1DefaultValid)
| val char1InvalidOptions = optionsOfColumn(TestDefault.baseTableRow.char1DefaultInvalid)
| //assertTrue("smallint_auto_inc should be AutoInc", smallserialOptions.exists(option => (option equals TestDefault.baseTableRow.O.AutoInc)))
| assertTrue("int_auto_inc should be AutoInc", serialOptions.exists(option => (option equals TestDefault.baseTableRow.O.AutoInc)))
| assertTrue("bigint_auto_inc should be AutoInc", bigserialOptions.exists(option => (option equals TestDefault.baseTableRow.O.AutoInc)))
| assertTrue("default value of char1_default_empty should be ' '", char1EmptyOptions.exists(option => (option equals TestDefault.baseTableRow.O.Default(Some(' ')))))
| assertTrue("default value of char1_default_valid should be 'a'", char1ValidOptions.exists(option => (option equals TestDefault.baseTableRow.O.Default(Some('a')))))
| assertTrue("default value of char1_default_invalid should not exist", char1InvalidOptions.forall(option => (option.isInstanceOf[RelationalProfile.ColumnOption.Default[_]])))
| }
|)
""".stripMargin
},
new Config("MySQL1", StandardTestDBs.MySQL, "MySQL", Nil) {
import tdb.profile.api._
class A(tag: Tag) extends Table[(String)](tag, "a") {
def quote = column[String]("x", O.Default("\"\"")) // column name with double quote
def * = quote
}
override def generator =
TableQuery[A].schema.create >>
tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_))
override def testCode =
"""
| val a1 = ARow("e")
| DBIO.seq(
| schema.create,
| A += a1,
| A.result.map { case Seq(ARow(quote)) => assertEquals("e", quote) }
| ).transactionally
""".stripMargin
},
new Config("MySQL", StandardTestDBs.MySQL, "MySQL", Seq("/dbs/mysql.sql") ){
override def generator: DBIO[SourceCodeGenerator] =
tdb.profile.createModel(ignoreInvalidDefaults=false).map(new SourceCodeGenerator(_){
override def parentType = Some("com.typesafe.slick.testkit.util.TestCodeRunner.TestCase")
override def code = {
val testcode =
"""
| val entry = DefaultNumericRow(d0 = scala.math.BigDecimal(123.45), d1 = scala.math.BigDecimal(90), d3 = 0)
| val createStmt = schema.create.statements.mkString
| assertTrue(createStmt contains "`entry1` LONGTEXT")
| assertTrue(createStmt contains "`entry2` MEDIUMTEXT")
| assertTrue(createStmt contains "`entry3` TEXT")
| assertTrue(createStmt contains "`entry4` VARCHAR(255)")
| DBIO.seq(
| schema.create,
| TableName += TableNameRow(0),
| TableName.result.map{ case Seq(TableNameRow(id) ) => assertTrue("Schema name should be `slick_test`" , TableName.baseTableRow.schemaName.get eq "slick_test" ) },
| DefaultNumeric += entry,
| DefaultNumeric.result.head.map{ r => assertEquals(r , entry) }
| )
""".stripMargin
s"""
|lazy val tdb = $fullTdbName
|def test = {
| import org.junit.Assert._
| import scala.concurrent.ExecutionContext.Implicits.global
| $testcode
|}
""".stripMargin + super.code
}
})
},
new Config("EmptyDB", StandardTestDBs.H2Mem, "H2Mem", Nil),
new Config("Oracle1", StandardTestDBs.Oracle, "Oracle", Seq("/dbs/oracle1.sql")) {
override def useSingleLineStatements = true
override def testCode =
"""
| val entry = PersonRow(1)
| assertEquals(scala.math.BigDecimal(0), entry.age)
| DBIO.seq (
| schema.create,
| Person += entry,
| Person.result.head.map{ r => assertEquals(r , entry) }
| )
""".stripMargin
}
)
//Unified UUID config
class UUIDConfig(objectName: String, tdb: JdbcTestDB, tdbName: String, initScripts: Seq[String])
extends Config(objectName, tdb, tdbName, initScripts) {
override def generator = tdb.profile.createModel(ignoreInvalidDefaults=false).map(new MyGen(_) {
override def Table = new Table(_) {
override def Column = new Column(_){
override def defaultCode: (Any) => String = {
case v: java.util.UUID => s"""java.util.UUID.fromString("${v.toString}")"""
case v => super.defaultCode(v)
}
}
override def code = {
Seq("""
| /* default UUID, which is the same as for 'uuid.sql' */
| val defaultUUID = java.util.UUID.fromString("2f3f866c-d8e6-11e2-bb56-50e549c9b654")
| /* convert UUID */
| implicit object GetUUID extends slick.jdbc.GetResult[java.util.UUID] {
| def apply(rs: slick.jdbc.PositionedResult) = rs.nextObject().asInstanceOf[java.util.UUID]
| }
| /* convert Option[UUID] for H2 */
| implicit object GetOptionUUID extends slick.jdbc.GetResult[Option[java.util.UUID]] {
| def apply(rs: slick.jdbc.PositionedResult) = Option(rs.nextObject().asInstanceOf[java.util.UUID])
| }
""".stripMargin) ++ super.code
}
}
})
override def testCode =
"""
| import java.util.UUID
| val u1 = UUID.randomUUID()
| val u2 = UUID.randomUUID()
| val u3 = UUID.randomUUID()
| val u4 = UUID.randomUUID()
| val p1 = PersonRow(1, u1, uuidFunc = Some(u3))
| val p2 = PersonRow(2, u2, uuidFunc = Some(u4))
|
| def assertAll(all: Seq[PersonRow]) = {
| assertEquals( 2, all.size )
| assertEquals( Set(1,2), all.map(_.id).toSet )
| assertEquals( Set(u1, u2), all.map(_.uuid).toSet )
| assertEquals( Set(Some(u3), Some(u4)), all.map(_.uuidFunc).toSet )
| //it should contain sample UUID
| assert(all.forall(_.uuidDef == Some(defaultUUID)))
| }
|
| DBIO.seq(
| schema.create,
| Person += p1,
| Person += p2,
| Person.result.map(assertAll)
| ).transactionally
""".stripMargin
}
}