Example #1
0
    def test_get_example(self):
        B = boolean('A bool', example=False)
        assert B.get_example() is False

        # Default example
        B = boolean('A bool')
        assert B.get_example() is True
Example #2
0
 def test_boolean_type(self):
     B = boolean('A bool')
     tests = (
         # (input, expected)
         ('true', True),
         ('false', False),
         ('True', True),
         ('False', False),
         ('on', True),
         ('off', False),
         ('1', True),
         ('0', False),
         ('', False),
     )
     for val, expected in tests:
         assert expected == B(val)
Example #3
0
    def test_native_type(self):
        B = boolean('A bool.')
        S = string('A string.')

        class Item(UnionType):
            description = 'B or S.'
            types = [B, S]

        # Should be the first native_type in the types attribute.
        assert Item.native_type == bool

        # After instantiating with a value, it should update the native_type
        # to match the value.
        assert 'S' == Item('S')
        assert Item.native_type == str
        assert Item(True)
        assert Item.native_type == bool
# Note that this file contains some inline comments starting with # --, used to
# generate documentation from this file. You're probably better served reading
# the actual documentation (see Using in Flask in the docs).

from flask import Flask
from flask_restful import Api
from doctor.errors import NotFoundError
from doctor.flask import create_routes
from doctor.routing import Route, get, post, put, delete
# -- mark-types
from doctor import types

# doctor provides helper functions to easily define simple types.
Body = types.string('Note body', example='body')
Done = types.boolean('Marks if a note is done or not.', example=False)
NoteId = types.integer('Note ID', example=1)
Status = types.string('API status')
NoteType = types.enum('The type of note',
                      enum=['quick', 'detailed'],
                      example='quick')


# You can also inherit from type classes to create more complex types.
class Note(types.Object):
    description = 'A note object'
    additional_properties = False
    properties = {
        'note_id': NoteId,
        'body': Body,
        'done': Done,
    }
Example #5
0
 def test_non_boolean(self):
     B = boolean('bool')
     with pytest.raises(TypeSystemError, match='Must be a valid boolean.'):
         B('dog')
Example #6
0
 def test_nullable(self):
     B = boolean('bool', nullable=True)
     assert B(None) is None
Example #7
0
 def test_type(self):
     B = boolean('bool')
     assert type(B('true')) is bool
    example = {'str': 'ex str'}


ExampleObjects = array('ex objects',
                       items=ExampleObject,
                       example=[{
                           'str': 'e'
                       }])
ExampleObjectsAndAge = array('ex obj and age', items=[Age, ExampleObject])
Foo = string('foo', example='foo')
FooId = integer('foo id', example=1)
Foos = array('foos', items=Foo, example=['foo'])
FoosWithParser = array('foos with parser',
                       items=Foo,
                       parser=parse_comma_separated_str)
IsAlive = boolean('Is alive?', example=True)
ItemId = integer('item id', minimum=1, example=1, nullable=True)
Item = new_type(Object,
                description='item',
                properties={'item_id': ItemId},
                additional_properties=False,
                required=['item_id'],
                example={'item_id': 1})
IncludeDeleted = boolean('indicates if deleted items should be included.',
                         example=False)
IsDeleted = boolean('Indicates if the item should be marked as deleted',
                    example=False)
Latitude = number('The latitude.',
                  example=44.322804,
                  param_name='location.lat',
                  nullable=True)