This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(base): support loading external modules from py and yaml
- Loading branch information
hanhxiao
committed
Jul 19, 2019
1 parent
66f26e0
commit 85333e0
Showing
2 changed files
with
29 additions
and
27 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 |
---|---|---|
@@ -1,19 +1,42 @@ | ||
import os | ||
import unittest | ||
import sys | ||
import unittest.mock | ||
|
||
import ruamel.yaml | ||
|
||
dirname = os.path.dirname(__file__) | ||
module_path = os.path.join(dirname, 'contrib', 'dummy_contrib.py') | ||
cls_name = 'FooContribEncoder' | ||
|
||
class TestYaml(unittest.TestCase): | ||
|
||
class TestYaml(unittest.TestCase): | ||
def setUp(self): | ||
dirname = os.path.dirname(__file__) | ||
module_path = os.path.join(dirname, 'contrib', 'dummy_contrib.py') | ||
cls_name = 'FooContribEncoder' | ||
os.environ['GNES_CONTRIB_MODULE'] = '%s:%s' % (cls_name, module_path) | ||
self.yaml_path = os.path.join(os.path.dirname(__file__), | ||
'contrib', 'dummy.yml') | ||
|
||
# reload gnes module on every unit test | ||
for mod in list(sys.modules.keys()): | ||
if mod.startswith('gnes.'): | ||
del (sys.modules[mod]) | ||
|
||
@unittest.mock.patch.dict(os.environ, {'GNES_CONTRIB_MODULE': '%s:%s' % (cls_name, module_path)}) | ||
def test_load_contrib(self): | ||
|
||
from gnes.encoder.base import BaseEncoder, BaseTextEncoder | ||
a = BaseEncoder.load_yaml(self.yaml_path) | ||
self.assertIsInstance(a, BaseTextEncoder) | ||
self.assertEqual(a.encode([]), 'hello 531') | ||
|
||
@unittest.mock.patch.dict(os.environ, {'GNES_CONTRIB_MODULE': '%s:%s' % ('blah', module_path)}) | ||
def test_bad_name(self): | ||
try: | ||
from gnes.encoder.base import BaseEncoder | ||
except AttributeError: | ||
pass | ||
|
||
@unittest.mock.patch.dict(os.environ, {'GNES_CONTRIB_MODULE': '%s:%s' % (cls_name, 'blah')}) | ||
def test_bad_path(self): | ||
try: | ||
from gnes.encoder.base import BaseEncoder | ||
except AttributeError: | ||
pass |
This file was deleted.
Oops, something went wrong.