tests for python3 wordnik.com api client

This commit is contained in:
Russell Horton 2012-12-20 14:38:15 -08:00
parent 6266a94857
commit 2bc8cc01a5
5 changed files with 342 additions and 0 deletions

View File

@ -0,0 +1,56 @@
#!/usr/bin/env python
import sys
import unittest
import urllib.request, urllib.error, urllib.parse
import json
from BaseApiTest import BaseApiTest
sys.path = ['./'] + sys.path
from wordnik import *
class AccountApiTest(BaseApiTest):
def setUp(self):
super(AccountApiTest, self).setUp()
self.authToken = self.accountApi.authenticate(self.username,
self.password).token
def testAuthenticate(self):
res = self.accountApi.authenticate(self.username, self.password)
assert res, 'null authenticate result'
assert res.token, 'invalid authentication token'
assert res.userId != 0, 'userId was 0'
assert res.userSignature, 'invalid userSignature'
def testAuthenticatePost(self):
res = self.accountApi.authenticatePost(self.username, self.password)
assert res, 'null authenticate result'
assert res.token, 'invalid authentication token'
assert res.userId != 0, 'userId was 0'
assert res.userSignature, 'invalid userSignature'
def testGetWordListsForLoggedInUser(self):
res = self.accountApi.getWordListsForLoggedInUser(self.authToken)
assert res, 'null getWordListsForLoggedInUser result'
assert len(res) != 0, 'number of lists shouldn\'t be 0'
def testGetApiTokenStatus(self):
res = self.accountApi.getApiTokenStatus()
assert res, 'null getApiTokenStatus result'
assert res.valid, 'token status not valid'
assert res.remainingCalls != 0, 'remainingCalls shouldn\'t be 0'
def testGetLoggedInUser(self):
res = self.accountApi.getLoggedInUser(self.authToken)
assert res, 'null getLoggedInUser result'
assert res.id != 0, 'if shouldn\'t be 0'
assert res.username == self.username, 'username was incorrect'
assert res.status == 0, 'user status should be 0'
assert res.email, 'email shouldn\'t be null'
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,45 @@
#!/usr/bin/env python
"""Unit tests for Python Wordnik API client.
Requires you to set four environment varibales:
WN_APIKEY your API key
WN_APIURL the API base url
WN_USERNAME the username of a user
WN_PASSWORD the user's password
Run all tests:
python BaseApiTest.py
"""
import sys
import os
import unittest
sys.path = ['./'] + sys.path
from wordnik import *
class BaseApiTest(unittest.TestCase):
def setUp(self):
self.apiUrl = 'http://api.wordnik.com/v4'
self.apiKey = os.environ.get('API_KEY')
self.username = os.environ.get('USER_NAME')
self.password = os.environ.get('PASSWORD')
client = swagger.ApiClient(self.apiKey, self.apiUrl)
self.accountApi = AccountApi.AccountApi(client)
self.wordApi = WordApi.WordApi(client)
self.wordListApi = WordListApi.WordListApi(client)
self.wordsApi = WordsApi.WordsApi(client)
if __name__ == "__main__":
from AccountApiTest import AccountApiTest
from WordApiTest import WordApiTest
from WordListApiTest import WordListApiTest
from WordsApiTest import WordsApiTest
unittest.main()

View File

@ -0,0 +1,91 @@
#!/usr/bin/env python
import sys
import unittest
import urllib.request, urllib.error, urllib.parse
import json
from BaseApiTest import BaseApiTest
sys.path = ['./'] + sys.path
from wordnik import *
class WordApiTest(BaseApiTest):
def testWordApis(self):
url = 'http://api.wordnik.com/v4/word.json'
request = urllib.request.urlopen(url)
encoding = request.headers.get_content_charset()
if not encoding:
encoding = 'iso-8859-1'
response = request.read().decode(encoding)
doc = json.loads(response)
assert len(doc['apis']) == 12, 'there should be 12 word apis'
def testGetWord(self):
res = self.wordApi.getWord('cat')
assert res, 'null getWord result'
assert res.word == 'cat', 'word should be "cat"'
def testGetWordWithSuggestions(self):
res = self.wordApi.getWord('cAt', includeSuggestions=True)
assert res, 'null getWord result'
assert res.word == 'cAt', 'word should be "cAt"'
def testGetWordWithCanonicalForm(self):
res = self.wordApi.getWord('cAt', useCanonical=True)
assert res, 'null getWord result'
assert res.word == 'cat', 'word should be "cAt"'
def testGetDefinitions(self):
res = self.wordApi.getDefinitions('cat', limit=10)
assert res, 'null getDefinitions result'
assert len(res) == 10, 'should have 10 definitions'
def testGetExamples(self):
res = self.wordApi.getExamples('cat', limit=5)
assert res, 'null getExamples result'
assert len(res.examples) == 5, 'should have 5 definitions'
def testGetTopExample(self):
res = self.wordApi.getTopExample('cat')
assert res, 'null getTopExample result'
assert res.word == 'cat', 'word should be "cat"'
def testGetHyphenation(self):
res = self.wordApi.getHyphenation('catalog', limit=1)
assert res, 'null getHyphenation result'
assert len(res) == 1, 'hypenation length should be 1'
def testGetWordFrequency(self):
res = self.wordApi.getWordFrequency('cat')
assert res, 'null getWordFrequency result'
assert res.totalCount != 0, 'total count should not be 0'
def testGetPhrases(self):
res = self.wordApi.getPhrases('money')
assert res, 'null getPhrases result'
assert len(res) != 0, 'getPhrases length should not be 0'
def testGetRelatedWords(self):
res = self.wordApi.getRelatedWords('cat')
assert res, 'null getRelatedWords result'
for related in res:
assert len(related.words) <= 10, 'should have <= 10 related words'
def testGetAudio(self):
res = self.wordApi.getAudio('cat', useCanonical=True, limit=2)
assert res, 'null getAudio result'
assert len(res) == 2, 'getAudio size should be 2'
def testGetScrabbleScore(self):
res = self.wordApi.getScrabbleScore('quixotry')
assert res.value == 27, 'quixotry should have a Scrabble score of 27'
def testGetEtymologies(self):
res = self.wordApi.getEtymologies('butter')
assert 'of Scythian origin' in res[0], 'etymology of "butter" should contain the phrase "of Scythian origin"'
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,103 @@
#!/usr/bin/env python
import sys
import unittest
import urllib.request, urllib.error, urllib.parse
import json
from BaseApiTest import BaseApiTest
sys.path = ['./'] + sys.path
from wordnik import *
class WordListApiTest(BaseApiTest):
def setUp(self):
super(WordListApiTest, self).setUp()
self.authToken = self.accountApi.authenticate(self.username,
self.password).token
self.existingList = self.accountApi.getWordListsForLoggedInUser(self.authToken,
limit=1)[0]
from wordnik.models import WordList
wordList = WordList.WordList()
wordList.name = "my test list"
wordList.type = "PUBLIC"
wordList.description = "some words I want to play with"
# sampleList = self.wordListApi.createWordList(wordList,
# self.authToken)
# if not sampleList:
# raise Exception("can't create test list to run tests with")
def testGetWordListByPermalink(self):
res = self.wordListApi.getWordListByPermalink(self.existingList.permalink,
self.authToken)
assert res, 'null getWordListByPermalink result'
def testGetWordListByPermalink(self):
res = self.wordListApi.getWordListByPermalink(self.existingList.permalink,
self.authToken)
assert res, 'null getWordListByPermalink result'
def testUpdateWordList(self):
import time
description = 'list updated at ' + str(time.time())
self.existingList.description = description
self.wordListApi.updateWordList(self.existingList.permalink,
self.authToken, body=self.existingList)
res = self.wordListApi.getWordListByPermalink(self.existingList.permalink,
self.authToken)
assert res.description == description, 'did not update wordlist'
def testAddWordsToWordList(self):
from wordnik.models import StringValue
wordsToAdd = []
word1 = StringValue.StringValue()
word1.word = "delicious"
wordsToAdd.append(word1)
word2 = StringValue.StringValue()
word2.word = "tasty"
wordsToAdd.append(word2)
word3 = StringValue.StringValue()
word3.word = "scrumptious"
wordsToAdd.append(word3)
self.wordListApi.addWordsToWordList(self.existingList.permalink,
self.authToken, body=wordsToAdd)
res = self.wordListApi.getWordListWords(self.existingList.permalink,
self.authToken)
listSet = set([word.word for word in res])
addedSet = set(["delicious", "tasty", "scrumptious"])
assert len(listSet.intersection(addedSet)) == 3, 'did not get added words'
def testDeleteWordsFromList(self):
from wordnik.models import StringValue
wordsToRemove = []
word1 = StringValue.StringValue()
word1.word = "delicious"
wordsToRemove.append(word1)
word2 = StringValue.StringValue()
word2.word = "tasty"
wordsToRemove.append(word2)
word3 = StringValue.StringValue()
word3.word = "scrumptious"
wordsToRemove.append(word3)
self.wordListApi.deleteWordsFromWordList(self.existingList.permalink,
self.authToken,
body=wordsToRemove)
res = self.wordListApi.getWordListWords(self.existingList.permalink,
self.authToken)
listSet = set([word.word for word in res])
addedSet = set(["delicious", "tasty", "scrumptious"])
assert len(listSet.intersection(addedSet)) == 0, 'did not get removed words'
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,47 @@
#!/usr/bin/env python
import sys
import unittest
import urllib.request, urllib.error, urllib.parse
import json
from BaseApiTest import BaseApiTest
sys.path = ['./'] + sys.path
from wordnik import *
class WordsApiTest(BaseApiTest):
def testSearchWords(self):
res = self.wordsApi.searchWords('tree')
assert res, 'null search result'
assert res.searchResults[0].word == 'tree', 'word should be "tree"'
assert res.totalResults != 0, 'should not have 0 results'
def testGetWordOfTheDay(self):
res = self.wordsApi.getWordOfTheDay()
assert res, 'null wordOfTheDay result'
def testReverseDictionary(self):
res = self.wordsApi.reverseDictionary("hairy")
assert res, 'null reverseDictionary result'
assert res.totalResults != 0, 'should not have 0 results'
assert len(res.results) != 0, 'should not have 0 results'
def testGetRandomWords(self):
res = self.wordsApi.getRandomWords()
assert res, 'null getRandomWords result'
assert len(res) == 10, 'should get 10 random words'
def testGetRandomWords(self):
res = self.wordsApi.getRandomWords()
assert res, 'null getRandomWord result'
def testGetRandomWord(self):
res = self.wordsApi.getRandomWords()
assert res, 'null getRandomWord result'
if __name__ == "__main__":
unittest.main()