-
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.
Merge pull request #24 from edx/bseverino/service
[MST-916]: Add service wrapper
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
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
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
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,40 @@ | ||
""" | ||
Name Affirmation Service | ||
""" | ||
|
||
import types | ||
|
||
|
||
class NameAffirmationService: | ||
""" | ||
Service to expose Name Affirmation API methods | ||
""" | ||
|
||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
""" | ||
This is a class factory to make sure this is a Singleton. | ||
""" | ||
if not cls._instance: | ||
cls._instance = super(NameAffirmationService, cls).__new__(cls, *args, **kwargs) | ||
return cls._instance | ||
|
||
def __init__(self): | ||
""" | ||
Class initializer, which just inspects the libraries and exposes the same functions | ||
as a direct pass through. | ||
""" | ||
# pylint: disable=import-outside-toplevel | ||
from edx_name_affirmation import api as edx_name_affirmation_api | ||
self._bind_to_module_functions(edx_name_affirmation_api) | ||
|
||
def _bind_to_module_functions(self, module): | ||
""" | ||
Bind module functions. | ||
""" | ||
for attr_name in dir(module): | ||
attr = getattr(module, attr_name, None) | ||
if isinstance(attr, types.FunctionType): | ||
if not hasattr(self, attr_name): | ||
setattr(self, attr_name, attr) |
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 @@ | ||
""" | ||
Tests for NameAffirmation services | ||
""" | ||
|
||
import types | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase | ||
|
||
from edx_name_affirmation import api as edx_name_affirmation_api | ||
from edx_name_affirmation.services import NameAffirmationService | ||
|
||
User = get_user_model() | ||
|
||
|
||
class NameAffirmationServiceTest(TestCase): | ||
""" | ||
Tests for NameAffirmationService | ||
""" | ||
def test_basic(self): | ||
""" | ||
See if the NameAffirmationService exposes the expected methods | ||
""" | ||
|
||
service = NameAffirmationService() | ||
|
||
for attr_name in dir(edx_name_affirmation_api): | ||
attr = getattr(edx_name_affirmation_api, attr_name, None) | ||
if isinstance(attr, types.FunctionType): | ||
self.assertTrue(hasattr(service, attr_name)) | ||
|
||
def test_singleton(self): | ||
""" | ||
Test to make sure the NameAffirmationService is a singleton. | ||
""" | ||
service1 = NameAffirmationService() | ||
service2 = NameAffirmationService() | ||
self.assertIs(service1, service2) |