Commits

Takeshi Komiya committed 5dda700

schema2rst command supports --datafile option

  • Participants
  • Parent commits a042964

Comments (0)

Files changed (6)

 # -*- coding: utf-8 -*-
+import sys
 from setuptools import setup, find_packages
 
 version = '0.1.0'
     "Topic :: Software Development :: Documentation",
 ]
 
+tests_requires = [
+    'nose',
+    'mock',
+    'pep8>=1.3',
+    'testing.mysqld',
+    'testing.postgresql',
+]
+
+if sys.version_info < (2, 7):
+    tests_requires.append('unittest2')
+
 setup(
     name='schema2rst',
     version=version,
         'six',
     ],
     extras_require=dict(
-        test=[
-            'Nose',
-            'pep8>=1.3',
-            'testing.mysqld',
-            'testing.postgresql',
-        ],
+        test=tests_requires,
     ),
     test_suite='nose.collector',
-    tests_require=[
-        'Nose',
-        'pep8>=1.3',
-        'testing.mysqld',
-        'testing.postgresql',
-    ],
+    tests_require=tests_requires,
     entry_points="""
        [console_scripts]
        schemadump = schema2rst.commands.dump:main

src/schema2rst/commands/graph.py

     usage = 'Usage: schema2graph CONFIG_FILE'
     parser = optparse.OptionParser(usage=usage)
     parser.add_option('-c', '--config', action='store')
+    parser.add_option('-d', '--datafile', action='store')
     parser.add_option('-o', '--output', action='store')
 
     options, args = parser.parse_args(args)
-    if options.config is None:
-        parser.error('--config (-c) is required')
+    if options.config is None and options.datafile is None:
+        parser.error('--config (-c) or --datafile (-d) is required')
+
+    if options.config and options.datafile:
+        parser.error('Specify either --config (-c) or --datafile (-d)')
 
     return options, args
 
 def main(args=sys.argv[1:]):
     options, args = parse_option(args)
 
-    config = yaml.load(io.open(options.config, encoding='utf-8'))
-    engine = inspectors.create_engine(config)
-    try:
-        schema = inspectors.create_for(engine).dump()
+    if options.datafile:
+        schema = yaml.safe_load(open(options.datafile))
+    else:
+        try:
+            config = yaml.load(io.open(options.config, encoding='utf-8'))
+            engine = inspectors.create_engine(config)
+            schema = inspectors.create_for(engine).dump()
+        finally:
+            engine.dispose()
 
-        doc = RestructuredTextWriter(options.output)
-        generate_doc(doc, schema)
-    finally:
-        engine.dispose()
+    doc = RestructuredTextWriter(options.output)
+    generate_doc(doc, schema)
 
 
 def generate_doc(doc, schema):

src/schema2rst/commands/rst.py

     usage = 'Usage: schema2rst CONFIG_FILE'
     parser = optparse.OptionParser(usage=usage)
     parser.add_option('-c', '--config', action='store')
+    parser.add_option('-d', '--datafile', action='store')
     parser.add_option('-o', '--output', action='store')
 
     options, args = parser.parse_args(args)
-    if options.config is None:
-        parser.error('--config (-c) is required')
+    if options.config is None and options.datafile is None:
+        parser.error('--config (-c) or --datafile (-d) is required')
+
+    if options.config and options.datafile:
+        parser.error('Specify either --config (-c) or --datafile (-d)')
 
     return options, args
 
 def main(args=sys.argv[1:]):
     options, args = parse_option(args)
 
-    config = yaml.load(io.open(args[0], encoding='utf-8'))
-    engine = inspectors.create_engine(config)
-    try:
-        schema = inspectors.create_for(engine).dump()
+    if options.datafile:
+        schema = yaml.safe_load(open(options.datafile))
+    else:
+        try:
+            config = yaml.load(io.open(options.config, encoding='utf-8'))
+            engine = inspectors.create_engine(config)
+            schema = inspectors.create_for(engine).dump()
+        finally:
+            engine.dispose()
 
-        doc = RestructuredTextWriter(options.output)
-        generate_doc(doc, schema)
-    finally:
-        engine.dispose()
+    doc = RestructuredTextWriter(options.output)
+    generate_doc(doc, schema)
 
 
 def generate_doc(doc, schema):

src/schema2rst/tests/test_schema2graph.py

+# -*- coding: utf-8 -*-
+
+import io
+import os
+import tempfile
+from mock import patch
+
+from schema2rst.commands import graph
+
+import sys
+if sys.version_info < (2, 7):
+    import unittest2 as unittest
+else:
+    import unittest
+
+
+class TestSchema2graph(unittest.TestCase):
+    def readfile(self, filename):
+        path = os.path.join(os.path.dirname(__file__), filename)
+        return io.open(path, encoding='utf-8').read()
+
+    @patch("optparse.OptionParser.error")
+    def test_parse_option(self, error):
+        error.side_effect = RuntimeError  # do not output error to stderr
+
+        # no arguments
+        with self.assertRaises(RuntimeError):
+            graph.parse_option([])
+
+        # no -c/-o options
+        with self.assertRaises(RuntimeError):
+            graph.parse_option(['-o', 'output.rst'])
+
+        # both -c and -o
+        with self.assertRaises(RuntimeError):
+            graph.parse_option(['-c', 'config.yaml', '-d', 'dump.yaml',
+                                '-o', 'output.rst'])
+
+        # success (1)
+        option, args = graph.parse_option(['-c', 'config.yaml',
+                                           '-o', 'output.rst'])
+        self.assertEqual('config.yaml', option.config)
+        self.assertEqual(None, option.datafile)
+        self.assertEqual('output.rst', option.output)
+        self.assertEqual([], args)
+
+        # success (2)
+        option, args = graph.parse_option(['-d', 'dump.yaml',
+                                           '-o', 'output.rst'])
+        self.assertEqual(None, option.config)
+        self.assertEqual('dump.yaml', option.datafile)
+        self.assertEqual('output.rst', option.output)
+        self.assertEqual([], args)
+
+    def test_from_datafile_basic(self):
+        try:
+            fd, output = tempfile.mkstemp()
+            os.close(fd)
+
+            datafile = os.path.join(os.path.dirname(__file__),
+                                    'yaml/mysql_basic.yaml')
+            graph.main(['-d', datafile, '-o', output])
+            self.assertEqual(self.readfile('rst/mysql_basic_graph.rst'),
+                             io.open(output, encoding='utf-8').read())
+        finally:
+            os.unlink(output)
+
+    def test_from_datafile_comment(self):
+        try:
+            fd, output = tempfile.mkstemp()
+            os.close(fd)
+
+            datafile = os.path.join(os.path.dirname(__file__),
+                                    'yaml/mysql_comments.yaml')
+            graph.main(['-d', datafile, '-o', output])
+            self.assertEqual(self.readfile('rst/mysql_comments_graph.rst'),
+                             io.open(output, encoding='utf-8').read())
+        finally:
+            os.unlink(output)

src/schema2rst/tests/test_schema2rst.py

+# -*- coding: utf-8 -*-
+
+import io
+import os
+import tempfile
+from mock import patch
+
+from schema2rst.commands import rst
+
+import sys
+if sys.version_info < (2, 7):
+    import unittest2 as unittest
+else:
+    import unittest
+
+
+class TestSchema2rst(unittest.TestCase):
+    def readfile(self, filename):
+        path = os.path.join(os.path.dirname(__file__), filename)
+        return io.open(path, encoding='utf-8').read()
+
+    @patch("optparse.OptionParser.error")
+    def test_parse_option(self, error):
+        if sys.version_info < (2, 7):  # skip tests in py2.6
+            return
+
+        error.side_effect = RuntimeError  # do not output error to stderr
+
+        # no arguments
+        with self.assertRaises(RuntimeError):
+            rst.parse_option([])
+
+        # no -c/-o options
+        with self.assertRaises(RuntimeError):
+            rst.parse_option(['-o', 'output.rst'])
+
+        # both -c and -o
+        with self.assertRaises(RuntimeError):
+            rst.parse_option(['-c', 'config.yaml', '-d', 'dump.yaml',
+                              '-o', 'output.rst'])
+
+        # success (1)
+        option, args = rst.parse_option(['-c', 'config.yaml',
+                                         '-o', 'output.rst'])
+        self.assertEqual('config.yaml', option.config)
+        self.assertEqual(None, option.datafile)
+        self.assertEqual('output.rst', option.output)
+        self.assertEqual([], args)
+
+        # success (2)
+        option, args = rst.parse_option(['-d', 'dump.yaml',
+                                         '-o', 'output.rst'])
+        self.assertEqual(None, option.config)
+        self.assertEqual('dump.yaml', option.datafile)
+        self.assertEqual('output.rst', option.output)
+        self.assertEqual([], args)
+
+    def test_from_datafile_basic(self):
+        try:
+            fd, output = tempfile.mkstemp()
+            os.close(fd)
+
+            datafile = os.path.join(os.path.dirname(__file__),
+                                    'yaml/mysql_basic.yaml')
+            rst.main(['-d', datafile, '-o', output])
+            self.assertEqual(self.readfile('rst/mysql_basic.rst'),
+                             io.open(output, encoding='utf-8').read())
+        finally:
+            os.unlink(output)
+
+    def test_from_datafile_comment(self):
+        try:
+            fd, output = tempfile.mkstemp()
+            os.close(fd)
+
+            datafile = os.path.join(os.path.dirname(__file__),
+                                    'yaml/mysql_comments.yaml')
+            rst.main(['-d', datafile, '-o', output])
+            self.assertEqual(self.readfile('rst/mysql_comments.rst'),
+                             io.open(output, encoding='utf-8').read())
+        finally:
+            os.unlink(output)
 [testenv]
 deps=
     nose
+    mock
     flake8
     testing.mysqld
     testing.postgresql
 commands=
     nosetests
     flake8 src
+
+[testenv:py26]
+deps=
+    nose
+    mock
+    flake8
+    unittest2
+    testing.mysqld
+    testing.postgresql