Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add convenience methods for accessing response and checkin errors

  • Loading branch information...
commit 9757c44386ccb5b314f0130b2acc14319b348661 1 parent cc5ea54
@nfolkert-foursquare nfolkert-foursquare authored
View
72 src/main/scala/org/scalafoursquare/call/Caller.scala
@@ -67,16 +67,59 @@ class RawRequest(val app: App, val endpoint: String, val params: List[(String, S
class Request[T](app: App, endpoint: String, params: List[(String, String)] = Nil)(implicit mf: Manifest[T]) extends RawRequest(app, endpoint, params, "GET", None) {
lazy val (get, extractDuration) = app.convertSingle[T](getRaw)
def force = get.response.get // For testing
+
+ def fail = {
+ val result = this.get
+ if (result.meta.code == 200 || result.response.isDefined)
+ throw new Exception("Expected failure, but received: " + getRaw)
+ result.meta
+ }
+
+ def expect = {
+ val result = this.get
+ if (result.meta.code != 200 || result.response.isEmpty)
+ throw new Exception("Expected success, but received: " + getRaw)
+ result.response.get
+ }
}
class PostRequest[T](app: App, endpoint: String, params: List[(String, String)] = Nil)(implicit mf: Manifest[T]) extends RawRequest(app, endpoint, params, "POST", None) {
lazy val (get, extractionDuration): (Response[T], Long) = app.convertSingle[T](getRaw)
def force = get.response.get // For testing
+
+ def fail = {
+ val result = this.get
+ if (result.meta.code == 200 || result.response.isDefined)
+ throw new Exception("Expected failure, but received: " + getRaw)
+ result.meta
+ }
+
+ def expect = {
+ val result = this.get
+ if (result.meta.code != 200 || result.response.isEmpty)
+ throw new Exception("Expected success, but received: " + getRaw)
+ result.response.get
+ }
}
class PostDataRequest[T](app: App, endpoint: String, params: List[(String, String)]=Nil, postData: PostData)(implicit mf: Manifest[T]) extends RawRequest(app, endpoint, params, "POST", Some(postData)) {
lazy val (get, extractionDuration): (Response[T], Long) = app.convertSingle[T](getRaw)
def force = get.response.get // For testing
+
+
+ def fail = {
+ val result = this.get
+ if (result.meta.code == 200 || result.response.isDefined)
+ throw new Exception("Expected failure, but received: " + getRaw)
+ result.meta
+ }
+
+ def expect = {
+ val result = this.get
+ if (result.meta.code != 200 || result.response.isEmpty)
+ throw new Exception("Expected success, but received: " + getRaw)
+ result.response.get
+ }
}
class RawMultiRequest(app: App, reqA: Option[RawRequest], reqB: Option[RawRequest], reqC: Option[RawRequest],
@@ -99,6 +142,21 @@ class MultiRequest[A,B,C,D,E](app: App, reqA: Option[Request[A]], reqB: Option[R
def f3 = get.responses._3.get
def f4 = get.responses._4.get
def f5 = get.responses._5.get
+
+
+ def fail = {
+ val result = this.get
+ if (result.meta.code == 200 || result.responses.productIterator.exists(_.asInstanceOf[Option[_]].isDefined))
+ throw new Exception("Expected failure, but received: " + getRaw)
+ result.meta
+ }
+
+ def expect = {
+ val result = this.get
+ if (result.meta.code != 200 || result.responses.productIterator.forall(_.asInstanceOf[Option[_]].isEmpty))
+ throw new Exception("Expected success, but received: " + getRaw)
+ result.responses
+ }
}
class RawMultiRequestList(val app: App, val subreqs: List[RawRequest], val method: String="GET") {
@@ -113,6 +171,20 @@ class RawMultiRequestList(val app: App, val subreqs: List[RawRequest], val metho
class MultiRequestList[A](app: App, subreqs: List[Request[A]])(implicit mf: Manifest[A]) extends RawMultiRequestList(app, subreqs) {
lazy val (get, extractionDuration): (MultiResponseList[A], Long) = app.convertMultiList[A](getRaw)
def force = get.responses.get
+
+ def fail = {
+ val result = this.get
+ if (result.meta.code == 200 || result.responses.isDefined)
+ throw new Exception("Expected failure, but received: " + getRaw)
+ result.meta
+ }
+
+ def expect = {
+ val result = this.get
+ if (result.meta.code != 200 || result.responses.isEmpty)
+ throw new Exception("Expected success, but received: " + getRaw)
+ result.responses.get
+ }
}
abstract class Caller {
View
14 src/test/scala/org/scalafoursquare/CallTest.scala
@@ -27,10 +27,14 @@ class CallTest extends SpecsMatchers {
failVenue.meta.errorType must_== Some("param_error")
failVenue.response.isDefined must_== false
+ mockApp.venueDetail("missingId").fail must_== failVenue.meta
+
case class ErrorTest()
val failEndpoint = new Request[ErrorTest](mockApp, "/bad/endpoint/blargh").get
failEndpoint.meta must_== Meta(404, Some("other"), Some("Endpoint not found"))
+
+ new Request[ErrorTest](mockApp, "/bad/endpoint/blargh").fail must_== failEndpoint.meta
}
@Test
@@ -46,6 +50,8 @@ class CallTest extends SpecsMatchers {
mockVenue.response.get.venue.location.crossStreet must_== Some("At Fake Street")
mockVenue.response.get.venue.mayor.count must_== 15
mockVenue.response.get.venue.tags(0) must_== "a tag"
+
+ mockApp.venueDetail("someVenueId").expect must_== mockVenue.response.get
}
@Test
@@ -69,6 +75,8 @@ class CallTest extends SpecsMatchers {
mockSelf.response.get.user.following.get.count must_== 70
mockSelf.response.get.user.followers.isDefined must_== false
mockSelf.response.get.user.scores.checkinsCount must_== 30
+
+ mockUserApp.self.expect must_== mockSelf.response.get
}
@Test
@@ -85,6 +93,8 @@ class CallTest extends SpecsMatchers {
mockVenueCategories.response.get.categories(0).id must_== Some("fakeId")
mockVenueCategories.response.get.categories(0).icon.prefix must_== Some("noImage")
mockVenueCategories.response.get.categories(0).categories.isDefined must_== false
+
+ mockApp.venueCategories.expect must_== mockVenueCategories.response.get
}
@Test
@@ -117,6 +127,8 @@ class CallTest extends SpecsMatchers {
mockVenueCategories.response.get.categories(0).id must_== Some("fakeId")
mockVenueCategories.response.get.categories(0).icon.prefix must_== Some("noImage")
mockVenueCategories.response.get.categories(0).categories.isDefined must_== false
+
+ mockApp.multi(mockVenueReq, mockCategoryReq).expect must_== mockMulti.responses
}
@Test
@@ -147,5 +159,7 @@ class CallTest extends SpecsMatchers {
mockSelf.response.get.user.following.get.count must_== 70
mockSelf.response.get.user.followers.isDefined must_== false
mockSelf.response.get.user.scores.checkinsCount must_== 30
+
+ mockUserApp.multi(mockSelfReq, mockByIdReq).expect must_== mockMulti.responses
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.