Created
November 12, 2013 12:29
-
-
Save Hayena/7430038 to your computer and use it in GitHub Desktop.
Raik usecase test
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 com.scalapenos.riak._ | |
import scala.concurrent.Future | |
import akka.actor._ | |
import concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
object Database { | |
val bucket = RiakClient("http://127.0.0.1:8098").bucket("service") | |
def storeUser(user: User): Future[User] = { | |
bucket.storeAndFetch(user.id.toString, user) | |
.map(_.as[User]) | |
} | |
def fetchUserById(id: Int): Future[User] = { | |
bucket.fetch(id.toString) | |
.map(_.get.as[User]) | |
} | |
def fetchByLocation( | |
maxLat: String, | |
maxLon: String, | |
minLat: String, | |
minLon: String | |
): Future[List[User]] = { | |
bucket.fetch("latitude", minLat, maxLat) | |
.map(_.map(value => value.as[User])) | |
} | |
} |
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 models._ | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
import scala.util.Success | |
import org.specs2.mutable._ | |
import org.specs2.time.NoTimeConversions | |
class FunctionTest extends Specification with NoTimeConversions{ | |
val user = User(2, 5.1, 54.3, 1563453134) | |
val user2 = User(2, 5.2, 54.2, 1563453157) | |
"A User" should { | |
"be created" in { | |
val futureUser = Database.storeUser(user) | |
val result = Await.ready(futureUser, 5.seconds) | |
result.value must beLike { | |
case Some(Success(storedUser)) => storedUser === user | |
} | |
} | |
"be updated" in { | |
val futureUser = Database.storeUser(user2) | |
val result = Await.ready(futureUser, 5.seconds) | |
result.value must beLike { | |
case Some(Success(storedUser)) => storedUser === user2 | |
} | |
} | |
"be read" in { | |
val futureUser = Database.fetchUserById(user.id) | |
val result = Await.ready(futureUser, 5.seconds) | |
result.value must beLike { | |
case Some(Success(storedUser)) => storedUser === user2 | |
} | |
} | |
"be found on location" in { | |
val futureUsers = Database.fetchByLocation("5.4", "54.5", "5.0", "54.0") | |
val result = Await.ready(futureUsers, 5.seconds) | |
result.value must beLike { | |
case Some(Success(users)) => users === Seq(user) | |
} | |
} | |
} | |
} |
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 scala.xml._ | |
import com.scalapenos.riak._ | |
import com.scalapenos.riak.MediaTypes._ | |
import spray.json.DefaultJsonProtocol | |
case class User ( | |
id: Int, | |
latitude: Double, | |
longitude: Double, | |
time: Int | |
) | |
object User extends DefaultJsonProtocol { | |
implicit val jsonFormat = jsonFormat4(User.apply) | |
implicit object indexer extends RiakIndexer[User] { | |
def index(user: User) = Set(RiakIndex("latitude", user.latitude.toString), | |
RiakIndex("longitude", user.longitude.toString), | |
RiakIndex("time", user.time)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment