Cleaner api handler interface + extended tests

This commit is contained in:
Madison May
2015-02-27 16:33:22 -05:00
parent 57f91a138b
commit 110abaf7a6
16 changed files with 258 additions and 93 deletions
+132 -9
View File
@@ -1,15 +1,138 @@
import os
import unittest
import textwrap
from StringIO import StringIO
from indicoio import config
from indicoio.config import Settings
def test_batch_set_url_root_as_env_var():
test_data = ['clearly an english sentence']
old_private_cloud_url = os.environ.get("INDICO_PRIVATE_CLOUD_URL")
os.environ["INDICO_PRIVATE_CLOUD_URL"] = "http://not.a.real.url/"
assert config.get_api_root() == "http://not.a.real.url/"
class TestConfigureEnv(unittest.TestCase):
if old_private_cloud_url:
os.environ["INDICO_PRIVATE_CLOUD_URL"] = old_private_cloud_url
else:
del(os.environ["INDICO_PRIVATE_CLOUD_URL"])
def setUp(self):
os.environ = {}
def test_set_cloud_from_env_var(self):
cloud = "invalid/cloud"
os.environ["INDICO_CLOUD"] = cloud
assert config.settings.cloud() == cloud
def test_set_auth_from_env_var(self):
username = "test"
password = "password"
os.environ["INDICO_USERNAME"] = username
os.environ["INDICO_PASSWORD"] = password
assert config.settings.auth() == (username, password)
class TestConfigurationFile(unittest.TestCase):
def setUp(self):
self.username = "test"
self.password = "password"
self.cloud = "localhost"
config = """
[auth]
username = %s
password = %s
[private_cloud]
cloud = %s
""" % (self.username, self.password, self.cloud)
config_file = StringIO(textwrap.dedent(config))
self.settings = Settings(files=[config_file])
os.environ = {}
def test_set_cloud_from_config_file(self):
assert self.settings.cloud() == self.cloud
def test_set_auth_from_config_file(self):
assert self.settings.auth() == (self.username, self.password)
class TestPrecedence(unittest.TestCase):
def setUp(self):
self.file_username = "file-username"
self.file_password = "file-password"
self.file_cloud = "file-cloud"
self.env_username = "env-username"
self.env_password = "env-password"
self.env_cloud = "env-cloud"
config = """
[auth]
username = %s
password = %s
[private_cloud]
cloud = %s
""" % (self.file_username, self.file_password, self.file_cloud)
config_file = StringIO(textwrap.dedent(config))
os.environ = {
'INDICO_CLOUD': self.env_cloud,
'INDICO_USERNAME': self.env_username,
'INDICO_PASSWORD': self.env_password
}
self.settings = Settings(files=[config_file])
def test_set_cloud_from_config_file(self):
assert self.settings.cloud() == self.env_cloud
def test_set_auth_from_config_file(self):
assert self.settings.auth() == (self.env_username, self.env_password)
class TestConfigFilePrecedence(unittest.TestCase):
def setUp(self):
self.high_priority_username = "high-priority-username"
self.high_priority_password = "high-priority-password"
self.high_priority_cloud = "high-priority-cloud"
self.low_priority_username = "low-priority-username"
self.low_priority_password = "low-priority-password"
self.low_priority_cloud = "low-priority-cloud"
high_priority_config = """
[auth]
username = %s
password = %s
[private_cloud]
cloud = %s
""" % (
self.high_priority_username,
self.high_priority_password,
self.high_priority_cloud
)
low_priority_config = """
[auth]
username = %s
password = %s
[private_cloud]
cloud = %s
""" % (
self.low_priority_username,
self.low_priority_password,
self.low_priority_cloud
)
high_priority_config_file = StringIO(textwrap.dedent(high_priority_config))
low_priority_config_file = StringIO(textwrap.dedent(low_priority_config))
os.environ = {}
self.settings = Settings(files=[
low_priority_config_file,
high_priority_config_file
])
def test_cloud_config_file_priority(self):
assert self.settings.cloud() == self.high_priority_cloud
def test_auth_config_file_priority(self):
assert self.settings.auth() == (self.high_priority_username, self.high_priority_password)
+9 -10
View File
@@ -6,6 +6,7 @@ import numpy as np
import skimage.io
from nose.plugins.skip import Skip, SkipTest
from indicoio import config
from indicoio import political, sentiment, fer, facial_features, language, image_features, text_tags
from indicoio import batch_political, batch_sentiment, batch_fer, batch_facial_features
from indicoio import batch_language, batch_image_features, batch_text_tags
@@ -15,11 +16,9 @@ DIR = os.path.dirname(os.path.realpath(__file__))
class BatchAPIRun(unittest.TestCase):
def setUp(self):
self.username = os.getenv("INDICO_USERNAME")
self.password = os.getenv("INDICO_PASSWORD")
self.auth = (self.username, self.password)
self.auth = config.auth
if not self.username or not self.password:
if not all(self.auth):
raise SkipTest
def test_batch_texttags(self):
@@ -71,13 +70,13 @@ class BatchAPIRun(unittest.TestCase):
self.assertTrue(isinstance(response, list))
self.assertTrue(response[0]['English'] > 0.25)
def test_batch_set_url_root(self):
def test_batch_set_cloud(self):
test_data = ['clearly an english sentence']
self.assertRaises(ConnectionError,
batch_language,
test_data,
auth=self.auth,
url_root='http://not.a.real.url/')
cloud='invalid/cloud')
class FullAPIRun(unittest.TestCase):
@@ -139,13 +138,13 @@ class FullAPIRun(unittest.TestCase):
self.assertEqual(fer_set, set(response.keys()))
def test_happy_fer(self):
test_face = self.load_image("../data/happy.png", as_grey=True)
test_face = self.load_image("data/happy.png", as_grey=True)
response = fer(test_face)
self.assertTrue(isinstance(response, dict))
self.assertTrue(response['Happy'] > 0.5)
def test_fear_fer(self):
test_face = self.load_image("../data/fear.png", as_grey=True)
test_face = self.load_image("data/fear.png", as_grey=True)
response = fer(test_face)
self.assertTrue(isinstance(response, dict))
self.assertTrue(response['Fear'] > 0.25)
@@ -223,12 +222,12 @@ class FullAPIRun(unittest.TestCase):
self.assertEqual(language_set, set(language_dict.keys()))
assert language_dict['English'] > 0.25
def test_set_url_root(self):
def test_set_cloud(self):
test_data = 'clearly an english sentence'
self.assertRaises(ConnectionError,
language,
test_data,
url_root='http://not.a.real.url/')
cloud='invalid/cloud')
if __name__ == "__main__":