-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import requests | ||
|
||
|
||
class LaaLaaError(Exception): | ||
pass | ||
|
||
|
||
class LaalaaProviderCategoriesApiClient: | ||
instance = None | ||
def __init__(self, laalaa_base_url, category_translator): | ||
self.categories = {} | ||
self.category_translator = category_translator | ||
self.laalaa_base_url = laalaa_base_url | ||
|
||
def get_categories(self): | ||
return self.categories or self._load_categories() | ||
|
||
def _load_categories(self): | ||
categories = self._fetch_categories() | ||
# Translate categories and force keys to lower case | ||
for category in categories: | ||
code = category["code"].lower() | ||
self.categories[code] = self.category_translator(category["name"]) | ||
return self.categories | ||
|
||
def _fetch_categories(self): | ||
url = "{host}/categories".format(host=self.laalaa_base_url) | ||
try: | ||
response = requests.get(url) | ||
return response.json() | ||
except (requests.exceptions.RequestException, ValueError) as e: | ||
raise LaaLaaError(e) | ||
|
||
@classmethod | ||
def singleton(cls, *args, **kwargs): | ||
if not cls.instance: | ||
cls.instance = LaalaaProviderCategoriesApiClient(*args, **kwargs) | ||
return cls.instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
import mock | ||
|
||
from ..laalaa import LaalaaProviderCategoriesApiClient | ||
|
||
|
||
def _laalaa_categories_endpoint_response(): | ||
return [ | ||
{"code": "disc", "civil": True, "name": "Discrimination"}, | ||
{"code": "edu", "civil": True, "name": "Education"} | ||
] | ||
|
||
|
||
class LaaLaaProvidersCategoryApiClientTestCase(unittest.TestCase): | ||
def test_categories(self): | ||
api = LaalaaProviderCategoriesApiClient("", lambda s: s) | ||
with mock.patch.object(api, '_fetch_categories') as mock_method: | ||
mock_method.return_value = _laalaa_categories_endpoint_response() | ||
categories = api.get_categories() | ||
expected_categories = {category["code"]: category["name"] for category in mock_method.return_value} | ||
self.assertDictEqual(categories, expected_categories) | ||
|
||
def test_fetch_call_count(self): | ||
api = LaalaaProviderCategoriesApiClient("", lambda s: s) | ||
with mock.patch.object(api, '_fetch_categories') as mock_method: | ||
mock_method.return_value = _laalaa_categories_endpoint_response() | ||
api.get_categories() | ||
api.get_categories() | ||
api.get_categories() | ||
api.get_categories() | ||
self.assertEqual(mock_method.call_count, 1) | ||
|
||
def test_category_translation(self): | ||
translations = {"Discrimination": "foo", "Education": "bar"} | ||
|
||
def translator(category): | ||
return translations.get(category) | ||
|
||
api = LaalaaProviderCategoriesApiClient("", translator) | ||
with mock.patch.object(api, '_fetch_categories') as mock_method: | ||
mock_method.return_value = _laalaa_categories_endpoint_response() | ||
categories = api.get_categories() | ||
|
||
expected_categories = {"disc": "foo", "edu": "bar"} | ||
self.assertDictEqual(categories, expected_categories) | ||
|
||
def test_singleton(self): | ||
instance1 = LaalaaProviderCategoriesApiClient.singleton("", lambda s: s) | ||
instance1.foo = "bar" | ||
instance2 = LaalaaProviderCategoriesApiClient.singleton("", lambda s: s) | ||
self.assertEqual(instance2.foo, "bar") |