Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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