Skip to content

Commit d5ac13e

Browse files
committed
test(Discovery v2): Add integration tests
1 parent 31b41e5 commit d5ac13e

File tree

3 files changed

+1507
-46
lines changed

3 files changed

+1507
-46
lines changed

test/Discovery.v2.IntegrationTests/DiscoveryIntegrationTests.cs

Lines changed: 257 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,35 @@
1515
*
1616
*/
1717

18+
using IBM.Cloud.SDK.Core.Authentication;
19+
using IBM.Cloud.SDK.Core.Authentication.Bearer;
20+
using IBM.Cloud.SDK.Core.Http;
1821
using IBM.Watson.Discovery.v2.Model;
1922
using Microsoft.VisualStudio.TestTools.UnitTesting;
2023
using System.Collections.Generic;
24+
using System.IO;
2125

2226
namespace IBM.Watson.Discovery.v2.IntegrationTests
2327
{
24-
[TestClass]
28+
//[TestClass]
2529
public class DiscoveryIntegrationTests
2630
{
2731
private DiscoveryService service;
2832
private string versionDate = "2019-11-20";
33+
private string filepathToIngest = @"DiscoveryTestData\watson_beats_jeopardy.html";
34+
private string metadata = "{\"Creator\": \".NET SDK Test\",\"Subject\": \"Discovery service\"}";
35+
private string bearerToken = "";
36+
private string serviceUrl = "";
2937
private string projectId = "";
30-
private string collectionId;
31-
private string queryId;
38+
private string collectionId = "";
3239

3340
[TestInitialize]
3441
public void Setup()
3542
{
36-
service = new DiscoveryService(versionDate);
43+
Authenticator discoveryAuthenticator = new BearerTokenAuthenticator(bearerToken: bearerToken);
44+
service = new DiscoveryService(versionDate: versionDate, authenticator: discoveryAuthenticator);
45+
service.SetServiceUrl(serviceUrl: serviceUrl);
46+
service.DisableSslVerification(true);
3747
}
3848

3949
#region Collections
@@ -56,15 +66,13 @@ public void TestListCollections()
5666
[TestMethod]
5767
public void TestQuery()
5868
{
59-
service.WithHeader("X-Watson-Test", "1");
60-
string filter = "";
61-
string query = "text:IBM";
62-
string naturalLanguageQuery = "";
63-
string aggregation = "";
69+
string filter = "entities.text:IBM";
70+
string query = "relations.action.lemmatized:acquire";
71+
string aggregation = "filter(enriched_text.concepts.text:cloud computing)";
6472
long count = 5;
65-
List<string> _return = new List<string>() { "" };
73+
List<string> _return = new List<string>() { "title", "url" };
6674
long offset = 1;
67-
string sort = "";
75+
string sort = "sort=enriched_text.sentiment.document.score";
6876
bool highlight = true;
6977
bool spellingSuggestions = true;
7078
QueryLargeTableResults tableResults = new QueryLargeTableResults()
@@ -84,17 +92,20 @@ public void TestQuery()
8492
MaxPerDocument = 3,
8593
Fields = new List<string>()
8694
{
87-
""
95+
"text",
96+
"abstract",
97+
"conclusion"
8898
},
8999
Count = 3,
90100
Characters = 100
91101
};
102+
103+
service.WithHeader("X-Watson-Test", "1");
92104
var queryResult = service.Query(
93105
projectId: projectId,
94106
collectionIds: new List<string>() { collectionId },
95107
filter: filter,
96108
query: query,
97-
naturalLanguageQuery: naturalLanguageQuery,
98109
aggregation: aggregation,
99110
count: count,
100111
_return: _return,
@@ -108,78 +119,278 @@ public void TestQuery()
108119
);
109120

110121
Assert.IsNotNull(queryResult.Result);
122+
Assert.IsTrue(queryResult.Result.Aggregations[0].Type == "filter");
123+
Assert.IsTrue((queryResult.Result.Aggregations[0] as QueryFilterAggregation).Match == "enriched_text.concepts.text:cloud computing");
124+
}
125+
126+
[TestMethod]
127+
public void TestNaturalLanguageQuery()
128+
{
129+
string filter = "entities.text:IBM";
130+
string naturalLanguageQuery = "What is IBM's stock price?";
131+
132+
service.WithHeader("X-Watson-Test", "1");
133+
var queryResult = service.Query(
134+
projectId: projectId,
135+
collectionIds: new List<string>() { collectionId },
136+
filter: filter,
137+
naturalLanguageQuery: naturalLanguageQuery
138+
);
139+
140+
Assert.IsNotNull(queryResult.Result);
141+
Assert.IsNotNull(queryResult.Result.Aggregations[0]);
142+
Assert.IsTrue((queryResult.Result.Aggregations[0] as QueryTermAggregation).Field == "enriched_text.entities.text");
143+
Assert.IsTrue((queryResult.Result.Aggregations[0] as QueryTermAggregation).Type == "term");
111144
}
112145

113146
[TestMethod]
114147
public void TestGetAutocompletion()
115148
{
116-
Assert.Fail();
149+
service.WithHeader("X-Watson-Test", "1");
150+
var getAutocompletionResult = service.GetAutocompletion(
151+
projectId: projectId,
152+
prefix: "ha"
153+
);
154+
155+
Assert.IsNotNull(getAutocompletionResult.Result);
156+
Assert.IsNotNull(getAutocompletionResult.Result._Completions);
157+
Assert.IsTrue(getAutocompletionResult.Result._Completions.Count > 0);
117158
}
118159

119160
[TestMethod]
120161
public void TestQueryNotices()
121162
{
122-
Assert.Fail();
163+
service.WithHeader("X-Watson-Test", "1");
164+
var queryNoticesResult0 = service.QueryNotices(
165+
projectId: projectId
166+
);
167+
168+
Assert.IsNotNull(queryNoticesResult0.Result);
169+
Assert.IsNotNull(queryNoticesResult0.Result.MatchingResults);
170+
Assert.IsNotNull(queryNoticesResult0.Result.Notices);
171+
172+
string filter = "entities.text:IBM";
173+
string query = "relations.action.lemmatized:acquire";
174+
175+
service.WithHeader("X-Watson-Test", "1");
176+
var queryNoticesResult1 = service.QueryNotices(
177+
projectId: projectId,
178+
filter: filter,
179+
query: query,
180+
count: 3,
181+
offset: 1
182+
);
183+
184+
Assert.IsNotNull(queryNoticesResult1.Result);
185+
Assert.IsNotNull(queryNoticesResult1.Result.MatchingResults);
186+
Assert.IsNotNull(queryNoticesResult1.Result.Notices);
187+
188+
string naturalLanguageQuery = "What is IBM's stock price?";
189+
190+
service.WithHeader("X-Watson-Test", "1");
191+
var queryNoticesResult2 = service.QueryNotices(
192+
projectId: projectId,
193+
filter: filter,
194+
naturalLanguageQuery: naturalLanguageQuery,
195+
count: 3,
196+
offset: 1
197+
);
198+
199+
Assert.IsNotNull(queryNoticesResult2.Result);
200+
Assert.IsNotNull(queryNoticesResult2.Result.MatchingResults);
201+
Assert.IsNotNull(queryNoticesResult2.Result.Notices);
123202
}
124203

125204
[TestMethod]
126205
public void TestListFields()
127206
{
128-
Assert.Fail();
207+
service.WithHeader("X-Watson-Test", "1");
208+
var listFieldsResult = service.ListFields(
209+
projectId: projectId
210+
);
211+
212+
Assert.IsNotNull(listFieldsResult.Result);
213+
Assert.IsNotNull(listFieldsResult.Result.Fields);
214+
Assert.IsTrue(listFieldsResult.Result.Fields.Count > 0);
129215
}
130216
#endregion
131217

132218
#region Component Settings
133219
[TestMethod]
134220
public void TestGetComponentSettings()
135221
{
136-
Assert.Fail();
222+
service.WithHeader("X-Watson-Test", "1");
223+
var getComponentSettingsResult = service.GetComponentSettings(
224+
projectId: projectId
225+
);
226+
227+
Assert.IsNotNull(getComponentSettingsResult.Result);
228+
Assert.IsNotNull(getComponentSettingsResult.Result.ResultsPerPage);
229+
Assert.IsNotNull(getComponentSettingsResult.Result.FieldsShown);
230+
Assert.IsNotNull(getComponentSettingsResult.Result.Aggregations);
231+
Assert.IsNotNull(getComponentSettingsResult.Result.Autocomplete);
137232
}
138233
#endregion
139234

140235
#region Documents
141236
[TestMethod]
142-
public void TestAddDocument()
237+
public void TestAddDeleteDocument()
143238
{
144-
Assert.Fail();
145-
}
239+
DetailedResponse<DocumentAccepted> addDocumentResult = null;
240+
using (FileStream fs = File.OpenRead(filepathToIngest))
241+
{
242+
using (MemoryStream ms = new MemoryStream())
243+
{
244+
fs.CopyTo(ms);
146245

147-
[TestMethod]
148-
public void TestDeleteDocument()
149-
{
150-
Assert.Fail();
246+
service.WithHeader("X-Watson-Test", "1");
247+
addDocumentResult = service.AddDocument(
248+
projectId: projectId,
249+
collectionId: collectionId,
250+
file: ms,
251+
filename: "watson_beats_jeopardy.html",
252+
fileContentType: "text/html",
253+
metadata: metadata,
254+
xWatsonDiscoveryForce: false
255+
);
256+
}
257+
}
258+
259+
Assert.IsNotNull(addDocumentResult.Result);
260+
Assert.IsNotNull(addDocumentResult.Result.DocumentId);
261+
Assert.IsNotNull(addDocumentResult.Result.Status);
262+
263+
var documentId = addDocumentResult.Result.DocumentId;
264+
265+
service.WithHeader("X-Watson-Test", "1");
266+
var deleteDocumentResult = service.DeleteDocument(
267+
projectId: projectId,
268+
collectionId: collectionId,
269+
documentId: documentId,
270+
xWatsonDiscoveryForce: false
271+
);
272+
273+
Assert.IsNotNull(deleteDocumentResult.Result);
274+
Assert.IsNotNull(deleteDocumentResult.Result.DocumentId);
275+
Assert.IsNotNull(deleteDocumentResult.Result.Status);
151276
}
152277
#endregion
153278

154279
#region Training Data
155280
[TestMethod]
156-
public void TestListTrainingQueries()
281+
public void TestTrainingQueries()
157282
{
158-
Assert.Fail();
159-
}
283+
DetailedResponse<DocumentAccepted> addDocumentResult = null;
284+
using (FileStream fs = File.OpenRead(filepathToIngest))
285+
{
286+
using (MemoryStream ms = new MemoryStream())
287+
{
288+
fs.CopyTo(ms);
160289

161-
[TestMethod]
162-
public void TestDeleteTrainingQueries()
163-
{
164-
Assert.Fail();
165-
}
290+
service.WithHeader("X-Watson-Test", "1");
291+
addDocumentResult = service.AddDocument(
292+
projectId: projectId,
293+
collectionId: collectionId,
294+
file: ms,
295+
filename: "watson_beats_jeopardy.html",
296+
fileContentType: "text/html",
297+
metadata: metadata,
298+
xWatsonDiscoveryForce: false
299+
);
300+
}
301+
}
166302

167-
[TestMethod]
168-
public void TestCreateTrainingQuery()
169-
{
170-
Assert.Fail();
171-
}
303+
var documentId = addDocumentResult.Result.DocumentId;
172304

173-
[TestMethod]
174-
public void TestGetTrainingQuery()
175-
{
176-
Assert.Fail();
177-
}
305+
service.WithHeader("X-Watson-Test", "1");
306+
var listTrainingQueriesResult = service.ListTrainingQueries(
307+
projectId: projectId
308+
);
178309

179-
[TestMethod]
180-
public void TestUpdateTrainingQuery()
181-
{
182-
Assert.Fail();
310+
Assert.IsNotNull(listTrainingQueriesResult.Result);
311+
Assert.IsNotNull(listTrainingQueriesResult.Result.Queries);
312+
313+
var naturalLanguageQuery = "What is IBM's stock price?";
314+
var filters = "entities.text:IBM";
315+
var examples = new List<TrainingExample>()
316+
{
317+
new TrainingExample()
318+
{
319+
DocumentId = documentId,
320+
CollectionId = collectionId,
321+
Relevance = 1
322+
323+
}
324+
};
325+
326+
service.WithHeader("X-Watson-Test", "1");
327+
var createTrainingQueryResult = service.CreateTrainingQuery(
328+
projectId: projectId,
329+
naturalLanguageQuery: naturalLanguageQuery,
330+
filter: filters,
331+
examples: examples
332+
);
333+
334+
var queryId = createTrainingQueryResult.Result.QueryId;
335+
Assert.IsNotNull(createTrainingQueryResult.Result);
336+
Assert.IsNotNull(createTrainingQueryResult.Result.QueryId);
337+
Assert.IsNotNull(createTrainingQueryResult.Result.Created);
338+
Assert.IsNotNull(createTrainingQueryResult.Result.Updated);
339+
Assert.IsNotNull(createTrainingQueryResult.Result.NaturalLanguageQuery);
340+
Assert.IsNotNull(createTrainingQueryResult.Result.Examples);
341+
Assert.IsTrue(createTrainingQueryResult.Result.NaturalLanguageQuery == naturalLanguageQuery);
342+
343+
service.WithHeader("X-Watson-Test", "1");
344+
var getTrainingQueryResult = service.GetTrainingQuery(
345+
projectId: projectId,
346+
queryId: queryId
347+
);
348+
349+
Assert.IsNotNull(getTrainingQueryResult.Result);
350+
Assert.IsTrue(getTrainingQueryResult.Result.QueryId == queryId);
351+
Assert.IsTrue(getTrainingQueryResult.Result.NaturalLanguageQuery == naturalLanguageQuery);
352+
353+
var updatedNaturalLanguageQuery = "Who did Watson beat on Jeopardy?";
354+
var updatedFilter = "entities.text:Jeopardy";
355+
var updatedExamples = new List<TrainingExample>()
356+
{
357+
new TrainingExample()
358+
{
359+
DocumentId = documentId,
360+
CollectionId = collectionId,
361+
Relevance = 2
362+
363+
}
364+
};
365+
366+
service.WithHeader("X-Watson-Test", "1");
367+
var updateTrainingQueryResult = service.UpdateTrainingQuery(
368+
projectId: projectId,
369+
queryId: queryId,
370+
naturalLanguageQuery: updatedNaturalLanguageQuery,
371+
filter: updatedFilter,
372+
examples: updatedExamples
373+
);
374+
375+
queryId = updateTrainingQueryResult.Result.QueryId;
376+
377+
Assert.IsTrue(updateTrainingQueryResult.Result.QueryId == queryId);
378+
Assert.IsTrue(updateTrainingQueryResult.Result.NaturalLanguageQuery == updatedNaturalLanguageQuery);
379+
380+
service.WithHeader("X-Watson-Test", "1");
381+
var deleteTrainingQueryResult = service.DeleteTrainingQueries(
382+
projectId: projectId
383+
);
384+
385+
Assert.IsTrue(deleteTrainingQueryResult.StatusCode == 204);
386+
387+
service.WithHeader("X-Watson-Test", "1");
388+
var deleteDocumentResult = service.DeleteDocument(
389+
projectId: projectId,
390+
collectionId: collectionId,
391+
documentId: documentId,
392+
xWatsonDiscoveryForce: false
393+
);
183394
}
184395
#endregion
185396
}

0 commit comments

Comments
 (0)