Skip to content

Commit

Permalink
Merge #232
Browse files Browse the repository at this point in the history
232: Add a new function deleteIfExists r=bidoubiwa a=brunoocasali

## What does this PR do?
Fixes #208

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to MeiliSearch!


Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
  • Loading branch information
bors[bot] and brunoocasali authored Nov 11, 2021
2 parents 8520a1b + 3a718c8 commit 496a0d6
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 496a0d6

Please sign in to comment.