Skip to content

Commit 88ede3a

Browse files
committed
record SimpleTable tests in reference.md
1 parent 40c3237 commit 88ede3a

File tree

3 files changed

+119
-19
lines changed

3 files changed

+119
-19
lines changed

docs/generateDocs.mill

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ def generateTutorial(sourcePath: os.Path, destPath: os.Path) = {
6969
}
7070
def generateReference(dest: os.Path, scalafmtCallback: (Seq[os.Path], os.Path) => Unit) = {
7171
def dropExprPrefix(s: String) = s.split('.').drop(2).mkString(".")
72+
def dropNTExprPrefix(s: String) = s.split('.').drop(3).mkString(".")
7273
val records = upickle.default.read[Seq[Record]](os.read.stream(mill.api.WorkspaceRoot.workspaceRoot / "out" / "recordedTests.json"))
74+
val ntRecords = upickle.default.read[Seq[Record]](os.read.stream(mill.api.WorkspaceRoot.workspaceRoot / "out" / "recordedTestsNT.json"))
7375
val suiteDescriptions = upickle.default.read[Map[String, String]](os.read.stream(mill.api.WorkspaceRoot.workspaceRoot / "out" / "recordedSuiteDescriptions.json"))
7476
.map{case (k, v) => (dropExprPrefix(k), v)}
7577

76-
val rawScalaStrs = records.flatMap(r => Seq(r.queryCodeString) ++ r.resultCodeString)
78+
val rawScalaStrs = (records ++ ntRecords)
79+
.flatMap(r => Seq(r.queryCodeString) ++ r.resultCodeString)
7780
val formattedScalaStrs = {
7881
val tmps = rawScalaStrs.map(os.temp(_, suffix = ".scala"))
7982
scalafmtCallback(tmps, mill.api.WorkspaceRoot.workspaceRoot / ".scalafmt.conf")
@@ -155,15 +158,26 @@ def generateReference(dest: os.Path, scalafmtCallback: (Seq[os.Path], os.Path) =
155158
.sortBy(_._2.head.suiteLine)
156159
.distinctBy { case (k, v) => dropExprPrefix(k)}
157160
.map{case (k, vs) => (dropExprPrefix(k), vs.map(r => r.copy(suiteName = dropExprPrefix(r.suiteName))))}
161+
val ntRecordsWithoutDuplicateSuites = ntRecords
162+
.groupBy(_.suiteName)
163+
.toSeq
164+
.sortBy(_._2.head.suiteLine)
165+
.distinctBy { case (k, v) => dropNTExprPrefix(k)}
166+
.map{case (k, vs) => (dropNTExprPrefix(k), vs.map(r => r.copy(suiteName = dropNTExprPrefix(r.suiteName))))}
167+
.toMap
158168

159169
for((suiteName, suiteGroup) <- recordsWithoutDuplicateSuites) {
160170
val seen = mutable.Set.empty[String]
161171
outputLines.append(s"## $suiteName")
162172
outputLines.append(suiteDescriptions(suiteName))
163173
var lastSeen = ""
164-
for(r <- suiteGroup){
165-
166-
val prettyName = (r.suiteName +: r.testPath).mkString(".")
174+
var remainingNTRecords = ntRecordsWithoutDuplicateSuites
175+
.get(suiteName)
176+
.getOrElse(Seq.empty).groupBy {r =>
177+
val prettyName = (r.suiteName +: r.testPath).mkString(".")
178+
prettyName
179+
}
180+
def addRecord(r: Record, prettyName: String) = {
167181
val titleOpt =
168182
if (prettyName == lastSeen) Some("----")
169183
else if (!seen(prettyName)) Some(s"### $prettyName")
@@ -174,21 +188,29 @@ def generateReference(dest: os.Path, scalafmtCallback: (Seq[os.Path], os.Path) =
174188
lastSeen = prettyName
175189
outputLines.append(
176190
s"""$title
177-
|
178-
|${dedent(r.docs, "")}
179-
|
180-
|```scala
181-
|${scalafmt(r.queryCodeString)}
182-
|```
183-
|
184-
|${sqlFormat(r.sqlString)}
185-
|
186-
|${renderResult(r.resultCodeString)}
187-
|
188-
|""".stripMargin
191+
|
192+
|${dedent(r.docs, "")}
193+
|
194+
|```scala
195+
|${scalafmt(r.queryCodeString)}
196+
|```
197+
|
198+
|${sqlFormat(r.sqlString)}
199+
|
200+
|${renderResult(r.resultCodeString)}
201+
|
202+
|""".stripMargin
189203
)
190204
}
191205
}
206+
for(r <- suiteGroup){
207+
val prettyName = (r.suiteName +: r.testPath).mkString(".")
208+
addRecord(r, prettyName)
209+
remainingNTRecords -= prettyName
210+
}
211+
for((prettyName, rs) <- remainingNTRecords; r <- rs) {
212+
addRecord(r, prettyName)
213+
}
192214
}
193215
os.write.over(dest, outputLines.mkString("\n"))
194216
}

docs/reference.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10286,6 +10286,80 @@ result._2 ==> None
1028610286
1028710287
1028810288
10289+
### DataTypes.enclosing - with SimpleTable
10290+
10291+
You can nest `case class`es in other `case class`es to DRY up common sets of
10292+
table columns. These nested `case class`es have their columns flattened out
10293+
into the enclosing `case class`'s columns, such that at the SQL level it is
10294+
all flattened out without nesting.
10295+
10296+
**Important**: When using nested `case class`es with `SimpleTable`,
10297+
make sure to extend `SimpleTable.Nested` in the nested class.
10298+
10299+
```scala
10300+
// case class Nested(
10301+
// fooId: Int,
10302+
// myBoolean: Boolean,
10303+
// ) extends SimpleTable.Nested
10304+
// object Nested extends SimpleTable[Nested]
10305+
//
10306+
// case class Enclosing(
10307+
// barId: Int,
10308+
// myString: String,
10309+
// foo: Nested
10310+
// )
10311+
// object Enclosing extends SimpleTable[Enclosing]
10312+
val value1 = Enclosing(
10313+
barId = 1337,
10314+
myString = "hello",
10315+
foo = Nested(
10316+
fooId = 271828,
10317+
myBoolean = true
10318+
)
10319+
)
10320+
val value2 = Enclosing(
10321+
barId = 31337,
10322+
myString = "world",
10323+
foo = Nested(
10324+
fooId = 1618,
10325+
myBoolean = false
10326+
)
10327+
)
10328+
10329+
val insertColumns = Enclosing.insert.columns(
10330+
_.barId := value1.barId,
10331+
_.myString := value1.myString,
10332+
_.foo.fooId := value1.foo.fooId,
10333+
_.foo.myBoolean := value1.foo.myBoolean
10334+
)
10335+
db.renderSql(insertColumns) ==>
10336+
"INSERT INTO enclosing (bar_id, my_string, foo_id, my_boolean) VALUES (?, ?, ?, ?)"
10337+
10338+
db.run(insertColumns) ==> 1
10339+
10340+
val insertValues = Enclosing.insert.values(value2)
10341+
db.renderSql(insertValues) ==>
10342+
"INSERT INTO enclosing (bar_id, my_string, foo_id, my_boolean) VALUES (?, ?, ?, ?)"
10343+
10344+
db.run(insertValues) ==> 1
10345+
10346+
db.renderSql(Enclosing.select) ==> """
10347+
SELECT
10348+
enclosing0.bar_id AS bar_id,
10349+
enclosing0.my_string AS my_string,
10350+
enclosing0.foo_id AS foo_id,
10351+
enclosing0.my_boolean AS my_boolean
10352+
FROM enclosing enclosing0
10353+
"""
10354+
10355+
db.run(Enclosing.select) ==> Seq(value1, value2)
10356+
```
10357+
10358+
10359+
10360+
10361+
10362+
1028910363
## Optional
1029010364
Queries using columns that may be `NULL`, `Expr[Option[T]]` or `Option[T]` in Scala
1029110365
### Optional

scalasql/namedtuples/test/src/datatypes/SimpleTableDataTypesTests.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,22 @@ trait SimpleTableDataTypesTests extends ScalaSqlSuite {
146146
}
147147
)
148148

149-
test("enclosing") - checker.recorded(
149+
// !! Important: '- with SimpleTable' so it will be detected by generateDocs.mill
150+
test("enclosing - with SimpleTable") - checker.recorded(
150151
"""
151152
You can nest `case class`es in other `case class`es to DRY up common sets of
152153
table columns. These nested `case class`es have their columns flattened out
153154
into the enclosing `case class`'s columns, such that at the SQL level it is
154155
all flattened out without nesting.
156+
157+
**Important**: When using nested `case class`es with `SimpleTable`,
158+
make sure to extend `SimpleTable.Nested` in the nested class.
155159
""",
156160
Text {
157161
// case class Nested(
158162
// fooId: Int,
159163
// myBoolean: Boolean,
160-
// )
164+
// ) extends SimpleTable.Nested
161165
// object Nested extends SimpleTable[Nested]
162166
//
163167
// case class Enclosing(
@@ -213,7 +217,7 @@ trait SimpleTableDataTypesTests extends ScalaSqlSuite {
213217

214218
}
215219
)
216-
test("JoinNullable proper type mappingg") - checker.recorded(
220+
test("JoinNullable proper type mapping") - checker.recorded(
217221
"",
218222
Text {
219223
case class A(id: Int, bId: Option[Int])

0 commit comments

Comments
 (0)