|
1 | 1 | import XCTest |
2 | | -import MapboxGeocoder |
| 2 | +import Nocilla |
3 | 3 | import CoreLocation |
4 | | -import OHHTTPStubs |
| 4 | +@testable import MapboxGeocoder |
5 | 5 |
|
6 | 6 | class ReverseGeocodingTests: XCTestCase { |
7 | 7 |
|
8 | | - let accessToken = "pk.eyJ1IjoianVzdGluIiwiYSI6IlpDbUJLSUEifQ.4mG8vhelFMju6HpIY-Hi5A" |
9 | | - |
| 8 | + override func setUp() { |
| 9 | + super.setUp() |
| 10 | + LSNocilla.sharedInstance().start() |
| 11 | + } |
| 12 | + |
10 | 13 | override func tearDown() { |
11 | | - super.tearDown() |
12 | | - OHHTTPStubs.removeAllStubs() |
| 14 | + LSNocilla.sharedInstance().clearStubs() |
| 15 | + LSNocilla.sharedInstance().stop() |
| 16 | + super.setUp() |
13 | 17 | } |
14 | 18 |
|
15 | 19 | func testValidReverseGeocode() { |
16 | | - let resultsExpectation = expectationWithDescription("reverse geocode should return results") |
17 | | - let descriptionExpectation = expectationWithDescription("reverse geocode should populate description") |
18 | | - let nameExpectation = expectationWithDescription("reverse geocode should populate name") |
19 | | - let locationExpectation = expectationWithDescription("reverse geocode should populate location") |
20 | | - let scopeExpectation = expectationWithDescription("reverse geocode should populate scope") |
21 | | - let countryCodeExpectation = expectationWithDescription("reverse geocode should populate ISO country code") |
22 | | - let countryExpectation = expectationWithDescription("reverse geocode should populate country") |
23 | | - let postalCodeExpectation = expectationWithDescription("reverse geocode should populate postal code") |
24 | | - let administrativeAreaExpectation = expectationWithDescription("reverse geocode should populate administrative area") |
25 | | - let subAdministrativeAreaExpectation = expectationWithDescription("reverse geocode should populate sub-administrative area") |
26 | | - let localityExpectation = expectationWithDescription("reverse geocode should populate locality") |
27 | | - let thoroughfareExpectation = expectationWithDescription("reverse geocode should populate thoroughfare") |
28 | | - let subThoroughfareExpectation = expectationWithDescription("reverse geocode should populate sub-thoroughfare") |
29 | | - let regionExpectation = expectationWithDescription("reverse geocode should populate region") |
30 | | - let addressStreetExpectation = expectationWithDescription("reverse geocode should populate street in address dictionary") |
31 | | - let addressCityExpectation = expectationWithDescription("reverse geocode should populate city in address dictionary") |
32 | | - let addressStateExpectation = expectationWithDescription("reverse geocode should populate state in address dictionary") |
33 | | - let addressCountryExpectation = expectationWithDescription("reverse geocode should populate country in address dictionary") |
34 | | - let addressISOCountryCodeExpectation = expectationWithDescription("reverse geocode should populate ISO country code in address dictionary") |
| 20 | + let expectation = expectationWithDescription("reverse geocode should return results") |
35 | 21 |
|
36 | | - stub(isHost("api.mapbox.com")) { _ in |
37 | | - let path = NSBundle(forClass: self.dynamicType).pathForResource("reverse_valid", ofType: "json") |
38 | | - return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) |
39 | | - } |
| 22 | + let json = Fixture.stringFromFileNamed("reverse_valid") |
| 23 | + stubRequest("GET", "https://api.mapbox.com/geocoding/v5/mapbox.places/-95.78558,37.13284.json?access_token=\(BogusToken)").andReturn(200).withHeaders(["Content-Type": "application/json"]).withBody(json) |
40 | 24 |
|
41 | | - let geocoder = MBGeocoder(accessToken: accessToken) |
| 25 | + let geocoder = MBGeocoder(accessToken: BogusToken) |
| 26 | + var addressPlacemark: MBPlacemark! = nil |
| 27 | + var placePlacemark: MBPlacemark! = nil |
42 | 28 | geocoder.reverseGeocodeLocation( |
43 | 29 | CLLocation(latitude: 37.13284000, longitude: -95.78558000)) { (placemarks, error) in |
44 | | - if let result = placemarks?.first where placemarks?.count > 0 { |
45 | | - resultsExpectation.fulfill() |
46 | | - if result.description == "3099 3100 Rd, Independence, Kansas 67301, United States" { |
47 | | - descriptionExpectation.fulfill() |
48 | | - } |
49 | | - if result.name == "3099 3100 Rd" { |
50 | | - nameExpectation.fulfill() |
51 | | - } |
52 | | - if let location = result.location where location.coordinate.latitude == 37.12787 && |
53 | | - location.coordinate.longitude == -95.783074 { |
54 | | - locationExpectation.fulfill() |
55 | | - } |
56 | | - if result.scope == .Address { |
57 | | - scopeExpectation.fulfill() |
58 | | - } |
59 | | - if result.ISOcountryCode == "US" { |
60 | | - countryCodeExpectation.fulfill() |
61 | | - } |
62 | | - if result.country == "United States" { |
63 | | - countryExpectation.fulfill() |
64 | | - } |
65 | | - if result.postalCode == "67301" { |
66 | | - postalCodeExpectation.fulfill() |
67 | | - } |
68 | | - if result.administrativeArea == "Kansas" { |
69 | | - administrativeAreaExpectation.fulfill() |
70 | | - } |
71 | | - if result.subAdministrativeArea == "Independence" { |
72 | | - subAdministrativeAreaExpectation.fulfill() |
73 | | - } |
74 | | - if result.locality == "Independence" { |
75 | | - localityExpectation.fulfill() |
76 | | - } |
77 | | - if result.thoroughfare == "3100 Rd" { |
78 | | - thoroughfareExpectation.fulfill() |
79 | | - } |
80 | | - if result.subThoroughfare == "3099" { |
81 | | - subThoroughfareExpectation.fulfill() |
82 | | - } |
83 | | - let southWest = CLLocationCoordinate2D(latitude: 37.109405, longitude: -95.783365) |
84 | | - let northEast = CLLocationCoordinate2D(latitude: 37.208643, longitude: -95.781811) |
85 | | - if result.region == MBRectangularRegion(southWest: southWest, northEast: northEast) { |
86 | | - regionExpectation.fulfill() |
87 | | - } |
88 | | - if let street = result.addressDictionary?[MBPostalAddressStreetKey] as? String where street == "3099 3100 Rd" { |
89 | | - addressStreetExpectation.fulfill() |
90 | | - } |
91 | | - if let city = result.addressDictionary?[MBPostalAddressCityKey] as? String where city == "Independence" { |
92 | | - addressCityExpectation.fulfill() |
93 | | - } |
94 | | - if let state = result.addressDictionary?[MBPostalAddressStateKey] as? String where state == "Kansas" { |
95 | | - addressStateExpectation.fulfill() |
96 | | - } |
97 | | - if let country = result.addressDictionary?[MBPostalAddressCountryKey] as? String where country == "United States" { |
98 | | - addressCountryExpectation.fulfill() |
99 | | - } |
100 | | - if let countryCode = result.addressDictionary?[MBPostalAddressISOCountryCodeKey] as? String where countryCode == "US" { |
101 | | - addressISOCountryCodeExpectation.fulfill() |
102 | | - } |
103 | | - } |
| 30 | + XCTAssertEqual(placemarks?.count, 5, "reverse geocode should have 5 results") |
| 31 | + addressPlacemark = placemarks![0] |
| 32 | + placePlacemark = placemarks![1] |
| 33 | + |
| 34 | + expectation.fulfill() |
104 | 35 | } |
105 | 36 |
|
106 | 37 | waitForExpectationsWithTimeout(1) { (error) in |
107 | 38 | XCTAssertNil(error, "Error: \(error)") |
108 | 39 | XCTAssertFalse(geocoder.geocoding) |
109 | 40 | } |
| 41 | + |
| 42 | + XCTAssertEqual(addressPlacemark.description, "3099 3100 Rd, Independence, Kansas 67301, United States", "reverse geocode should populate description") |
| 43 | + XCTAssertEqual(addressPlacemark.name, "3099 3100 Rd", "reverse geocode should populate name") |
| 44 | + XCTAssertEqual(addressPlacemark.location?.coordinate, CLLocationCoordinate2D(latitude: 37.12787, longitude: -95.783074), "reverse geocode should populate location") |
| 45 | + XCTAssertEqual(addressPlacemark.scope, .Address, "reverse geocode should populate scope") |
| 46 | + XCTAssertEqual(addressPlacemark.ISOcountryCode, "US", "reverse geocode should populate ISO country code") |
| 47 | + XCTAssertEqual(addressPlacemark.country, "United States", "reverse geocode should populate country") |
| 48 | + XCTAssertEqual(addressPlacemark.postalCode, "67301", "reverse geocode should populate postal code") |
| 49 | + XCTAssertEqual(addressPlacemark.administrativeArea, "Kansas", "reverse geocode should populate administrative area") |
| 50 | + XCTAssertEqual(addressPlacemark.subAdministrativeArea, "Independence", "reverse geocode should populate sub-administrative area") |
| 51 | + XCTAssertEqual(addressPlacemark.locality, "Independence", "reverse geocode should populate locality") |
| 52 | + XCTAssertEqual(addressPlacemark.thoroughfare, "3100 Rd", "reverse geocode should populate thoroughfare") |
| 53 | + XCTAssertEqual(addressPlacemark.subThoroughfare, "3099", "reverse geocode should populate sub-thoroughfare") |
| 54 | + |
| 55 | + XCTAssertNotNil(addressPlacemark.addressDictionary) |
| 56 | + let addressDictionary = addressPlacemark.addressDictionary! |
| 57 | + XCTAssertEqual(addressDictionary[MBPostalAddressStreetKey] as? String, "3099 3100 Rd", "reverse geocode should populate street in address dictionary") |
| 58 | + XCTAssertEqual(addressDictionary[MBPostalAddressCityKey] as? String, "Independence", "reverse geocode should populate city in address dictionary") |
| 59 | + XCTAssertEqual(addressDictionary[MBPostalAddressStateKey] as? String, "Kansas", "reverse geocode should populate state in address dictionary") |
| 60 | + XCTAssertEqual(addressDictionary[MBPostalAddressCountryKey] as? String, "United States", "reverse geocode should populate country in address dictionary") |
| 61 | + XCTAssertEqual(addressDictionary[MBPostalAddressISOCountryCodeKey] as? String, "US", "reverse geocode should populate ISO country code in address dictionary") |
| 62 | + |
| 63 | + let southWest = CLLocationCoordinate2D(latitude: 37.033229992893, longitude: -95.927990005645) |
| 64 | + let northEast = CLLocationCoordinate2D(latitude: 37.35632800706, longitude: -95.594628992671) |
| 65 | + let region = placePlacemark.region |
| 66 | + XCTAssertNotNil(region, "reverse geocode should populate region") |
| 67 | + XCTAssertEqualWithAccuracy(region!.southWest.latitude, southWest.latitude, accuracy: 0.000000000001) |
| 68 | + XCTAssertEqualWithAccuracy(region!.southWest.longitude, southWest.longitude, accuracy: 0.000000000001) |
| 69 | + XCTAssertEqualWithAccuracy(region!.northEast.latitude, northEast.latitude, accuracy: 0.000000000001) |
| 70 | + XCTAssertEqualWithAccuracy(region!.northEast.longitude, northEast.longitude, accuracy: 0.000000000001) |
110 | 71 | } |
111 | 72 |
|
112 | 73 | func testInvalidReverseGeocode() { |
113 | | - let resultsExpection = expectationWithDescription("reverse geocode should return no results for invalid query") |
114 | | - |
115 | | - stub(isHost("api.mapbox.com")) { _ in |
116 | | - let path = NSBundle(forClass: self.dynamicType).pathForResource("reverse_invalid", ofType: "json") |
117 | | - return OHHTTPStubsResponse(fileAtPath: path!, statusCode: 200, headers: nil) |
118 | | - } |
119 | | - |
120 | | - let geocoder = MBGeocoder(accessToken: accessToken) |
| 74 | + let json = Fixture.stringFromFileNamed("reverse_invalid") |
| 75 | + stubRequest("GET", "https://api.mapbox.com/geocoding/v5/mapbox.places/0.00000,0.00000.json?access_token=\(BogusToken)").andReturn(200).withHeaders(["Content-Type": "application/json"]).withBody(json) |
| 76 | + |
| 77 | + let expection = expectationWithDescription("reverse geocode execute completion handler for invalid query") |
| 78 | + let geocoder = MBGeocoder(accessToken: BogusToken) |
121 | 79 | geocoder.reverseGeocodeLocation(CLLocation(latitude: 0, longitude: 0)) { (placemarks, error) in |
122 | | - if placemarks?.count == 0 { |
123 | | - resultsExpection.fulfill() |
124 | | - } |
| 80 | + XCTAssertEqual(placemarks?.count, 0, "reverse geocode should return no results for invalid query") |
| 81 | + expection.fulfill() |
125 | 82 | } |
126 | 83 |
|
127 | 84 | waitForExpectationsWithTimeout(1) { (error) in |
|
0 commit comments