Skip to content

Commit d3cf0de

Browse files
committed
test(NLC): Add integration tests to NLC
1 parent b6602f3 commit d3cf0de

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

examples/natural_language_classifier_v1.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from __future__ import print_function
22
import json
33
import os
4-
import time
4+
55
# from os.path import join, dirname
66
from watson_developer_cloud import NaturalLanguageClassifierV1
7-
FIVE_SECONDS = 5
87

98
natural_language_classifier = NaturalLanguageClassifierV1(
109
username='YOUR SERVICE USERNAME',
@@ -23,7 +22,6 @@
2322
classifier_id = classifier['classifier_id']
2423
print(json.dumps(classifier, indent=2))
2524

26-
time.sleep(FIVE_SECONDS)
2725
status = natural_language_classifier.get_classifier(classifier_id)
2826
print(json.dumps(status, indent=2))
2927

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# coding: utf-8
2+
from unittest import TestCase
3+
import os
4+
import watson_developer_cloud
5+
import pytest
6+
import json
7+
import time
8+
9+
FIVE_SECONDS = 5
10+
11+
@pytest.mark.skipif(os.getenv('VCAP_SERVICES') is None, reason='requires VCAP_SERVICES')
12+
class TestNaturalLanguageClassifierV1(TestCase):
13+
def setUp(self):
14+
self.natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1(
15+
username='YOUR SERVICE USERNAME',
16+
password='YOUR SERVICE PASSWORD')
17+
self.natural_language_classifier.set_default_headers({
18+
'X-Watson-Learning-Opt-Out': '1',
19+
'X-Watson-Test': '1'
20+
})
21+
22+
# Create a classifier
23+
with open(os.path.join(os.path.dirname(__file__), '../../resources/weather_data_train.csv'), 'rb') as training_data:
24+
metadata = json.dumps({'name': 'my-classifier', 'language': 'en'})
25+
classifier = self.natural_language_classifier.create_classifier(
26+
metadata=metadata,
27+
training_data=training_data
28+
)
29+
self.classifier_id = classifier['classifier_id']
30+
31+
def tearDown(self):
32+
self.natural_language_classifier.delete_classifier(self.classifier_id)
33+
34+
def test_list_classifier(self):
35+
list_classifiers = self.natural_language_classifier.list_classifiers()
36+
assert list_classifiers is not None
37+
38+
@pytest.mark.skip(reason="The classifier takes more than a minute")
39+
def test_classify_text(self):
40+
iterations = 0
41+
while iterations < 15:
42+
status = self.natural_language_classifier.get_classifier(self.classifier_id)
43+
iterations += 1
44+
if status['status'] != 'Available':
45+
time.sleep(FIVE_SECONDS)
46+
47+
if status['status'] != 'Available':
48+
assert False, 'Classifier is not available'
49+
50+
classes = self.natural_language_classifier.classify(self.classifier_id, 'How hot will it be tomorrow?')
51+
assert classes is not None
52+
53+
collection = ['{"text":"How hot will it be today?"}', '{"text":"Is it hot outside?"}']
54+
classes = self.natural_language_classifier.classify_collection(
55+
self.classifier_id, collection)
56+
assert classes is not None

0 commit comments

Comments
 (0)