pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

commit 6f51d3fdaa0d1f790fc82f1184292c09b91e763e
parent f265eaac76131dc6047b0e25a99371c9b07bc589
Author: Jul <jul@9o.is>
Date:   Thu, 17 Apr 2014 23:53:53 -0400

changed package name to inc.pyc in test cases

Diffstat:
Dsrc/test/scala/com/pyd/BaseSpec.scala | 31-------------------------------
Dsrc/test/scala/com/pyd/HtmlSourceSpec.scala | 59-----------------------------------------------------------
Dsrc/test/scala/com/pyd/MongoTestKit.scala | 85-------------------------------------------------------------------------------
Dsrc/test/scala/com/pyd/model/UserSpec.scala | 64----------------------------------------------------------------
Asrc/test/scala/inc/pyc/BaseSpec.scala | 31+++++++++++++++++++++++++++++++
Asrc/test/scala/inc/pyc/HtmlSourceSpec.scala | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test/scala/inc/pyc/MongoTestKit.scala | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test/scala/inc/pyc/model/UserSpec.scala | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 241 insertions(+), 239 deletions(-)

diff --git a/src/test/scala/com/pyd/BaseSpec.scala b/src/test/scala/com/pyd/BaseSpec.scala @@ -1,31 +0,0 @@ -package com.pyd - -import config.MongoConfig - -import org.scalatest._ -import org.scalatest.matchers.ShouldMatchers - -import net.liftweb._ -import common._ -import http._ -import util._ -import Helpers._ - -trait BaseWordSpec extends WordSpec with ShouldMatchers - -trait BaseMongoWordSpec extends BaseWordSpec with MongoSuite { - def mongoIdentifier = MongoConfig.defaultId -} -trait BaseMongoSessionWordSpec extends BaseWordSpec with MongoSessionSuite { - def mongoIdentifier = MongoConfig.defaultId -} - -trait WithSessionSpec extends AbstractSuite { this: Suite => - - protected def session = new LiftSession("", randomString(20), Empty) - - abstract override def withFixture(test: NoArgTest) { - S.initIfUninitted(session) { super.withFixture(test) } - } -} - diff --git a/src/test/scala/com/pyd/HtmlSourceSpec.scala b/src/test/scala/com/pyd/HtmlSourceSpec.scala @@ -1,59 +0,0 @@ -package com.pyd - -import java.io.File - -import scala.xml.XML - -import net.liftweb.common.Full -import net.liftweb.util.Html5 - -object HtmlSourceSpec extends BaseWordSpec { - - "HTML Sources" should { - - "be well-formed" in { - /** - * Tests to make sure the project's HTML files are well-formed. - * - * Finds every *.html and *.xml file in src/main/webapp (and its - * subdirectories) and tests to make sure they are well-formed. - */ - var failed: List[File] = Nil - - def handledXml(file: String) = file.endsWith(".xml") - - def handledXHtml(file: String) = - file.endsWith(".html") || file.endsWith(".htm") || file.endsWith(".xhtml") - - def wellFormed(file: File) { - if (file.isDirectory) - for (f <- file.listFiles) wellFormed(f) - - if (file.isFile && handledXml(file.getName)) { - try { - XML.loadFile(file) - } catch { - case e: org.xml.sax.SAXParseException => failed = file :: failed - } - } - if (file.isFile && handledXHtml(file.getName)) { - Html5.parse(new java.io.FileInputStream(file.getAbsolutePath)) match { - case Full(_) => // file is ok - case _ => failed = file :: failed - } - } - } - - wellFormed(new File("src/main/webapp")) - - val numFails = failed.size - if (numFails > 0) { - val fileStr = if (numFails == 1) "file" else "files" - val msg = "Malformed HTML in " + numFails + " " + fileStr + ": " + failed.mkString(", ") - fail(msg) - } - - numFails should equal (0) - } - } -} diff --git a/src/test/scala/com/pyd/MongoTestKit.scala b/src/test/scala/com/pyd/MongoTestKit.scala @@ -1,85 +0,0 @@ -package com.pyd - -import org.scalatest._ - -import net.liftweb._ -import common._ -import http._ -import mongodb._ -import util.{Props, StackableMaker} -import util.Helpers.randomString - -import com.mongodb.{MongoClient, ServerAddress} - -// The sole mongo object for testing -object TestMongo { - val mongo = new MongoClient(new ServerAddress( - Props.get("mongo.default.host", "127.0.0.1"), - Props.getInt("mongo.default.port", 27017) - )) -} - -/** - * Creates a `MongoIdentifier` and database named after the class. - * Therefore, each Suite class shares the same database. - * Database is dropped after all tests have been run in the suite. - */ -trait MongoBeforeAndAfterAll extends BeforeAndAfterAll { - this: Suite => - - lazy val dbName = "pyd_test_"+this.getClass.getName - .replace(".", "_") - .toLowerCase - - def debug = false // db won't be dropped if this is true - - lazy val identifier = new MongoIdentifier { - val jndiName = dbName - } - - override def beforeAll(configMap: Map[String, Any]) { - // define the db - MongoDB.defineDb(identifier, TestMongo.mongo, dbName) - } - - override def afterAll(configMap: Map[String, Any]) { - if (!debug) { dropDb() } - } - - protected def dropDb(): Unit = MongoDB.use(identifier) { db => db.dropDatabase } -} - -/** - * Basic Mongo suite for running Mongo tests. - */ -trait MongoSuite extends AbstractSuite with MongoBeforeAndAfterAll { - this: Suite => - - def mongoIdentifier: StackableMaker[MongoIdentifier] - - abstract override def withFixture(test: NoArgTest) { - mongoIdentifier.doWith(identifier) { - super.withFixture(test) - } - } -} - -/** - * Mongo suite running within a LiftSession. - */ -trait MongoSessionSuite extends AbstractSuite with MongoBeforeAndAfterAll { - this: Suite => - - def mongoIdentifier: StackableMaker[MongoIdentifier] - - // Override with `val` to share session amongst tests. - protected def session = new LiftSession("", randomString(20), Empty) - - abstract override def withFixture(test: NoArgTest) { - S.initIfUninitted(session) { - mongoIdentifier.doWith(identifier) { - super.withFixture(test) - } - } - } -} diff --git a/src/test/scala/com/pyd/model/UserSpec.scala b/src/test/scala/com/pyd/model/UserSpec.scala @@ -1,64 +0,0 @@ -package com.pyd -package model - -class UserSpec extends BaseMongoSessionWordSpec { - "User" should { - "create, validate, save, and retrieve properly" in { - - val userPass = "testpass1" - // create a new User instance - val newUser = User.createRecord - .email("test@liftweb.net") - - newUser.password(userPass) - - val errs = newUser.validate - if (errs.length > 1) { - fail("Validation error: "+errs.mkString(", ")) - } - - newUser.name("Test") - newUser.username("Test") - newUser.validate.length should equal (0) - - // save to db - newUser.password.hashIt - newUser.save - - // retrieve from db and compare - val userFromDb = User.find(newUser.id.get) - userFromDb.isDefined should equal (true) - userFromDb.map(u => u.id.get should equal (newUser.id.get)) - } - - "Support password properly" in { - - val userPass = "testpass2" - // create a new User instance - val newUser = User.createRecord - .email("test2@liftweb.net") - .name("Test2") - .username("Test2") - .password(userPass, true) - - // check password - newUser.password.isMatch("xxxxx") should equal (false) - newUser.password.isMatch(userPass) should equal (true) - - newUser.validate.length should equal (0) - - // save to db - newUser.save - - // retrieve from db and compare - val userFromDb = User.find(newUser.id.get) - - userFromDb.isDefined should equal (true) - userFromDb.map(u => { - u.id.get should equal (newUser.id.get) - u.password.isMatch("xxxxx") should equal (false) - u.password.isMatch(userPass) should equal (true) - }) - } - } -} diff --git a/src/test/scala/inc/pyc/BaseSpec.scala b/src/test/scala/inc/pyc/BaseSpec.scala @@ -0,0 +1,31 @@ +package inc.pyc + +import config.MongoConfig + +import org.scalatest._ +import org.scalatest.matchers.ShouldMatchers + +import net.liftweb._ +import common._ +import http._ +import util._ +import Helpers._ + +trait BaseWordSpec extends WordSpec with ShouldMatchers + +trait BaseMongoWordSpec extends BaseWordSpec with MongoSuite { + def mongoIdentifier = MongoConfig.defaultId +} +trait BaseMongoSessionWordSpec extends BaseWordSpec with MongoSessionSuite { + def mongoIdentifier = MongoConfig.defaultId +} + +trait WithSessionSpec extends AbstractSuite { this: Suite => + + protected def session = new LiftSession("", randomString(20), Empty) + + abstract override def withFixture(test: NoArgTest) { + S.initIfUninitted(session) { super.withFixture(test) } + } +} + diff --git a/src/test/scala/inc/pyc/HtmlSourceSpec.scala b/src/test/scala/inc/pyc/HtmlSourceSpec.scala @@ -0,0 +1,59 @@ +package inc.pyc + +import java.io.File + +import scala.xml.XML + +import net.liftweb.common.Full +import net.liftweb.util.Html5 + +object HtmlSourceSpec extends BaseWordSpec { + + "HTML Sources" should { + + "be well-formed" in { + /** + * Tests to make sure the project's HTML files are well-formed. + * + * Finds every *.html and *.xml file in src/main/webapp (and its + * subdirectories) and tests to make sure they are well-formed. + */ + var failed: List[File] = Nil + + def handledXml(file: String) = file.endsWith(".xml") + + def handledXHtml(file: String) = + file.endsWith(".html") || file.endsWith(".htm") || file.endsWith(".xhtml") + + def wellFormed(file: File) { + if (file.isDirectory) + for (f <- file.listFiles) wellFormed(f) + + if (file.isFile && handledXml(file.getName)) { + try { + XML.loadFile(file) + } catch { + case e: org.xml.sax.SAXParseException => failed = file :: failed + } + } + if (file.isFile && handledXHtml(file.getName)) { + Html5.parse(new java.io.FileInputStream(file.getAbsolutePath)) match { + case Full(_) => // file is ok + case _ => failed = file :: failed + } + } + } + + wellFormed(new File("src/main/webapp")) + + val numFails = failed.size + if (numFails > 0) { + val fileStr = if (numFails == 1) "file" else "files" + val msg = "Malformed HTML in " + numFails + " " + fileStr + ": " + failed.mkString(", ") + fail(msg) + } + + numFails should equal (0) + } + } +} diff --git a/src/test/scala/inc/pyc/MongoTestKit.scala b/src/test/scala/inc/pyc/MongoTestKit.scala @@ -0,0 +1,85 @@ +package inc.pyc + +import org.scalatest._ + +import net.liftweb._ +import common._ +import http._ +import mongodb._ +import util.{Props, StackableMaker} +import util.Helpers.randomString + +import com.mongodb.{MongoClient, ServerAddress} + +// The sole mongo object for testing +object TestMongo { + val mongo = new MongoClient(new ServerAddress( + Props.get("mongo.default.host", "127.0.0.1"), + Props.getInt("mongo.default.port", 27017) + )) +} + +/** + * Creates a `MongoIdentifier` and database named after the class. + * Therefore, each Suite class shares the same database. + * Database is dropped after all tests have been run in the suite. + */ +trait MongoBeforeAndAfterAll extends BeforeAndAfterAll { + this: Suite => + + lazy val dbName = "pyc_test_"+this.getClass.getName + .replace(".", "_") + .toLowerCase + + def debug = false // db won't be dropped if this is true + + lazy val identifier = new MongoIdentifier { + val jndiName = dbName + } + + override def beforeAll(configMap: Map[String, Any]) { + // define the db + MongoDB.defineDb(identifier, TestMongo.mongo, dbName) + } + + override def afterAll(configMap: Map[String, Any]) { + if (!debug) { dropDb() } + } + + protected def dropDb(): Unit = MongoDB.use(identifier) { db => db.dropDatabase } +} + +/** + * Basic Mongo suite for running Mongo tests. + */ +trait MongoSuite extends AbstractSuite with MongoBeforeAndAfterAll { + this: Suite => + + def mongoIdentifier: StackableMaker[MongoIdentifier] + + abstract override def withFixture(test: NoArgTest) { + mongoIdentifier.doWith(identifier) { + super.withFixture(test) + } + } +} + +/** + * Mongo suite running within a LiftSession. + */ +trait MongoSessionSuite extends AbstractSuite with MongoBeforeAndAfterAll { + this: Suite => + + def mongoIdentifier: StackableMaker[MongoIdentifier] + + // Override with `val` to share session amongst tests. + protected def session = new LiftSession("", randomString(20), Empty) + + abstract override def withFixture(test: NoArgTest) { + S.initIfUninitted(session) { + mongoIdentifier.doWith(identifier) { + super.withFixture(test) + } + } + } +} diff --git a/src/test/scala/inc/pyc/model/UserSpec.scala b/src/test/scala/inc/pyc/model/UserSpec.scala @@ -0,0 +1,66 @@ +package inc.pyc +package model + +class UserSpec extends BaseMongoSessionWordSpec { + "User" should { + "create, validate, save, and retrieve properly" in { + + val userPass = "testpass1" + // create a new User instance + val newUser = User.createRecord + .email("test@liftweb.net") + + newUser.password(userPass) + + val errs = newUser.validate + if (errs.length > 1) { + fail("Validation error: "+errs.mkString(", ")) + } + + newUser.fname("First Name Test") + newUser.lname("Last Name Test") + newUser.username("Test") + newUser.validate.length should equal (0) + + // save to db + newUser.password.hashIt + newUser.save + + // retrieve from db and compare + val userFromDb = User.find(newUser.id.get) + userFromDb.isDefined should equal (true) + userFromDb.map(u => u.id.get should equal (newUser.id.get)) + } + + "Support password properly" in { + + val userPass = "testpass2" + // create a new User instance + val newUser = User.createRecord + .email("test2@liftweb.net") + .fname("First Name Test2") + .lname("Last Name Test2") + .username("Test2") + .password(userPass, true) + + // check password + newUser.password.isMatch("xxxxx") should equal (false) + newUser.password.isMatch(userPass) should equal (true) + + newUser.validate.length should equal (0) + + // save to db + newUser.save + + // retrieve from db and compare + val userFromDb = User.find(newUser.id.get) + + userFromDb.isDefined should equal (true) + userFromDb.map(u => { + u.id.get should equal (newUser.id.get) + u.password.isMatch("xxxxx") should equal (false) + u.password.isMatch(userPass) should equal (true) + }) + } + } +}