This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from Sage-Bionetworks/syoung/add-answers-file
[iOS] Add a local schema to building of “answers.json”
- Loading branch information
Showing
5 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ackage/Tests/BridgeClientExtensionTests/FileUpload/V2/AssessmentArchiveBuilderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Created 9/13/23 | ||
// swift-tools-version:5.0 | ||
|
||
import XCTest | ||
@testable import BridgeClientExtension | ||
import JsonModel | ||
import ResultModel | ||
|
||
final class AssessmentArchiveBuilderTests: XCTestCase { | ||
|
||
override func setUpWithError() throws { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
} | ||
|
||
func testSurveyAnswerBuilder() async throws { | ||
|
||
let assessmentResult = AssessmentResultObject(identifier: "example_survey") | ||
let answerResult1 = AnswerResultObject(identifier: "step1", value: .boolean(true), questionText: "Do you like pizza?") | ||
let answerResult2 = AnswerResultObject(identifier: "step2", value: .integer(42), questionText: "What is the answer to the universe and everything?") | ||
let answerResult3 = AnswerResultObject(identifier: "step3", value: .number(52.25), questionText: "How old are you?") | ||
let answerResult4 = AnswerResultObject(identifier: "step4", value: .string("brown fox"), questionText: "Who jumped over the lazy dog?") | ||
let answerResult5 = AnswerResultObject(identifier: "step5", value: .array([1,11]), questionText: "What are your favorite numbers?") | ||
assessmentResult.stepHistory = [answerResult1, answerResult2, answerResult3, answerResult4, answerResult5] | ||
|
||
guard let builder = AssessmentArchiveBuilder(assessmentResult) else { | ||
XCTFail("Unexpected NULL when creating the archiver") | ||
return | ||
} | ||
builder.archive.isTest = true | ||
let _ = try await builder.buildArchive() | ||
|
||
// Check each answer type | ||
XCTAssertEqual(builder.answers["step1"] as? Bool, true) | ||
XCTAssertEqual(builder.answers["step2"] as? Int, 42) | ||
XCTAssertEqual(builder.answers["step3"] as? Double, 52.25) | ||
XCTAssertEqual(builder.answers["step4"] as? String, "brown fox") | ||
XCTAssertEqual(builder.answers["step5"] as? String, "1,11") | ||
|
||
// Check that the answers match the expected values | ||
let expectedAnswers = try JSONSerialization.jsonObject(with: """ | ||
{ | ||
"step1" : true, | ||
"step2" : 42, | ||
"step3" : 52.25, | ||
"step4" : "brown fox", | ||
"step5" : "1,11" | ||
} | ||
""".data(using: .utf8)!) as! NSDictionary | ||
let answers = try builder.archive.addedFiles["answers.json"].map { try JSONSerialization.jsonObject(with: $0) } as? NSDictionary | ||
XCTAssertEqual(expectedAnswers, answers) | ||
|
||
// Check that the schema matches | ||
let expectedSchema = try JSONSerialization.jsonObject(with: """ | ||
{ | ||
"$id" : "answers_schema.json", | ||
"$schema" : "http://json-schema.org/draft-07/schema#", | ||
"type" : "object", | ||
"title" : "answers_schema", | ||
"description" : "example_survey", | ||
"properties" : { | ||
"step1" : { | ||
"type" : "boolean", | ||
"description" : "Do you like pizza?" | ||
}, | ||
"step2" : { | ||
"type" : "integer", | ||
"description" : "What is the answer to the universe and everything?" | ||
}, | ||
"step3" : { | ||
"type" : "number", | ||
"description" : "How old are you?" | ||
}, | ||
"step4" : { | ||
"type" : "string", | ||
"description" : "Who jumped over the lazy dog?" | ||
}, | ||
"step5" : { | ||
"type" : "string", | ||
"description" : "What are your favorite numbers?" | ||
}, | ||
} | ||
} | ||
""".data(using: .utf8)!) as! NSDictionary | ||
let schema = try builder.archive.addedFiles["answers_schema.json"].map { try JSONSerialization.jsonObject(with: $0) } as? NSDictionary | ||
XCTAssertEqual(expectedSchema, schema) | ||
} | ||
} |