Created
October 4, 2010 01:23
-
-
Save nraychaudhuri/609118 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.specs._ | |
import Relation._ | |
class RelationalSpec extends Specification { | |
"Any Relation" should { | |
"have a restrict operator to filter tuples" in { | |
type PT = {def name: String; def state: String; def city: String; def zipCode: Int} | |
var relVar = relation[PT](new {def name = "Nilanjan"; def state = "OH"; def city = "Columbus"; def zipCode = 43230}, | |
new {def name = "Manisha"; def state = "OH"; def city = "Westerville"; def zipCode = 43333}) | |
relVar = relVar where { _.city == "Columbus" } | |
relVar.body must exist({ t => t.name == "Nilanjan" && t.state == "OH" && t.city == "Columbus" && t.zipCode == 43230}) | |
} | |
"have a project operator to create new relations " in { | |
var relVar = relation(PersonTuple("Nilanjan", "OH", "Columbus", 43230), PersonTuple("Manisha", "OH", "Westerville", 43333)) | |
var nameRelVar = relVar project { t: PersonTuple => NameTuple(t.name) } | |
nameRelVar.body must contain(NameTuple("Nilanjan")) | |
nameRelVar.body must contain(NameTuple("Manisha")) | |
} | |
"have a project operator to create same relation again " in { | |
var relVar = relation(PersonTuple("Nilanjan", "OH", "Columbus", 43230), PersonTuple("Manisha", "OH", "Westerville", 43333)) | |
var newRelVar = relVar project { t => t } | |
newRelVar.body must contain(PersonTuple("Nilanjan", "OH", "Columbus", 43230)) | |
newRelVar.body must contain(PersonTuple("Manisha", "OH", "Westerville", 43333)) | |
} | |
"have a project operator to create new relations with restriction " in { | |
var relVar = relation(PersonTuple("Nilanjan", "OH", "Columbus", 43230), PersonTuple("Manisha", "OH", "Westerville", 43333)) | |
var nameRelVar = relVar where { _.city == "Columbus" } project { t: PersonTuple => NameTuple(t.name) } | |
nameRelVar.body must contain(NameTuple("Nilanjan")) | |
nameRelVar.body must notContain(NameTuple("Manisha")) | |
} | |
} | |
} | |
case class NameTuple(name: String) | |
case class PersonTuple(name: String, state: String, city: String, zipCode: Int) | |
class Relation[H](val body: Seq[H]) { | |
def where(restrict : (H) => Boolean): Relation[H] = { | |
def matches = body filter restrict | |
relation(matches:_*) | |
} | |
def project[H1](f: (H) => H1): Relation[H1] = { | |
def projection = body map f | |
relation(projection:_*) | |
} | |
} | |
object Relation { | |
def relation[H](body: H*) = new Relation(body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment