From e94b9e0646a70d891da17be3ba9fb6b0fc79715d Mon Sep 17 00:00:00 2001 From: Wheaties Date: Sun, 22 Jul 2012 13:38:50 -0400 Subject: [PATCH] unit tests in good condition --- .../generator/scala/squeryl/ClassTree.scala | 4 +- .../scala/squeryl/SquerylGenerator.scala | 3 +- .../squealer/generator/FormatoSpec.scala | 5 -- .../scalaquery/ScalaQueryGeneratorSpec.scala | 30 ++++++++ .../scala/squeryl/ClassTreeSpec.scala | 10 +-- .../scala/squeryl/SquerylGeneratorSpec.scala | 73 +++++++++++++++++++ 6 files changed, 111 insertions(+), 14 deletions(-) delete mode 100644 src/test/scala/com/wheaties/squealer/generator/FormatoSpec.scala create mode 100644 src/test/scala/com/wheaties/squealer/generator/scala/scalaquery/ScalaQueryGeneratorSpec.scala create mode 100644 src/test/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGeneratorSpec.scala diff --git a/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTree.scala b/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTree.scala index 822cc42..19d56c5 100644 --- a/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTree.scala +++ b/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTree.scala @@ -82,10 +82,10 @@ object DefinitionsTree extends (List[Column] => List[Tree]){ def apply(columns: List[Column])={ if(columns.exists(col => col.colType == PrimaryKey || col.colType == NullablePrimaryKey)){ - id(columns) :: optionConstructor (columns) :: Nil + id(columns) :: optionConstructor(columns) :: Nil } else{ - optionConstructor (columns) :: Nil + optionConstructor(columns) :: Nil } } diff --git a/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGenerator.scala b/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGenerator.scala index db66b64..2d79e91 100644 --- a/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGenerator.scala +++ b/src/main/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGenerator.scala @@ -34,8 +34,7 @@ object SquerylGenerator extends LibraryGenerator{ } def writeDatabase(db: Database, pack: String, formato: Formato):String = { - val imports = IMPORT("org.squeryl.PrimitiveTypeMode._") :: - IMPORT("org.squery.Schema") :: Nil + val imports = IMPORT("org.squeryl.PrimitiveTypeMode._") :: IMPORT("org.squery.Schema") :: Nil val obj = OBJECTDEF(formato.databaseName(db.name)) withParents("Schema") := BLOCK{ db.tables.map(table => tableDef(table.name, formato)) } diff --git a/src/test/scala/com/wheaties/squealer/generator/FormatoSpec.scala b/src/test/scala/com/wheaties/squealer/generator/FormatoSpec.scala deleted file mode 100644 index b3d269f..0000000 --- a/src/test/scala/com/wheaties/squealer/generator/FormatoSpec.scala +++ /dev/null @@ -1,5 +0,0 @@ -package com.wheaties.squealer.generator - -import com.wheaties.squealer.db._ -import org.specs2.mutable.Specification -import scala.format.{RegexFormato, CamelCase} diff --git a/src/test/scala/com/wheaties/squealer/generator/scala/scalaquery/ScalaQueryGeneratorSpec.scala b/src/test/scala/com/wheaties/squealer/generator/scala/scalaquery/ScalaQueryGeneratorSpec.scala new file mode 100644 index 0000000..b4a9f1c --- /dev/null +++ b/src/test/scala/com/wheaties/squealer/generator/scala/scalaquery/ScalaQueryGeneratorSpec.scala @@ -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") + } + } +} \ No newline at end of file diff --git a/src/test/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTreeSpec.scala b/src/test/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTreeSpec.scala index a4ec359..e93ea93 100644 --- a/src/test/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTreeSpec.scala +++ b/src/test/scala/com/wheaties/squealer/generator/scala/squeryl/ClassTreeSpec.scala @@ -79,29 +79,29 @@ class DefinitionTreeSpec extends Specification{ "handle nullable fields with a default" in{ val columns = Column("foo", IntType, Some("1"), None, NullablePrimaryKey) :: Nil 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{ val columns = Column("foo", IntType, Some("1"), None, ColumnDef) :: Nil 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{ val columns = Column("foo", IntType, None, None, NullableColumn) :: Nil 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{ val columns = Column("foo", IntType, None, None, ColumnDef) :: Nil val tree = DefinitionsTree.optionConstructor(columns) - treeToString(tree) must be_==("def this() = this(0)") + treeToString(tree) must be_==("def this = this(0)") } "multiple fields" in{ val columns = Column("foo", IntType, Some("1"), None, ColumnDef) :: Column("bar", IntType, None, None, ColumnDef) :: Column("baz", IntType, None, None, NullableColumn) :: Nil 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))") } } diff --git a/src/test/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGeneratorSpec.scala b/src/test/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGeneratorSpec.scala new file mode 100644 index 0000000..7b61482 --- /dev/null +++ b/src/test/scala/com/wheaties/squealer/generator/scala/squeryl/SquerylGeneratorSpec.scala @@ -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\")") + } + } +} \ No newline at end of file