<commit_before>//
//  StravaActivityTests.swift
//  StravaKit
//
//  Created by <NAME> on 8/22/16.
//  Copyright © 2016 SmallSharpTools LLC. All rights reserved.
//

import XCTest

@testable import StravaKit

class StravaActivityTests: XCTestCase {

    func testActivitiesCreationFromGoodArray() {
        // all required values are in the JSON file
        guard let dictionaries = activitiesDictionaries("activities-good") else {
            XCTFail()
            return
        }

        let activities = Activity.activities(dictionaries)
        XCTAssertNotNil(activities)
        XCTAssertNotNil(activities?.count == dictionaries.count)

        if let activity = activities?.first {
            let startCoordinate = activity.startCoordinate
            let endCoordinate = activity.endCoordinate

            XCTAssertTrue(startCoordinate.latitude == 37.73697)
            XCTAssertTrue(startCoordinate.longitude == -122.416727)
            XCTAssertTrue(endCoordinate.latitude == 37.786237)
            XCTAssertTrue(endCoordinate.longitude == -122.398677)
        }
    }

    func testActivitiesCreationFromBadArray() {
        // required values are missing from the JSON file
        guard let dictionaries = activitiesDictionaries("activities-bad") else {
            XCTFail()
            return
        }

        let activities = Activity.activities(dictionaries)
        XCTAssertNil(activities)
    }

    func testActivitiesCreationFromEmptyArray() {
        // use an empty array
        let dictionaries: [JSONDictionary] = []
        let activities = Activity.activities(dictionaries)
        XCTAssertNil(activities)
    }

    func testActivityCreationFromEmptyDictionary() {
        // required values are missing from the JSON file
        guard let dictionary = JSONLoader.sharedInstance.loadJSON("empty") as? JSONDictionary else {
            XCTFail()
            return
        }

        let activity = Activity(dictionary: dictionary)
        XCTAssertNil(activity)
    }

    func testGetActivitiesGood() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activities-good")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivitiesBad() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activities-bad")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertTrue(activities?.count == 0)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivityGood() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activity-good")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivity(1) { (activity, error) in
            XCTAssertNotNil(activity)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivityBad() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activity-bad")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivity(1) { (activity, error) in
            XCTAssertNil(activity)
            XCTAssertNotNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetFollowingActivitiesGood() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activities-following")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getFollowingActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetFollowingActivitiesBad() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("empty")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getFollowingActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertTrue(activities?.count == 0)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }


    // MARK: - Private Functions -

    private func activitiesDictionaries(name: String) -> [JSONDictionary]? {
        let bundle = NSBundle(forClass: self.classForCoder)
        guard let path = bundle.pathForResource(name, ofType: "json") else {
            return nil
        }
        let fm = NSFileManager.defaultManager()
        if fm.isReadableFileAtPath(path) {
            guard let data = fm.contentsAtPath(path) else {
                return nil
            }
            do {
                if let dictionaries = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [JSONDictionary] {
                    return dictionaries
                }
            }<commit_msg>updates tests to increase code coverage<commit_after>//
//  StravaActivityTests.swift
//  StravaKit
//
//  Created by <NAME> on 8/22/16.
//  Copyright © 2016 SmallSharpTools LLC. All rights reserved.
//

import XCTest

@testable import StravaKit

class StravaActivityTests: XCTestCase {

    func testActivitiesCreationFromGoodArray() {
        // all required values are in the JSON file
        guard let dictionaries = activitiesDictionaries("activities-good") else {
            XCTFail()
            return
        }

        let activities = Activity.activities(dictionaries)
        XCTAssertNotNil(activities)
        XCTAssertNotNil(activities?.count == dictionaries.count)

        if let activity = activities?.first {
            let startCoordinate = activity.startCoordinate
            let endCoordinate = activity.endCoordinate

            XCTAssertTrue(startCoordinate.latitude == 37.73697)
            XCTAssertTrue(startCoordinate.longitude == -122.416727)
            XCTAssertTrue(endCoordinate.latitude == 37.786237)
            XCTAssertTrue(endCoordinate.longitude == -122.398677)
        }
    }

    func testActivitiesCreationFromBadArray() {
        // required values are missing from the JSON file
        guard let dictionaries = activitiesDictionaries("activities-bad") else {
            XCTFail()
            return
        }

        let activities = Activity.activities(dictionaries)
        XCTAssertNil(activities)
    }

    func testActivitiesCreationFromEmptyArray() {
        // use an empty array
        let dictionaries: [JSONDictionary] = []
        let activities = Activity.activities(dictionaries)
        XCTAssertNil(activities)
    }

    func testActivityCreationFromEmptyDictionary() {
        // required values are missing from the JSON file
        guard let dictionary = JSONLoader.sharedInstance.loadJSON("empty") as? JSONDictionary else {
            XCTFail()
            return
        }

        let activity = Activity(dictionary: dictionary)
        XCTAssertNil(activity)
    }

    func testGetActivitiesGood() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activities-good")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivitiesBad() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activities-bad")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertTrue(activities?.count == 0)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivitiesError() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = nil
        jsonRequestor.error = NSError(domain: "Testing", code: 500, userInfo: nil)
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivities() { (activities, error) in
            XCTAssertNil(activities)
            XCTAssertNotNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivityGood() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activity-good")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivity(1) { (activity, error) in
            XCTAssertNotNil(activity)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivityBad() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activity-bad")
        jsonRequestor.error = NSError(domain: "Testing", code: 500, userInfo: nil)
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivity(1) { (activity, error) in
            XCTAssertNil(activity)
            XCTAssertNotNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetActivityInvalid() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("invalid")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getActivity(1) { (activity, error) in
            XCTAssertNil(activity)
            XCTAssertNotNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetFollowingActivitiesGood() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("activities-following")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getFollowingActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetFollowingActivitiesBad() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = JSONLoader.sharedInstance.loadJSON("empty")
        jsonRequestor.error = nil
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getFollowingActivities() { (activities, error) in
            XCTAssertNotNil(activities)
            XCTAssertTrue(activities?.count == 0)
            XCTAssertNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    func testGetFollowingActivitiesError() {
        let expectation = self.expectationWithDescription("API Call")

        let jsonRequestor = JSONRequestor()
        jsonRequestor.response = nil
        jsonRequestor.error = NSError(domain: "Testing", code: 500, userInfo: nil)
        Strava.sharedInstance.alternateRequestor = jsonRequestor

        Strava.getFollowingActivities() { (activities, error) in
            XCTAssertNil(activities)
            XCTAssertNotNil(error)
            expectation.fulfill()
        }

        let timeout: NSTimeInterval = 3
        self.waitForExpectationsWithTimeout(timeout) { (error) in
            // do nothing
        }
    }

    // MARK: - Private Functions -

    private func activitiesDictionaries(name: String) -> [JSONDictionary]? {
        let bundle = NSBundle(forClass: self.classForCoder)
        guard let path = bundle.pathForResource(name, ofType: "json") else {
            return nil
        }
        let fm = NSFileManager.defaultManager()
        if fm.isReadableFileAtPath(path) {
            guard let data = fm.contentsAtPath(path) else {
                return nil
            }
            do {
                if let dictionaries = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [JSONDictionary] {
                    return dictionaries
                }
            }
{'fraction_non_alphanumeric': 0.08046859996159017, 'fraction_numerical': 0.007489917418859228, 'mean_word_length': 2.3181818181818183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '237560', 'n_tokens_mistral': 4231, 'n_tokens_neox': 4062, 'n_words': 829}