Skip to content

Commit

Permalink
Added test for getting arrivals via the more continuation.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottwilliams committed May 10, 2017
1 parent 9639aae commit e04e065
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Proper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
261035F61E8DCDA500364D20 /* TimetableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 261035F51E8DCDA500364D20 /* TimetableTests.swift */; };
261035F81E8DD3F300364D20 /* timetable.visits_between.BUS215.json in Resources */ = {isa = PBXBuildFile; fileRef = 261035F71E8DD3F300364D20 /* timetable.visits_between.BUS215.json */; };
2612B5271D50490500BF671D /* ModelDecodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2612B5261D50490500BF671D /* ModelDecodeTests.swift */; };
2612B52C1D50513600BF671D /* routes.4B.json in Resources */ = {isa = PBXBuildFile; fileRef = 2612B5291D50513600BF671D /* routes.4B.json */; };
2612B52D1D50513600BF671D /* stations.BUS100W.json in Resources */ = {isa = PBXBuildFile; fileRef = 2612B52A1D50513600BF671D /* stations.BUS100W.json */; };
Expand Down Expand Up @@ -138,6 +139,7 @@

/* Begin PBXFileReference section */
261035F51E8DCDA500364D20 /* TimetableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimetableTests.swift; sourceTree = "<group>"; };
261035F71E8DD3F300364D20 /* timetable.visits_between.BUS215.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = timetable.visits_between.BUS215.json; sourceTree = "<group>"; };
2612B5261D50490500BF671D /* ModelDecodeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModelDecodeTests.swift; sourceTree = "<group>"; };
2612B5291D50513600BF671D /* routes.4B.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = routes.4B.json; sourceTree = "<group>"; };
2612B52A1D50513600BF671D /* stations.BUS100W.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = stations.BUS100W.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -306,6 +308,7 @@
2612B5291D50513600BF671D /* routes.4B.json */,
2612B52A1D50513600BF671D /* stations.BUS100W.json */,
266CC4D71D826FC70022D064 /* vehicles.1801.json */,
261035F71E8DD3F300364D20 /* timetable.visits_between.BUS215.json */,
);
path = Data;
sourceTree = "<group>";
Expand Down Expand Up @@ -720,6 +723,7 @@
buildActionMask = 2147483647;
files = (
2644B89B1E7F7385008A0A4D /* PMU.gpx in Resources */,
261035F81E8DD3F300364D20 /* timetable.visits_between.BUS215.json in Resources */,
2612B52D1D50513600BF671D /* stations.BUS100W.json in Resources */,
2644B8A11E7F74F8008A0A4D /* Windsor.gpx in Resources */,
266CC4D81D826FC70022D064 /* vehicles.1801.json in Resources */,
Expand Down
62 changes: 62 additions & 0 deletions ProperTests/Data/timetable.visits_between.BUS215.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
[
"20170316 13:51:00",
"20170316 13:51:00",
"23",
"to Wabash Landing & Lafayette"
],
[
"20170316 14:06:00",
"20170316 14:06:00",
"23",
"to Wabash Landing & Lafayette"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"6B",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"3",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"2A",
"to/from CityBus Center & Schuyler Ave"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"9",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"7",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"1A",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"4A",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"2B",
"to Union & Underwood"
]
]
13 changes: 6 additions & 7 deletions ProperTests/Fixtures/Model+fixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import Foundation
import Argo
import ReactiveCocoa
import Result
@testable import Proper

extension Model where Self: Decodable, Self.DecodedType == Self {

extension Decodable where Self.DecodedType == Self {
/// Look up a decoded model fixture for `identifier`.
static func fixture(identifier: String) -> SignalProducer<Self, TestError> {
static func fixture(identifier: String) -> SignalProducer<Self, NoError> {
return rawFixture(identifier).map { Argo.decode($0) as Self! }
}

/// Look up a raw model fixture for `identifier`.
/// Returns a SignalProducer that gets the raw (json) form of a module. For now, this searches for
/// a `<identifier>.json` file in the test resource bundle, but in the future it could call the server, use a
/// cassette system, etc.
static func rawFixture(identifier: String) -> SignalProducer<AnyObject, TestError> {
static func rawFixture(identifier: String) -> SignalProducer<AnyObject, NoError> {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
let scheduler = QueueScheduler(queue: queue, name: "ProperTests.fixtureQueue")
return SignalProducer { observer, _ in
Expand All @@ -33,12 +33,11 @@ extension Model where Self: Decodable, Self.DecodedType == Self {
let data = NSData(contentsOfFile: resource),
let json = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
else {
observer.sendFailed(.modelLoadError)
return
fatalError("Model load error")
}
observer.sendNext(json)
observer.sendCompleted()
}
}.observeOn(scheduler).logEvents(identifier: "Model.baseFixture", logger: logSignalEvent)
}
}
}
141 changes: 134 additions & 7 deletions ProperTests/TimetableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,148 @@
//

import XCTest
import ReactiveCocoa
import Argo
@testable import Proper

class TimetableTests: XCTestCase {

var connection: ConnectionMock!
var scheduler: TestScheduler!
var disposable: CompositeDisposable!

var station: MutableStation!

let startDate = NSDate(timeIntervalSince1970: 1489686630) // Thu, 16 Mar 2017 17:50:30 GMT
let endDate = NSDate(timeIntervalSince1970: 1489690230) // Thu, 16 Mar 2017 18:50:30 GMT

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
connection = ConnectionMock()
disposable = CompositeDisposable()

station = try! MutableStation(from: Station(stopCode: "TEST1"), connection: connection)
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
disposable.dispose()
super.tearDown()
}

func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.

func testGetArrivalsFromMoreContinuation() {
// Setup: Form two Timetable responses.
let limit = Timetable.Limit(window: endDate.timeIntervalSinceDate(startDate), count: 2)
let responses = [
response.map({ Array($0[0..<2]) }),
response.map({ Array($0[2..<3]) })
]
connection.on("timetable.visits_between", send: TopicEvent.Timetable(.arrivals(responses[0])))
let completed = expectationWithDescription("got an arrival by calling the continuation")

// Given a call to `visits`...
let producer = Timetable.visits(for: station, occurring: .between(startDate, endDate), using: connection,
initialLimit: limit).logEvents(identifier: #function, logger: logSignalEvent)


var seen = 0
var calledMore = false
producer.startWithResult { result in
guard let result = result.value else {
XCTFail("Error returned from producer")
return
}

// Each nth arrival should match the nth member of the response array.
XCTAssertEqual(try! self.response.dematerialize()[seen].makeArrival(using: self.connection),
result.arrival)

switch seen {
case 0:
break
case 1:
// Call the `more` continuation after the second arrival is received.
self.connection.on("timetable.visits_between", send: TopicEvent.Timetable(.arrivals(responses[1])))
let time_10ms = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC) / 10)
dispatch_after(time_10ms, dispatch_get_main_queue()) {
calledMore = true
result.more()
}
case 2:
// The 3rd arrival should *only* appear after `more` was called.
if calledMore { completed.fulfill() }
else { XCTFail("More than the initial count received") }
default:
// Exactly 3 arrivals should be sent.
XCTFail("received message \(seen), only 0...2 expected")
}

seen += 1
}

waitForExpectationsWithTimeout(2, handler: nil)
}


let response: Decoded<[Proper.Timetable.Response]> = decodeArray(JSON([
[
"20170316 13:51:00",
"20170316 13:51:00",
"23",
"to Wabash Landing & Lafayette"
],
[
"20170316 14:06:00",
"20170316 14:06:00",
"23",
"to Wabash Landing & Lafayette"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"6B",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"3",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"2A",
"to/from CityBus Center & Schuyler Ave"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"9",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"7",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"1A",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"4A",
"to CityBus Center"
],
[
"20170316 14:10:00",
"20170316 14:10:00",
"2B",
"to Union & Underwood"
]
]))
}

0 comments on commit e04e065

Please sign in to comment.