123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- //
- // AlamofireObjectMapperTests.swift
- // AlamofireObjectMapperTests
- //
- // Created by Tristan Himmelman on 2015-04-30.
- //
- // The MIT License (MIT)
- //
- // Copyright (c) 2014-2015 Tristan Himmelman
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- import Foundation
- import XCTest
- import ObjectMapper
- import Alamofire
- import AlamofireObjectMapper
- class AlamofireObjectMapperTests: XCTestCase {
-
- override func setUp() {
- super.setUp()
- // Put setup code here. This method is called before the invocation of each test method in the class.
- }
-
- override func tearDown() {
- // Put teardown code here. This method is called after the invocation of each test method in the class.
- super.tearDown()
- }
-
- func testResponseObject() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
- let expectation = self.expectation(description: "\(URL)")
- _ = AF.request(URL, method: .get).responseObject { (response: AFDataResponse<WeatherResponse>) in
- expectation.fulfill()
-
- let mappedObject = response.value
-
- XCTAssertNotNil(mappedObject, "Response should not be nil")
- XCTAssertNotNil(mappedObject?.location, "Location should not be nil")
- XCTAssertNotNil(mappedObject?.threeDayForecast, "ThreeDayForcast should not be nil")
-
- for forecast in mappedObject!.threeDayForecast! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- func testResponseObjectMapToObject() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
- let expectation = self.expectation(description: "\(URL)")
-
- let weatherResponse = WeatherResponse()
- weatherResponse.date = Date()
-
- _ = AF.request(URL, method: .get).responseObject(mapToObject: weatherResponse) { (response: AFDataResponse<WeatherResponse>) in
- expectation.fulfill()
-
- let mappedObject = response.value
- print(weatherResponse)
- XCTAssertNotNil(mappedObject, "Response should not be nil")
- XCTAssertNotNil(mappedObject?.date, "Date should not be nil") // Date is not in JSON but should not be nil because we mapped onto an existing object with a date set
- XCTAssertNotNil(mappedObject?.location, "Location should not be nil")
- XCTAssertNotNil(mappedObject?.threeDayForecast, "ThreeDayForcast should not be nil")
-
- for forecast in mappedObject!.threeDayForecast! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- func testResponseObjectWithKeyPath() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/2ee8f34d21e8febfdefb2b3a403f18a43818d70a/sample_keypath_json"
- let expectation = self.expectation(description: "\(URL)")
-
- _ = AF.request(URL, method: .get).responseObject(keyPath: "data") { (response: AFDataResponse<WeatherResponse>) in
- expectation.fulfill()
-
- let mappedObject = response.value
-
- XCTAssertNotNil(mappedObject, "Response should not be nil")
- XCTAssertNotNil(mappedObject?.location, "Location should not be nil")
- XCTAssertNotNil(mappedObject?.threeDayForecast, "ThreeDayForcast should not be nil")
-
- for forecast in mappedObject!.threeDayForecast! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- func testResponseObjectWithNestedKeyPath() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/97231a04e6e4970612efcc0b7e0c125a83e3de6e/sample_keypath_json"
- let expectation = self.expectation(description: "\(URL)")
-
- _ = AF.request(URL, method: .get).responseObject(keyPath: "response.data") { (response: AFDataResponse<WeatherResponse>) in
- expectation.fulfill()
-
- let mappedObject = response.value
-
- XCTAssertNotNil(mappedObject, "Response should not be nil")
- XCTAssertNotNil(mappedObject?.location, "Location should not be nil")
- XCTAssertNotNil(mappedObject?.threeDayForecast, "ThreeDayForcast should not be nil")
-
- for forecast in mappedObject!.threeDayForecast! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
- func testResponseArray() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
- let expectation = self.expectation(description: "\(URL)")
- _ = AF.request(URL, method: .get).responseArray { (response: AFDataResponse<[Forecast]>) in
- expectation.fulfill()
-
- let mappedArray = response.value
-
- XCTAssertNotNil(mappedArray, "Response should not be nil")
-
- XCTAssertTrue(mappedArray?.count == 3, "Didn't parse correct amount of objects")
-
- for forecast in mappedArray! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- func testArrayResponseArrayWithKeyPath() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
- let expectation = self.expectation(description: "\(URL)")
-
- _ = AF.request(URL, method: .get).responseArray(keyPath: "three_day_forecast") { (response: AFDataResponse<[Forecast]>) in
-
- expectation.fulfill()
-
- let mappedArray = response.value
-
- XCTAssertNotNil(mappedArray, "Response should not be nil")
-
- for forecast in mappedArray! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- func testArrayResponseArrayWithNestedKeyPath() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/97231a04e6e4970612efcc0b7e0c125a83e3de6e/sample_keypath_json"
- let expectation = self.expectation(description: "\(URL)")
-
- _ = AF.request(URL, method: .get).responseArray(keyPath: "response.data.three_day_forecast") { (response: AFDataResponse<[Forecast]>) in
-
- expectation.fulfill()
-
- let mappedArray = response.value
-
- XCTAssertNotNil(mappedArray, "Response should not be nil")
-
- for forecast in mappedArray! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- // MARK: - Immutable Tests
-
- func testResponseImmutableObject() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json"
- let expectation = self.expectation(description: "\(URL)")
-
- _ = AF.request(URL, method: .get).responseObject { (response: AFDataResponse<WeatherResponseImmutable>) in
- expectation.fulfill()
-
- let mappedObject = response.value
-
- XCTAssertNotNil(mappedObject, "Response should not be nil")
- XCTAssertNotNil(mappedObject?.location, "Location should not be nil")
- XCTAssertNotNil(mappedObject?.threeDayForecast, "ThreeDayForcast should not be nil")
-
- for forecast in mappedObject!.threeDayForecast {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- func testResponseImmutableArray() {
- // This is an example of a functional test case.
- let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
- let expectation = self.expectation(description: "\(URL)")
-
- _ = AF.request(URL, method: .get).responseArray { (response: AFDataResponse<[ImmutableForecast]>) in
- expectation.fulfill()
-
- let mappedArray = response.value
-
- XCTAssertNotNil(mappedArray, "Response should not be nil")
-
- XCTAssertTrue(mappedArray?.count == 3, "Didn't parse correct amount of objects")
-
- for forecast in mappedArray! {
- XCTAssertNotNil(forecast.day, "day should not be nil")
- XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
- XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
- }
- }
-
- waitForExpectations(timeout: 10) { error in
- XCTAssertNil(error, "\(String(describing: error))")
- }
- }
-
- }
- // MARK: - Response classes
- // MARK: - ImmutableMappable
- class ImmutableWeatherResponse: ImmutableMappable {
- let location: String
- let threeDayForecast: [ImmutableForecast]
-
- required init(map: Map) throws {
- location = try map.value("location")
- threeDayForecast = try map.value("three_day_forecast")
- }
- func mapping(map: Map) {
- location >>> map["location"]
- threeDayForecast >>> map["three_day_forecast"]
- }
- }
- class ImmutableForecast: ImmutableMappable {
- let day: String
- let temperature: Int
- let conditions: String
-
- required init(map: Map) throws {
- day = try map.value("day")
- temperature = try map.value("temperature")
- conditions = try map.value("conditions")
- }
-
- func mapping(map: Map) {
- day >>> map["day"]
- temperature >>> map["temperature"]
- conditions >>> map["conditions"]
- }
-
- }
- // MARK: - Mappable
- class WeatherResponse: Mappable {
- var location: String?
- var threeDayForecast: [Forecast]?
- var date: Date?
-
- init(){}
-
- required init?(map: Map){
- }
-
- func mapping(map: Map) {
- location <- map["location"]
- threeDayForecast <- map["three_day_forecast"]
- }
- }
- class Forecast: Mappable {
- var day: String?
- var temperature: Int?
- var conditions: String?
-
- required init?(map: Map){
- }
-
- func mapping(map: Map) {
- day <- map["day"]
- temperature <- map["temperature"]
- conditions <- map["conditions"]
- }
- }
-
- struct WeatherResponseImmutable: ImmutableMappable {
- let location: String
- var threeDayForecast: [Forecast]
- var date: Date?
-
- init(map: Map) throws {
- location = try map.value("location")
- threeDayForecast = try map.value("three_day_forecast")
- }
-
- func mapping(map: Map) {
- location >>> map["location"]
- threeDayForecast >>> map["three_day_forecast"]
- }
- }
- struct ForecastImmutable: ImmutableMappable {
- let day: String
- var temperature: Int
- var conditions: String?
-
- init(map: Map) throws {
- day = try map.value("day")
- temperature = try map.value("temperature")
- conditions = try? map.value("conditions")
- }
-
- func mapping(map: Map) {
- day >>> map["day"]
- temperature >>> map["temperature"]
- conditions >>> map["conditions"]
- }
- }
|