Esempio n. 1
0
        public void TestTokenDict()
        {
            var td = new TokenDict <string> {
                { "b", "b" }, { "p", "p" }, { "m", "m" }, { "f", "f" }, { "v", "v" },
                { "d", "d" }, { "t", "t" }, { "n", "n" }, { "l", "l" },
                { "g", "g" }, { "k", "k" }, { "ng", "ng" }, { "h", "h" },
                { "j", "g" }, { "q", "k" }, { "gn", "gn" }, { "x", "h" },
                { "zh", "j" }, { "ch", "ch" }, { "sh", "sh" }, { "r", "r" },
                { "z", "tz" }, { "c", "ts" }, { "s", "s" },
            };
            var td2 = new TokenDict <string> {
                { "", "empty" }, { "●", "symbol" }
            };
            string s = "gang", v;
            int    p = 0;

            Assert.IsTrue(td2.TryMatchStart(s, ref p, out v));
            Assert.AreEqual("empty", v);
            Assert.AreEqual(0, p);

            s = "b";
            Assert.IsTrue(td.TryMatchStart(s, ref p, out v));
            Assert.AreEqual("b", v);
            Assert.AreEqual(1, p);
            s = "012";
            Assert.IsFalse(td.TryMatchStart(s, ref p, out v));
            Assert.AreEqual(1, p);
            p = 0;
            s = "gnzc";
            Assert.IsTrue(td.TryMatchStart(s, ref p, out v));
            Assert.AreEqual("gn", v);
            Assert.AreEqual(2, p);
            Assert.IsTrue(td.TryMatchStart(s, ref p, out v));
            Assert.AreEqual("tz", v);
            Assert.AreEqual(3, p);
            Assert.IsTrue(td.TryMatchStart(s, ref p, out v));
            Assert.AreEqual("ts", v);
            Assert.AreEqual(4, p);
            Assert.IsFalse(td.TryMatchStart(s, ref p, out v));
            Assert.AreEqual(4, p);
        }
Esempio n. 2
0
        public void TestTokenDict()
        {
            var td = new TokenDict<string>{
                {"b","b"},{"p","p"},{"m","m"},{"f","f"},{"v","v"},
                {"d","d"},{"t","t"},{"n","n"},{"l","l"},
                {"g","g"},{"k","k"},{"ng","ng"},{"h","h"},
                {"j","g"},{"q","k"},{"gn","gn"},{"x","h"},
                {"zh","j"},{"ch","ch"},{"sh","sh"},{"r","r"},
                {"z","tz"},{"c","ts"},{"s","s"},

            };
            var td2 = new TokenDict<string>{{"","empty"},{"●","symbol"}};
            string s = "gang",v;
            int p = 0;
            Assert.IsTrue(td2.TryMatchStart(s,ref p, out v));
            Assert.AreEqual("empty",v);
            Assert.AreEqual(0,p);

            s = "b";
            Assert.IsTrue(td.TryMatchStart(s,ref p, out v));
            Assert.AreEqual("b",v);
            Assert.AreEqual(1,p);
            s = "012";
            Assert.IsFalse(td.TryMatchStart(s,ref p,out v));
            Assert.AreEqual(1,p);
            p = 0;
            s = "gnzc";
            Assert.IsTrue(td.TryMatchStart(s,ref p, out v));
            Assert.AreEqual("gn",v);
            Assert.AreEqual(2,p);
            Assert.IsTrue(td.TryMatchStart(s,ref p, out v));
            Assert.AreEqual("tz",v);
            Assert.AreEqual(3,p);
            Assert.IsTrue(td.TryMatchStart(s,ref p, out v));
            Assert.AreEqual("ts",v);
            Assert.AreEqual(4,p);
            Assert.IsFalse(td.TryMatchStart(s,ref p, out v));
            Assert.AreEqual(4,p);
        }
Esempio n. 3
0
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            VcapCredentials vcapCredentials = new VcapCredentials();
            fsData          data            = null;

            string result = null;
            string credentialsFilepath = "../sdk-credentials/credentials.json";

            //  Load credentials file if it exists. If it doesn't exist, don't run the tests.
            if (File.Exists(credentialsFilepath))
            {
                result = File.ReadAllText(credentialsFilepath);
            }
            else
            {
                yield break;
            }

            //  Add in a parent object because Unity does not like to deserialize root level collection types.
            result = Utility.AddTopLevelObjectToJson(result, "VCAP_SERVICES");

            //  Convert json to fsResult
            fsResult r = fsJsonParser.Parse(result, out data);

            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Convert fsResult to VcapCredentials
            object obj = vcapCredentials;

            r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
            if (!r.Succeeded)
            {
                throw new WatsonException(r.FormattedMessages);
            }

            //  Set credentials from imported credntials
            Credential credential = vcapCredentials.GetCredentialByname("discovery-sdk")[0].Credentials;
            //  Create credential and instantiate service
            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey = credential.IamApikey,
            };

            //  Create credential and instantiate service
            Credentials credentials = new Credentials(tokenOptions, credential.Url);

            //  Wait for tokendata
            while (!credentials.HasIamTokenData())
            {
                yield return(null);
            }

            _discovery             = new Discovery(credentials);
            _discovery.VersionDate = _discoveryVersionDate;
            _filePathToIngest      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";
            _documentFilePath      = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/constitution.pdf";

            //  Get Environments
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environments");
            if (!_discovery.GetEnvironments(OnGetEnvironments, OnFail))
            {
                Log.Debug("TestDiscovery.GetEnvironments()", "Failed to get environments");
            }
            while (!_getEnvironmentsTested)
            {
                yield return(null);
            }

            //  Wait for environment to be ready
            Runnable.Run(CheckEnvironmentState(0f));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  GetEnvironment
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get environment");
            if (!_discovery.GetEnvironment(OnGetEnvironment, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetEnvironment()", "Failed to get environment");
            }
            while (!_getEnvironmentTested)
            {
                yield return(null);
            }

            //  Get Configurations
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configurations");
            if (!_discovery.GetConfigurations(OnGetConfigurations, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetConfigurations()", "Failed to get configurations");
            }
            while (!_getConfigurationsTested)
            {
                yield return(null);
            }

            //  Add Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add configuration");
            if (!_discovery.AddConfiguration(OnAddConfiguration, OnFail, _environmentId, _configurationJson.Replace("{guid}", System.Guid.NewGuid().ToString())))
            {
                Log.Debug("TestDiscovery.AddConfiguration()", "Failed to add configuration");
            }
            while (!_addConfigurationTested)
            {
                yield return(null);
            }

            //  Get Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get configuration");
            if (!_discovery.GetConfiguration(OnGetConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.GetConfiguration()", "Failed to get configuration");
            }
            while (!_getConfigurationTested)
            {
                yield return(null);
            }

            //  Preview Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to preview configuration");
            if (!_discovery.PreviewConfiguration(OnPreviewConfiguration, OnFail, _environmentId, _createdConfigurationID, null, _filePathToIngest, _metadata))
            {
                Log.Debug("TestDiscovery.PreviewConfiguration()", "Failed to preview configuration");
            }
            while (!_previewConfigurationTested)
            {
                yield return(null);
            }

            //  Get Collections
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collections");
            if (!_discovery.GetCollections(OnGetCollections, OnFail, _environmentId))
            {
                Log.Debug("TestDiscovery.GetCollections()", "Failed to get collections");
            }
            while (!_getCollectionsTested)
            {
                yield return(null);
            }

            //  Add Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add collection");
            if (!_discovery.AddCollection(OnAddCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _createdConfigurationID, "en"))
            {
                Log.Debug("TestDiscovery.AddCollection()", "Failed to add collection");
            }
            while (!_addCollectionTested)
            {
                yield return(null);
            }

            //  Get Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get collection");
            if (!_discovery.GetCollection(OnGetCollection, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.GetCollection()", "Failed to get collection");
            }
            while (!_getCollectionTested)
            {
                yield return(null);
            }

            if (!_discovery.GetFields(OnGetFields, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.GetFields()", "Failed to get fields");
            }
            while (!_getFieldsTested)
            {
                yield return(null);
            }

            //  Add Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to add document");
            if (!_discovery.AddDocument(OnAddDocument, OnFail, _environmentId, _createdCollectionId, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.AddDocument()", "Failed to add document");
            }
            while (!_addDocumentTested)
            {
                yield return(null);
            }

            //  Get Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get document");
            if (!_discovery.GetDocument(OnGetDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.GetDocument()", "Failed to get document");
            }
            while (!_getDocumentTested)
            {
                yield return(null);
            }

            //  Update Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to update document");
            if (!_discovery.UpdateDocument(OnUpdateDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID, _documentFilePath, _createdConfigurationID, null))
            {
                Log.Debug("TestDiscovery.UpdateDocument()", "Failed to update document");
            }
            while (!_updateDocumentTested)
            {
                yield return(null);
            }

            //  Query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to query");
            if (!_discovery.Query(OnQuery, OnFail, _environmentId, _createdCollectionId, naturalLanguageQuery: _query))
            {
                while (!_queryTested)
                {
                    yield return(null);
                }
            }

            //  List Credentials
            Log.Debug("TestDiscovery.RunTest()", "Attempting to list credentials");
            _discovery.ListCredentials(OnListCredentials, OnFail, _environmentId);
            while (!_listCredentialsTested)
            {
                yield return(null);
            }

            //  Create Credentials
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create credentials");
            SourceCredentials credentialsParameter = new SourceCredentials()
            {
                SourceType        = SourceCredentials.SourceTypeEnum.box,
                CredentialDetails = new CredentialDetails()
                {
                    CredentialType = CredentialDetails.CredentialTypeEnum.oauth2,
                    EnterpriseId   = "myEnterpriseId",
                    ClientId       = "myClientId",
                    ClientSecret   = "myClientSecret",
                    PublicKeyId    = "myPublicIdKey",
                    Passphrase     = "myPassphrase",
                    PrivateKey     = "myPrivateKey"
                }
            };

            _discovery.CreateCredentials(OnCreateCredentials, OnFail, _environmentId, credentialsParameter);
            while (!_createCredentialsTested)
            {
                yield return(null);
            }

            //  Get Credential
            Log.Debug("TestDiscovery.RunTest()", "Attempting to get credential");
            _discovery.GetCredential(OnGetCredential, OnFail, _environmentId, _createdCredentialId);
            while (!_getCredentialTested)
            {
                yield return(null);
            }

            //  Get metrics event rate
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics event rate");
            _discovery.GetMetricsEventRate(OnGetMetricsEventRate, OnFail);
            while (!_getMetricsEventRateTested)
            {
                yield return(null);
            }

            //  Get metrics query
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query");
            _discovery.GetMetricsQuery(OnGetMetricsQuery, OnFail);
            while (!_getMetricsQueryTested)
            {
                yield return(null);
            }

            //  Get metrics query event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query event");
            _discovery.GetMetricsQueryEvent(OnGetMetricsQueryEvent, OnFail);
            while (!_getMetricsQueryEventTested)
            {
                yield return(null);
            }

            //  Get metrics query no result
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query no result");
            _discovery.GetMetricsQueryNoResults(OnGetMetricsQueryNoResult, OnFail);
            while (!_getMetricsQueryNoResultTested)
            {
                yield return(null);
            }

            //  Get metrics query token event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Get metrics query token event");
            _discovery.GetMetricsQueryTokenEvent(OnGetMetricsQueryTokenEvent, OnFail);
            while (!_getMetricsQueryTokenEventTested)
            {
                yield return(null);
            }

            //  Query log
            Log.Debug("TestDiscovery.RunTest()", "Attempting to Query log");
            _discovery.QueryLog(OnQueryLog, OnFail);
            while (!_queryLogTested)
            {
                yield return(null);
            }

            //  Create event
            Log.Debug("TestDiscovery.RunTest()", "Attempting to create event");
            CreateEventObject queryEvent = new CreateEventObject()
            {
                Type = CreateEventObject.TypeEnum.click,
                Data = new EventData()
                {
                    EnvironmentId = _environmentId,
                    SessionToken  = _sessionToken,
                    CollectionId  = _createdCollectionId,
                    DocumentId    = _createdDocumentID
                }
            };

            _discovery.CreateEvent(OnCreateEvent, OnFail, queryEvent);
            while (!_createEventTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTests()", "Attempting to list expansions");
            _discovery.ListExpansions(OnListExpansions, OnFail, _environmentId, _createdCollectionId);
            while (!_listExpansionsTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTests()", "Attempting to create expansion");
            Expansions expansions = new Expansions()
            {
                ExpansionsProperty = new List <Expansion>()
                {
                    new Expansion()
                    {
                        ExpandedTerms = new List <string>()
                        {
                            "expanded-term"
                        },
                        InputTerms = new List <string>()
                        {
                            "input-term"
                        }
                    }
                }
            };

            _discovery.CreateExpansions(OnCreateExpansion, OnFail, _environmentId, _createdCollectionId, expansions);
            while (!_createExpansionTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTests()", "Attempting to delete expansion");
            _discovery.DeleteExpansions(OnDeleteExpansion, OnFail, _environmentId, _createdCollectionId);
            while (!_deleteExpansionTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTest()", "Attempting to create jp collection");
            _discovery.AddCollection(OnCreateJpCollection, OnFail, _environmentId, _createdCollectionName + System.Guid.NewGuid().ToString(), _createdCollectionDescription, _createdConfigurationID, "ja");
            while (!_createJpCollectionTested)
            {
                yield return(null);
            }

            TokenDict tokenizationDictionary = new TokenDict()
            {
                TokenizationRules = new List <TokenDictRule>()
                {
                    new TokenDictRule()
                    {
                        Text   = "すしネコ",
                        Tokens = new List <string>()
                        {
                            "すし", "ネコ"
                        },
                        Readings = new List <string>()
                        {
                            "寿司", "ネコ"
                        },
                        PartOfSpeech = "カスタム名詞"
                    }
                }
            };

            Log.Debug("TestDiscovery.RunTests()", "Attempting to create tokenization dict");
            _discovery.CreateTokenizationDictionary(OnCreateTokenizationDictionary, OnCreateTokenizationDictionaryFail, _environmentId, _createdJpCollection, tokenizationDictionary);
            while (!_createTokenizationDictTested)
            {
                yield return(null);
            }

            if (!_getTokenizationDictStatusTested)
            {
                Log.Debug("TestDiscovery.RunTests()", "Attempting to get tokenization dict status");
                _discovery.GetTokenizationDictionaryStatus(OnGetTokenizationDictonaryStatus, OnFail, _environmentId, _createdJpCollection);
                while (!_getTokenizationDictStatusTested)
                {
                    yield return(null);
                }
            }

            if (!_deleteTokenizationDictTested)
            {
                Log.Debug("TestDiscovery.RunTests()", "Attempting to delete tokenization dict");
                _discovery.DeleteTokenizationDictionary(OnDeleteTokenizationDictionary, OnFail, _environmentId, _createdJpCollection);
                while (!_deleteTokenizationDictTested)
                {
                    yield return(null);
                }
            }

            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete jp collection");
            _discovery.DeleteCollection(OnDeleteJpCollection, OnFail, _environmentId, _createdJpCollection);
            while (!_deleteJpCollectionTested)
            {
                yield return(null);
            }

            //  DeleteCredential
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete credential");
            _discovery.DeleteCredentials(OnDeleteCredentials, OnFail, _environmentId, _createdCredentialId);
            while (!_deleteCredentialsTested)
            {
                yield return(null);
            }

            //  Delete Document
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete document {0}", _createdDocumentID);
            if (!_discovery.DeleteDocument(OnDeleteDocument, OnFail, _environmentId, _createdCollectionId, _createdDocumentID))
            {
                Log.Debug("TestDiscovery.DeleteDocument()", "Failed to delete document");
            }
            while (!_deleteDocumentTested)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  Delete Collection
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionId);
            if (!_discovery.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionId))
            {
                Log.Debug("TestDiscovery.DeleteCollection()", "Failed to delete collection");
            }
            while (!_deleteCollectionTested)
            {
                yield return(null);
            }

            _isEnvironmentReady = false;
            Runnable.Run(CheckEnvironmentState(_waitTime));
            while (!_isEnvironmentReady)
            {
                yield return(null);
            }

            //  Delete Configuration
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _createdConfigurationID);
            if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _createdConfigurationID))
            {
                Log.Debug("TestDiscovery.DeleteConfiguration()", "Failed to delete configuration");
            }
            while (!_deleteConfigurationTested)
            {
                yield return(null);
            }

            //  Delete User Data
            Log.Debug("TestDiscovery.RunTest()", "Attempting to delete user data.");
            _discovery.DeleteUserData(OnDeleteUserData, OnFail, "test-unity-user-id");
            while (!_deleteUserDataTested)
            {
                yield return(null);
            }

            Log.Debug("TestDiscovery.RunTest()", "Discovery unit tests complete.");

            yield break;
        }