diff --git a/Tests/AssistantV1Tests/AssistantTests.swift b/Tests/AssistantV1Tests/AssistantTests.swift index 5e6a18420..879fe4c78 100644 --- a/Tests/AssistantV1Tests/AssistantTests.swift +++ b/Tests/AssistantV1Tests/AssistantTests.swift @@ -205,41 +205,41 @@ class AssistantTests: XCTestCase { entities: entities, context: context, output: output) { - response, error in - - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let message = response?.result else { - XCTFail(missingResultMessage) - return - } + response, error in - // verify objects are non-nil - XCTAssertNotNil(entities) - XCTAssertNotNil(intents) - XCTAssertNotNil(output) + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let message = response?.result else { + XCTFail(missingResultMessage) + return + } - // verify intents are equal - for index in 0.. Data? { #if os(Linux) - let url = URL(fileURLWithPath: "Tests/DiscoveryV1Tests/Resources/" + name + "." + ext) + let url = URL(fileURLWithPath: "Tests/DiscoveryV1Tests/Resources/" + name + "." + ext) #else - let bundle = Bundle(for: type(of: self)) - guard let url = bundle.url(forResource: name, withExtension: ext) else { return nil } + let bundle = Bundle(for: type(of: self)) + guard let url = bundle.url(forResource: name, withExtension: ext) else { return nil } #endif let data = try? Data(contentsOf: url) return data diff --git a/Tests/DiscoveryV1Tests/DiscoveryTests.swift b/Tests/DiscoveryV1Tests/DiscoveryTests.swift index 21d270b52..626b7e47c 100644 --- a/Tests/DiscoveryV1Tests/DiscoveryTests.swift +++ b/Tests/DiscoveryV1Tests/DiscoveryTests.swift @@ -54,10 +54,10 @@ class DiscoveryTests: XCTestCase { func loadDocument(name: String, ext: String) -> Data? { #if os(Linux) - let url = URL(fileURLWithPath: "Tests/DiscoveryV1Tests/Resources/" + name + "." + ext) + let url = URL(fileURLWithPath: "Tests/DiscoveryV1Tests/Resources/" + name + "." + ext) #else - let bundle = Bundle(for: type(of: self)) - guard let url = bundle.url(forResource: name, withExtension: ext) else { return nil } + let bundle = Bundle(for: type(of: self)) + guard let url = bundle.url(forResource: name, withExtension: ext) else { return nil } #endif let data = try? Data(contentsOf: url) return data @@ -268,19 +268,19 @@ class DiscoveryTests: XCTestCase { description: "A collection created while testing the Swift SDK. Safe to delete.", configurationID: configurationID, language: language) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - collection = result - expectation.fulfill() + collection = result + expectation.fulfill() } waitForExpectations(timeout: timeout) return collection @@ -296,19 +296,19 @@ class DiscoveryTests: XCTestCase { collectionID: collectionID, file: document, filename: "KennedySpeech.html") { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - documentAccepted = result - expectation.fulfill() + documentAccepted = result + expectation.fulfill() } waitForExpectations(timeout: timeout) return documentAccepted @@ -633,26 +633,26 @@ class DiscoveryTests: XCTestCase { name: name, description: description, source: source) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - configuration = result - XCTAssertEqual(configuration.name, name) - XCTAssertEqual(configuration.description, description) - XCTAssertNotNil(configuration.source) - XCTAssertEqual(configuration.source?.type, source.type) - XCTAssertEqual(configuration.source?.credentialID, "my box credentialID") - XCTAssertEqual(configuration.source?.schedule?.timeZone, sourceSchedule.timeZone) - XCTAssertEqual(configuration.source?.schedule?.frequency, SourceSchedule.Frequency.weekly.rawValue) - expectation1.fulfill() + configuration = result + XCTAssertEqual(configuration.name, name) + XCTAssertEqual(configuration.description, description) + XCTAssertNotNil(configuration.source) + XCTAssertEqual(configuration.source?.type, source.type) + XCTAssertEqual(configuration.source?.credentialID, "my box credentialID") + XCTAssertEqual(configuration.source?.schedule?.timeZone, sourceSchedule.timeZone) + XCTAssertEqual(configuration.source?.schedule?.frequency, SourceSchedule.Frequency.weekly.rawValue) + expectation1.fulfill() } waitForExpectations(timeout: timeout) @@ -773,30 +773,30 @@ class DiscoveryTests: XCTestCase { description: "A collection created while testing the Swift SDK. Safe to delete.", configurationID: configuration.configurationID!, language: "en") { - response, error in + response, error in - if let error = error { - let allowedErrorMessage = "Your environment does not allow more than 2 collections to be added." - if !(error.localizedDescription.contains(allowedErrorMessage)) { - XCTFail(unexpectedErrorMessage(error)) + if let error = error { + let allowedErrorMessage = "Your environment does not allow more than 2 collections to be added." + if !(error.localizedDescription.contains(allowedErrorMessage)) { + XCTFail(unexpectedErrorMessage(error)) + } + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return } - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } - collection = result + collection = result - XCTAssertNotNil(collection.name) - XCTAssertEqual(collection.name!, collectionName) - XCTAssertEqual(collection.description, "A collection created while testing the Swift SDK. Safe to delete.") - XCTAssertNotNil(collection.configurationID) - XCTAssertEqual(collection.configurationID!, configuration.configurationID!) - XCTAssertNotNil(collection.language) - XCTAssertEqual(collection.language!, "en") - expectation1.fulfill() + XCTAssertNotNil(collection.name) + XCTAssertEqual(collection.name!, collectionName) + XCTAssertEqual(collection.description, "A collection created while testing the Swift SDK. Safe to delete.") + XCTAssertNotNil(collection.configurationID) + XCTAssertEqual(collection.configurationID!, configuration.configurationID!) + XCTAssertNotNil(collection.language) + XCTAssertEqual(collection.language!, "en") + expectation1.fulfill() } waitForExpectations(timeout: timeout) @@ -908,21 +908,21 @@ class DiscoveryTests: XCTestCase { environmentID: environmentID, collectionID: collectionID, expansions: [expansion]) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertEqual(result.expansions.count, 1) - XCTAssertEqual(result.expansions.first!.expandedTerms.count, 2) - XCTAssertEqual(result.expansions.first!.inputTerms!.count, 1) - expectation1.fulfill() + XCTAssertEqual(result.expansions.count, 1) + XCTAssertEqual(result.expansions.first!.expandedTerms.count, 2) + XCTAssertEqual(result.expansions.first!.inputTerms!.count, 1) + expectation1.fulfill() } waitForExpectations(timeout: timeout) @@ -1032,22 +1032,22 @@ class DiscoveryTests: XCTestCase { file: document, filename: "KennedySpeech.html", metadata: metadata) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - documentID = result.documentID - XCTAssertNotNil(result.documentID) - XCTAssert(result.status == "pending" || result.status == "available") - XCTAssertNil(result.notices) - expectation1.fulfill() + documentID = result.documentID + XCTAssertNotNil(result.documentID) + XCTAssert(result.status == "pending" || result.status == "available") + XCTAssertNil(result.notices) + expectation1.fulfill() } waitForExpectations(timeout: timeout) @@ -1058,24 +1058,24 @@ class DiscoveryTests: XCTestCase { environmentID: environmentID, collectionID: collectionID, documentID: documentID) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertEqual(result.documentID, documentID) - XCTAssert(result.status == "processing" || result.status == "available") - XCTAssertGreaterThan(result.statusDescription.count, 0) - XCTAssertEqual(result.filename, "KennedySpeech.html") - XCTAssertEqual(result.fileType, "html") - XCTAssertEqual(result.notices.count, 0) - expectation2.fulfill() + XCTAssertEqual(result.documentID, documentID) + XCTAssert(result.status == "processing" || result.status == "available") + XCTAssertGreaterThan(result.statusDescription.count, 0) + XCTAssertEqual(result.filename, "KennedySpeech.html") + XCTAssertEqual(result.fileType, "html") + XCTAssertEqual(result.notices.count, 0) + expectation2.fulfill() } waitForExpectations(timeout: timeout) @@ -1086,21 +1086,21 @@ class DiscoveryTests: XCTestCase { collectionID: collectionID, documentID: documentID, metadata: newMetadata) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } - documentID = result.documentID - XCTAssertNotNil(result.documentID) - XCTAssert(result.status == "pending" || result.status == "available") - XCTAssertNil(result.notices) - expectation3.fulfill() + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } + documentID = result.documentID + XCTAssertNotNil(result.documentID) + XCTAssert(result.status == "pending" || result.status == "available") + XCTAssertNil(result.notices) + expectation3.fulfill() } waitForExpectations(timeout: timeout) @@ -1111,20 +1111,20 @@ class DiscoveryTests: XCTestCase { environmentID: environmentID, collectionID: collectionID, documentID: documentID) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertEqual(result.documentID, documentID) - XCTAssertEqual(result.status, "deleted") - expectation4.fulfill() + XCTAssertEqual(result.documentID, documentID) + XCTAssertEqual(result.status, "deleted") + expectation4.fulfill() } waitForExpectations(timeout: timeout) } @@ -1145,27 +1145,27 @@ class DiscoveryTests: XCTestCase { highlight: true, deduplicate: true, deduplicateField: "title") { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.matchingResults) - XCTAssertGreaterThan(query.matchingResults!, 0) - XCTAssertNotNil(query.results) - XCTAssertEqual(query.results!.count, 5) - for result in query.results! { - XCTAssertNotNil(result.id) - XCTAssertNotNil(result.resultMetadata) - XCTAssertNotNil(result.resultMetadata!.score) - } - expectation.fulfill() + XCTAssertNotNil(query.matchingResults) + XCTAssertGreaterThan(query.matchingResults!, 0) + XCTAssertNotNil(query.results) + XCTAssertEqual(query.results!.count, 5) + for result in query.results! { + XCTAssertNotNil(result.id) + XCTAssertNotNil(result.resultMetadata) + XCTAssertNotNil(result.resultMetadata!.score) + } + expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1177,27 +1177,27 @@ class DiscoveryTests: XCTestCase { collectionID: newsCollectionID, naturalLanguageQuery: "Kubernetes", count: 5) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.matchingResults) - XCTAssertGreaterThan(query.matchingResults!, 0) - XCTAssertNotNil(query.results) - XCTAssertEqual(query.results!.count, 5) - for result in query.results! { - XCTAssertNotNil(result.id) - XCTAssertNotNil(result.resultMetadata) - XCTAssertNotNil(result.resultMetadata!.score) - } - expectation.fulfill() + XCTAssertNotNil(query.matchingResults) + XCTAssertGreaterThan(query.matchingResults!, 0) + XCTAssertNotNil(query.results) + XCTAssertEqual(query.results!.count, 5) + for result in query.results! { + XCTAssertNotNil(result.id) + XCTAssertNotNil(result.resultMetadata) + XCTAssertNotNil(result.resultMetadata!.score) + } + expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1212,21 +1212,21 @@ class DiscoveryTests: XCTestCase { passagesFields: "text", passagesCount: 1, passagesCharacters: 400) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.matchingResults) - XCTAssertGreaterThan(query.matchingResults!, 0) - XCTAssertNotNil(query.passages) - expectation.fulfill() + XCTAssertNotNil(query.matchingResults) + XCTAssertGreaterThan(query.matchingResults!, 0) + XCTAssertNotNil(query.passages) + expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1240,20 +1240,20 @@ class DiscoveryTests: XCTestCase { similar: true, similarDocumentIDs: nil, similarFields: "text") { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.matchingResults) - XCTAssertGreaterThan(query.matchingResults!, 0) - expectation.fulfill() + XCTAssertNotNil(query.matchingResults) + XCTAssertGreaterThan(query.matchingResults!, 0) + expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1266,34 +1266,34 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "term(enriched_text.concepts.text,count:10)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .term(term) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .term(term) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(term.type, "term") + XCTAssertEqual(term.field, "enriched_text.concepts.text") + XCTAssertEqual(term.count, 10) + XCTAssertNotNil(term.results) + XCTAssertEqual(term.results!.count, 10) + XCTAssertNotNil(term.results!.first!.key) + XCTAssertEqual(term.results!.first!.key, "Cloud computing") + XCTAssertNotNil(term.results!.first!.matchingResults) + XCTAssertGreaterThan(term.results!.first!.matchingResults!, 0) expectation.fulfill() - return - } - XCTAssertEqual(term.type, "term") - XCTAssertEqual(term.field, "enriched_text.concepts.text") - XCTAssertEqual(term.count, 10) - XCTAssertNotNil(term.results) - XCTAssertEqual(term.results!.count, 10) - XCTAssertNotNil(term.results!.first!.key) - XCTAssertEqual(term.results!.first!.key, "Cloud computing") - XCTAssertNotNil(term.results!.first!.matchingResults) - XCTAssertGreaterThan(term.results!.first!.matchingResults!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1306,29 +1306,29 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "filter(enriched_text.concepts.text:\"cloud computing\")", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .filter(filter) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .filter(filter) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(filter.type, "filter") + XCTAssertEqual(filter.match, "enriched_text.concepts.text:\"cloud computing\"") + XCTAssertNotNil(filter.matchingResults) + XCTAssertGreaterThan(filter.matchingResults!, 0) expectation.fulfill() - return - } - XCTAssertEqual(filter.type, "filter") - XCTAssertEqual(filter.match, "enriched_text.concepts.text:\"cloud computing\"") - XCTAssertNotNil(filter.matchingResults) - XCTAssertGreaterThan(filter.matchingResults!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1341,29 +1341,29 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "nested(enriched_text.entities)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .nested(nested) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .nested(nested) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(nested.type, "nested") + XCTAssertEqual(nested.path, "enriched_text.entities") + XCTAssertNotNil(nested.matchingResults) + XCTAssertGreaterThan(nested.matchingResults!, 0) expectation.fulfill() - return - } - XCTAssertEqual(nested.type, "nested") - XCTAssertEqual(nested.path, "enriched_text.entities") - XCTAssertNotNil(nested.matchingResults) - XCTAssertGreaterThan(nested.matchingResults!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1376,34 +1376,34 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "histogram(enriched_text.concepts.relevance,interval:1)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .histogram(histogram) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .histogram(histogram) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(histogram.type, "histogram") + XCTAssertEqual(histogram.field, "enriched_text.concepts.relevance") + XCTAssertEqual(histogram.interval, 1) + XCTAssertNotNil(histogram.results) + XCTAssertGreaterThan(histogram.results!.count, 0) + XCTAssertNotNil(histogram.results!.first!.key) + XCTAssertEqual(histogram.results!.first!.key!, "0") + XCTAssertNotNil(histogram.results!.first!.matchingResults) + XCTAssertGreaterThan(histogram.results!.first!.matchingResults!, 0) expectation.fulfill() - return - } - XCTAssertEqual(histogram.type, "histogram") - XCTAssertEqual(histogram.field, "enriched_text.concepts.relevance") - XCTAssertEqual(histogram.interval, 1) - XCTAssertNotNil(histogram.results) - XCTAssertGreaterThan(histogram.results!.count, 0) - XCTAssertNotNil(histogram.results!.first!.key) - XCTAssertEqual(histogram.results!.first!.key!, "0") - XCTAssertNotNil(histogram.results!.first!.matchingResults) - XCTAssertGreaterThan(histogram.results!.first!.matchingResults!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1416,33 +1416,33 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "timeslice(publication_date,12hours)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .timeslice(timeslice) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .timeslice(timeslice) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(timeslice.type, "timeslice") + XCTAssertEqual(timeslice.field, "publication_date") + XCTAssertEqual(timeslice.interval, "12h") + XCTAssertNotNil(timeslice.results) + XCTAssertGreaterThan(timeslice.results!.count, 0) + XCTAssertNotNil(timeslice.results!.first!.key) + XCTAssertNotNil(timeslice.results!.first!.matchingResults) + XCTAssertGreaterThan(timeslice.results!.first!.matchingResults!, 0) expectation.fulfill() - return - } - XCTAssertEqual(timeslice.type, "timeslice") - XCTAssertEqual(timeslice.field, "publication_date") - XCTAssertEqual(timeslice.interval, "12h") - XCTAssertNotNil(timeslice.results) - XCTAssertGreaterThan(timeslice.results!.count, 0) - XCTAssertNotNil(timeslice.results!.first!.key) - XCTAssertNotNil(timeslice.results!.first!.matchingResults) - XCTAssertGreaterThan(timeslice.results!.first!.matchingResults!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1455,32 +1455,32 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "top_hits(1)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .topHits(topHits) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .topHits(topHits) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(topHits.type, "top_hits") + XCTAssertEqual(topHits.size, 1) + XCTAssertNotNil(topHits.hits) + XCTAssertNotNil(topHits.hits!.matchingResults) + XCTAssertGreaterThan(topHits.hits!.matchingResults!, 0) + XCTAssertNotNil(topHits.hits!.hits) + XCTAssertGreaterThan(topHits.hits!.hits!.count, 0) expectation.fulfill() - return - } - XCTAssertEqual(topHits.type, "top_hits") - XCTAssertEqual(topHits.size, 1) - XCTAssertNotNil(topHits.hits) - XCTAssertNotNil(topHits.hits!.matchingResults) - XCTAssertGreaterThan(topHits.hits!.matchingResults!, 0) - XCTAssertNotNil(topHits.hits!.hits) - XCTAssertGreaterThan(topHits.hits!.hits!.count, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1493,29 +1493,29 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "unique_count(enriched_text.keywords.text)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .uniqueCount(uniqueCount) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .uniqueCount(uniqueCount) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(uniqueCount.type, "unique_count") + XCTAssertEqual(uniqueCount.field, "enriched_text.keywords.text") + XCTAssertNotNil(uniqueCount.value) + XCTAssertGreaterThan(uniqueCount.value!, 0) expectation.fulfill() - return - } - XCTAssertEqual(uniqueCount.type, "unique_count") - XCTAssertEqual(uniqueCount.field, "enriched_text.keywords.text") - XCTAssertNotNil(uniqueCount.value) - XCTAssertGreaterThan(uniqueCount.value!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1528,29 +1528,29 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "max(enriched_text.entities.count)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .max(calculation) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .max(calculation) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(calculation.type, "max") + XCTAssertEqual(calculation.field, "enriched_text.entities.count") + XCTAssertNotNil(calculation.value) + XCTAssertGreaterThan(calculation.value!, 0) expectation.fulfill() - return - } - XCTAssertEqual(calculation.type, "max") - XCTAssertEqual(calculation.field, "enriched_text.entities.count") - XCTAssertNotNil(calculation.value) - XCTAssertGreaterThan(calculation.value!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1563,29 +1563,29 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "min(enriched_text.entities.count)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .min(calculation) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .min(calculation) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(calculation.type, "min") + XCTAssertEqual(calculation.field, "enriched_text.entities.count") + XCTAssertNotNil(calculation.value) + XCTAssertGreaterThan(calculation.value!, 0) expectation.fulfill() - return - } - XCTAssertEqual(calculation.type, "min") - XCTAssertEqual(calculation.field, "enriched_text.entities.count") - XCTAssertNotNil(calculation.value) - XCTAssertGreaterThan(calculation.value!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1598,29 +1598,29 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "average(enriched_text.entities.count)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .average(calculation) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .average(calculation) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(calculation.type, "average") + XCTAssertEqual(calculation.field, "enriched_text.entities.count") + XCTAssertNotNil(calculation.value) + XCTAssertGreaterThan(calculation.value!, 0) expectation.fulfill() - return - } - XCTAssertEqual(calculation.type, "average") - XCTAssertEqual(calculation.field, "enriched_text.entities.count") - XCTAssertNotNil(calculation.value) - XCTAssertGreaterThan(calculation.value!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1633,29 +1633,29 @@ class DiscoveryTests: XCTestCase { query: "enriched_text.concepts.text:\"Cloud computing\"", aggregation: "sum(enriched_text.entities.count)", count: 1) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let query = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let query = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertNotNil(query.aggregations) - XCTAssertEqual(query.aggregations!.count, 1) - guard case let .sum(calculation) = query.aggregations!.first! else { - XCTFail(self.unexpectedAggregationTypeMessage) + XCTAssertNotNil(query.aggregations) + XCTAssertEqual(query.aggregations!.count, 1) + guard case let .sum(calculation) = query.aggregations!.first! else { + XCTFail(self.unexpectedAggregationTypeMessage) + expectation.fulfill() + return + } + XCTAssertEqual(calculation.type, "sum") + XCTAssertEqual(calculation.field, "enriched_text.entities.count") + XCTAssertNotNil(calculation.value) + XCTAssertGreaterThan(calculation.value!, 0) expectation.fulfill() - return - } - XCTAssertEqual(calculation.type, "sum") - XCTAssertEqual(calculation.field, "enriched_text.entities.count") - XCTAssertNotNil(calculation.value) - XCTAssertGreaterThan(calculation.value!, 0) - expectation.fulfill() } waitForExpectations(timeout: timeout) } @@ -1924,25 +1924,25 @@ class DiscoveryTests: XCTestCase { naturalLanguageQuery: "1962 State of the Union", filter: "text:politics", examples: [example]) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - trainingQuery = result - XCTAssertNotNil(result.queryID) - XCTAssertEqual(result.naturalLanguageQuery, "1962 State of the Union") - XCTAssertNotNil(result.filter) - XCTAssertEqual(result.filter, "text:politics") - XCTAssertNotNil(result.examples) - XCTAssertEqual(result.examples!.count, 1) - expectation1.fulfill() + trainingQuery = result + XCTAssertNotNil(result.queryID) + XCTAssertEqual(result.naturalLanguageQuery, "1962 State of the Union") + XCTAssertNotNil(result.filter) + XCTAssertEqual(result.filter, "text:politics") + XCTAssertNotNil(result.examples) + XCTAssertEqual(result.examples!.count, 1) + expectation1.fulfill() } waitForExpectations(timeout: timeout) @@ -2029,19 +2029,19 @@ class DiscoveryTests: XCTestCase { naturalLanguageQuery: "1962 State of the Union", filter: "text:politics", examples: [example]) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - trainingQuery = result - expectation1.fulfill() + trainingQuery = result + expectation1.fulfill() } waitForExpectations(timeout: timeout) @@ -2095,19 +2095,19 @@ class DiscoveryTests: XCTestCase { collectionID: collectionID, naturalLanguageQuery: "1962 State of the Union", filter: "text:politics") { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - trainingQuery = result - expectation1.fulfill() + trainingQuery = result + expectation1.fulfill() } waitForExpectations(timeout: timeout) @@ -2119,20 +2119,20 @@ class DiscoveryTests: XCTestCase { queryID: queryID, documentID: documentID, relevance: 4) { - response, error in + response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let result = response?.result else { - XCTFail(missingResultMessage) - return - } + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let result = response?.result else { + XCTFail(missingResultMessage) + return + } - XCTAssertEqual(result.documentID, documentID) - XCTAssertEqual(result.relevance, 4) - expectation2.fulfill() + XCTAssertEqual(result.documentID, documentID) + XCTAssertEqual(result.relevance, 4) + expectation2.fulfill() } waitForExpectations(timeout: timeout) @@ -2230,7 +2230,7 @@ class DiscoveryTests: XCTestCase { var credentials: Credentials? let createDetails = CredentialDetails(credentialType: "username_password", url: "https://login.salesforce.com", - username: "email@server.xyz", password: "{my_salesforce_password}{my_salesforce_security_token}") + username: "email@server.xyz", password: "{my_salesforce_password}{my_salesforce_security_token}") discovery.createCredentials(environmentID: environmentID, sourceType: Source.TypeEnum.salesforce.rawValue, credentialDetails: createDetails) { response, error in diff --git a/Tests/DiscoveryV2Tests/DiscoveryV2Tests.swift b/Tests/DiscoveryV2Tests/DiscoveryV2Tests.swift index c6c510a8b..04f36aeb8 100644 --- a/Tests/DiscoveryV2Tests/DiscoveryV2Tests.swift +++ b/Tests/DiscoveryV2Tests/DiscoveryV2Tests.swift @@ -40,7 +40,7 @@ class DiscoveryTests: XCTestCase { let authenticator = WatsonIAMAuthenticator(apiKey: WatsonCredentials.DiscoveryV2APIKey) discovery = Discovery(version: "2020-08-12", authenticator: authenticator) - + discovery.serviceURL = WatsonCredentials.DiscoveryV2ServiceURL discovery.defaultHeaders["X-Watson-Learning-Opt-Out"] = "true" @@ -818,14 +818,14 @@ class DiscoveryTests: XCTestCase { waitForExpectations(timeout: timeout) } - + func testCollectionCRUD() { let createExpectation = self.expectation(description: "createCollection") var generatedCollectionID: String! - + discovery.createCollection(projectID: projectID, name: "swift-test-collection", description: "generated by the swift sdk integration test suite", language: "en-US", enrichments: nil, headers: nil) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -839,19 +839,19 @@ class DiscoveryTests: XCTestCase { XCTAssertNotNil(result.collectionID) XCTAssertEqual(result.name, "swift-test-collection") - + generatedCollectionID = result.collectionID! createExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let getExpectation = self.expectation(description: "getCollection") - + discovery.getCollection(projectID: projectID, collectionID: generatedCollectionID, headers: nil) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -865,17 +865,17 @@ class DiscoveryTests: XCTestCase { XCTAssertNotNil(result.collectionID) XCTAssertEqual(result.name, "swift-test-collection") - + getExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let updateExpectation = self.expectation(description: "updateCollection") - + discovery.updateCollection(projectID: projectID, collectionID: generatedCollectionID, name: "updated-generated-swift-sdk", description: "updated by the integration test", enrichments: nil, headers: nil) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -889,39 +889,39 @@ class DiscoveryTests: XCTestCase { XCTAssertNotNil(result.collectionID) XCTAssertEqual(result.name, "updated-generated-swift-sdk") - + updateExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let deleteExpectation = self.expectation(description: "deleteCollection") - + discovery.deleteCollection(projectID: projectID, collectionID: generatedCollectionID, headers: nil) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) return } - + deleteExpectation.fulfill() } - + waitForExpectations(timeout: timeout) } - + func testEnrichmentCRUD() { let createExpectation = self.expectation(description: "createEnrichment") var generatedEnrichmentID: String! - + let enrichmentData = loadDocument(name: "TestEnrichments", ext: "csv") let enrichmentOptions = EnrichmentOptions(languages: ["en"], entityType: "keyword", regularExpression: nil, resultField: nil) let enrichment = CreateEnrichment(name: "Dictionary", description: "test dictionary", type: "dictionary", options: enrichmentOptions) discovery.createEnrichment(projectID: projectID, file: enrichmentData, enrichment: enrichment) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -932,21 +932,21 @@ class DiscoveryTests: XCTestCase { XCTFail("No response") return } - + XCTAssertNotNil(result.enrichmentID) - + generatedEnrichmentID = result.enrichmentID - + createExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let listExpectation = self.expectation(description: "listEnrichments") - + discovery.listEnrichments(projectID: projectID) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -957,19 +957,19 @@ class DiscoveryTests: XCTestCase { XCTFail("No response") return } - + XCTAssertNotNil(result.enrichments?.firstIndex { $0.enrichmentID == generatedEnrichmentID }) - + listExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let updateExpectation = self.expectation(description: "updateEnrichment") - + discovery.updateEnrichment(projectID: projectID, enrichmentID: generatedEnrichmentID, name: "updated-enrichment-swift", description: "updated by integration test") { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -980,39 +980,39 @@ class DiscoveryTests: XCTestCase { XCTFail("No response") return } - + XCTAssertEqual(result.enrichmentID, generatedEnrichmentID) XCTAssertEqual(result.name, "updated-enrichment-swift") - + updateExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let deleteExpectation = self.expectation(description: "deleteEnrichment") - + discovery.deleteEnrichment(projectID: projectID, enrichmentID: generatedEnrichmentID) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) return } - + deleteExpectation.fulfill() } - + waitForExpectations(timeout: timeout) } - + func testProjectCRUD() { let createExpectation = self.expectation(description: "createProject") var generatedProjectID: String! - + discovery.createProject(name: "swift-test-project-new-2", type: "document_retrieval", defaultQueryParameters: nil) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -1023,22 +1023,22 @@ class DiscoveryTests: XCTestCase { XCTFail("No response") return } - + XCTAssertNotNil(result.projectID) XCTAssertEqual(result.name, "swift-test-project-new-2") - + generatedProjectID = result.projectID - + createExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let listExpectation = self.expectation(description: "listProjects") - + discovery.listProjects() { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -1049,19 +1049,19 @@ class DiscoveryTests: XCTestCase { XCTFail("No response") return } - + XCTAssertNotNil(result.projects?.firstIndex { $0.projectID == generatedProjectID }) - + listExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let updateExpectation = self.expectation(description: "updateProject") - + discovery.updateProject(projectID: generatedProjectID, name: "updated-swift-project") { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) @@ -1072,29 +1072,29 @@ class DiscoveryTests: XCTestCase { XCTFail("No response") return } - + XCTAssertEqual(result.projectID, generatedProjectID) XCTAssertEqual(result.name, "updated-swift-project") - + updateExpectation.fulfill() } - + waitForExpectations(timeout: timeout) - + let deleteExpectation = self.expectation(description: "deleteProject") - + discovery.deleteProject(projectID: generatedProjectID) { response, error in - + if let error = error { debugPrint(error.localizedDescription) XCTFail(unexpectedErrorMessage(error)) return } - + deleteExpectation.fulfill() } - + waitForExpectations(timeout: timeout) } } diff --git a/Tests/LanguageTranslatorV3Tests/LanguageTranslatorTests.swift b/Tests/LanguageTranslatorV3Tests/LanguageTranslatorTests.swift index b9c6e4b20..05409f145 100644 --- a/Tests/LanguageTranslatorV3Tests/LanguageTranslatorTests.swift +++ b/Tests/LanguageTranslatorV3Tests/LanguageTranslatorTests.swift @@ -193,13 +193,13 @@ class LanguageTranslatorTests: XCTestCase { let expectation = self.expectation(description: "Create and delete a custom language model.") #if os(Linux) - let url = URL(fileURLWithPath: "Tests/LanguageTranslatorV3Tests/Resources/glossary.tmx") + let url = URL(fileURLWithPath: "Tests/LanguageTranslatorV3Tests/Resources/glossary.tmx") #else - let bundle = Bundle(for: type(of: self)) - guard let url = bundle.url(forResource: "glossary", withExtension: "tmx") else { - XCTFail("Unable to read forced glossary.") - return - } + let bundle = Bundle(for: type(of: self)) + guard let url = bundle.url(forResource: "glossary", withExtension: "tmx") else { + XCTFail("Unable to read forced glossary.") + return + } #endif let glossary = try? Data(contentsOf: url) @@ -462,8 +462,8 @@ class LanguageTranslatorTests: XCTestCase { XCTAssertGreaterThan(document.count, 0) // document has content let contentType = response?.headers - .first { $0.key.compare("content-type", options: .caseInsensitive) == .orderedSame } - .map { $0.value } + .first { $0.key.compare("content-type", options: .caseInsensitive) == .orderedSame } + .map { $0.value } XCTAssertNotNil(contentType) expectation3.fulfill() } diff --git a/Tests/NaturalLanguageClassifierV1Tests/NaturalLanguageClassifierTests.swift b/Tests/NaturalLanguageClassifierV1Tests/NaturalLanguageClassifierTests.swift index 71b6b7ac7..0530fd2c3 100644 --- a/Tests/NaturalLanguageClassifierV1Tests/NaturalLanguageClassifierTests.swift +++ b/Tests/NaturalLanguageClassifierV1Tests/NaturalLanguageClassifierTests.swift @@ -95,10 +95,10 @@ class NaturalLanguageClassifierTests: XCTestCase { /** Load a file used when creating a classifier. */ func loadClassifierFile(name: String, withExtension: String) -> Data? { #if os(Linux) - let url = URL(fileURLWithPath: "Tests/NaturalLanguageClassifierV1Tests/" + name + "." + withExtension) + let url = URL(fileURLWithPath: "Tests/NaturalLanguageClassifierV1Tests/" + name + "." + withExtension) #else - let bundle = Bundle(for: type(of: self)) - guard let url = bundle.url(forResource: name, withExtension: withExtension) else { return nil } + let bundle = Bundle(for: type(of: self)) + guard let url = bundle.url(forResource: name, withExtension: withExtension) else { return nil } #endif let data = try? Data(contentsOf: url) return data @@ -261,12 +261,12 @@ class NaturalLanguageClassifierTests: XCTestCase { trainingMetadata: metadataFileEmpty, trainingData: trainingFile ) { - _, error in + _, error in - if error == nil { - XCTFail(missingErrorMessage) - } - expectation.fulfill() + if error == nil { + XCTFail(missingErrorMessage) + } + expectation.fulfill() } waitForExpectations() } diff --git a/Tests/NaturalLanguageUnderstandingV1Tests/NaturalLanguageUnderstandingV1Tests.swift b/Tests/NaturalLanguageUnderstandingV1Tests/NaturalLanguageUnderstandingV1Tests.swift index 66281161d..38d2b0908 100644 --- a/Tests/NaturalLanguageUnderstandingV1Tests/NaturalLanguageUnderstandingV1Tests.swift +++ b/Tests/NaturalLanguageUnderstandingV1Tests/NaturalLanguageUnderstandingV1Tests.swift @@ -70,14 +70,14 @@ class NaturalLanguageUnderstandingTests: XCTestCase { func loadHTML() { #if os(Linux) - let file = URL(fileURLWithPath: "Tests/NaturalLanguageUnderstandingV1Tests/testArticle.html").path - html = try! String(contentsOfFile: file, encoding: .utf8) + let file = URL(fileURLWithPath: "Tests/NaturalLanguageUnderstandingV1Tests/testArticle.html").path + html = try! String(contentsOfFile: file, encoding: .utf8) #else - let bundle = Bundle(for: type(of: self)) - guard let file = bundle.path(forResource: "testArticle", ofType: "html") else { - XCTFail("Unable to locate testArticle.html") - return - } + let bundle = Bundle(for: type(of: self)) + guard let file = bundle.path(forResource: "testArticle", ofType: "html") else { + XCTFail("Unable to locate testArticle.html") + return + } html = try! String(contentsOfFile: file) #endif } @@ -222,9 +222,9 @@ class NaturalLanguageUnderstandingTests: XCTestCase { let description = "Analyze text for emotions." let expectation = self.expectation(description: description) let text = "But I believe this thinking is wrong. I believe the road of true democracy remains the better path. " - + "I believe that in the 21st century, economies can only grow to a certain point until they need to open up -- " - + "because entrepreneurs need to access information in order to invent; young people need a global education " - + "in order to thrive; independent media needs to check the abuses of power." + + "I believe that in the 21st century, economies can only grow to a certain point until they need to open up -- " + + "because entrepreneurs need to access information in order to invent; young people need a global education " + + "in order to thrive; independent media needs to check the abuses of power." let emotion = EmotionOptions(targets: ["democracy", "entrepreneurs", "media", "economies"]) let features = Features(emotion: emotion) naturalLanguageUnderstanding.analyze(features: features, text: text, returnAnalyzedText: true) { @@ -272,9 +272,9 @@ class NaturalLanguageUnderstandingTests: XCTestCase { let description = "Analyze text for emotions without targets." let expectation = self.expectation(description: description) let text = "But I believe this thinking is wrong. I believe the road of true democracy remains the better path. " - + "I believe that in the 21st century, economies can only grow to a certain point until they need to open up -- " - + "because entrepreneurs need to access information in order to invent; young people need a global education " - + "in order to thrive; independent media needs to check the abuses of power." + + "I believe that in the 21st century, economies can only grow to a certain point until they need to open up -- " + + "because entrepreneurs need to access information in order to invent; young people need a global education " + + "in order to thrive; independent media needs to check the abuses of power." let features = Features(emotion: EmotionOptions()) naturalLanguageUnderstanding.analyze(features: features, text: text, returnAnalyzedText: true) { response, error in diff --git a/Tests/PersonalityInsightsV3Tests/PersonalityInsightsTests.swift b/Tests/PersonalityInsightsV3Tests/PersonalityInsightsTests.swift index a5d116fc7..b9ff88416 100644 --- a/Tests/PersonalityInsightsV3Tests/PersonalityInsightsTests.swift +++ b/Tests/PersonalityInsightsV3Tests/PersonalityInsightsTests.swift @@ -63,15 +63,15 @@ class PersonalityInsightsTests: XCTestCase { func load(forResource resource: String, ofType ext: String) -> String? { #if os(Linux) - let file = URL(fileURLWithPath: "Tests/PersonalityInsightsV3Tests/" + resource + "." + ext).path - return try? String(contentsOfFile: file, encoding: .utf8) + let file = URL(fileURLWithPath: "Tests/PersonalityInsightsV3Tests/" + resource + "." + ext).path + return try? String(contentsOfFile: file, encoding: .utf8) #else - let bundle = Bundle(for: type(of: self)) - guard let file = bundle.path(forResource: resource, ofType: ext) else { - XCTFail("Unable to locate \(resource).\(ext) file.") - return nil - } - return try? String(contentsOfFile: file) + let bundle = Bundle(for: type(of: self)) + guard let file = bundle.path(forResource: resource, ofType: ext) else { + XCTFail("Unable to locate \(resource).\(ext) file.") + return nil + } + return try? String(contentsOfFile: file) #endif } diff --git a/Tests/SpeechToTextV1Tests/SpeechToTextRecognizeTests.swift b/Tests/SpeechToTextV1Tests/SpeechToTextRecognizeTests.swift index 24a51b152..c03ca0809 100644 --- a/Tests/SpeechToTextV1Tests/SpeechToTextRecognizeTests.swift +++ b/Tests/SpeechToTextV1Tests/SpeechToTextRecognizeTests.swift @@ -130,7 +130,7 @@ class SpeechToTextRecognizeTests: XCTestCase { wait(for: [expectation], timeout: timeout) } - // MARK: - Transcribe File, Custom Settings + // MARK: - Transcribe File, Custom Settings func testTranscribeFileCustomWAV() { transcribeFileCustom(filename: "SpeechSample", withExtension: "wav", format: "audio/wav") @@ -306,7 +306,7 @@ class SpeechToTextRecognizeTests: XCTestCase { } } - // MARK: - Transcribe Data, Custom Settings + // MARK: - Transcribe Data, Custom Settings func testTranscribeDataCustomWAV() { transcribeDataCustom(filename: "SpeechSample", withExtension: "wav", format: "audio/wav") @@ -505,7 +505,7 @@ class SpeechToTextRecognizeTests: XCTestCase { } } - // MARK: - Transcribe Data with Smart Formatting + // MARK: - Transcribe Data with Smart Formatting func testTranscribeStockAnnouncementCustomWAV() { transcribeDataCustomForNumbers( @@ -617,9 +617,9 @@ class SpeechToTextRecognizeTests: XCTestCase { return } } - + // MARK: - All settings - + func testTranscribeDataWithAllSettingsWAV() { transcribeDataWithAllSettings(filename: "SpeechSample", withExtension: "wav", format: "audio/wav") } @@ -635,7 +635,7 @@ class SpeechToTextRecognizeTests: XCTestCase { func testTranscribeDataWithAllSettingsNoFormat() { transcribeDataWithAllSettings(filename: "SpeechSample", withExtension: "ogg", format: nil) } - + func transcribeDataWithAllSettings(filename: String, withExtension: String, format: String?) { let description = "Transcribe an audio file." let expectation = self.expectation(description: description) @@ -726,7 +726,7 @@ class SpeechToTextRecognizeTests: XCTestCase { var callback = RecognizeCallback() callback.onError = { results in - gotError.fulfill() + gotError.fulfill() } speechToText.recognizeUsingWebSocket(audio: audio, settings: settings, model: "en-US_NarrowbandModel", learningOptOut: true, callback: callback) @@ -738,67 +738,67 @@ class SpeechToTextRecognizeTests: XCTestCase { func testResultsAccumulator() { let results1 = """ - { - "results": [{ - "alternatives": [{ - "transcript": "the quick " - }], - "final": false - }], - "result_index": 0 - } - """ + { + "results": [{ + "alternatives": [{ + "transcript": "the quick " + }], + "final": false + }], + "result_index": 0 + } + """ let results2 = """ + { + "results": [{ + "alternatives": [{ + "confidence": 0.922, + "transcript": "the quick brown fox" + }], + "final": true + }], + "result_index": 0, + "speaker_labels": [ { - "results": [{ - "alternatives": [{ - "confidence": 0.922, - "transcript": "the quick brown fox" - }], - "final": true - }], - "result_index": 0, - "speaker_labels": [ - { - "from": 0.68, - "to": 1.19, - "speaker": 2, - "confidence": 0.418, - "final": false - }, - { - "from": 1.47, - "to": 1.93, - "speaker": 1, - "confidence": 0.521, - "final": false - } - ] + "from": 0.68, + "to": 1.19, + "speaker": 2, + "confidence": 0.418, + "final": false + }, + { + "from": 1.47, + "to": 1.93, + "speaker": 1, + "confidence": 0.521, + "final": false } - """ + ] + } + """ let results3 = """ + { + "results": [{ + "alternatives": [{ + "confidence": 0.873, + "transcript": "jumps over the lazy dog" + }], + "final": true + }], + "result_index": 1, + "speaker_labels": [ { - "results": [{ - "alternatives": [{ - "confidence": 0.873, - "transcript": "jumps over the lazy dog" - }], - "final": true - }], - "result_index": 1, - "speaker_labels": [ - { - "from": 1.96, - "to": 2.59, - "speaker": 2, - "confidence": 0.418, - "final": false - } - ] + "from": 1.96, + "to": 2.59, + "speaker": 2, + "confidence": 0.418, + "final": false } - """ + ] + } + """ var accumulator = SpeechRecognitionResultsAccumulator() accumulator.add(results: try! JSONDecoder().decode(SpeechRecognitionResults.self, from: results1.data(using: .utf8)!)) @@ -810,7 +810,7 @@ class SpeechToTextRecognizeTests: XCTestCase { XCTAssertEqual(accumulator.bestTranscript, "the quick brown fox jumps over the lazy dog") } - // MARK: - Transcribe Streaming + // MARK: - Transcribe Streaming func testTranscribeStreaming() { print("") @@ -822,7 +822,7 @@ class SpeechToTextRecognizeTests: XCTestCase { print("") } - // MARK: - Validation Functions + // MARK: - Validation Functions func validateSTTResults(results: SpeechRecognitionResults, settings: RecognitionSettings) { guard let results = results.results else { return } diff --git a/Tests/SpeechToTextV1Tests/SpeechToTextTests.swift b/Tests/SpeechToTextV1Tests/SpeechToTextTests.swift index 0e4e25336..5a570fdea 100644 --- a/Tests/SpeechToTextV1Tests/SpeechToTextTests.swift +++ b/Tests/SpeechToTextV1Tests/SpeechToTextTests.swift @@ -165,11 +165,11 @@ class SpeechToTextTests: XCTestCase { corpusName: "swift-test-corpus", corpusFile: fileData, allowOverwrite: true) { - _, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - } - expectation.fulfill() + _, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + } + expectation.fulfill() } wait(for: [expectation], timeout: timeout) waitUntil(languageModel, is: "ready") @@ -183,11 +183,11 @@ class SpeechToTextTests: XCTestCase { audioName: "audio", audioResource: audio, contentType: "audio/wav", allowOverwrite: true) { - _, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - } - expectation.fulfill() + _, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + } + expectation.fulfill() } wait(for: [expectation], timeout: timeout) waitUntil(acousticModel, is: "ready") @@ -905,12 +905,12 @@ class SpeechToTextTests: XCTestCase { grammarFile: grammarFile.absoluteString, contentType: "application/srgs", allowOverwrite: true) { - _, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - expectation1.fulfill() + _, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + expectation1.fulfill() } wait(for: [expectation1], timeout: timeout) diff --git a/Tests/TextToSpeechV1Tests/TextToSpeechPlaybackTests.swift b/Tests/TextToSpeechV1Tests/TextToSpeechPlaybackTests.swift index 950fa08f3..8dd791c8c 100644 --- a/Tests/TextToSpeechV1Tests/TextToSpeechPlaybackTests.swift +++ b/Tests/TextToSpeechV1Tests/TextToSpeechPlaybackTests.swift @@ -28,7 +28,7 @@ class TextToSpeechPlaybackTests: XCTestCase { private let text = "Swift at IBM is awesome. You should try it!" private let ssmlString = "" + - "Hello" + "Hello" private let failedToInitializeAudioPlayerMessage = "Failed to initialize an AVAudioPlayer with the received data" // MARK: - Test Configuration @@ -91,7 +91,7 @@ class TextToSpeechPlaybackTests: XCTestCase { } waitForExpectations() } - + func testSynthesizeAllVoices() { let voices: [String : String] = [ "ar-AR_OmarVoice": "تقوم خدمة I B M النص إلى خدمة الكلام بتحويل النص المكتوب إلى صوت طبيعي في مجموعة متنوعة من اللغات والأصوات.", @@ -129,27 +129,27 @@ class TextToSpeechPlaybackTests: XCTestCase { "es-US_SofiaVoice": "Consciente de su patrimonio espiritual y moral, la Unión está ", "es-US_SofiaV3Voice": "Consciente de su patrimonio espiritual y moral, la Unión está ß" ] - + // test all voices for (voice, sampleText) in voices { let voiceDescription = "Test of the \(voice) voice." let voiceExpectation = self.expectation(description: voiceDescription) - + textToSpeech.synthesize(text: sampleText, accept: "audio/wav", voice: voice) { response, error in - + if let error = error { XCTFail(unexpectedErrorMessage(error)) return } - + guard let data = response?.result else { XCTFail(missingResultMessage) return } - + XCTAssertGreaterThan(data.count, 0) - + do { let audioPlayer = try AVAudioPlayer(data: data) audioPlayer.prepareToPlay() @@ -162,7 +162,7 @@ class TextToSpeechPlaybackTests: XCTestCase { } voiceExpectation.fulfill() } - + waitForExpectations() } } diff --git a/Tests/ToneAnalyzerV3Tests/ToneAnalyzerTests.swift b/Tests/ToneAnalyzerV3Tests/ToneAnalyzerTests.swift index 58e77ec1f..58fa2b461 100644 --- a/Tests/ToneAnalyzerV3Tests/ToneAnalyzerTests.swift +++ b/Tests/ToneAnalyzerV3Tests/ToneAnalyzerTests.swift @@ -177,21 +177,21 @@ class ToneAnalyzerTests: XCTestCase { sentences: false, contentLanguage: "en", acceptLanguage: "en") { - response, error in - - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let toneAnalysis = response?.result else { - XCTFail(missingResultMessage) - return - } - - XCTAssertNotNil(toneAnalysis.documentTone.tones) - XCTAssertGreaterThan(toneAnalysis.documentTone.tones!.count, 0) - XCTAssertNil(toneAnalysis.sentencesTone) - expectation.fulfill() + response, error in + + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let toneAnalysis = response?.result else { + XCTFail(missingResultMessage) + return + } + + XCTAssertNotNil(toneAnalysis.documentTone.tones) + XCTAssertGreaterThan(toneAnalysis.documentTone.tones!.count, 0) + XCTAssertNil(toneAnalysis.sentencesTone) + expectation.fulfill() } waitForExpectations() } diff --git a/Tests/VisualRecognitionV3Tests/VisualRecognitionTestUtilities.swift b/Tests/VisualRecognitionV3Tests/VisualRecognitionTestUtilities.swift index c0db4e7a4..9e3f6ce14 100644 --- a/Tests/VisualRecognitionV3Tests/VisualRecognitionTestUtilities.swift +++ b/Tests/VisualRecognitionV3Tests/VisualRecognitionTestUtilities.swift @@ -29,7 +29,7 @@ let obama: Data = loadResource(name: "obama", ext: "jpg") let sign: Data = loadResource(name: "sign", ext: "jpg") let obamaURL = "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/" + -"Administration/People/president_official_portrait_lores.jpg" + "Administration/People/president_official_portrait_lores.jpg" let carURL = "https://raw.githubusercontent.com/watson-developer-cloud/java-sdk/master/visual-recognition/src/test/resources/visual_recognition/v4/turtle_to_classify.jpg" let signURL = "https://raw.githubusercontent.com/watson-developer-cloud/java-sdk/master/visual-recognition/src/test/resources/visual_recognition/v4/turtle_to_classify.jpg" diff --git a/Tests/VisualRecognitionV3Tests/VisualRecognitionTests.swift b/Tests/VisualRecognitionV3Tests/VisualRecognitionTests.swift index ff3b5a249..f4eb94817 100644 --- a/Tests/VisualRecognitionV3Tests/VisualRecognitionTests.swift +++ b/Tests/VisualRecognitionV3Tests/VisualRecognitionTests.swift @@ -460,7 +460,7 @@ class VisualRecognitionTests: XCTestCase { tries += 1 if tries > 8 { - XCTFail("Could not train a new classifier. Try again later.") + XCTFail("Could not train a new classifier. Try again later.") return } } @@ -602,51 +602,51 @@ class VisualRecognitionTests: XCTestCase { owners: ["IBM"], classifierIDs: ["default"], acceptLanguage: "en") { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } - - var containsPersonClass = false - var classifierScore: Double? + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 1) + var containsPersonClass = false + var classifierScore: Double? - // verify the image's metadata - let image = classifiedImages.images.first - XCTAssertEqual(image?.sourceURL, obamaURL) - XCTAssertEqual(image?.resolvedURL, obamaURL) - XCTAssertNil(image?.image) - XCTAssertNil(image?.error) - XCTAssertEqual(image?.classifiers.count, 1) + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 1) - // verify the image's classifier - let classifier = image?.classifiers.first - XCTAssertEqual(classifier?.classifierID, "default") - XCTAssertEqual(classifier?.name, "default") - guard let classes = classifier?.classes else { - XCTFail("Did not return any classes.") - return - } - XCTAssertGreaterThan(classes.count, 0) - for cls in classes where cls.class == "person" { - containsPersonClass = true - classifierScore = cls.score - break - } - XCTAssertEqual(containsPersonClass, true) - if let score = classifierScore { - XCTAssertGreaterThan(score, 0.5) - } + // verify the image's metadata + let image = classifiedImages.images.first + XCTAssertEqual(image?.sourceURL, obamaURL) + XCTAssertEqual(image?.resolvedURL, obamaURL) + XCTAssertNil(image?.image) + XCTAssertNil(image?.error) + XCTAssertEqual(image?.classifiers.count, 1) + + // verify the image's classifier + let classifier = image?.classifiers.first + XCTAssertEqual(classifier?.classifierID, "default") + XCTAssertEqual(classifier?.name, "default") + guard let classes = classifier?.classes else { + XCTFail("Did not return any classes.") + return + } + XCTAssertGreaterThan(classes.count, 0) + for cls in classes where cls.class == "person" { + containsPersonClass = true + classifierScore = cls.score + break + } + XCTAssertEqual(containsPersonClass, true) + if let score = classifierScore { + XCTAssertGreaterThan(score, 0.5) + } - expectation.fulfill() + expectation.fulfill() } waitForExpectations() } @@ -658,38 +658,38 @@ class VisualRecognitionTests: XCTestCase { url: carURL, classifierIDs: [classifierID], acceptLanguage: "en") { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } - - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 1) + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - // verify the image's metadata - let image = classifiedImages.images.first - XCTAssertEqual(image?.sourceURL, carURL) - XCTAssertEqual(image?.resolvedURL, carURL) - XCTAssertNil(image?.image) - XCTAssertNil(image?.error) - XCTAssertEqual(image?.classifiers.count, 1) + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 1) - // verify the image's classifier - let classifier = image?.classifiers.first - XCTAssertEqual(classifier?.classifierID, self.classifierID) - XCTAssertEqual(classifier?.classes.count, 1) - XCTAssertEqual(classifier?.classes.first?.class, "turtle") - if let score = classifier?.classes.first?.score { - XCTAssertGreaterThan(score, 0.5) - } + // verify the image's metadata + let image = classifiedImages.images.first + XCTAssertEqual(image?.sourceURL, carURL) + XCTAssertEqual(image?.resolvedURL, carURL) + XCTAssertNil(image?.image) + XCTAssertNil(image?.error) + XCTAssertEqual(image?.classifiers.count, 1) + + // verify the image's classifier + let classifier = image?.classifiers.first + XCTAssertEqual(classifier?.classifierID, self.classifierID) + XCTAssertEqual(classifier?.classes.count, 1) + XCTAssertEqual(classifier?.classes.first?.class, "turtle") + if let score = classifier?.classes.first?.score { + XCTAssertGreaterThan(score, 0.5) + } - expectation.fulfill() + expectation.fulfill() } waitForExpectations() } @@ -703,38 +703,38 @@ class VisualRecognitionTests: XCTestCase { owners: ["me"], classifierIDs: [classifierID], acceptLanguage: "en") { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } - - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 1) + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - // verify the image's metadata - let image = classifiedImages.images.first - XCTAssertEqual(image?.sourceURL, carURL) - XCTAssertEqual(image?.resolvedURL, carURL) - XCTAssertNil(image?.image) - XCTAssertNil(image?.error) - XCTAssertEqual(image?.classifiers.count, 1) + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 1) - // verify the image's classifier - let classifier = image?.classifiers.first - XCTAssertEqual(classifier?.classifierID, self.classifierID) - XCTAssertEqual(classifier?.classes.count, 1) - XCTAssertEqual(classifier?.classes.first?.class, "turtle") - if let score = classifier?.classes.first?.score { - XCTAssertGreaterThan(score, 0.5) - } + // verify the image's metadata + let image = classifiedImages.images.first + XCTAssertEqual(image?.sourceURL, carURL) + XCTAssertEqual(image?.resolvedURL, carURL) + XCTAssertNil(image?.image) + XCTAssertNil(image?.error) + XCTAssertEqual(image?.classifiers.count, 1) + + // verify the image's classifier + let classifier = image?.classifiers.first + XCTAssertEqual(classifier?.classifierID, self.classifierID) + XCTAssertEqual(classifier?.classes.count, 1) + XCTAssertEqual(classifier?.classes.first?.class, "turtle") + if let score = classifier?.classes.first?.score { + XCTAssertGreaterThan(score, 0.5) + } - expectation.fulfill() + expectation.fulfill() } waitForExpectations() } @@ -860,50 +860,50 @@ class VisualRecognitionTests: XCTestCase { threshold: 0.5, owners: ["IBM"], classifierIDs: ["default"]) { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } - - var containsPersonClass = false - var classifierScore: Double? + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 1) + var containsPersonClass = false + var classifierScore: Double? - // verify the image's metadata - let image = classifiedImages.images.first - XCTAssertNil(image?.sourceURL) - XCTAssertNil(image?.resolvedURL) - XCTAssert(image?.image == "car.png") - XCTAssertNil(image?.error) - XCTAssertEqual(image?.classifiers.count, 1) + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 1) - // verify the image's classifier - let classifier = image?.classifiers.first - XCTAssertEqual(classifier?.classifierID, "default") - XCTAssertEqual(classifier?.name, "default") - guard let classes = classifier?.classes else { - XCTFail("Did not return any classes.") - return - } - XCTAssertGreaterThan(classes.count, 0) - for cls in classes where cls.class == "car" { - containsPersonClass = true - classifierScore = cls.score - break - } - XCTAssertEqual(containsPersonClass, true) - if let score = classifierScore { - XCTAssertGreaterThan(score, 0.5) - } - expectation.fulfill() + // verify the image's metadata + let image = classifiedImages.images.first + XCTAssertNil(image?.sourceURL) + XCTAssertNil(image?.resolvedURL) + XCTAssert(image?.image == "car.png") + XCTAssertNil(image?.error) + XCTAssertEqual(image?.classifiers.count, 1) + + // verify the image's classifier + let classifier = image?.classifiers.first + XCTAssertEqual(classifier?.classifierID, "default") + XCTAssertEqual(classifier?.name, "default") + guard let classes = classifier?.classes else { + XCTFail("Did not return any classes.") + return + } + XCTAssertGreaterThan(classes.count, 0) + for cls in classes where cls.class == "car" { + containsPersonClass = true + classifierScore = cls.score + break + } + XCTAssertEqual(containsPersonClass, true) + if let score = classifierScore { + XCTAssertGreaterThan(score, 0.5) + } + expectation.fulfill() } waitForExpectations() } @@ -915,38 +915,38 @@ class VisualRecognitionTests: XCTestCase { imagesFile: car, imagesFilename: "car.png", classifierIDs: [classifierID]) { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } - - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 1) + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - // verify the image's metadata - let image = classifiedImages.images.first - XCTAssertNil(image?.sourceURL) - XCTAssertNil(image?.resolvedURL) - XCTAssert(image?.image == "car.png") - XCTAssertNil(image?.error) - XCTAssertEqual(image?.classifiers.count, 1) + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 1) - // verify the image's classifier - let classifier = image?.classifiers.first - XCTAssertEqual(classifier?.classifierID, self.classifierID) - XCTAssertEqual(classifier?.classes.count, 1) - XCTAssertEqual(classifier?.classes.first?.class, "turtle") - if let score = classifier?.classes.first?.score { - XCTAssertGreaterThan(score, 0.5) - } + // verify the image's metadata + let image = classifiedImages.images.first + XCTAssertNil(image?.sourceURL) + XCTAssertNil(image?.resolvedURL) + XCTAssert(image?.image == "car.png") + XCTAssertNil(image?.error) + XCTAssertEqual(image?.classifiers.count, 1) + + // verify the image's classifier + let classifier = image?.classifiers.first + XCTAssertEqual(classifier?.classifierID, self.classifierID) + XCTAssertEqual(classifier?.classes.count, 1) + XCTAssertEqual(classifier?.classes.first?.class, "turtle") + if let score = classifier?.classes.first?.score { + XCTAssertGreaterThan(score, 0.5) + } - expectation.fulfill() + expectation.fulfill() } waitForExpectations() } @@ -960,38 +960,38 @@ class VisualRecognitionTests: XCTestCase { threshold: 0.5, owners: ["me"], classifierIDs: [classifierID]) { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } - - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 1) + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - // verify the image's metadata - let image = classifiedImages.images.first - XCTAssertNil(image?.sourceURL) - XCTAssertNil(image?.resolvedURL) - XCTAssert(image?.image == "car.png") - XCTAssertNil(image?.error) - XCTAssertEqual(image?.classifiers.count, 1) + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 1) - // verify the image's classifier - let classifier = image?.classifiers.first - XCTAssertEqual(classifier?.classifierID, self.classifierID) - XCTAssertEqual(classifier?.classes.count, 1) - XCTAssertEqual(classifier?.classes.first?.class, "turtle") - if let score = classifier?.classes.first?.score { - XCTAssertGreaterThan(score, 0.5) - } + // verify the image's metadata + let image = classifiedImages.images.first + XCTAssertNil(image?.sourceURL) + XCTAssertNil(image?.resolvedURL) + XCTAssert(image?.image == "car.png") + XCTAssertNil(image?.error) + XCTAssertEqual(image?.classifiers.count, 1) + + // verify the image's classifier + let classifier = image?.classifiers.first + XCTAssertEqual(classifier?.classifierID, self.classifierID) + XCTAssertEqual(classifier?.classes.count, 1) + XCTAssertEqual(classifier?.classes.first?.class, "turtle") + if let score = classifier?.classes.first?.score { + XCTAssertGreaterThan(score, 0.5) + } - expectation.fulfill() + expectation.fulfill() } waitForExpectations() } @@ -1003,62 +1003,62 @@ class VisualRecognitionTests: XCTestCase { imagesFile: car, imagesFilename: "car.png", classifierIDs: ["default", classifierID]) { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - var containsCarClass = false - var classifierScore: Double? + var containsCarClass = false + var classifierScore: Double? - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 1) + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 1) - // verify the image's metadata - let image = classifiedImages.images.first - XCTAssertNil(image?.sourceURL) - XCTAssertNil(image?.resolvedURL) - XCTAssert(image?.image == "car.png") - XCTAssertNil(image?.error) - - // verify 2 classifiers are returned - guard let classifiers = image?.classifiers else { - XCTFail("No classifiers found") - return - } + // verify the image's metadata + let image = classifiedImages.images.first + XCTAssertNil(image?.sourceURL) + XCTAssertNil(image?.resolvedURL) + XCTAssert(image?.image == "car.png") + XCTAssertNil(image?.error) + + // verify 2 classifiers are returned + guard let classifiers = image?.classifiers else { + XCTFail("No classifiers found") + return + } - for classifier in classifiers { - // verify the image's default classifier - if classifier.name == "default" { - XCTAssertEqual(classifier.classifierID, "default") - XCTAssertEqual(classifier.name, "default") + for classifier in classifiers { + // verify the image's default classifier + if classifier.name == "default" { + XCTAssertEqual(classifier.classifierID, "default") + XCTAssertEqual(classifier.name, "default") - XCTAssertGreaterThan(classifier.classes.count, 0) - for cls in classifier.classes where cls.class == "car" { - containsCarClass = true - classifierScore = cls.score - } - XCTAssertEqual(containsCarClass, true) - if let score = classifierScore { - XCTAssertGreaterThan(score, 0.5) - } - } else { - // verify the image's custom classifier - XCTAssertEqual(classifier.classifierID, self.classifierID) - XCTAssertEqual(classifier.classes.count, 1) - XCTAssertEqual(classifier.classes.first?.class, "turtle") - if let score = classifier.classes.first?.score { - XCTAssertGreaterThan(score, 0.5) + XCTAssertGreaterThan(classifier.classes.count, 0) + for cls in classifier.classes where cls.class == "car" { + containsCarClass = true + classifierScore = cls.score + } + XCTAssertEqual(containsCarClass, true) + if let score = classifierScore { + XCTAssertGreaterThan(score, 0.5) + } + } else { + // verify the image's custom classifier + XCTAssertEqual(classifier.classifierID, self.classifierID) + XCTAssertEqual(classifier.classes.count, 1) + XCTAssertEqual(classifier.classes.first?.class, "turtle") + if let score = classifier.classes.first?.score { + XCTAssertGreaterThan(score, 0.5) + } } } - } - expectation.fulfill() + expectation.fulfill() } waitForExpectations() } @@ -1070,57 +1070,57 @@ class VisualRecognitionTests: XCTestCase { imagesFile: carz, imagesFilename: "cars.zip", classifierIDs: ["default", classifierID]) { - response, error in - if let error = error { - XCTFail(unexpectedErrorMessage(error)) - return - } - guard let classifiedImages = response?.result else { - XCTFail(missingResultMessage) - return - } - - // verify classified images object - XCTAssertNil(classifiedImages.warnings) - XCTAssertEqual(classifiedImages.images.count, 4) + response, error in + if let error = error { + XCTFail(unexpectedErrorMessage(error)) + return + } + guard let classifiedImages = response?.result else { + XCTFail(missingResultMessage) + return + } - for image in classifiedImages.images { - // verify the image's metadata - XCTAssertNil(image.sourceURL) - XCTAssertNil(image.resolvedURL) - XCTAssert(image.image?.hasPrefix("car") == true) - XCTAssertNil(image.error) - - for classifier in image.classifiers { - var containsCarClass = false - var classifierScore: Double? - if classifier.name == "default" { - // verify the image's default classifier - XCTAssertEqual(classifier.classifierID, "default") - XCTAssertEqual(classifier.name, "default") - XCTAssertGreaterThan(classifier.classes.count, 0) - for cls in classifier.classes { - let classes = ["car", "vehicle", "sedan", "Parking Garage (Indoor)"] - if classes.contains(cls.class) { - containsCarClass = true - classifierScore = cls.score + // verify classified images object + XCTAssertNil(classifiedImages.warnings) + XCTAssertEqual(classifiedImages.images.count, 4) + + for image in classifiedImages.images { + // verify the image's metadata + XCTAssertNil(image.sourceURL) + XCTAssertNil(image.resolvedURL) + XCTAssert(image.image?.hasPrefix("car") == true) + XCTAssertNil(image.error) + + for classifier in image.classifiers { + var containsCarClass = false + var classifierScore: Double? + if classifier.name == "default" { + // verify the image's default classifier + XCTAssertEqual(classifier.classifierID, "default") + XCTAssertEqual(classifier.name, "default") + XCTAssertGreaterThan(classifier.classes.count, 0) + for cls in classifier.classes { + let classes = ["car", "vehicle", "sedan", "Parking Garage (Indoor)"] + if classes.contains(cls.class) { + containsCarClass = true + classifierScore = cls.score + } + } + XCTAssertEqual(containsCarClass, true) + if let score = classifierScore { + XCTAssertGreaterThan(score, 0.5) + } + } else { + // verify the image's custom classifier + XCTAssertEqual(classifier.classifierID, self.classifierID) + if let score = classifier.classes.first?.score { + XCTAssertGreaterThan(score, 0.5) } - } - XCTAssertEqual(containsCarClass, true) - if let score = classifierScore { - XCTAssertGreaterThan(score, 0.5) - } - } else { - // verify the image's custom classifier - XCTAssertEqual(classifier.classifierID, self.classifierID) - if let score = classifier.classes.first?.score { - XCTAssertGreaterThan(score, 0.5) } } } - } - expectation.fulfill() + expectation.fulfill() } waitForExpectations(timeout: 60) } diff --git a/Tests/VisualRecognitionV3Tests/VisualRecognitionUnitTests.swift b/Tests/VisualRecognitionV3Tests/VisualRecognitionUnitTests.swift index 55904a621..644913ec6 100644 --- a/Tests/VisualRecognitionV3Tests/VisualRecognitionUnitTests.swift +++ b/Tests/VisualRecognitionV3Tests/VisualRecognitionUnitTests.swift @@ -351,7 +351,7 @@ class VisualRecognitionUnitTests: XCTestCase { } #endif -#if os(iOS) || os(tvOS) || os(watchOS) + #if os(iOS) || os(tvOS) || os(watchOS) // MARK: - VisualRecognition+CoreML @@ -698,7 +698,7 @@ class VisualRecognitionUnitTests: XCTestCase { } } -#endif + #endif } diff --git a/Tests/VisualRecognitionV4Tests/VisualRecognitionTestUtilities.swift b/Tests/VisualRecognitionV4Tests/VisualRecognitionTestUtilities.swift index 525806fad..ebade1963 100644 --- a/Tests/VisualRecognitionV4Tests/VisualRecognitionTestUtilities.swift +++ b/Tests/VisualRecognitionV4Tests/VisualRecognitionTestUtilities.swift @@ -29,7 +29,7 @@ let obama: Data = loadResource(name: "obama", ext: "jpg") let sign: Data = loadResource(name: "sign", ext: "jpg") let obamaURL = "https://www.whitehouse.gov/sites/whitehouse.gov/files/images/" + -"Administration/People/president_official_portrait_lores.jpg" + "Administration/People/president_official_portrait_lores.jpg" let carURL = "https://raw.githubusercontent.com/watson-developer-cloud/java-sdk/master/visual-recognition/src/test/resources/visual_recognition/v4/turtle_to_classify.jpg" let signURL = "https://raw.githubusercontent.com/watson-developer-cloud/java-sdk/master/visual-recognition/src/test/resources/visual_recognition/v4/turtle_to_classify.jpg" let giraffeURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Giraffe_Mikumi_National_Park.jpg/220px-Giraffe_Mikumi_National_Park.jpg" diff --git a/Tests/VisualRecognitionV4Tests/VisualRecognitionV4Tests.swift b/Tests/VisualRecognitionV4Tests/VisualRecognitionV4Tests.swift index d2be10385..0643869f0 100644 --- a/Tests/VisualRecognitionV4Tests/VisualRecognitionV4Tests.swift +++ b/Tests/VisualRecognitionV4Tests/VisualRecognitionV4Tests.swift @@ -457,16 +457,16 @@ class VisualRecognitionV4Tests: XCTestCase { waitForExpectations() } - + // MARK: - Downloading model files locally - + func testGetModelFile() { let description = "Get Model File" let modelFileExpectation = self.expectation(description: description) - + visualRecognition.getModelFile(collectionID: collectionID, feature: "objects", modelFormat: "rscnn", headers: nil) { response, error in - + // make sure we didn't get an error if let error = error { XCTFail(unexpectedErrorMessage(error)) @@ -478,10 +478,10 @@ class VisualRecognitionV4Tests: XCTestCase { XCTFail("No model data returned") return } - + // we expect the model to contain data XCTAssertFalse(modelData.isEmpty) - + modelFileExpectation.fulfill() } waitForExpectations() @@ -638,7 +638,7 @@ class VisualRecognitionV4Tests: XCTestCase { } waitForExpectations() - + let expectation = self.expectation(description: "Train a collection") visualRecognition.train(collectionID: trainingDummyCollectionID) { response, error in @@ -691,10 +691,10 @@ class VisualRecognitionV4Tests: XCTestCase { waitForExpectations() } - + func testObjectMetadataCRUD() { var testObject: String! - + let expectation = self.expectation(description: "Train a collection") let location = Location(top: 15, left: 15, width: 20, height: 10) @@ -717,14 +717,14 @@ class VisualRecognitionV4Tests: XCTestCase { XCTAssertEqual(result.objects?.first?.location, location) XCTAssertEqual(result.objects?.first?.object, "test") - + testObject = "test" expectation.fulfill() } waitForExpectations() - + let listExpectation = self.expectation(description: "list object metadata") visualRecognition.listObjectMetadata(collectionID: trainingDummyCollectionID) { @@ -741,21 +741,21 @@ class VisualRecognitionV4Tests: XCTestCase { XCTFail(missingResultMessage) return } - + XCTAssertNotNil(result.objects?.first) - + testObject = result.objects?.first?.object - + listExpectation.fulfill() } - + waitForExpectations() - + let updateExpectation = self.expectation(description: "update object metadata") - + visualRecognition.updateObjectMetadata(collectionID: trainingDummyCollectionID, object: testObject, newObject: "updated") { response, error in - + // make sure we didn't get an error if let error = error { XCTFail(unexpectedErrorMessage(error)) @@ -767,21 +767,21 @@ class VisualRecognitionV4Tests: XCTestCase { XCTFail(missingResultMessage) return } - + XCTAssertEqual(result.object, "updated") - + testObject = result.object - + updateExpectation.fulfill() } - + waitForExpectations() - + let getExpectation = self.expectation(description: "get object metadata") - + visualRecognition.getObjectMetadata(collectionID: trainingDummyCollectionID, object: testObject) { response, error in - + // make sure we didn't get an error if let error = error { XCTFail(unexpectedErrorMessage(error)) @@ -793,28 +793,28 @@ class VisualRecognitionV4Tests: XCTestCase { XCTFail(missingResultMessage) return } - + XCTAssertEqual(result.object, testObject) - + getExpectation.fulfill() } - + waitForExpectations() - + let deleteExpectation = self.expectation(description: "delete object metadata") - + visualRecognition.deleteObject(collectionID: trainingDummyCollectionID, object: testObject) { response, error in - + // make sure we didn't get an error if let error = error { XCTFail(unexpectedErrorMessage(error)) return } - + deleteExpectation.fulfill() } - + waitForExpectations() } }