Skip to content

Commit

Permalink
Make DataBlock conform to Decodable
Browse files Browse the repository at this point in the history
  • Loading branch information
sxg committed Mar 13, 2019
1 parent 02958dd commit f90a74c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
22 changes: 1 addition & 21 deletions Source/DataBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// Weather data for a specific location over a period of time.
public struct DataBlock {
public struct DataBlock: Decodable {

/// A human-readable text summary.
public let summary: String?
Expand All @@ -20,24 +20,4 @@ public struct DataBlock {
/// `DataPoint`s ordered by time, which describe the weather conditions at the requested location over time.
public let data: [DataPoint]

/// Creates a new `DataBlock` from a JSON object.
///
/// - parameter json: A JSON object with keys corresponding to the `DataBlock`'s properties.
///
/// - returns: A new `DataBlock` filled with data from the given JSON object.
public init(fromJSON json: NSDictionary) {
summary = json["summary"] as? String
if let jsonIcon = json["icon"] as? String {
icon = Icon(rawValue: jsonIcon)
} else {
icon = nil
}

let jsonData = json["data"] as! [NSDictionary]
var tempData = [DataPoint]()
for jsonDataPoint in jsonData {
tempData.append(DataPoint(fromJSON: jsonDataPoint))
}
data = tempData
}
}
31 changes: 17 additions & 14 deletions Tests/DataBlockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@ import XCTest

class DataBlockTests: XCTestCase {

var dataBlockJSON: NSDictionary!
var dataBlockNoOptionalsJSON: NSDictionary!
var dataBlockJSONData: Data!
var dataBlockNoOptionalsJSONData: Data!
var decoder: JSONDecoder!

override func setUp() {
super.setUp()

let forecastJSONPath = Bundle(for: type(of: self)).path(forResource: "forecast", ofType: "json")!
let forecastJSONData = try! Data(contentsOf: URL(fileURLWithPath: forecastJSONPath))
let forecastJSON = try! JSONSerialization.jsonObject(with: forecastJSONData, options: .mutableContainers) as! NSDictionary
self.dataBlockJSON = forecastJSON["minutely"] as? NSDictionary
// Load datablock.json as Data
let dataBlockJSONPath = Bundle(for: type(of: self)).path(forResource: "datablock", ofType: "json")!
self.dataBlockJSONData = try! Data(contentsOf: URL(fileURLWithPath: dataBlockJSONPath))

let forecastNoOptionalsJSONPath = Bundle(for: type(of: self)).path(forResource: "forecast_no_optionals", ofType: "json")!
let forecastNoOptionalsJSONData = try! Data(contentsOf: URL(fileURLWithPath: forecastNoOptionalsJSONPath))
let forecastNoOptionalsJSON = try! JSONSerialization.jsonObject(with: forecastNoOptionalsJSONData, options: .mutableContainers) as! NSDictionary
self.dataBlockNoOptionalsJSON = forecastNoOptionalsJSON["minutely"] as? NSDictionary
// Load datablock_no_optionals.json as Data
let dataBlockNoOptionalsJSONPath = Bundle(for: type(of: self)).path(forResource: "datablock_no_optionals", ofType: "json")!
self.dataBlockNoOptionalsJSONData = try! Data(contentsOf: URL(fileURLWithPath: dataBlockNoOptionalsJSONPath))

// Setup the decoder
self.decoder = JSONDecoder()
self.decoder.dateDecodingStrategy = .secondsSince1970
}

override func tearDown() {
super.tearDown()
}

func testInitFromJSON() {
func testInitFromDecoder() {
// Given
// When
let dataBlock = DataBlock(fromJSON: self.dataBlockJSON)
let dataBlock = try! self.decoder.decode(DataBlock.self, from: self.dataBlockJSONData)

// Then
XCTAssertNotNil(dataBlock)
Expand All @@ -44,10 +47,10 @@ class DataBlockTests: XCTestCase {
XCTAssertFalse(dataBlock.data.isEmpty)
}

func testInitNoOptionalsFromJSON() {
func testInitFromDecoderNoOptionals() {
// Given
// When
let dataBlock = DataBlock(fromJSON: self.dataBlockNoOptionalsJSON)
let dataBlock = try! self.decoder.decode(DataBlock.self, from: self.dataBlockNoOptionalsJSONData)

// Then
XCTAssertNotNil(dataBlock)
Expand Down

0 comments on commit f90a74c

Please sign in to comment.