Skip to content

Commit 5d2a370

Browse files
authored
fix: throw .missingObjectId error when fetching (#300)
* fix: throw .missingObjectId error when fetching * fix flaky tests
1 parent ebaaf12 commit 5d2a370

File tree

9 files changed

+19
-16
lines changed

9 files changed

+19
-16
lines changed

Sources/ParseSwift/API/API+Command.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal extension API {
121121
currentNotificationQueue = callbackQueue
122122
}
123123
if !path.urlComponent.contains("/files/") {
124-
//All ParseObjects use the shared URLSession
124+
// All ParseObjects use the shared URLSession
125125
switch self.prepareURLRequest(options: options,
126126
childObjects: childObjects,
127127
childFiles: childFiles) {
@@ -395,7 +395,6 @@ internal extension API.Command {
395395
return create(object)
396396
}
397397

398-
// MARK: Saving ParseObjects - private
399398
static func create<T>(_ object: T) -> API.Command<T, T> where T: ParseObject {
400399
var object = object
401400
if object.ACL == nil,
@@ -439,10 +438,11 @@ internal extension API.Command {
439438
mapper: mapper)
440439
}
441440

442-
// MARK: Fetching
441+
// MARK: Fetching ParseObjects
443442
static func fetch<T>(_ object: T, include: [String]?) throws -> API.Command<T, T> where T: ParseObject {
444443
guard object.objectId != nil else {
445-
throw ParseError(code: .unknownError, message: "Cannot Fetch an object without id")
444+
throw ParseError(code: .missingObjectId,
445+
message: "objectId must not be nil")
446446
}
447447

448448
var params: [String: String]?
@@ -462,7 +462,7 @@ internal extension API.Command {
462462

463463
internal extension API.Command where T: ParseObject {
464464

465-
// MARK: Batch - Saving, Fetching
465+
// MARK: Batch - Saving, Fetching ParseObjects
466466
static func batch(commands: [API.Command<T, T>],
467467
transaction: Bool) -> RESTBatchCommandType<T> {
468468
let batchCommands = commands.compactMap { (command) -> API.Command<T, T>? in
@@ -514,7 +514,7 @@ internal extension API.Command where T: ParseObject {
514514
return RESTBatchCommandType<T>(method: .POST, path: .batch, body: batchCommand, mapper: mapper)
515515
}
516516

517-
// MARK: Batch - Deleting
517+
// MARK: Batch - Deleting ParseObjects
518518
static func batch(commands: [API.NonParseBodyCommand<NoBody, NoBody>],
519519
transaction: Bool) -> RESTBatchCommandNoBodyType<NoBody> {
520520
let commands = commands.compactMap { (command) -> API.NonParseBodyCommand<NoBody, NoBody>? in

Sources/ParseSwift/Objects/ParseInstallation+combine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public extension ParseInstallation {
8282

8383
- parameter options: A set of header options sent to the server. Defaults to an empty set.
8484
- returns: A publisher that eventually produces a single value and then finishes or fails.
85-
- important: If an object replcaed has the same objectId as current, it will automatically replace the current.
85+
- important: If an object replaced has the same objectId as current, it will automatically replace the current.
8686
*/
8787
func replacePublisher(options: API.Options = []) -> Future<Self, ParseError> {
8888
Future { promise in

Sources/ParseSwift/Objects/ParseInstallation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ extension ParseInstallation {
424424

425425
func fetchCommand(include: [String]?) throws -> API.Command<Self, Self> {
426426
guard objectId != nil else {
427-
throw ParseError(code: .unknownError, message: "Cannot fetch an object without id")
427+
throw ParseError(code: .missingObjectId,
428+
message: "objectId must not be nil")
428429
}
429430

430431
var params: [String: String]?

Sources/ParseSwift/Objects/ParseUser.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,8 @@ extension ParseUser {
815815

816816
func fetchCommand(include: [String]?) throws -> API.Command<Self, Self> {
817817
guard objectId != nil else {
818-
throw ParseError(code: .unknownError, message: "Cannot fetch an object without id")
818+
throw ParseError(code: .missingObjectId,
819+
message: "objectId must not be nil")
819820
}
820821

821822
var params: [String: String]?

Tests/ParseSwiftTests/ParseAnalyticsTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class ParseAnalyticsTests: XCTestCase {
119119
#if canImport(AppTrackingTransparency)
120120
func testTrackAppOpenedUIKitNotAuthorized() {
121121
if #available(macOS 11.0, iOS 14.0, macCatalyst 14.0, tvOS 14.0, *) {
122-
ParseSwift.configuration.isTestingSDK = false //Allow authorization check
122+
ParseSwift.configuration.isTestingSDK = false // Allow authorization check
123123
let expectation = XCTestExpectation(description: "Analytics save")
124124
let options = [UIApplication.LaunchOptionsKey.remoteNotification: ["stop": "drop"]]
125125
ParseAnalytics.trackAppOpened(launchOptions: options) { result in
@@ -192,7 +192,7 @@ class ParseAnalyticsTests: XCTestCase {
192192
#if canImport(AppTrackingTransparency)
193193
func testTrackAppOpenedNotAuthorized() {
194194
if #available(macOS 11.0, iOS 14.0, macCatalyst 14.0, tvOS 14.0, *) {
195-
ParseSwift.configuration.isTestingSDK = false //Allow authorization check
195+
ParseSwift.configuration.isTestingSDK = false // Allow authorization check
196196
let expectation = XCTestExpectation(description: "Analytics save")
197197
ParseAnalytics.trackAppOpened(dimensions: ["stop": "drop"]) { result in
198198

@@ -317,7 +317,7 @@ class ParseAnalyticsTests: XCTestCase {
317317
#if canImport(AppTrackingTransparency)
318318
func testTrackEventNotAuthorized() {
319319
if #available(macOS 11.0, iOS 14.0, macCatalyst 14.0, tvOS 14.0, *) {
320-
ParseSwift.configuration.isTestingSDK = false //Allow authorization check
320+
ParseSwift.configuration.isTestingSDK = false // Allow authorization check
321321

322322
let expectation = XCTestExpectation(description: "Analytics save")
323323
let event = ParseAnalytics(name: "hello")
@@ -338,7 +338,7 @@ class ParseAnalyticsTests: XCTestCase {
338338

339339
func testTrackEventNotAuthorizedMutated() {
340340
if #available(macOS 11.0, iOS 14.0, macCatalyst 14.0, tvOS 14.0, *) {
341-
ParseSwift.configuration.isTestingSDK = false //Allow authorization check
341+
ParseSwift.configuration.isTestingSDK = false // Allow authorization check
342342

343343
let expectation = XCTestExpectation(description: "Analytics save")
344344
var event = ParseAnalytics(name: "hello")

Tests/ParseSwiftTests/ParseInstallationTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ class ParseInstallationTests: XCTestCase { // swiftlint:disable:this type_body_l
351351
}
352352
installation.objectId = testInstallationObjectId
353353
installation.createdAt = Calendar.current.date(byAdding: .init(day: -1), to: Date())
354-
installation.updatedAt = Calendar.current.date(byAdding: .init(day: -1), to: Date())
355354
installation.ACL = nil
356355

357356
var installationOnServer = installation
@@ -400,7 +399,6 @@ class ParseInstallationTests: XCTestCase { // swiftlint:disable:this type_body_l
400399
}
401400
installation.objectId = testInstallationObjectId
402401
installation.createdAt = Calendar.current.date(byAdding: .init(day: -1), to: Date())
403-
installation.updatedAt = Calendar.current.date(byAdding: .init(day: -1), to: Date())
404402
installation.ACL = nil
405403

406404
var installationOnServer = installation
@@ -551,7 +549,6 @@ class ParseInstallationTests: XCTestCase { // swiftlint:disable:this type_body_l
551549
return
552550
}
553551
installation.objectId = testInstallationObjectId
554-
installation.createdAt = Calendar.current.date(byAdding: .init(day: -1), to: Date())
555552
installation.updatedAt = Calendar.current.date(byAdding: .init(day: -1), to: Date())
556553
installation.ACL = nil
557554

@@ -581,6 +578,7 @@ class ParseInstallationTests: XCTestCase { // swiftlint:disable:this type_body_l
581578

582579
func testFetchCommand() {
583580
var installation = Installation()
581+
XCTAssertThrowsError(try installation.fetchCommand(include: nil))
584582
let objectId = "yarr"
585583
installation.objectId = objectId
586584
do {

Tests/ParseSwiftTests/ParseObjectTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
333333
func testFetchCommand() {
334334
var score = GameScore(score: 10)
335335
let className = score.className
336+
XCTAssertThrowsError(try score.fetchCommand(include: nil))
336337
let objectId = "yarr"
337338
score.objectId = objectId
338339
do {

Tests/ParseSwiftTests/ParseSessionTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class ParseSessionTests: XCTestCase {
7878

7979
func testFetchCommand() throws {
8080
var session = Session<User>()
81+
XCTAssertThrowsError(try session.fetchCommand(include: nil))
8182
session.objectId = "me"
8283
do {
8384
let command = try session.fetchCommand(include: nil)

Tests/ParseSwiftTests/ParseUserTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class ParseUserTests: XCTestCase { // swiftlint:disable:this type_body_length
100100

101101
func testFetchCommand() {
102102
var user = User()
103+
XCTAssertThrowsError(try user.fetchCommand(include: nil))
103104
let objectId = "yarr"
104105
user.objectId = objectId
105106
do {

0 commit comments

Comments
 (0)