Skip to content

Commit

Permalink
test(discoveryv2): add enrichment and collection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-arn authored and apaparazzi0329 committed Aug 20, 2020
1 parent d885cd5 commit 216c357
Show file tree
Hide file tree
Showing 8 changed files with 1,578 additions and 19 deletions.
31 changes: 31 additions & 0 deletions Tests/AssistantV2Tests/AssistantV2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -561,5 +561,36 @@ class AssistantV2Tests: XCTestCase {

waitForExpectations()
}

// NOTE: this function is only available on premium instances of Assistant
func testListLogs() {
let description = "Test listLogs"
let expectation = self.expectation(description: description)

let premiumAuthenticator = WatsonIAMAuthenticator(apiKey: WatsonCredentials.AssistantV2PremiumAPIKey!, url: "https://iam.test.cloud.ibm.com/identity/token")
let premiumAssistant = Assistant(version: versionDate, authenticator: premiumAuthenticator)

premiumAssistant.serviceURL = WatsonCredentials.AssistantV2PremiumURL!

premiumAssistant.listLogs(assistantID: WatsonCredentials.AssistantV2PremiumAssistantID!, sort: nil, filter: nil, pageLimit: nil, cursor: nil, headers: nil) {
response, error in

if let error = error {
XCTFail(unexpectedErrorMessage(error))
return
}

guard let logsCollection = response?.result else {
XCTFail(missingResultMessage)
return
}

XCTAssertNotNil(logsCollection.logs)

expectation.fulfill()
}

waitForExpectations()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class DiscoveryCPDTests: XCTestCase {

discovery = Discovery(version: "2020-08-12", authenticator: authenticator)

// discovery.serviceURL = WatsonCredentials.DiscoveryV2CPDServiceURL
discovery.serviceURL = "http://localhost:9009"
discovery.serviceURL = WatsonCredentials.DiscoveryV2CPDServiceURL

discovery.defaultHeaders["X-Watson-Learning-Opt-Out"] = "true"
discovery.defaultHeaders["X-Watson-Test"] = "true"
Expand Down
305 changes: 289 additions & 16 deletions Tests/DiscoveryV2Tests/DiscoveryV2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,24 @@ class DiscoveryTests: XCTestCase {
}

func instantiateDiscovery() {
// TODO: CP4D authenticator
let username = WatsonCredentials.DiscoveryV2CPDUsername
let password = WatsonCredentials.DiscoveryV2CPDPassword
let url = WatsonCredentials.DiscoveryV2CPDURL

let authenticator = WatsonCloudPakForDataAuthenticator.init(username: username, password: password, url: url)
authenticator.disableSSLVerification()

discovery = Discovery(version: "2019-11-30", authenticator: authenticator)
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"
discovery.defaultHeaders["X-Watson-Test"] = "true"

discovery.disableSSLVerification()

projectID = WatsonCredentials.DiscoveryV2TestProjectID
collectionID = WatsonCredentials.DiscoveryV2TestCollectionID
projectID = WatsonCredentials.DiscoveryV2ProjectID
collectionID = WatsonCredentials.DiscoveryV2CollectionID
}

func loadDocument(name: String, ext: String) -> Data? {
#if os(Linux)
let url = URL(fileURLWithPath: "Tests/DiscoveryV1Tests/Resources/" + name + "." + ext)
let url = URL(fileURLWithPath: "Tests/DiscoveryV2Tests/Resources/" + name + "." + ext)
#else
let bundle = Bundle(for: type(of: self))
guard let url = bundle.url(forResource: name, withExtension: ext) else { return nil }
Expand Down Expand Up @@ -317,14 +311,14 @@ class DiscoveryTests: XCTestCase {
return
}

guard case let .term(term) = aggregation else {
guard case let .timeslice(timeslice) = aggregation else {
XCTFail("Unexpected aggregation return type")
expectation.fulfill()
return
}

XCTAssertNotNil(term)
XCTAssertNotNil(term.field)
XCTAssertNotNil(timeslice)
XCTAssertNotNil(timeslice.field)

expectation.fulfill()
}
Expand Down Expand Up @@ -627,7 +621,7 @@ class DiscoveryTests: XCTestCase {

// MARK: - Component Settings

func testComponentSettings() {
func testGetComponentSettings() {
let expectation = self.expectation(description: "componentSettings")
discovery.getComponentSettings(projectID: projectID) { response, error in
if let error = error {
Expand Down Expand Up @@ -824,4 +818,283 @@ 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))
return
}

guard let result = response?.result else {
XCTFail("No response")
return
}

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))
return
}

guard let result = response?.result else {
XCTFail("No response")
return
}

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))
return
}

guard let result = response?.result else {
XCTFail("No response")
return
}

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))
return
}

guard let result = response?.result else {
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))
return
}

guard let result = response?.result else {
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))
return
}

guard let result = response?.result else {
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))
return
}

guard let result = response?.result else {
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))
return
}

guard let result = response?.result else {
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))
return
}

guard let result = response?.result else {
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)
}
}
Loading

0 comments on commit 216c357

Please sign in to comment.