Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Move results into a different target
Browse files Browse the repository at this point in the history
  • Loading branch information
syoung-smallwisdom committed Oct 28, 2022
1 parent 4431eda commit cf13a8a
Show file tree
Hide file tree
Showing 19 changed files with 297 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// An `AnswerResult` is used to hold a serializable answer to a question or measurement. This
/// protocol is defined as a class to allow for mutating the `jsonValue` without requiring the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// The coding information for an answer. This allows for adding custom information to the "kind" of question because
/// JSON only supports a small subset of value types that are often encoded and can be described using JSON Schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// An ``AssessmentResult`` is the top-level ``ResultData`` for an assessment.
public protocol AssessmentResult : BranchNodeResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// The ``BranchNodeResult`` is the result created for a given level of navigation of a node tree. The
/// ``stepHistory`` is additive where each time a node is traversed, it is added to the list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// A `CollectionResult` is used to describe a collection of results.
public protocol CollectionResult : AnyObject, ResultData, AnswerFinder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// `ErrorResult` is a result that holds information about an error.
public protocol ErrorResult : ResultData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//

import Foundation
import JsonModel

/// A data archive is a class object that can be used to add multiple files to a zipped archive for upload as
/// a package. The data archive could also be a service that implements the logic for uploading results where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// `FileResult` is a result that holds a pointer to a file url.
public protocol FileResult : ResultData {
Expand Down
1 change: 0 additions & 1 deletion Sources/ResultModel/Placeholder.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//

import Foundation
import JsonModel

/// `ResultData` is the base protocol for an object that stores data.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ResultDataFactory.swift
//
//
// Copyright © 2020-2021 Sage Bionetworks. All rights reserved.
// Copyright © 2020-2022 Sage Bionetworks. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel

/// `ResultDataFactory` is a subclass of the `SerializationFactory` that registers a serializer
/// for `JsonResultData` objects that can be used to deserialize the results.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//

import Foundation
import JsonModel


/// `SerializableResultData` is the base implementation for `ResultData` that is serialized using
Expand Down
232 changes: 0 additions & 232 deletions Tests/JsonModelTests/DocumentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,80 +206,6 @@ final class DocumentableTests: XCTestCase {
XCTFail("Missing definition mapping for \(key)")
}
}

func testResultDataFactory() {
do {
let factory = TestResultFactoryA()
let doc = JsonDocumentBuilder(factory: factory)
let schemas = try doc.buildSchemas()

XCTAssertEqual(schemas.count, 4)

if let testSchema = schemas.first(where: { $0.id.className == "TestResult"}),
let sampleDef = testSchema.definitions?["Sample"],
case .object(let sampleObj) = sampleDef, let sampleProps = sampleObj.properties {
XCTAssertNotNil(sampleProps["index"])
XCTAssertNotNil(sampleProps["identifier"])
XCTAssertNotNil(sampleProps["startDate"])
}
else {
XCTFail("Failed to build schema.")
}

if let rootSchema = schemas.first(where: { $0.id.className == "ResultData"}),
let def = rootSchema.definitions?["ResultObject"],
case .object(let obj) = def, let props = obj.properties {
XCTAssertNotNil(props["type"])
XCTAssertNil(props["identifier"])
XCTAssertNil(props["startDate"])
XCTAssertNil(props["endDate"])
}
else {
XCTFail("Failed to build schema.")
}
}
catch let err {
XCTFail("Failed to build the JsonSchema: \(err)")
}
}

func testResultDataFactoryExternal() {
do {
let factory = TestResultFactoryB()
let doc = JsonDocumentBuilder(factory: factory)
let schemas = try doc.buildSchemas()

let schemaNames = schemas.map { $0.id.className }
XCTAssertEqual(["TestResultExternal"], schemaNames)

guard let schema = schemas.first, schema.id.className == "TestResultExternal" else {
XCTFail("Failed to build and filter schemas")
return
}

if let interface = schema.root.allOf?.first?.refId {
XCTAssertEqual("ResultData", interface.className)
XCTAssertTrue(interface.isExternal)
XCTAssertEqual("https://sage-bionetworks.github.io/mobile-client-json/schemas/v2/ResultData.json", interface.classPath)
}
else {
XCTFail("Failed to add expected interfaces.")
}

if let props = schema.root.properties,
let typeProp = props["type"],
case .const(let constType) = typeProp {
XCTAssertEqual("test", constType.const)
XCTAssertEqual("https://sage-bionetworks.github.io/mobile-client-json/schemas/v2/ResultData.json#SerializableResultType", constType.ref?.classPath)
}
else {
XCTFail("Failed to add expected property.")
}
}
catch let err {
XCTFail("Failed to build the JsonSchema: \(err)")
}
}
}

// MARK: Test objects
Expand Down Expand Up @@ -437,163 +363,5 @@ extension AnotherB : DocumentableStruct {
}
}

class TestResultFactoryA : ResultDataFactory {
required init() {
super.init()
resultSerializer.add(TestResult())
}
}

extension SerializableResultType {
static let test: SerializableResultType = "test"
}

struct TestResult : SerializableResultData, DocumentableStruct, DocumentableRootObject {
private enum CodingKeys : String, OrderedEnumCodingKey {
case serializableType="type", identifier, startDate, endDate, sample
}

var serializableType: SerializableResultType = .test
var identifier: String = "test"
var startDate: Date = Date()
var endDate: Date = Date()
var sample: Sample = .init()

func deepCopy() -> TestResult {
self
}

public var jsonSchema: URL {
URL(string: "\(self.className).json", relativeTo: kSageJsonSchemaBaseURL)!
}

public var documentDescription: String? {
"A result used to test root serialization."
}

static func examples() -> [TestResult] {
[.init()]
}

static func codingKeys() -> [CodingKey] {
CodingKeys.allCases
}

static func isRequired(_ codingKey: CodingKey) -> Bool {
true
}

static func documentProperty(for codingKey: CodingKey) throws -> DocumentProperty {
guard let key = codingKey as? CodingKeys else {
throw DocumentableError.invalidCodingKey(codingKey, "\(codingKey) is not recognized for this class")
}
switch key {
case .serializableType:
return .init(constValue: SerializableResultType.test)
case .identifier:
return .init(propertyType: .primitive(.string))
case .startDate, .endDate:
return .init(propertyType: .format(.dateTime))
case .sample:
return .init(propertyType: .reference(Sample.documentableType()))
}
}

struct Sample : DocumentableStruct {
private enum CodingKeys : String, OrderedEnumCodingKey {
case index, identifier, startDate
}

var index: Int = 0
var identifier: String = "sample"
var startDate: Date = Date()

static func examples() -> [Sample] {
[.init()]
}

static func codingKeys() -> [CodingKey] {
CodingKeys.allCases
}

static func isRequired(_ codingKey: CodingKey) -> Bool {
true
}

static func documentProperty(for codingKey: CodingKey) throws -> DocumentProperty {
guard let key = codingKey as? CodingKeys else {
throw DocumentableError.invalidCodingKey(codingKey, "\(codingKey) is not recognized for this class")
}
switch key {
case .index:
return .init(propertyType: .primitive(.integer))
case .identifier:
return .init(propertyType: .primitive(.string))
case .startDate:
return .init(propertyType: .format(.dateTime))
}
}
}
}

let testBURL = URL(string: "https://foo.org/schemas/")!

class TestResultFactoryB : ResultDataFactory {
required init() {
super.init()
resultSerializer.add(TestResultExternal())
}

override var jsonSchemaBaseURL: URL {
testBURL
}
}

struct TestResultExternal : SerializableResultData, DocumentableStruct, DocumentableRootObject {
private enum CodingKeys : String, OrderedEnumCodingKey {
case serializableType="type", identifier, startDate, endDate
}

var serializableType: SerializableResultType = .test
var identifier: String = "test"
var startDate: Date = Date()
var endDate: Date = Date()

func deepCopy() -> TestResultExternal {
self
}

public var jsonSchema: URL {
URL(string: "\(self.className).json", relativeTo: testBURL)!
}

public var documentDescription: String? {
"A result used to test root serialization."
}

static func examples() -> [TestResultExternal] {
[.init()]
}

static func codingKeys() -> [CodingKey] {
CodingKeys.allCases
}

static func isRequired(_ codingKey: CodingKey) -> Bool {
true
}

static func documentProperty(for codingKey: CodingKey) throws -> DocumentProperty {
guard let key = codingKey as? CodingKeys else {
throw DocumentableError.invalidCodingKey(codingKey, "\(codingKey) is not recognized for this class")
}
switch key {
case .serializableType:
return .init(constValue: SerializableResultType.test)
case .identifier:
return .init(propertyType: .primitive(.string))
case .startDate, .endDate:
return .init(propertyType: .format(.dateTime))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
//

import XCTest
@testable import JsonModel
import JsonModel
@testable import ResultModel

class AnswerTypeTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
//

import XCTest
@testable import JsonModel
import JsonModel
@testable import ResultModel

class AnswerResultObjectTests: XCTestCase {

Expand Down
Loading

0 comments on commit cf13a8a

Please sign in to comment.