This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
forked from steveherschleb/sdk_python
-
Notifications
You must be signed in to change notification settings - Fork 91
/
tests.py
268 lines (227 loc) · 8.76 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
# Copyright 2013 AlchemyAPI
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from alchemyapi import AlchemyAPI
test_text = 'Bob broke my heart, and then made up this silly sentence to test the PHP SDK'
test_html = '<html><head><title>The best SDK Test | AlchemyAPI</title></head><body><h1>Hello World!</h1><p>My favorite language is PHP</p></body></html>'
test_url = 'http://www.nytimes.com/2013/07/13/us/politics/a-day-of-friction-notable-even-for-a-fractious-congress.html?_r=0'
test_jpg = 'pigeon.jpg'
alchemyapi = AlchemyAPI()
# Entities
print('Checking entities . . . ')
response = alchemyapi.entities('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.entities('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.entities('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.entities('random', test_url)
assert(response['status'] == 'ERROR') # invalid flavor
print('Entity tests complete!')
print('')
# Keywords
print('Checking keywords . . . ')
response = alchemyapi.keywords('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.keywords('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.keywords('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.keywords('random', test_url)
assert(response['status'] == 'ERROR') # invalid flavor
print('Keyword tests complete!')
print('')
# Concepts
print('Checking concepts . . . ')
response = alchemyapi.concepts('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.concepts('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.concepts('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.concepts('random', test_url)
assert(response['status'] == 'ERROR') # invalid flavor
print('Concept tests complete!')
print('')
# Sentiment
print('Checking sentiment . . . ')
response = alchemyapi.sentiment('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.sentiment('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.sentiment('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.sentiment('random', test_url)
assert(response['status'] == 'ERROR') # invalid flavor
print('Sentiment tests complete!')
print('')
# Targeted Sentiment
print('Checking targeted sentiment . . . ')
response = alchemyapi.sentiment_targeted('text', test_text, 'heart')
assert(response['status'] == 'OK')
response = alchemyapi.sentiment_targeted('html', test_html, 'language')
assert(response['status'] == 'OK')
response = alchemyapi.sentiment_targeted('url', test_url, 'Congress')
assert(response['status'] == 'OK')
response = alchemyapi.sentiment_targeted('random', test_url, 'Congress')
assert(response['status'] == 'ERROR') # invalid flavor
response = alchemyapi.sentiment_targeted('text', test_text, None)
assert(response['status'] == 'ERROR') # missing target
print('Targeted sentiment tests complete!')
print('')
# Text
print('Checking text . . . ')
response = alchemyapi.text('text', test_text)
assert(response['status'] == 'ERROR') # only works for html and url content
response = alchemyapi.text('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.text('url', test_url)
assert(response['status'] == 'OK')
print('Text tests complete!')
print('')
# Text Raw
print('Checking raw text . . . ')
response = alchemyapi.text_raw('text', test_text)
assert(response['status'] == 'ERROR') # only works for html and url content
response = alchemyapi.text_raw('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.text_raw('url', test_url)
assert(response['status'] == 'OK')
print('Raw text tests complete!')
print('')
# Author
print('Checking author . . . ')
response = alchemyapi.author('text', test_text)
assert(response['status'] == 'ERROR') # only works for html and url content
response = alchemyapi.author('html', test_html)
assert(response['status'] == 'ERROR') # there's no author in the test HTML
response = alchemyapi.author('url', test_url)
assert(response['status'] == 'OK')
print('Author tests complete!')
print('')
# Language
print('Checking language . . . ')
response = alchemyapi.language('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.language('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.language('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.language('random', test_url)
assert(response['status'] == 'ERROR') # invalid flavor
print('Language tests complete!')
print('')
# Title
print('Checking title . . . ')
response = alchemyapi.title('text', test_text)
assert(response['status'] == 'ERROR') # only works for html and url content
response = alchemyapi.title('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.title('url', test_url)
assert(response['status'] == 'OK')
print('Title tests complete!')
print('')
# Relations
print('Checking relations . . . ')
response = alchemyapi.relations('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.relations('html', test_html)
assert(response['status'] == 'OK')
response = alchemyapi.relations('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.relations('random', test_url)
assert(response['status'] == 'ERROR') # invalid flavor
print('Relation tests complete!')
print('')
# Category
print('Checking category . . . ')
response = alchemyapi.category('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.category('html', test_html, {'url': 'test'})
assert(response['status'] == 'OK')
response = alchemyapi.category('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.category('random', test_url)
assert(response['status'] == 'ERROR') # invalid flavor
print('Category tests complete!')
print('')
# Feeds
print('Checking feeds . . . ')
response = alchemyapi.feeds('text', test_text)
assert(response['status'] == 'ERROR') # only works for html and url content
response = alchemyapi.feeds('html', test_html, {'url': 'test'})
assert(response['status'] == 'OK')
response = alchemyapi.feeds('url', test_url)
assert(response['status'] == 'OK')
print('Feed tests complete!')
print('')
# Microformats
print('Checking microformats . . . ')
response = alchemyapi.microformats('text', test_text)
assert(response['status'] == 'ERROR') # only works for html and url content
response = alchemyapi.microformats('html', test_html, {'url': 'test'})
assert(response['status'] == 'OK')
response = alchemyapi.microformats('url', test_url)
assert(response['status'] == 'OK')
print('Microformat tests complete!')
print('')
print('')
# imagetagging
print('Checking imagetagging . . . ')
response = alchemyapi.imageTagging('text', test_text)
assert(response['status'] == 'ERROR')
response = alchemyapi.imageTagging('html', test_html)
assert(response['status'] == 'ERROR')
response = alchemyapi.imageTagging('url', test_url)
assert(response['status'] == 'OK')
response = alchemyapi.imageTagging('image', test_jpg)
assert(response['status'] == 'OK')
print('Image tagging tests complete!')
print('')
print('')
# combined
print('Checking combined . . . ')
response = alchemyapi.combined('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.combined('html', test_html)
assert(response['status'] == 'ERROR')
response = alchemyapi.combined('url', test_url)
assert(response['status'] == 'OK')
print('Combined tests complete!')
print('')
print('')
# taxonomy
print('Checking taxonomy . . . ')
response = alchemyapi.taxonomy('text', test_text)
assert(response['status'] == 'OK')
response = alchemyapi.taxonomy('html', test_html, {'url': 'test'})
assert(response['status'] == 'OK')
response = alchemyapi.taxonomy('url', test_url)
assert(response['status'] == 'OK')
print('Taxonomy tests complete!')
print('')
print('')
# image
print('Checking image extraction . . . ')
response = alchemyapi.imageExtraction('text', test_text)
assert(response['status'] == 'ERROR')
response = alchemyapi.imageExtraction('html', test_html)
assert(response['status'] == 'ERROR')
response = alchemyapi.imageExtraction('url', test_url)
assert(response['status'] == 'OK')
print('Image Extraction tests complete!')
print('')
print('')
print('**** All tests complete! ****')