1+ from unittest import TestCase
2+ import watson_developer_cloud
3+ import responses
4+ import pytest
5+
6+ class TestAlchemyLanguageV1 (TestCase ):
7+
8+ def test_api_key (self ):
9+ default_url = 'https://gateway-a.watsonplatform.net/calls'
10+ inited = watson_developer_cloud .AlchemyLanguageV1 (url = default_url , api_key = 'boguskey' , x_watson_learning_opt_out = True )
11+ assert inited .x_watson_learning_opt_out
12+ assert inited .api_key == 'boguskey'
13+ assert inited .url == default_url
14+ inited .set_url (url = "http://google.com" )
15+ assert inited .url == "http://google.com"
16+
17+ with pytest .raises (watson_developer_cloud .WatsonException ):
18+ watson_developer_cloud .AlchemyLanguageV1 ()
19+
20+ with pytest .raises (watson_developer_cloud .WatsonException ):
21+ watson_developer_cloud .AlchemyLanguageV1 (api_key = 'YOUR API KEY' )
22+
23+ def test_unpack_id (self ):
24+
25+ testdict = {'one' : 10 }
26+ assert watson_developer_cloud .AlchemyLanguageV1 .unpack_id (testdict , 'one' ) == 10
27+ assert watson_developer_cloud .AlchemyLanguageV1 .unpack_id (testdict , 'two' ) == testdict
28+
29+ @responses .activate
30+ def test_author (self ):
31+ url = 'https://gateway-a.watsonplatform.net'
32+ default_url = 'https://gateway-a.watsonplatform.net/calls'
33+ responses .add (responses .POST , '{0}/html/HTMLGetAuthor' .format (url ),
34+ body = '{"bogus": "response"}' , status = 200 ,
35+ content_type = 'application/json' )
36+ responses .add (responses .POST , '{0}/url/URLGetAuthor' .format (url ),
37+ body = '{"bogus": "response"}' , status = 200 ,
38+ content_type = 'application/json' )
39+ responses .add (responses .POST , '{0}/html/HTMLGetAuthor' .format (default_url ),
40+ body = '{"bogus": "response"}' , status = 200 ,
41+ content_type = 'application/json' )
42+ responses .add (responses .POST , '{0}/url/URLGetAuthor' .format (default_url ),
43+ body = '{"bogus": "response"}' , status = 200 ,
44+ content_type = 'application/json' )
45+ alang = watson_developer_cloud .AlchemyLanguageV1 (url = url , api_key = 'boguskey' , x_watson_learning_opt_out = True )
46+ alang .author (html = "I'm html" )
47+ alang .author (url = "http://google.com" )
48+ with pytest .raises (watson_developer_cloud .WatsonInvalidArgument ):
49+ alang .author ()
50+
51+ alang = watson_developer_cloud .AlchemyLanguageV1 (url = default_url , api_key = 'boguskey' , x_watson_learning_opt_out = True )
52+ alang .author (html = "I'm html" )
53+ alang .author (url = "http://google.com" )
54+ assert len (responses .calls ) == 4
55+
56+ @responses .activate
57+ def test_auth_exception (self ):
58+ default_url = 'https://gateway-a.watsonplatform.net/calls'
59+ responses .add (responses .POST , '{0}/url/URLGetAuthor' .format (default_url ),
60+ body = '{"bogus": "response"}' , status = 401 ,
61+ content_type = 'application/json' )
62+
63+ alang = watson_developer_cloud .AlchemyLanguageV1 (url = default_url , api_key = 'boguskey' , x_watson_learning_opt_out = True )
64+ with pytest .raises (watson_developer_cloud .WatsonException ):
65+ alang .author (url = "http://google.com" )
66+ assert len (responses .calls ) == 1
67+
68+ @responses .activate
69+ def test_authors (self ):
70+ default_url = 'https://gateway-a.watsonplatform.net/calls'
71+ responses .add (responses .POST , '{0}/url/URLGetAuthors' .format (default_url ),
72+ body = '{"bogus": "response"}' , status = 200 ,
73+ content_type = 'application/json' )
74+ responses .add (responses .POST , '{0}/html/HTMLGetAuthors' .format (default_url ),
75+ body = '{"bogus": "response"}' , status = 200 ,
76+ content_type = 'application/json' )
77+
78+ alang = watson_developer_cloud .AlchemyLanguageV1 (url = default_url , api_key = 'boguskey' ,
79+ x_watson_learning_opt_out = True )
80+ alang .authors (url = "http://google.com" )
81+ alang .authors (html = "<h1>Author</h1>" )
82+ assert len (responses .calls ) == 2
0 commit comments