Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #142 from MITLibraries/135_moar_tests
Browse files Browse the repository at this point in the history
135 Model Tests
  • Loading branch information
JPrevost committed Mar 30, 2015
2 parents ff456e3 + fe68ac8 commit d90f4cf
Show file tree
Hide file tree
Showing 23 changed files with 1,863 additions and 77 deletions.
8 changes: 6 additions & 2 deletions app/models/ContentFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ object ContentFormat {
}
}

def create(tag: String, label: String, description: String, url: String, mimetype: String, logo: Option[String]) {
def create(tag: String, label: String, description: String, url: String, mimetype: String, logo: Option[String]) = {
DB.withConnection { implicit c =>
SQL("insert into content_format (tag, label, description, url, mimetype, logo) values ({tag}, {label}, {description}, {url}, {mimetype}, {logo})")
.on('tag -> tag, 'label -> label, 'description -> description, 'url -> url, 'mimetype -> mimetype, 'logo -> logo).executeUpdate()
.on('tag -> tag, 'label -> label, 'description -> description, 'url -> url, 'mimetype -> mimetype, 'logo -> logo).executeInsert()
}
}

def make(tag: String, label: String, description: String, url: String, mimetype: String, logo: Option[String]): ContentFormat = {
findById(create(tag, label, description, url, mimetype, logo).get.toInt).get
}

def all: List[ContentFormat] = {
DB.withConnection { implicit c =>
SQL("select * from content_format").as(format *)
Expand Down
8 changes: 6 additions & 2 deletions app/models/ContentType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ object ContentType {
}
}

def create(tag: String, label: String, description: String, logo: Option[String]) {
def create(tag: String, label: String, description: String, logo: Option[String]) = {
DB.withConnection { implicit c =>
SQL("insert into content_type (tag, label, description, logo) values ({tag}, {label}, {description}, {logo})")
.on('tag -> tag, 'label -> label, 'description -> description, 'logo -> logo).executeUpdate()
.on('tag -> tag, 'label -> label, 'description -> description, 'logo -> logo).executeInsert()
}
}

def make(tag: String, label: String, description: String, logo: Option[String]): ContentType = {
findById(create(tag, label, description, logo).get.toInt).get
}

def all: List[ContentType] = {
DB.withConnection { implicit c =>
SQL("select * from content_type").as(ctype *)
Expand Down
15 changes: 13 additions & 2 deletions app/models/Finder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object Finder {
}

def create(schemeId: Int, formatId: Int, description: String, cardinality: String,
idKey: String, idLabel: String, author: String) {
idKey: String, idLabel: String, author: String) = {
DB.withConnection { implicit c =>
SQL(
"""
Expand All @@ -44,10 +44,21 @@ object Finder {
"""
).on('scheme_id -> schemeId, 'format_id -> formatId, 'description -> description, 'cardinality -> cardinality,
'idKey -> idKey, 'idLabel -> idLabel, 'author -> author, 'created -> new Date)
.executeUpdate()
.executeInsert()
}
}

def make(schemeId: Int, formatId: Int, description: String, cardinality: String,
idKey: String, idLabel: String, author: String): Finder = {
findById(create(schemeId, formatId, description, cardinality, idKey, idLabel, author).get.toInt).get
}

def findById(id: Int): Option[Finder] = {
DB.withConnection { implicit c =>
SQL("select * from finder where id = {id}").on('id -> id).as(finder.singleOpt)
}
}

def findByScheme(schemeId: Int): List[Finder] = {
DB.withConnection { implicit c =>
SQL("select * from finder where scheme_id = {scheme_id}").on('scheme_id -> schemeId).as(finder *)
Expand Down
6 changes: 5 additions & 1 deletion app/models/Interest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object Interest {
"""
select * from interest
where scheme_id = {scheme_id}
order by name
order by id
limit 10 offset {offset}
"""
).on('scheme_id -> schemeId, 'offset -> offset).as(interest *)
Expand All @@ -85,4 +85,8 @@ object Interest {
).on('subscriber_id -> subscriberId, 'scheme_id -> schemeId, 'action -> action, 'created -> created).executeInsert()
}
}

def make(subscriberId: Int, schemeId: Int, action: String): Interest = {
findById(create(subscriberId, schemeId, action).get.toInt).get
}
}
10 changes: 7 additions & 3 deletions app/models/ResourceMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ case class ResourceMap(id: Int, tag: String, description: String, swordUrl: Opti

def schemes: List[Scheme] = {
DB.withConnection { implicit c =>
SQL("select scheme.* from scheme, resource_map_scheme, resource_map where scheme.id = resource_map_scheme.scheme_id and resource_map_scheme.resource_map_id = resource_map.id and resource_map.id = {resmap_id}")
SQL("select distinct scheme.id, scheme.* from scheme, resource_map_scheme, resource_map where scheme.id = resource_map_scheme.scheme_id and resource_map_scheme.resource_map_id = resource_map.id and resource_map.id = {resmap_id}")
.on('resmap_id -> id).as(Scheme.scheme *)
}
}
Expand All @@ -58,13 +58,17 @@ object ResourceMap {
}
}

def create(tag: String, description: String, swordUrl: Option[String]) {
def create(tag: String, description: String, swordUrl: Option[String]) = {
DB.withConnection { implicit c =>
SQL("insert into resource_map (tag, description, sword_url) values ({tag}, {description}, {swordUrl})")
.on('tag -> tag, 'description -> description, 'swordUrl -> swordUrl).executeUpdate()
.on('tag -> tag, 'description -> description, 'swordUrl -> swordUrl).executeInsert()
}
}

def make(tag: String, description: String, swordUrl: Option[String]): ResourceMap = {
findById(create(tag, description, swordUrl).get.toInt).get
}

def all: List[ResourceMap] = {
DB.withConnection { implicit c =>
SQL("select * from resource_map").as(resmap *)
Expand Down
8 changes: 6 additions & 2 deletions app/models/Scheme.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ object Scheme {
}
}

def create(tag: String, gentype: String, category: String, description: String, link: Option[String], logo: Option[String]) {
def create(tag: String, gentype: String, category: String, description: String, link: Option[String], logo: Option[String]) = {
DB.withConnection { implicit c =>
SQL("insert into scheme (tag, gentype, category, description, link, logo, created) values ({tag}, {gentype}, {category}, {description}, {link}, {logo}, {created})")
.on('tag -> tag, 'gentype -> gentype, 'category -> category, 'description -> description, 'link -> link, 'logo -> logo, 'created -> new Date).executeUpdate()
.on('tag -> tag, 'gentype -> gentype, 'category -> category, 'description -> description, 'link -> link, 'logo -> logo, 'created -> new Date).executeInsert()
}
}

def make(tag: String, gentype: String, category: String, description: String, link: Option[String], logo: Option[String]): Scheme = {
findById(create(tag, gentype, category, description, link, logo).get.toInt).get
}

def all: List[Scheme] = {
DB.withConnection { implicit c =>
SQL("select * from scheme").as(scheme *)
Expand Down
8 changes: 5 additions & 3 deletions app/models/Subscription.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ object Subscription {
}
}

// todo: maybe a better name would be schemeTopicCount? The current name makes me expect a count
// of schemes... which would always be 1 as we pass in the Scheme to count.
def schemeCount(subscriberId: Int, schemeId: Int) = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from subscription, topic where subscription.subscriber_id = {sub_id} and subscription.topic_id = topic.id and topic.scheme_id = {sch_id}")
Expand All @@ -111,9 +113,9 @@ object Subscription {
}

def create(subscriberId: Int, topicId: Int, action: String, earliest: Date, latest: Date) = {
val created = new Date
val updated = created
val cancelled = created
val created = new Date // todo: should db defaults should handle this?
val updated = created // todo: should db defaults should handle this?
val cancelled = created // todo: null might be better?
DB.withConnection { implicit c =>
SQL("insert into subscription (subscriber_id, topic_id, action, created, updated, cancelled, earliest, latest, active) values ({subscriber_id}, {topic_id}, {action}, {created}, {updated}, {cancelled}, {earliest}, {latest}, {active})")
.on('subscriber_id -> subscriberId, 'topic_id -> topicId, 'action -> action, 'created -> created, 'updated -> updated, 'cancelled -> cancelled, 'earliest -> earliest, 'latest -> latest, 'active -> true).executeInsert()
Expand Down
16 changes: 8 additions & 8 deletions app/models/Topic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ case class Topic(id: Int, scheme_id: Int, tag: String, name: String,
select item.* from item, item_topic
where item.id = item_topic.item_id and
item_topic.topic_id = {topic_id} and
item.created > {created}
item.created >= {created}
order by item.created asc
"""
).on('topic_id -> id, 'created -> start).as(Item.item *)
}
}

def pagedItems(page: Int, max: Int) = {
val offset = page * max
def pagedItems(page: Int, perPage: Int) = {
val offset = page * perPage
DB.withConnection { implicit c =>
SQL(
"""
select item.* from item, item_topic
where item.id = item_topic.item_id and item_topic.topic_id = {topic_id}
order by item.created desc
limit {max} offset {offset}
limit {perPage} offset {offset}
"""
).on('topic_id -> id, 'max -> max, 'offset -> offset).as(Item.item *)
).on('topic_id -> id, 'perPage -> perPage, 'offset -> offset).as(Item.item *)
}
}

Expand All @@ -71,7 +71,7 @@ case class Topic(id: Int, scheme_id: Int, tag: String, name: String,

def itemCountSince(start: Date) = {
DB.withConnection { implicit c =>
val count = SQL("select count(*) as c from item_topic where topic_id = {id} and item_created > {created}")
val count = SQL("select count(*) as c from item_topic where topic_id = {id} and item_created >= {created}")
.on('id -> id, 'created -> start).apply.head
count[Long]("c")
}
Expand Down Expand Up @@ -158,9 +158,9 @@ object Topic {
}
}

def deleteUnlinkedBefore(date: Date) = {
def deleteUnlinkedBefore(date: Date) {
DB.withConnection { implicit c =>
SQL("delete from topic where created < {created}").on('created -> date)
SQL("delete from topic where created < {created}").on('created -> date).executeUpdate
}
}
}
8 changes: 6 additions & 2 deletions app/models/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ object User {
}
}

def create(name: String, email: String, password: String, role: String) {
def create(name: String, email: String, password: String, role: String) = {
DB.withConnection { implicit c =>
SQL("insert into hub_user (name, email, password, role, created, accessed) values ({name}, {email}, {password}, {role}, {created}, {accessed})")
.on('name -> name, 'email -> email, 'password -> password, 'role -> role, 'created -> new Date, 'accessed -> new Date).executeUpdate()
.on('name -> name, 'email -> email, 'password -> password, 'role -> role, 'created -> new Date, 'accessed -> new Date).executeInsert()
}
}

def make(name: String, email: String, password: String, role: String): User = {
findById(create(name, email, password, role).get.toInt).get
}

}
80 changes: 80 additions & 0 deletions test/unit/ChannelSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import org.specs2.mutable._

import play.api.test._
import play.api.test.Helpers._
import models.Channel
import models.Subscriber
import models.User

class ChannelSpec extends Specification {

"Channel model" should {

"#findById" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Subscriber.all must haveSize(0)
User.create("bob", "bob@example.com", "pwd", "role1")
Subscriber.create(1, "Sub Name", "cat", "contact", Some("link"), Some("logo"))

Channel.findById(1) must equalTo(None)
Channel.create(Subscriber.findById(1).get.id, "protocol", "mode", "description", "userid", "password", "http://example.com")
Channel.findById(1).get.protocol must equalTo("protocol")
}
}

"#create" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Subscriber.all must haveSize(0)
User.create("bob", "bob@example.com", "pwd", "role1")
Subscriber.create(1, "Sub Name", "cat", "contact", Some("link"), Some("logo"))

Channel.findById(1) must equalTo(None)
Channel.create(Subscriber.findById(1).get.id, "protocol", "mode", "description", "userid", "password", "http://example.com")
Channel.findById(1).get.protocol must equalTo("protocol")
}
}

"#make" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Subscriber.all must haveSize(0)
User.create("bob", "bob@example.com", "pwd", "role1")
Subscriber.create(1, "Sub Name", "cat", "contact", Some("link"), Some("logo"))

Channel.findById(1) must equalTo(None)
val c = Channel.make(Subscriber.findById(1).get.id, "protocol", "mode", "description", "userid", "password", "http://example.com")
c.protocol must equalTo("protocol")
}
}

"#delete" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Subscriber.all must haveSize(0)
User.create("bob", "bob@example.com", "pwd", "role1")
Subscriber.create(1, "Sub Name", "cat", "contact", Some("link"), Some("logo"))

Channel.findById(1) must equalTo(None)
val c = Channel.make(Subscriber.findById(1).get.id, "protocol", "mode", "description", "userid", "password", "http://example.com")
Channel.findById(1) must not equalTo(None)
Channel.delete(c.id)
Channel.findById(1) must equalTo(None)
}
}

"#recordTransfer" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
Subscriber.all must haveSize(0)
User.create("bob", "bob@example.com", "pwd", "role1")
Subscriber.create(1, "Sub Name", "cat", "contact", Some("link"), Some("logo"))

Channel.findById(1) must equalTo(None)
var c = Channel.make(Subscriber.findById(1).get.id, "protocol", "mode", "description", "userid", "password", "http://example.com")
c.transfers must equalTo(0)
c.recordTransfer

//need to reload to get new value
c = Channel.findById(1).get
c.transfers must equalTo(1)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class ContentFormatSpec extends Specification {
}
}

"#make" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
ContentFormat.all must haveSize(0)
val cf = ContentFormat.make("tag", "label", "desc", "http://www.example.com", "mimetype", Some("logo"))
ContentFormat.all must haveSize(1)
cf.tag must equalTo("tag")
}
}

"#all" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
ContentFormat.all must haveSize(0)
Expand Down
Loading

0 comments on commit d90f4cf

Please sign in to comment.