コード例 #1
0
ファイル: test_python_client.py プロジェクト: yanks/stone
 def test_route_argument_doc_string(self):
     backend = PythonClientBackend(
         target_folder_path='output',
         args=['-m', 'files', '-c', 'DropboxBase', '-t', 'dropbox'])
     ns = ApiNamespace('files')
     self.assertEqual(backend._format_type_in_doc(ns, Int32()), 'int')
     self.assertEqual(backend._format_type_in_doc(ns, Void()), 'None')
     self.assertEqual(backend._format_type_in_doc(ns, List(String())),
                      'List[str]')
     self.assertEqual(backend._format_type_in_doc(ns, Nullable(String())),
                      'Nullable[str]')
     self.assertEqual(
         backend._format_type_in_doc(ns, Map(String(), Int32())),
         'Map[str, int]')
コード例 #2
0
def _make_namespace_with_many_structs():
    # type: (...) -> ApiNamespace
    ns = ApiNamespace('ns_with_many_structs')

    struct1 = Struct(name='Struct1', namespace=ns, ast_node=None)
    struct1.set_attributes(None, [StructField('f1', Boolean(), None, None)])
    ns.add_data_type(struct1)

    struct2 = Struct(name='Struct2', namespace=ns, ast_node=None)
    struct2.set_attributes(
        doc=None,
        fields=[
            StructField('f2', List(UInt64()), None, None),
            StructField('f3', Timestamp(ISO_8601_FORMAT), None, None),
            StructField('f4', Map(String(), UInt64()), None, None)
        ]
    )
    ns.add_data_type(struct2)

    return ns
コード例 #3
0
    def test_check_example(self):

        #
        # Test string
        #

        s = String(min_length=1, max_length=5)
        s.check_example(
            AstExampleField(
                path='test.stone',
                lineno=1,
                lexpos=0,
                name='v',
                value='hello',
            ))

        with self.assertRaises(InvalidSpec) as cm:
            s.check_example(
                AstExampleField(
                    path='test.stone',
                    lineno=1,
                    lexpos=0,
                    name='v',
                    value='',
                ))
        self.assertIn("'' has fewer than 1 character(s)", cm.exception.msg)

        #
        # Test list
        #

        l1 = List(String(min_length=1), min_items=1, max_items=3)

        l1.check_example(
            AstExampleField(
                path='test.stone',
                lineno=1,
                lexpos=0,
                name='v',
                value=['asd'],
            ))

        with self.assertRaises(InvalidSpec) as cm:
            l1.check_example(
                AstExampleField(
                    path='test.stone',
                    lineno=1,
                    lexpos=0,
                    name='v',
                    value=[],
                ))
        self.assertIn("has fewer than 1 item(s)", cm.exception.msg)

        #
        # Test list of lists
        #

        l1 = List(List(String(min_length=1), min_items=1))

        l1.check_example(
            AstExampleField(
                path='test.stone',
                lineno=1,
                lexpos=0,
                name='v',
                value=[['asd']],
            ))

        with self.assertRaises(InvalidSpec) as cm:
            l1.check_example(
                AstExampleField(
                    path='test.stone',
                    lineno=1,
                    lexpos=0,
                    name='v',
                    value=[[]],
                ))
        self.assertIn("has fewer than 1 item(s)", cm.exception.msg)

        #
        # Test Map type
        #

        m = Map(String(), String())
        # valid example
        m.check_example(
            AstExampleField(path='test.stone',
                            lineno=1,
                            lexpos=0,
                            name='v',
                            value={"foo": "bar"}))

        # does not conform to declared type
        with self.assertRaises(InvalidSpec):
            m.check_example(
                AstExampleField(path='test.stone',
                                lineno=1,
                                lexpos=0,
                                name='v',
                                value={1: "bar"}))

        with self.assertRaises(ParameterError):
            # errors because only string types can be used as keys
            Map(Int32(), String())

        s = Struct('S', None, None)
        s.set_attributes(
            "Docstring",
            [
                StructField('a', UInt64(), 'a field', None),
                StructField('b', List(String()), 'a field', None),
            ],
        )

        s._add_example(
            AstExample('test.stone',
                       lineno=1,
                       lexpos=0,
                       label='default',
                       text='Default example',
                       fields={
                           'a':
                           AstExampleField(
                               path='test.stone',
                               lineno=2,
                               lexpos=0,
                               name='a',
                               value=132,
                           ),
                           'b':
                           AstExampleField(
                               path='test.stone',
                               lineno=2,
                               lexpos=0,
                               name='b',
                               value=['a'],
                           ),
                       }))
コード例 #4
0
    def test_check_example(self):

        #
        # Test string
        #

        s = String(min_length=1, max_length=5)
        s.check_example(
            AstExampleField(
                path='test.stone',
                lineno=1,
                lexpos=0,
                name='v',
                value='hello',
            ))

        with self.assertRaises(InvalidSpec) as cm:
            s.check_example(
                AstExampleField(
                    path='test.stone',
                    lineno=1,
                    lexpos=0,
                    name='v',
                    value='',
                ))
        self.assertIn("'' has fewer than 1 character(s)", cm.exception.msg)

        #
        # Test list
        #

        l = List(String(min_length=1), min_items=1, max_items=3)

        l.check_example(
            AstExampleField(
                path='test.stone',
                lineno=1,
                lexpos=0,
                name='v',
                value=['asd'],
            ))

        with self.assertRaises(InvalidSpec) as cm:
            l.check_example(
                AstExampleField(
                    path='test.stone',
                    lineno=1,
                    lexpos=0,
                    name='v',
                    value=[],
                ))
        self.assertIn("has fewer than 1 item(s)", cm.exception.msg)

        #
        # Test list of lists
        #

        l = List(List(String(min_length=1), min_items=1))

        l.check_example(
            AstExampleField(
                path='test.stone',
                lineno=1,
                lexpos=0,
                name='v',
                value=[['asd']],
            ))

        with self.assertRaises(InvalidSpec) as cm:
            l.check_example(
                AstExampleField(
                    path='test.stone',
                    lineno=1,
                    lexpos=0,
                    name='v',
                    value=[[]],
                ))
        self.assertIn("has fewer than 1 item(s)", cm.exception.msg)

        #
        # Test Map type
        #

        m = Map(String(), String())
        # valid example
        m.check_example(
            AstExampleField(
                path='test.stone',
                lineno=1,
                lexpos=0,
                name='v',
                value={"foo": "bar"}
            )
        )

        # does not conform to declared type
        with self.assertRaises(InvalidSpec):
            m.check_example(
                AstExampleField(
                    path='test.stone',
                    lineno=1,
                    lexpos=0,
                    name='v',
                    value={1: "bar"}
                )
            )

        with self.assertRaises(ParameterError):
            # errors because only string types can be used as keys
            Map(Int32(), String())

        s = Struct('S', None, None)
        s.set_attributes(
            "Docstring",
            [
                StructField('a', UInt64(), 'a field', None),
                StructField('b', List(String()), 'a field', None),
            ],
        )

        s._add_example(
            AstExample(
                'test.stone',
                lineno=1,
                lexpos=0,
                label='default',
                text='Default example',
                fields={
                    'a': AstExampleField(
                        path='test.stone',
                        lineno=2,
                        lexpos=0,
                        name='a',
                        value=132,
                    ),
                    'b': AstExampleField(
                        path='test.stone',
                        lineno=2,
                        lexpos=0,
                        name='b',
                        value=['a'],
                    ),
                }
            ))