Skip to content

Commit b9bfd3f

Browse files
committed
test(Discovery): Added test for GetStopwordList
1 parent b742398 commit b9bfd3f

File tree

1 file changed

+128
-31
lines changed

1 file changed

+128
-31
lines changed

Scripts/UnitTests/TestDiscovery.cs

Lines changed: 128 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public class TestDiscovery : UnitTest
8686
private string _createdJpCollection = null;
8787
private bool _createJpCollectionTested = false;
8888
private bool _deleteJpCollectionTested = false;
89+
private bool _isTokenizationDictionaryReady = false;
8990

9091
private bool _createEventTested = false;
9192
private bool _getMetricsEventRateTested = false;
@@ -95,9 +96,11 @@ public class TestDiscovery : UnitTest
9596
private bool _getMetricsQueryTokenEventTested = false;
9697
private bool _queryLogTested = false;
9798

98-
//private string _stopwordsFilepath;
99-
//private bool _createStopwordListTested = false;
100-
//private bool _deleteStopwordListTested = false;
99+
private string _stopwordsFilepath;
100+
private bool _createStopwordListTested = false;
101+
private bool _getStopwordListTested = false;
102+
private bool _deleteStopwordListTested = false;
103+
private bool _isStopwordsListReady = false;
101104

102105
private bool _listGatewaysTested = false;
103106
private bool _createGatewayTested = false;
@@ -163,7 +166,7 @@ public override IEnumerator RunTest()
163166
_discovery.VersionDate = _discoveryVersionDate;
164167
_filePathToIngest = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";
165168
_documentFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";
166-
//_stopwordsFilepath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/stopwords.txt";
169+
_stopwordsFilepath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/stopwords.txt";
167170
// Get Environments
168171
Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
169172
if (!_discovery.GetEnvironments(OnGetEnvironments, OnFail))
@@ -391,6 +394,10 @@ public override IEnumerator RunTest()
391394
while (!_createJpCollectionTested)
392395
yield return null;
393396

397+
//Runnable.Run(CheckTokenizationDictionaryState(0f));
398+
//while (!_isTokenizationDictionaryReady)
399+
// yield return null;
400+
394401
// Create tokenization dictionary
395402
TokenDict tokenizationDictionary = new TokenDict()
396403
{
@@ -416,6 +423,7 @@ public override IEnumerator RunTest()
416423
while (!_createTokenizationDictTested)
417424
yield return null;
418425

426+
419427
// Get tokenization dictionary status
420428
if (!_getTokenizationDictStatusTested)
421429
{
@@ -425,6 +433,10 @@ public override IEnumerator RunTest()
425433
yield return null;
426434
}
427435

436+
//Runnable.Run(CheckTokenizationDictionaryState(0f));
437+
//while (!_isTokenizationDictionaryReady)
438+
// yield return null;
439+
428440
// Delete tokenization dictionary
429441
if (!_deleteTokenizationDictTested)
430442
{
@@ -458,21 +470,35 @@ public override IEnumerator RunTest()
458470
while (!_isEnvironmentReady)
459471
yield return null;
460472

461-
//// Create stopword list
462-
//using (FileStream fs = File.OpenRead(_stopwordsFilepath))
463-
//{
473+
//Runnable.Run(CheckStopwordsListState(0f));
474+
//while (!_isStopwordsListReady)
475+
// yield return null;
464476

465-
// Log.Debug("TestDiscovery.RunTest()", "Attempting to create stopword list {0}", _createdCollectionId);
466-
// _discovery.CreateStopwordList(OnCreateStopwordList, OnFail, _environmentId, _createdCollectionId, fs);
467-
// while (!_createStopwordListTested)
468-
// yield return null;
469-
//}
477+
// Create stopword list
478+
using (FileStream fs = File.OpenRead(_stopwordsFilepath))
479+
{
470480

471-
//// Delete stopword list
472-
//Log.Debug("TestDiscovery.RunTest()", "Attempting to delete stopword list {0}", _createdCollectionId);
473-
//_discovery.DeleteStopwordList(OnDeleteStopwordList, OnFail, _environmentId, _createdCollectionId);
474-
//while (!_deleteStopwordListTested)
475-
// yield return null;
481+
Log.Debug("TestDiscovery.RunTest()", "Attempting to create stopword list {0}", _createdCollectionId);
482+
_discovery.CreateStopwordList(OnCreateStopwordList, OnFail, _environmentId, _createdCollectionId, fs);
483+
while (!_createStopwordListTested)
484+
yield return null;
485+
}
486+
487+
// Get stopword list
488+
Log.Debug("TestDiscovery.RunTest()", "Attempting to get stopword list {0}", _createdCollectionId);
489+
_discovery.GetStopwordListStatus(OnGetStopwordList, OnFail, _environmentId, _createdCollectionId);
490+
while (!_getStopwordListTested)
491+
yield return null;
492+
493+
Runnable.Run(CheckStopwordsListState(0f));
494+
while (!_isStopwordsListReady)
495+
yield return null;
496+
497+
// Delete stopword list
498+
Log.Debug("TestDiscovery.RunTest()", "Attempting to delete stopword list {0}", _createdCollectionId);
499+
_discovery.DeleteStopwordList(OnDeleteStopwordList, OnFail, _environmentId, _createdCollectionId);
500+
while (!_deleteStopwordListTested)
501+
yield return null;
476502

477503
// List Gatways
478504
Log.Debug("TestDiscovery.RunTest()", "Attempting to list gateways.");
@@ -631,6 +657,70 @@ private void HandleCheckEnvironmentState(Environment resp, Dictionary<string, ob
631657
}
632658
#endregion
633659

660+
#region Check stopwords list state
661+
private IEnumerator CheckStopwordsListState(float waitTime)
662+
{
663+
yield return new WaitForSeconds(waitTime);
664+
665+
Log.Debug("TestDiscovery.CheckStopwordsListState()", "Attempting to get stopwords list state");
666+
try
667+
{
668+
_discovery.GetStopwordListStatus(HandleGetStopwordsListState, OnFail, _environmentId, _createdCollectionId);
669+
}
670+
catch (System.Exception e)
671+
{
672+
Log.Debug("TestDiscovery.CheckStopwordsListState()", string.Format("Failed to get stopwords list state: {0}", e.Message));
673+
Runnable.Run(CheckStopwordsListState(10f));
674+
}
675+
}
676+
677+
private void HandleGetStopwordsListState(TokenDictStatusResponse resp, Dictionary<string, object> customData)
678+
{
679+
Log.Debug("TestDiscovery.HandleGetStopwordsListState()", "Stopwords list is {1}", resp.Status);
680+
681+
if (resp.Status.ToLower() == "pending")
682+
{
683+
Runnable.Run(CheckStopwordsListState(10f));
684+
}
685+
else
686+
{
687+
_isStopwordsListReady = true;
688+
}
689+
}
690+
#endregion
691+
692+
#region Check tokenization dictionary state
693+
private IEnumerator CheckTokenizationDictionaryState(float waitTime)
694+
{
695+
yield return new WaitForSeconds(waitTime);
696+
697+
Log.Debug("TestDiscovery.CheckTokenizationDictionaryState()", "Attempting to get tokenization dictionary state");
698+
try
699+
{
700+
_discovery.GetTokenizationDictionaryStatus(HandleGetTokenizationDictionaryState, OnFail, _environmentId, _createdCollectionId);
701+
}
702+
catch (System.Exception e)
703+
{
704+
Log.Debug("TestDiscovery.CheckTokenizationDictionaryState()", string.Format("Failed to get tokenization dictionary state: {0}", e.Message));
705+
Runnable.Run(CheckTokenizationDictionaryState(10f));
706+
}
707+
}
708+
709+
private void HandleGetTokenizationDictionaryState(TokenDictStatusResponse resp, Dictionary<string, object> customData)
710+
{
711+
Log.Debug("TestDiscovery.HandleTokenizationDictionaryState()", "Tokenization dictionary state is {1}", resp.Status);
712+
713+
if (resp.Status.ToLower() == "pending")
714+
{
715+
Runnable.Run(CheckTokenizationDictionaryState(10f));
716+
}
717+
else
718+
{
719+
_isTokenizationDictionaryReady = true;
720+
}
721+
}
722+
#endregion
723+
634724
private void OnGetEnvironments(GetEnvironmentsResponse resp, Dictionary<string, object> customData)
635725
{
636726
Log.Debug("TestDiscovery.OnGetEnvironments()", "Discovery - GetEnvironments Response: {0}", customData["json"].ToString());
@@ -854,20 +944,27 @@ private void OnQueryLog(LogQueryResponse response, Dictionary<string, object> cu
854944
_queryLogTested = true;
855945
}
856946

857-
//private void OnCreateStopwordList(TokenDictStatusResponse response, Dictionary<string, object> customData)
858-
//{
859-
// Log.Debug("TestDiscovery.OnCreateStopwordList()", "Response: {0}", customData["json"].ToString());
860-
// Test(response != null);
861-
// Test(response.Status == "pending");
862-
// _createStopwordListTested = true;
863-
//}
864-
865-
//private void OnDeleteStopwordList(object response, Dictionary<string, object> customData)
866-
//{
867-
// Log.Debug("TestDiscovery.OnDeleteStopwordList()", "Success!");
868-
// Test(response != null);
869-
// _deleteStopwordListTested = true;
870-
//}
947+
private void OnCreateStopwordList(TokenDictStatusResponse response, Dictionary<string, object> customData)
948+
{
949+
Log.Debug("TestDiscovery.OnCreateStopwordList()", "Response: {0}", customData["json"].ToString());
950+
Test(response != null);
951+
Test(response.Status == "pending");
952+
_createStopwordListTested = true;
953+
}
954+
955+
private void OnGetStopwordList(TokenDictStatusResponse response, Dictionary<string, object> customData)
956+
{
957+
Log.Debug("TestDiscovery.OnGetStopwordList()", "Response: {0}", customData["json"].ToString());
958+
Test(response != null);
959+
_getStopwordListTested = true;
960+
}
961+
962+
private void OnDeleteStopwordList(object response, Dictionary<string, object> customData)
963+
{
964+
Log.Debug("TestDiscovery.OnDeleteStopwordList()", "Success!");
965+
Test(response != null);
966+
_deleteStopwordListTested = true;
967+
}
871968

872969
private void OnListGateways(GatewayList response, Dictionary<string, object> customData)
873970
{

0 commit comments

Comments
 (0)