Skip to content

Commit

Permalink
Merge pull request #10 from carambalabs/feature/JsonTests
Browse files Browse the repository at this point in the history
API Call Tests
  • Loading branch information
sergigracia authored Oct 30, 2016
2 parents 02c943f + 6f7bda1 commit efd0b6d
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 71 deletions.
2 changes: 1 addition & 1 deletion DarkSkyKit/TimeMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Alamofire

public extension DarkSkyKit {
public func timeMachine(latitude lat: Double, lognitude long: Double, date: Date, result: @escaping (Result<Forecast>) -> Void) {
public func timeMachine(latitude lat: Double, longitude long: Double, date: Date, result: @escaping (Result<Forecast>) -> Void) {
Alamofire.request(Router.timeMachine(configuration, lat, long, date)).responseJSON { response in
switch response.result {
case .success(let value):
Expand Down
20 changes: 12 additions & 8 deletions Example/DarkSkyKitExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
1E433E951D47923A0056479A /* ForecastFlags.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E433E931D47923A0056479A /* ForecastFlags.swift */; };
1E433E971D4792500056479A /* ForecastAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E433E961D4792500056479A /* ForecastAlert.swift */; };
1E433E981D4792500056479A /* ForecastAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1E433E961D4792500056479A /* ForecastAlert.swift */; };
1EA9820D1D48FF1900B3CAD3 /* ForecastFlagsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EA9820C1D48FF1900B3CAD3 /* ForecastFlagsTests.swift */; };
1EA982111D48FFEB00B3CAD3 /* flags.json in Resources */ = {isa = PBXBuildFile; fileRef = 1EA982101D48FFEB00B3CAD3 /* flags.json */; };
1EA9820D1D48FF1900B3CAD3 /* ForecastCurrentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1EA9820C1D48FF1900B3CAD3 /* ForecastCurrentTests.swift */; };
1EA982111D48FFEB00B3CAD3 /* forecast.json in Resources */ = {isa = PBXBuildFile; fileRef = 1EA982101D48FFEB00B3CAD3 /* forecast.json */; };
5525DC861D3D8ACA00D3967C /* DarkSkyKitExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5525DC7C1D3D8ACA00D3967C /* DarkSkyKitExample.framework */; };
5525DC951D3D8C2E00D3967C /* DarkSkyKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5525DC941D3D8C2E00D3967C /* DarkSkyKit.swift */; };
5525DC971D3D973A00D3967C /* Current.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5525DC961D3D973A00D3967C /* Current.swift */; };
5525DC991D3D974700D3967C /* TimeMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5525DC981D3D974700D3967C /* TimeMachine.swift */; };
5525DC9B1D3D9ECA00D3967C /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5525DC9A1D3D9ECA00D3967C /* Configuration.swift */; };
5535ADED1D44270D002959EC /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5535ADEC1D44270D002959EC /* Router.swift */; };
555AF6DF1DC685EB00178012 /* ForecastTimeMachineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 555AF6DE1DC685EB00178012 /* ForecastTimeMachineTests.swift */; };
D5E32BD5105B038CA4A10B05 /* Pods_DarkSkyKitExampleTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 39C1E25F35A04708AEEBE999 /* Pods_DarkSkyKitExampleTests.framework */; };
F5ADC57F0B8F2D88A238781B /* Pods_DarkSkyKitExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A4C2E930139FA5009D7F9220 /* Pods_DarkSkyKitExample.framework */; };
/* End PBXBuildFile section */
Expand All @@ -50,8 +51,8 @@
1E433E901D4792250056479A /* ForecastDataPoint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForecastDataPoint.swift; sourceTree = "<group>"; };
1E433E931D47923A0056479A /* ForecastFlags.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForecastFlags.swift; sourceTree = "<group>"; };
1E433E961D4792500056479A /* ForecastAlert.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForecastAlert.swift; sourceTree = "<group>"; };
1EA9820C1D48FF1900B3CAD3 /* ForecastFlagsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForecastFlagsTests.swift; sourceTree = "<group>"; };
1EA982101D48FFEB00B3CAD3 /* flags.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = flags.json; sourceTree = "<group>"; };
1EA9820C1D48FF1900B3CAD3 /* ForecastCurrentTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForecastCurrentTests.swift; sourceTree = "<group>"; };
1EA982101D48FFEB00B3CAD3 /* forecast.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = forecast.json; sourceTree = "<group>"; };
21505191131C1AA1AC555975 /* Pods-DarkSkyKitExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DarkSkyKitExample.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DarkSkyKitExample/Pods-DarkSkyKitExample.debug.xcconfig"; sourceTree = "<group>"; };
39C1E25F35A04708AEEBE999 /* Pods_DarkSkyKitExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DarkSkyKitExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5525DC6C1D3D89FF00D3967C /* ConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationTests.swift; sourceTree = "<group>"; };
Expand All @@ -63,6 +64,7 @@
5525DC981D3D974700D3967C /* TimeMachine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimeMachine.swift; sourceTree = "<group>"; };
5525DC9A1D3D9ECA00D3967C /* Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
5535ADEC1D44270D002959EC /* Router.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
555AF6DE1DC685EB00178012 /* ForecastTimeMachineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ForecastTimeMachineTests.swift; sourceTree = "<group>"; };
55B43F361D8AD9640000B3D3 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A4C2E930139FA5009D7F9220 /* Pods_DarkSkyKitExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DarkSkyKitExample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
DE43E9BF7E6793CD2ECD5389 /* Pods-DarkSkyKitExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DarkSkyKitExampleTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DarkSkyKitExampleTests/Pods-DarkSkyKitExampleTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -105,7 +107,7 @@
1EA9820F1D48FFB200B3CAD3 /* Data */ = {
isa = PBXGroup;
children = (
1EA982101D48FFEB00B3CAD3 /* flags.json */,
1EA982101D48FFEB00B3CAD3 /* forecast.json */,
);
name = Data;
path = jsons;
Expand Down Expand Up @@ -152,7 +154,8 @@
1EA9820F1D48FFB200B3CAD3 /* Data */,
1E1FE0C71D47CF4D003A4AAE /* ForecastKitTests.swift */,
5525DC6C1D3D89FF00D3967C /* ConfigurationTests.swift */,
1EA9820C1D48FF1900B3CAD3 /* ForecastFlagsTests.swift */,
1EA9820C1D48FF1900B3CAD3 /* ForecastCurrentTests.swift */,
555AF6DE1DC685EB00178012 /* ForecastTimeMachineTests.swift */,
5525DC6E1D3D89FF00D3967C /* Info.plist */,
);
path = DarkSkyKitTests;
Expand Down Expand Up @@ -291,7 +294,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1EA982111D48FFEB00B3CAD3 /* flags.json in Resources */,
1EA982111D48FFEB00B3CAD3 /* forecast.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -402,10 +405,11 @@
1E1FE0C11D47C0B6003A4AAE /* Router.swift in Sources */,
1E433E921D4792250056479A /* ForecastDataPoint.swift in Sources */,
1E433E951D47923A0056479A /* ForecastFlags.swift in Sources */,
1EA9820D1D48FF1900B3CAD3 /* ForecastFlagsTests.swift in Sources */,
1EA9820D1D48FF1900B3CAD3 /* ForecastCurrentTests.swift in Sources */,
1E1FE0C41D47C0BE003A4AAE /* Configuration.swift in Sources */,
1E1FE0C51D47C0C0003A4AAE /* DarkSkyKit.swift in Sources */,
1E1FE0C31D47C0BB003A4AAE /* Current.swift in Sources */,
555AF6DF1DC685EB00178012 /* ForecastTimeMachineTests.swift in Sources */,
1E1FE0C61D47C11C003A4AAE /* ConfigurationTests.swift in Sources */,
1E1FE0C81D47CF4D003A4AAE /* ForecastKitTests.swift in Sources */,
);
Expand Down
67 changes: 67 additions & 0 deletions Example/DarkSkyKitTests/ForecastCurrentTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import XCTest
import Foundation
import OHHTTPStubs

class ForecastCurrentTests: XCTestCase {

let client = DarkSkyKit(apiToken: "0")

func testForecastRequest() {
stubAPIResponse()
let exp = expectation(description: "API Call")
client.current(latitude: 0.00, longitude: 0.00) { result in
switch result {
case .success(let data):
self.testForecastBasicInfo(fromData: data)
self.testForecastCurrently(fromData: data)
self.testForecastMinutely(fromData: data)
self.testForecastHourly(fromData: data)
self.testForecastDaily(fromData: data)
self.testForecastAlerts(fromData: data)
self.testForecastFlags(fromData: data)
default: break
}
exp.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}

func testForecastBasicInfo(fromData data: Forecast) {
XCTAssertEqual(data.latitude, 37.8267)
XCTAssertEqual(data.longitude, -122.4233)
XCTAssertEqual(data.timezone, "America/Los_Angeles")
XCTAssertEqual(data.offset, -7)
}

func testForecastCurrently(fromData data: Forecast) {
XCTAssertEqual(data.currently?.summary, "Mostly Cloudy")
XCTAssertEqual(data.currently?.apparentTemperature, 57.34)
}

func testForecastMinutely(fromData data: Forecast) {
XCTAssertEqual(data.minutely?.first?.time?.timeIntervalSince1970, 1477854360)
}

func testForecastHourly(fromData data: Forecast) {
XCTAssertEqual(data.hourly?.first?.temperature, 57.02)
}

func testForecastDaily(fromData data: Forecast) {
XCTAssertEqual(data.daily?.first?.precipIntensity, 0.0136)
}

func testForecastAlerts(fromData data: Forecast) {
XCTAssertEqual(data.alerts?.first?.title, "Flood Watch for Mason, WA")
}

func testForecastFlags(fromData data: Forecast) {
XCTAssertEqual(data.flags?.sources?.first, "darksky")
}

func stubAPIResponse() {
stub(condition: isHost("api.darksky.net")) { _ in
let stubPath = OHPathForFile("forecast.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject:"application/json" as AnyObject])
}
}
}
14 changes: 0 additions & 14 deletions Example/DarkSkyKitTests/ForecastFlagsTests.swift

This file was deleted.

67 changes: 67 additions & 0 deletions Example/DarkSkyKitTests/ForecastTimeMachineTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import XCTest
import Foundation
import OHHTTPStubs

class ForecastTimeMachineTests: XCTestCase {

let client = DarkSkyKit(apiToken: "0")

func testForecastRequest() {
stubAPIResponse()
let exp = expectation(description: "API Call")
client.timeMachine(latitude: 0.00, longitude: 0.00, date: Date(timeIntervalSince1970: 1477854360)) { result in
switch result {
case .success(let data):
self.testForecastBasicInfo(fromData: data)
self.testForecastCurrently(fromData: data)
self.testForecastMinutely(fromData: data)
self.testForecastHourly(fromData: data)
self.testForecastDaily(fromData: data)
self.testForecastAlerts(fromData: data)
self.testForecastFlags(fromData: data)
default: break
}
exp.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}

func testForecastBasicInfo(fromData data: Forecast) {
XCTAssertEqual(data.latitude, 37.8267)
XCTAssertEqual(data.longitude, -122.4233)
XCTAssertEqual(data.timezone, "America/Los_Angeles")
XCTAssertEqual(data.offset, -7)
}

func testForecastCurrently(fromData data: Forecast) {
XCTAssertEqual(data.currently?.summary, "Mostly Cloudy")
XCTAssertEqual(data.currently?.apparentTemperature, 57.34)
}

func testForecastMinutely(fromData data: Forecast) {
XCTAssertEqual(data.minutely?.first?.time?.timeIntervalSince1970, 1477854360)
}

func testForecastHourly(fromData data: Forecast) {
XCTAssertEqual(data.hourly?.first?.temperature, 57.02)
}

func testForecastDaily(fromData data: Forecast) {
XCTAssertEqual(data.daily?.first?.precipIntensity, 0.0136)
}

func testForecastAlerts(fromData data: Forecast) {
XCTAssertEqual(data.alerts?.first?.title, "Flood Watch for Mason, WA")
}

func testForecastFlags(fromData data: Forecast) {
XCTAssertEqual(data.flags?.sources?.first, "darksky")
}

func stubAPIResponse() {
stub(condition: isHost("api.darksky.net")) { _ in
let stubPath = OHPathForFile("forecast.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject:"application/json" as AnyObject])
}
}
}
45 changes: 0 additions & 45 deletions Example/DarkSkyKitTests/jsons/flags.json

This file was deleted.

Loading

0 comments on commit efd0b6d

Please sign in to comment.