Skip to content

Commit

Permalink
Add a new function deleteIfExists -> Bool
Browse files Browse the repository at this point in the history
Closes #208
  • Loading branch information
brunoocasali committed Nov 11, 2021
1 parent 8520a1b commit 3a718c8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ public struct Indexes {
}
}

/**
Delete the index only if it exists.

- parameter completion: The completion closure used to notify when the server
completes the delete request, it returns a `Bool` that is `true`
If the request sucessfully deleted an existent index or `false` if a failure occured or the index do not exist.
*/
public func deleteIfExists(_ completion: @escaping (Bool) -> Void) {
self.request.delete(api: "/indexes/\(self.uid)") { result in
switch result {
case .success:
completion(true)
default:
completion(false)
}
}
}

// MARK: Document

/**
Expand Down
39 changes: 39 additions & 0 deletions Tests/MeiliSearchUnitTests/IndexesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,45 @@ class IndexesTests: XCTestCase {
self.wait(for: [expectation], timeout: 10.0)
}

func testDeleteIndexIfExists() {
// Prepare the mock server
session.pushEmpty(code: 204)

// Start the test with the mocked server
let expectation = XCTestExpectation(description: "Delete Movies index")

self.index.deleteIfExists { result in
if result {
XCTAssertTrue(result)
expectation.fulfill()
} else {
XCTFail("Failed to delete Movies index, it was not present on the server")
}
expectation.fulfill()
}

self.wait(for: [expectation], timeout: 10.0)
}

func testDeleteIndexIfExistsWhenIsnt() {
// Prepare the mock server
session.pushEmpty(code: 404)

// Start the test with the mocked server
let expectation = XCTestExpectation(description: "Delete Movies index only if exists")

self.index.deleteIfExists { result in
if !result {
XCTAssertFalse(result)
expectation.fulfill()
} else {
XCTFail("Deleting the index should have returned false as the index does not exist on the server")
}
expectation.fulfill()
}

self.wait(for: [expectation], timeout: 10.0)
}
}
// swiftlint:enable force_unwrapping
// swiftlint:enable force_try

0 comments on commit 3a718c8

Please sign in to comment.