Skip to content

Commit

Permalink
unit tests in good condition
Browse files Browse the repository at this point in the history
  • Loading branch information
wheaties committed Jul 22, 2012
1 parent 32a3382 commit e94b9e0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 14 deletions.
Expand Up @@ -82,10 +82,10 @@ object DefinitionsTree extends (List[Column] => List[Tree]){


def apply(columns: List[Column])={ def apply(columns: List[Column])={
if(columns.exists(col => col.colType == PrimaryKey || col.colType == NullablePrimaryKey)){ if(columns.exists(col => col.colType == PrimaryKey || col.colType == NullablePrimaryKey)){
id(columns) :: optionConstructor (columns) :: Nil id(columns) :: optionConstructor(columns) :: Nil
} }
else{ else{
optionConstructor (columns) :: Nil optionConstructor(columns) :: Nil
} }
} }


Expand Down
Expand Up @@ -34,8 +34,7 @@ object SquerylGenerator extends LibraryGenerator{
} }


def writeDatabase(db: Database, pack: String, formato: Formato):String = { def writeDatabase(db: Database, pack: String, formato: Formato):String = {
val imports = IMPORT("org.squeryl.PrimitiveTypeMode._") :: val imports = IMPORT("org.squeryl.PrimitiveTypeMode._") :: IMPORT("org.squery.Schema") :: Nil
IMPORT("org.squery.Schema") :: Nil
val obj = OBJECTDEF(formato.databaseName(db.name)) withParents("Schema") := BLOCK{ val obj = OBJECTDEF(formato.databaseName(db.name)) withParents("Schema") := BLOCK{
db.tables.map(table => tableDef(table.name, formato)) db.tables.map(table => tableDef(table.name, formato))
} }
Expand Down

This file was deleted.

@@ -0,0 +1,30 @@
package com.wheaties.squealer.generator.scala.scalaquery

import org.specs2.mutable.Specification
import com.wheaties.squealer.generator.Formato
import com.wheaties.squealer.db.{ColumnDef, IntType, Column, Table}

class ScalaQueryGeneratorSpec extends Specification{
val formato = new Formato{
def databaseName(name: String) = name
def tableName(name: String) = name
def columnName(name: String) = name
}

"writeTable" should{
val table = Table("foo", None, Column("bar", IntType, None, None, ColumnDef) :: Nil)
val output = ScalaQueryGenerator.writeTable(table, "foo", formato)

"produce import statements" in{
output must contain("import org.scalaquery.ql.basic.BasicTable") and contain("org.scalaquery.ql.TypeMapper._")
}

"produce the table object" in{
output must contain("object foo extends BasicTable[Int]")
}

"produce the package the table object is contained" in{
output must contain("package foo")
}
}
}
Expand Up @@ -79,29 +79,29 @@ class DefinitionTreeSpec extends Specification{
"handle nullable fields with a default" in{ "handle nullable fields with a default" in{
val columns = Column("foo", IntType, Some("1"), None, NullablePrimaryKey) :: Nil val columns = Column("foo", IntType, Some("1"), None, NullablePrimaryKey) :: Nil
val tree = DefinitionsTree.optionConstructor(columns) val tree = DefinitionsTree.optionConstructor(columns)
treeToString(tree) must be_==("def this() = this(Some(1))") treeToString(tree) must be_==("def this = this(Some(1))")
} }
"handle non-nullable fields with a default" in{ "handle non-nullable fields with a default" in{
val columns = Column("foo", IntType, Some("1"), None, ColumnDef) :: Nil val columns = Column("foo", IntType, Some("1"), None, ColumnDef) :: Nil
val tree = DefinitionsTree.optionConstructor(columns) val tree = DefinitionsTree.optionConstructor(columns)
treeToString(tree) must be_==("def this() = this(1)") treeToString(tree) must be_==("def this = this(1)")
} }
"handle nullable fields without a default" in{ "handle nullable fields without a default" in{
val columns = Column("foo", IntType, None, None, NullableColumn) :: Nil val columns = Column("foo", IntType, None, None, NullableColumn) :: Nil
val tree = DefinitionsTree.optionConstructor(columns) val tree = DefinitionsTree.optionConstructor(columns)
treeToString(tree) must be_==("def this() = this(Some(0))") treeToString(tree) must be_==("def this = this(Some(0))")
} }
"handle nullable fields without a default" in{ "handle nullable fields without a default" in{
val columns = Column("foo", IntType, None, None, ColumnDef) :: Nil val columns = Column("foo", IntType, None, None, ColumnDef) :: Nil
val tree = DefinitionsTree.optionConstructor(columns) val tree = DefinitionsTree.optionConstructor(columns)
treeToString(tree) must be_==("def this() = this(0)") treeToString(tree) must be_==("def this = this(0)")
} }
"multiple fields" in{ "multiple fields" in{
val columns = Column("foo", IntType, Some("1"), None, ColumnDef) :: val columns = Column("foo", IntType, Some("1"), None, ColumnDef) ::
Column("bar", IntType, None, None, ColumnDef) :: Column("bar", IntType, None, None, ColumnDef) ::
Column("baz", IntType, None, None, NullableColumn) :: Nil Column("baz", IntType, None, None, NullableColumn) :: Nil
val tree = DefinitionsTree.optionConstructor(columns) val tree = DefinitionsTree.optionConstructor(columns)
treeToString(tree) must be_==("def this() = this(1, 0, Some(0)") treeToString(tree) must be_==("def this = this(1, 0, Some(0))")
} }
} }


Expand Down
@@ -0,0 +1,73 @@
package com.wheaties.squealer.generator.scala.squeryl

import org.specs2.mutable.Specification
import com.wheaties.squealer.generator.Formato
import com.wheaties.squealer.db._
import treehugger.forest._

class SquerylGeneratorSpec extends Specification{
val formato = new Formato{
def databaseName(name: String) = name
def tableName(name: String) = name
def columnName(name: String) = name
}

"writeTable" should{
val table = Table("foo", None, Column("bar", IntType, None, None, PrimaryKey) :: Nil)
val output = SquerylGenerator.writeTable(table, "baz", formato)

"produce import statements" in{
output must contain("org.squeryl.PrimitiveTypeMode._") and contain("org.squeryl.annotations.Column")
}

"produce the table object" in{
output must contain("case class foo")
}

"produce the package the table object is contained" in{
output must contain("package baz")
}
}

"makeClass" should{
val table = Table("foo", None, Column("bar", IntType, None, None, NullablePrimaryKey) :: Nil)
val output = SquerylGenerator.makeClass(table.name, table.columns, Nil, formato)

"produce a class" in{
treeToString(output) must contain("class foo")
}

"produce the member functions of a class" in{
treeToString(output) must contain("def id = compositeKey(bar)") and contain("def this = this(Some(0))")
}
}

"writeDatabase" should{
val table = Table("foo", None, Column("bar", IntType, None, None, PrimaryKey) :: Nil)
val db = Database("baz", table :: Nil)
val output = SquerylGenerator.writeDatabase(db, "baz", formato)

"produce import statements" in{
output must contain("org.squeryl.PrimitiveTypeMode._") and contain("org.squery.Schema")
}

"handle a schema with one table" in{
output must contain("object baz extends Schema {\n val foo = table[foo](\"foo\")\n}")
}

"handle a schema with multiple tables" in{
val multi = Database("baz", table :: table.copy(name = "yo") :: Nil)
val output = SquerylGenerator.writeDatabase(multi, "baz", formato)

output must contain("object baz extends Schema {\n val foo = table[foo](\"foo\")\n val yo = table[yo](\"yo\")\n}")
}
}

"tableDef" should{
"produce something" in{
val output = SquerylGenerator.tableDef("foo", formato)

treeToString(output) must be_==("val foo = table[foo](\"foo\")")
}
}
}

0 comments on commit e94b9e0

Please sign in to comment.