My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package couch.db

import org.scalatest.Spec
import org.scalatest.BeforeAndAfter
import org.scalatest.FeatureSuite
import org.scalatest.matchers.ShouldMatchers

import dispatch.json._
import dispatch.json.Js._
import couch.json._
import couch.db.Options._

class ScalaViewServerSpec extends Spec with ShouldMatchers with BeforeAndAfter {
val couch = Couch("127.0.0.1")
val test = Db("test_db")

override def beforeAll {
couch(test.create)
println(couch(test as_str))
println("** created database")
}

override def afterAll {
couch(test.delete)
println("** destroyed database")
}

describe("create records") {
it("should create new records") {
couch(test doc Js("""{"item":"banana","prices":{"Fresh Mart":1.99,"Price Max":0.79,"Banana Montana":4.22}}"""))
couch(test doc Js("""{"item":"apple","prices":{"Fresh Mart":1.59,"Price Max":5.99,"Apples Express":0.79}}"""))
couch(test doc Js("""{"item":"orange","prices":{"Fresh Mart":1.99,"Price Max":3.19,"Citrus Circus":1.09}}"""))
}
}

describe("Create a design document, for scala view") {
val d = DesignDocument("power", null, Map[String, View]())
d.language = "scala"
val mapfn1 = """(doc: dispatch.json.JsValue) => {
val it = couch.json.JsBean.toBean(doc, classOf[couch.json.TestBeans.Item_1])._3;
for (st <- it.prices)
yield(List(it.item, st._2))
}"""

val mapfn2 = """(doc: dispatch.json.JsValue) => {
import dispatch.json.Js._;
val x = Symbol("item") ? dispatch.json.Js.str;
val x(x_) = doc;
val i = Symbol("_id") ? dispatch.json.Js.str;
val i(i_) = doc;
List(List(i_, x_)) ;
}"""

val vi_1 = new View(mapfn1, null)
val vi_2 = new View(mapfn2, null)

var revision: String = null
var nir: (String, String) = null
val de = Doc(test, d._id)

it("creation should be successful") {
d._id should equal("_design/power")
couch(de add d)
val ir = couch(de ># %(Id._id, Id._rev))
ir._1 should equal(d._id)
}
it("query by id should fetch the document") {
val ir = couch(test by_id d._id)
ir._1 should equal(d._id)
val sh = couch(test by_id(d._id, classOf[DesignDocument]))
sh._1 should equal(d._id)
sh._2 should equal(ir._2)
sh._3._id should equal(sh._1)
sh._3._rev should equal(sh._2)
revision = sh._2
}
it("update the document with 2 views") {
val doc = DesignDocument(d._id, revision, Map("power_lunch" -> vi_1, "mega_lunch" -> vi_2))
doc.language = "scala"
couch(de update(doc, revision))
nir = couch(de ># %(Id._id, Id._rev))
nir._1 should equal(d._id)
nir._2 should not equal(revision)
}
it("should fetch 9 rows from view power_lunch") {
val ls1 = couch(test view(
Views builder("power/power_lunch") build))
println(ls1)
ls1.size should equal(9)
}
it("should fetch 3 rows from view mega_lunch") {
val ls1 = couch(test view(
Views builder("power/mega_lunch") build))
println(ls1)
ls1.size should equal(3)
}
}

describe("Create a design document, for scala view with map and reduce") {
val d = DesignDocument("big", null, Map[String, View]())
d.language = "scala"
val mapfn1 = """(doc: dispatch.json.JsValue) => {
val it = couch.json.JsBean.toBean(doc, classOf[couch.json.TestBeans.Item_1])._3;
for (st <- it.prices)
yield(List(it.item, st._2))
}"""

val redfn1 = """(key: List[(String, String)], values: List[dispatch.json.JsNumber], rereduce: Boolean) => {
values.foldLeft(BigDecimal(0.00))((s, f) => s + (f match { case dispatch.json.JsNumber(n) => n }))
}"""

val vi_1 = new View(mapfn1, redfn1)

var revision: String = null
var nir: (String, String) = null
val de = Doc(test, d._id)

it("creation should be successful") {
d._id should equal("_design/big")
couch(de add d)
val ir = couch(de ># %(Id._id, Id._rev))
ir._1 should equal(d._id)
}
it("query by id should fetch the document") {
val ir = couch(test by_id d._id)
ir._1 should equal(d._id)
val sh = couch(test by_id(d._id, classOf[DesignDocument]))
sh._1 should equal(d._id)
sh._2 should equal(ir._2)
sh._3._id should equal(sh._1)
sh._3._rev should equal(sh._2)
revision = sh._2
}
it("update the document with the view") {
val doc = DesignDocument(d._id, revision, Map("big_lunch" -> vi_1))
doc.language = "scala"
couch(de update(doc, revision))
nir = couch(de ># %(Id._id, Id._rev))
nir._1 should equal(d._id)
nir._2 should not equal(revision)
}
it("should fetch 1 row from view big_lunch") {
val ls1 = couch(test view(
Views.builder("big/big_lunch")
.build))
println(ls1)
ls1.size should equal(1)
}
it("should fetch 3 rows from view big_lunch") {
val ls1 = couch(test view(
Views.builder("big/big_lunch")
.options(optionBuilder group(true) build)
.build))
println(ls1)
ls1.size should equal(3)
}
}
}

Change log

r16 by ghosh.debasish on Jun 7, 2009   Diff
ViewServer gets reduce.
Go to: 
Project members, sign in to write a code review

Older revisions

r13 by ghosh.debasish on May 17, 2009   Diff
commit for release 0.3
- bugs fixed
- view server added (map only)
All revisions of this file

File info

Size: 5334 bytes, 158 lines
Powered by Google Project Hosting