Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 101 lines (88 sloc) 3.596 kb
df06b4d9 »
2010-01-20 - Adjust package declaration to work with both scala 2.7.x and scala …
1 /*
2 * Copyright 2009-2010 WorldWide Conferencing, LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
a5b2982d »
2011-01-15 Double can be coerced to Int or Long
17 package net.liftweb
18 package json
4d2fd9ec »
2009-09-16 Add some query examples.
19
ae00042d »
2010-12-29 Scala 2.8 cleanups
20 import org.specs.Specification
21 import org.specs.runner.{Runner, JUnit}
4d2fd9ec »
2009-09-16 Add some query examples.
22
23 class QueryExamplesTest extends Runner(QueryExamples) with JUnit
24 object QueryExamples extends Specification {
25 "List of IPs" in {
26 val ips = for { JString(ip) <- json \\ "ip" } yield ip
27 ips mustEqual List("192.168.1.125", "192.168.1.126", "192.168.1.127", "192.168.2.125", "192.168.2.126")
28 }
29
d80dc07e »
2009-10-29 Add example which converts query results to XML.
30 "List of IPs converted to XML" in {
31 val ips = <ips>{ for { JString(ip) <- json \\ "ip" } yield <ip>{ ip }</ip> }</ips>
32 ips mustEqual <ips><ip>192.168.1.125</ip><ip>192.168.1.126</ip><ip>192.168.1.127</ip><ip>192.168.2.125</ip><ip>192.168.2.126</ip></ips>
33 }
34
4d2fd9ec »
2009-09-16 Add some query examples.
35 "List of IPs in cluster2" in {
36 val ips = for {
37 cluster @ JObject(x) <- json \ "data_center"
38 if (x contains JField("name", JString("cluster2")))
39 JString(ip) <- cluster \\ "ip" } yield ip
40 ips mustEqual List("192.168.2.125", "192.168.2.126")
41 }
42
43 "Total cpus in data center" in {
44 (for { JInt(x) <- json \\ "cpus" } yield x) reduceLeft (_ + _) mustEqual 40
45 }
46
47 "Servers sorted by uptime" in {
48 case class Server(ip: String, uptime: Long)
49
50 val servers = for {
51 JArray(servers) <- json \\ "servers"
52 JObject(server) <- servers
53 JField("ip", JString(ip)) <- server
54 JField("uptime", JInt(uptime)) <- server
55 } yield Server(ip, uptime.longValue)
56
ae00042d »
2010-12-29 Scala 2.8 cleanups
57 servers sortWith (_.uptime > _.uptime) mustEqual List(Server("192.168.1.127", 901214), Server("192.168.2.125", 453423), Server("192.168.2.126", 214312), Server("192.168.1.126", 189822), Server("192.168.1.125", 150123))
4d2fd9ec »
2009-09-16 Add some query examples.
58 }
59
60 "Clusters administered by liza" in {
61 val clusters = for {
62 JObject(cluster) <- json
63 JField("admins", JArray(admins)) <- cluster
64 if admins contains JString("liza")
65 JField("name", JString(name)) <- cluster
66 } yield name
67
68 clusters mustEqual List("cluster2")
69 }
70
71 val json = parse("""
72 { "data_center": [
73 {
74 "name": "cluster1",
75 "servers": [
76 {"ip": "192.168.1.125", "uptime": 150123, "specs": {"cpus": 8, "ram": 2048}},
77 {"ip": "192.168.1.126", "uptime": 189822, "specs": {"cpus": 16, "ram": 4096}},
78 {"ip": "192.168.1.127", "uptime": 901214, "specs": {"cpus": 8, "ram": 4096}}
79 ],
80 "links": [
81 {"href": "http://www.example.com/admin", "name": "admin"},
82 {"href": "http://www,example.com/home", "name": "home"}
83 ],
84 "admins": ["jim12", "joe", "maddog"]
85 },
86 {
87 "name": "cluster2",
88 "servers": [
89 {"ip": "192.168.2.125", "uptime": 453423, "specs": {"cpus": 4, "ram": 2048}},
90 {"ip": "192.168.2.126", "uptime": 214312, "specs": {"cpus": 4, "ram": 2048}},
91 ],
92 "links": [
93 {"href": "http://www.example2.com/admin", "name": "admin"},
94 {"href": "http://www,example2.com/home", "name": "home"}
95 ],
96 "admins": ["joe", "liza"]
97 }
98 ]}
99 """)
100 }
Something went wrong with that request. Please try again.