Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
feat(base): support loading external modules from py and yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhxiao committed Jul 19, 2019
1 parent 66f26e0 commit 85333e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
35 changes: 29 additions & 6 deletions tests/test_contrib_module.py
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
21 changes: 0 additions & 21 deletions tests/test_contrib_module_negative.py

This file was deleted.

0 comments on commit 85333e0

Please sign in to comment.