Skip to content

Commit

Permalink
Merge pull request #20 from csync/tgb/19-DeleteWildcard
Browse files Browse the repository at this point in the history
Allow swift sdk to delete a wildcard
  • Loading branch information
Narine C authored Feb 9, 2017
2 parents a4c2f7a + 0525b34 commit 6578d84
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CSyncSDK/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ class PubOperation : Operation
return nil
}

guard !key.isKeyPattern else {
error = err(CSError.invalidKey, msg:"Key may not contain wildcard characters")
guard !key.isKeyPattern || delete == true else {
error = err(CSError.invalidKey, msg:"Key for write may not contain wildcard characters")
return nil
}

Expand Down
149 changes: 149 additions & 0 deletions CSyncSDKTests/WriteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,153 @@ class WriteTests: XCTestCase {
waitForExpectations(timeout: 10.0, handler:nil)
}

func testDeleteWildcardThatDoesNotExist() {
let expectation = self.expectation(description: "\(#function)")

// Connect to the CSync store
let config = getConfig()
let app = App(host: config.host, port: config.port, options: config.options)
app.authenticate(config.authenticationProvider, token: config.token) { authData, error in
}

let testKey = app.key(testKeyString("\(#function)")+".*")
testKey.delete() {key, error in
// Wildcard deletes should always return success, even if nothing was deleted
assert(error == nil)
expectation.fulfill()
}
waitForExpectations(timeout: 10.0, handler:nil)
}

func testDeleteThatDoesNotExist() {
let expectation = self.expectation(description: "\(#function)")

// Connect to the CSync store
let config = getConfig()
let app = App(host: config.host, port: config.port, options: config.options)
app.authenticate(config.authenticationProvider, token: config.token) { authData, error in
}

let testKey = app.key(testKeyString("\(#function)"))
testKey.delete() {key, error in
// Single Key deletes will return an error if you delete something that doesn't exist.
assert(error?.code == CSError.requestError.rawValue)
expectation.fulfill()
}
waitForExpectations(timeout: 10.0, handler:nil)
}

func testDeleteWildcard(){
let expectation = self.expectation(description: "\(#function)")
let config = getConfig()
let uuid = UUID().uuidString
let app = App(host: config.host, port: config.port, options: config.options)
app.authenticate(config.authenticationProvider, token: config.token) { authData, error in
//Check to be sure the right 3 keys are deleted
var keyOne = false, keyTwo = false, keyThree = false
let listenKey = app.key("tests.DeleteWildcard." + uuid + "a.*")
let writeKey = app.key("tests.DeleteWildcard." + uuid + "a.b")
writeKey.write("b")
let writeKey2 = app.key("tests.DeleteWildcard." + uuid + "a.c")
writeKey2.write("c")
let writeKey3 = app.key("tests.DeleteWildcard." + uuid + "a.d")
writeKey3.write("d")
let writeKey4 = app.key("tests.DeleteWildcard." + uuid + "b.e")
writeKey4.write("be")
let writeKey5 = app.key("tests.DeleteWildcard." + uuid + "a.e.f")
writeKey5.write("aef")
let writeKey6 = app.key("tests.DeleteWildcard." + uuid + "b.a.g")
writeKey6.write("bag")
listenKey.listen { (value, error) -> () in
if let key = value?.key {
if key == "tests.DeleteWildcard." + uuid + "a.b" && (value?.exists)! == false {
keyOne = true
} else if key == "tests.DeleteWildcard." + uuid + "a.c" && value?.exists == false {
keyTwo = true
} else if key == "tests.DeleteWildcard." + uuid + "a.d" && value?.exists == false {
keyThree = true
} else if key == "tests.DeleteWildcard." + uuid + "b.e" && value?.exists == false {
XCTFail("a.* delete should not delete b.e")
} else if key == "tests.DeleteWildcard." + uuid + "a.e.f" && value?.exists == false {
XCTFail("a.* delete should not delete a.e.f")
} else if key == "tests.DeleteWildcard." + uuid + "b.a.g" && value?.exists == false {
XCTFail("a.* delete should not delete b.a.g")
}
//If all three keys that we expected to get deleted were deleted, pass
if keyOne && keyTwo && keyThree {
expectation.fulfill()
}
}
}
let writeKey7 = app.key("tests.DeleteWildcard." + uuid + "a.*")
writeKey7.delete()
}
waitForExpectations(timeout: 20.0, handler:nil)
}

func testDeleteWildcardInMiddle(){
let expectation = self.expectation(description: "\(#function)")
let config = getConfig()
let uuid = UUID().uuidString
let app = App(host: config.host, port: config.port, options: config.options)
app.authenticate(config.authenticationProvider, token: config.token) { authData, error in
//Check to be sure the right 3 keys are deleted
var keyOne = false, keyTwo = false, keyThree = false
let listenKey = app.key("tests.DeleteWildcardInMiddle." + uuid + "a.*.e")
let writeKey = app.key("tests.DeleteWildcardInMiddle." + uuid + "a.b.e")
writeKey.write("b")
let writeKey2 = app.key("tests.DeleteWildcardInMiddle." + uuid + "a.c.e")
writeKey2.write("c")
let writeKey3 = app.key("tests.DeleteWildcardInMiddle." + uuid + "a.d.e")
writeKey3.write("d")
let writeKey4 = app.key("tests.DeleteWildcardInMiddle." + uuid + "b.e.e")
writeKey4.write("be")
let writeKey5 = app.key("tests.DeleteWildcardInMiddle." + uuid + "a.e.e.f")
writeKey5.write("aef")
let writeKey6 = app.key("tests.DeleteWildcardInMiddle." + uuid + "b.a.e.g")
writeKey6.write("bag")
listenKey.listen { (value, error) -> () in
if let key = value?.key {
if key == "tests.DeleteWildcardInMiddle." + uuid + "a.b.e" && (value?.exists)! == false {
keyOne = true
} else if key == "tests.DeleteWildcardInMiddle." + uuid + "a.c.e" && value?.exists == false {
keyTwo = true
} else if key == "tests.DeleteWildcardInMiddle." + uuid + "a.d.e" && value?.exists == false {
keyThree = true
} else if key == "tests.DeleteWildcardInMiddle." + uuid + "b.e.e" && value?.exists == false {
XCTFail("a.* delete should not delete b.e")
} else if key == "tests.DeleteWildcardInMiddle." + uuid + "a.e.e.f" && value?.exists == false {
XCTFail("a.* delete should not delete a.e.f")
} else if key == "tests.DeleteWildcardInMiddle." + uuid + "b.a.e.g" && value?.exists == false {
XCTFail("a.* delete should not delete b.a.g")
}
//If all three keys that we expected to get deleted were deleted, pass
if keyOne && keyTwo && keyThree {
expectation.fulfill()
}
}
}
let writeKey7 = app.key("tests.DeleteWildcardInMiddle." + uuid + "a.*.e")
writeKey7.delete()
}
waitForExpectations(timeout: 20.0, handler:nil)
}

func testDeleteNonexistantWildcard(){
//Deleting something that does not exist should return a success
let expectation = self.expectation(description: "\(#function)")
let config = getConfig()
let app = App(host: config.host, port: config.port, options: config.options)
app.authenticate(config.authenticationProvider, token: config.token) { authData, error in
let writeKey7 = app.key("tests.DeleteNonexistantWildcard.*")
writeKey7.delete(){ key, error in
assert(error == nil)
assert(key.key == "tests.DeleteNonexistantWildcard.*")
expectation.fulfill()
}

}
waitForExpectations(timeout: 10.0, handler:nil)
}

}

0 comments on commit 6578d84

Please sign in to comment.