Skip to content

Commit

Permalink
testInsert: bugfix for ComputedTestInserts where it didn't have the d…
Browse files Browse the repository at this point in the history
…ata to resolve foreign keys to other modules in a modularized build
  • Loading branch information
oyvindberg committed Jun 11, 2024
1 parent ba83872 commit 79dd77c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package adventureworks

import adventureworks.public.{AccountNumber, Flag, Mydomain, Name, NameStyle, Phone, ShortText}
import adventureworks.public.*

import scala.util.Random

Expand All @@ -12,4 +12,5 @@ object DomainInsert extends TestDomainInsert {
override def publicNameStyle(random: Random): NameStyle = NameStyle(random.nextBoolean())
override def publicPhone(random: Random): Phone = Phone(random.nextString(10))
override def publicShortText(random: Random): ShortText = ShortText(random.nextString(10))
override def publicOrderNumber(random: Random): OrderNumber = OrderNumber(random.nextString(10))
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ object DomainInsert extends TestDomainInsert {
override def publicNameStyle(random: Random): NameStyle = NameStyle(random.nextBoolean())
override def publicPhone(random: Random): Phone = Phone(random.nextString(10))
override def publicShortText(random: Random): ShortText = ShortText(random.nextString(10))
override def publicOrderNumber(random: Random): OrderNumber = OrderNumber(random.nextString(10))
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ object DomainInsert extends TestDomainInsert {
override def publicNameStyle(random: Random): NameStyle = NameStyle(random.nextBoolean())
override def publicPhone(random: Random): Phone = Phone(random.nextString(10))
override def publicShortText(random: Random): ShortText = ShortText(random.nextString(10))
override def publicOrderNumber(random: Random): OrderNumber = OrderNumber(random.nextString(10))
}
20 changes: 14 additions & 6 deletions typo/src/scala/typo/internal/ComputedTestInserts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ object ComputedTestInserts {
val random: sc.Ident = sc.Ident("random")
val domainInsert: sc.Ident = sc.Ident("domainInsert")

def apply(projectName: String, options: InternalOptions, customTypes: CustomTypes, domains: List[ComputedDomain], enums: List[ComputedStringEnum], computedTables: Iterable[ComputedTable]) = {
val tablesByName: Map[db.RelationName, ComputedTable] =
computedTables.iterator.map(x => x.dbTable.name -> x).toMap
def apply(
projectName: String,
options: InternalOptions,
// global data
customTypes: CustomTypes,
domains: List[ComputedDomain],
enums: List[ComputedStringEnum],
allTablesByName: Map[db.RelationName, ComputedTable],
// project data
tables: Iterable[ComputedTable]
): ComputedTestInserts = {
val enumsByName: Map[sc.Type, ComputedStringEnum] =
enums.iterator.map(x => x.tpe -> x).toMap

val maybeDomainMethods: Option[GenerateDomainMethods] =
GenerateDomainMethod
.of(domains, computedTables)
.of(domains, tables)
.map { methods =>
val tpe = sc.Type.Qualified(options.pkg / sc.Ident(s"${Naming.titleCase(projectName)}TestDomainInsert"))
GenerateDomainMethods(tpe, methods)
Expand Down Expand Up @@ -106,12 +114,12 @@ object ComputedTestInserts {

new ComputedTestInserts(
sc.Type.Qualified(options.pkg / sc.Ident(s"${Naming.titleCase(projectName)}TestInsert")),
computedTables.collect {
tables.collect {
case table if !options.readonlyRepo.include(table.dbTable.name) =>
val hasConstraints: Set[db.ColName] =
table.dbTable.cols.iterator.flatMap(_.constraints.flatMap(_.columns)).toSet

FkAnalysis(tablesByName, table).createWithFkIdsUnsavedRowOrRow match {
FkAnalysis(allTablesByName, table).createWithFkIdsUnsavedRowOrRow match {
case Some(colsFromFks) =>
val valuesFromFk: List[(sc.Ident, sc.Code)] =
colsFromFks.allColumns.toList.map { col =>
Expand Down
2 changes: 1 addition & 1 deletion typo/src/scala/typo/internal/generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ object generate {
val keptTables =
computedRelations.collect { case x: ComputedTable if options.enableTestInserts.include(x.dbTable.name) && keptTypes(x.names.RepoImplName) => x }
if (keptTables.nonEmpty) {
val computed = ComputedTestInserts(project.name, options, customTypes, domains, enums, keptTables)
val computed = ComputedTestInserts(project.name, options, customTypes, domains, enums, computedRelationsByName, keptTables)
FileTestInserts(computed, dbLib)
} else Nil
case _ => Nil
Expand Down

0 comments on commit 79dd77c

Please sign in to comment.