diff options
| -rw-r--r-- | lesana/collection.py | 2 | ||||
| -rw-r--r-- | lesana/templating.py | 2 | ||||
| -rw-r--r-- | tests/data/simple/templates/from_self.yaml | 3 | ||||
| -rw-r--r-- | tests/test_commands.py | 119 | 
4 files changed, 122 insertions, 4 deletions
diff --git a/lesana/collection.py b/lesana/collection.py index 87f767f..cbef6ee 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -482,7 +482,7 @@ class Collection(object):          try:              template = env.get_template(template_fname)          except jinja2.exceptions.TemplateNotFound as e: -            raise TemplatingError('Could not find template' + str(e)) +            raise TemplatingError('Could not find template ' + str(e))          return template      @classmethod diff --git a/lesana/templating.py b/lesana/templating.py index ff21d14..845904b 100644 --- a/lesana/templating.py +++ b/lesana/templating.py @@ -41,5 +41,5 @@ def to_yaml(data):      s_io = io.StringIO()      yaml.dump({'data': data}, s_io)      res = s_io.getvalue() -    res = res.lstrip('{data:').lstrip().strip() +    res = res.lstrip('{data:').lstrip().strip('...\n').strip()      return res diff --git a/tests/data/simple/templates/from_self.yaml b/tests/data/simple/templates/from_self.yaml new file mode 100644 index 0000000..f4c535f --- /dev/null +++ b/tests/data/simple/templates/from_self.yaml @@ -0,0 +1,3 @@ +name: {{ name | to_yaml }} +description: {{ description | to_yaml }} +position: {{ position | to_yaml }} diff --git a/tests/test_commands.py b/tests/test_commands.py index c948d69..92b603f 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,9 +1,11 @@  import contextlib  import io +import os  import tempfile  import unittest  from lesana import command +from . import utils  class Args: @@ -22,7 +24,12 @@ class Args:  class testCommands(unittest.TestCase):      def setUp(self): -        self.collection_dir = tempfile.TemporaryDirectory() +        self.tmpdir = tempfile.TemporaryDirectory() +        utils.copytree( +            'tests/data/simple', +            self.tmpdir.name, +            dirs_exist_ok=True, +        )      def tearDown(self):          pass @@ -44,13 +51,121 @@ class testCommands(unittest.TestCase):      def test_init(self):          args = { -            'collection': self.collection_dir.name, +            'collection': self.tmpdir.name,              'git': True,          }          streams = self._run_command(command.Init(), args)          self.assertEqual(streams['stdout'].getvalue(), '')          self.assertEqual(streams['stderr'].getvalue(), '') +    def test_new(self): +        args = { +            'collection': self.tmpdir.name, +            'git': True, +        } +        streams = self._run_command(command.New(), args) +        self.assertEqual(len(streams['stdout'].getvalue()), 33) +        self.assertEqual(streams['stderr'].getvalue(), '') + +    def test_edit(self): +        args = { +            'collection': self.tmpdir.name, +            'git': True, +            'eid': '11189ee4', +        } +        streams = self._run_command(command.Edit(), args) +        self.assertTrue(args['eid'] in streams['stdout'].getvalue()) +        self.assertEqual(streams['stderr'].getvalue(), '') + +    def test_show(self): +        args = { +            'collection': self.tmpdir.name, +            'git': True, +            'eid': '11189ee4', +            'template': False, +        } +        streams = self._run_command(command.Show(), args) +        self.assertTrue( +            'name: Another item' in streams['stdout'].getvalue() +        ) +        self.assertEqual(streams['stderr'].getvalue(), '') + +    def test_index(self): +        args = { +            'collection': self.tmpdir.name, +            'git': True, +            'files': None, +            'reset': True, +        } +        streams = self._run_command(command.Index(), args) +        self.assertEqual( +            streams['stdout'].getvalue(), +            'Found and indexed 3 entries\n', +        ) +        self.assertEqual(streams['stderr'].getvalue(), '') + +    def test_search(self): +        args = { +            'collection': self.tmpdir.name, +            'git': True, +            'template': False, +            'query': 'Another', +            'offset': None, +            'pagesize': None, +            'sort': None, +            'all': False, +        } +        streams = self._run_command(command.Search(), args) +        self.assertTrue( +            '11189ee4' in streams['stdout'].getvalue() +        ) +        self.assertEqual(streams['stderr'].getvalue(), '') + +    def test_export(self): +        dest_tmpdir = tempfile.TemporaryDirectory() +        utils.copytree( +            'tests/data/simple', +            dest_tmpdir.name, +            dirs_exist_ok=True, +        ) +        # TODO: make finding the templates less prone to breaking and +        # then remove the cwd change from here +        old_cwd = os.getcwd() +        os.chdir(self.tmpdir.name) +        args = { +            'collection': self.tmpdir.name, +            'git': True, +            'template': 'templates/from_self.yaml', +            'query': 'Another', +            'destination': dest_tmpdir.name, +        } +        streams = self._run_command(command.Export(), args) +        os.chdir(old_cwd) +        self.assertEqual(streams['stdout'].getvalue(), '') +        self.assertEqual(streams['stderr'].getvalue(), '') + +    def test_remove(self): +        args = { +            'collection': self.tmpdir.name, +            'git': True, +            'entries': '11189ee4', +        } +        streams = self._run_command(command.Remove(), args) +        self.assertEqual(streams['stdout'].getvalue(), '') +        self.assertEqual(streams['stderr'].getvalue(), '') + +    def test_update(self): +        args = { +            'collection': self.tmpdir.name, +            'git': True, +            'query': 'Another', +            'field': 'position', +            'value': 'here', +        } +        streams = self._run_command(command.Update(), args) +        self.assertEqual(streams['stdout'].getvalue(), '') +        self.assertEqual(streams['stderr'].getvalue(), '') +  if __name__ == '__main__':      unittest.main()  | 
