|
| 1 | +// |
| 2 | +// IOS13Tests.swift |
| 3 | +// ParseSwift |
| 4 | +// |
| 5 | +// Created by Corey Baker on 7/2/21. |
| 6 | +// Copyright © 2021 Parse Community. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import XCTest |
| 11 | +@testable import ParseSwift |
| 12 | + |
| 13 | +@available(macOS 10.15, iOS 13.0, macCatalyst 13.0, watchOS 6.0, tvOS 13.0, *) |
| 14 | +class IOS13Tests: XCTestCase { // swiftlint:disable:this type_body_length |
| 15 | + struct Level: ParseObject { |
| 16 | + var objectId: String? |
| 17 | + |
| 18 | + var createdAt: Date? |
| 19 | + |
| 20 | + var updatedAt: Date? |
| 21 | + |
| 22 | + var ACL: ParseACL? |
| 23 | + |
| 24 | + var name = "First" |
| 25 | + } |
| 26 | + |
| 27 | + struct GameScore: ParseObject, Identifiable { |
| 28 | + |
| 29 | + // Comform to Identifiable |
| 30 | + var id: String { // swiftlint:disable:this identifier_name |
| 31 | + if let objectId = self.objectId { |
| 32 | + return objectId |
| 33 | + } else { |
| 34 | + return UUID().uuidString |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + //: Those are required for Object |
| 39 | + var objectId: String? |
| 40 | + var createdAt: Date? |
| 41 | + var updatedAt: Date? |
| 42 | + var ACL: ParseACL? |
| 43 | + |
| 44 | + //: Your own properties |
| 45 | + var score: Int? |
| 46 | + var player: String? |
| 47 | + var level: Level? |
| 48 | + var levels: [Level]? |
| 49 | + |
| 50 | + //custom initializers |
| 51 | + init (objectId: String?) { |
| 52 | + self.objectId = objectId |
| 53 | + } |
| 54 | + init(score: Int) { |
| 55 | + self.score = score |
| 56 | + self.player = "Jen" |
| 57 | + } |
| 58 | + init(score: Int, name: String) { |
| 59 | + self.score = score |
| 60 | + self.player = name |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + override func setUpWithError() throws { |
| 65 | + try super.setUpWithError() |
| 66 | + guard let url = URL(string: "http://localhost:1337/1") else { |
| 67 | + XCTFail("Should create valid URL") |
| 68 | + return |
| 69 | + } |
| 70 | + ParseSwift.initialize(applicationId: "applicationId", |
| 71 | + clientKey: "clientKey", |
| 72 | + masterKey: "masterKey", |
| 73 | + serverURL: url, |
| 74 | + testing: true) |
| 75 | + } |
| 76 | + |
| 77 | + override func tearDownWithError() throws { |
| 78 | + try super.tearDownWithError() |
| 79 | + MockURLProtocol.removeAll() |
| 80 | + #if !os(Linux) && !os(Android) |
| 81 | + try KeychainStore.shared.deleteAll() |
| 82 | + #endif |
| 83 | + try ParseStorage.shared.deleteAll() |
| 84 | + |
| 85 | + guard let fileManager = ParseFileManager(), |
| 86 | + let defaultDirectoryPath = fileManager.defaultDataDirectoryPath else { |
| 87 | + throw ParseError(code: .unknownError, message: "Should have initialized file manage") |
| 88 | + } |
| 89 | + |
| 90 | + let directory2 = defaultDirectoryPath |
| 91 | + .appendingPathComponent(ParseConstants.fileDownloadsDirectory, isDirectory: true) |
| 92 | + let expectation2 = XCTestExpectation(description: "Delete files2") |
| 93 | + fileManager.removeDirectoryContents(directory2) { _ in |
| 94 | + expectation2.fulfill() |
| 95 | + } |
| 96 | + wait(for: [expectation2], timeout: 20.0) |
| 97 | + } |
| 98 | + |
| 99 | + #if !os(Linux) && !os(Android) |
| 100 | + func testSaveCommand() throws { |
| 101 | + let score = GameScore(score: 10) |
| 102 | + let className = score.className |
| 103 | + |
| 104 | + let command = try score.saveCommand() |
| 105 | + XCTAssertNotNil(command) |
| 106 | + XCTAssertEqual(command.path.urlComponent, "/classes/\(className)") |
| 107 | + XCTAssertEqual(command.method, API.Method.POST) |
| 108 | + XCTAssertNil(command.params) |
| 109 | + XCTAssertNotNil(command.data) |
| 110 | + |
| 111 | + let expected = "GameScore ({\"score\":10,\"player\":\"Jen\"})" |
| 112 | + let decoded = score.debugDescription |
| 113 | + XCTAssertEqual(decoded, expected) |
| 114 | + } |
| 115 | + |
| 116 | + func testUpdateCommand() throws { |
| 117 | + var score = GameScore(score: 10) |
| 118 | + let className = score.className |
| 119 | + let objectId = "yarr" |
| 120 | + score.objectId = objectId |
| 121 | + score.createdAt = Date() |
| 122 | + score.updatedAt = score.createdAt |
| 123 | + |
| 124 | + let command = try score.saveCommand() |
| 125 | + XCTAssertNotNil(command) |
| 126 | + XCTAssertEqual(command.path.urlComponent, "/classes/\(className)/\(objectId)") |
| 127 | + XCTAssertEqual(command.method, API.Method.PUT) |
| 128 | + XCTAssertNil(command.params) |
| 129 | + XCTAssertNotNil(command.data) |
| 130 | + |
| 131 | + guard let body = command.body else { |
| 132 | + XCTFail("Should be able to unwrap") |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + let expected = "{\"score\":10,\"player\":\"Jen\"}" |
| 137 | + let encoded = try ParseCoding.parseEncoder() |
| 138 | + .encode(body, collectChildren: false, |
| 139 | + objectsSavedBeforeThisOne: nil, |
| 140 | + filesSavedBeforeThisOne: nil).encoded |
| 141 | + let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8)) |
| 142 | + XCTAssertEqual(decoded, expected) |
| 143 | + } |
| 144 | + #endif |
| 145 | +} |
0 commit comments