def test_get_example(self): I = integer('An int', example=1022) # noqa assert 1022 == I.get_example() # Default example I = integer('An ID') # noqa assert 1 == I.get_example()
def test_additional_items(self): A = array('a', items=[ string('string', max_length=1), integer('int', maximum=1234)], additional_items=True) # no exception A(['a', 2, 3, 4, True]) A = array('a', items=[ string('string', max_length=1), integer('int', maximum=1234)], additional_items=False) with pytest.raises(TypeSystemError, match='Too many items.'): A(['a', 2, 3, 4, True])
class RequiredPropsObject(Object): description = 'required' properties = { 'foo': string('foo property', min_length=2), 'bar': integer('an int'), } required = ['bar']
def test_exclusive_mimimum(self): N = integer('int', minimum=2, exclusive_minimum=True) # No exception N(3) # number is equal to minimum with pytest.raises(TypeSystemError, match='Must be greater than 2.'): N(2)
def test_items_multiple_types(self): A = array('a', items=[ string('string', max_length=1, example='foo'), integer('int', maximum=1234, example=123)]) # no exception A(['b', 1234]) # Invalid type with pytest.raises(TypeSystemError, match="{1: 'Must be less than or equal to 1234.'}"): A(['b', 1235])
def test_get_example(self): A = array('No example of items') assert [1] == A.get_example() A = array('Defined example', example=['a', 'b']) assert ['a', 'b'] == A.get_example() A = array('No example, defined items', items=string('letter')) assert ['string'] == A.get_example() A = array('a', items=[ string('string', max_length=1, example='foo'), integer('int', maximum=1234, example=123)]) assert ['foo', 123] == A.get_example()
def test_multiple_of(self): N = integer('int', multiple_of=2) # No exception N(4) # Not multiple with pytest.raises(TypeSystemError, match='Must be a multiple of 2.'): N(5) # Also test with float N = number('float', multiple_of=1.1) # No exception N(2.2) # Not multiple with pytest.raises(TypeSystemError, match='Must be a multiple of 1.1.'): N(5)
# 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, }
def test_type(self): I = integer('int') # noqa assert type(I(1)) is int
""" This module contains custom types used by tests. """ from doctor.types import (array, boolean, enum, integer, new_type, number, string, Object, UnionType) def parse_comma_separated_str(value): return value.split(',') Age = integer('age', minimum=1, maximum=120, example=34) Auth = string('auth token', example='testtoken') Color = enum('Color', enum=['blue', 'green'], example='blue', case_insensitive=True) Colors = array('colors', items=Color, example=['green']) ExampleArray = array('ex description e', items=Auth, example=['ex', 'array']) TwoItems = array('two items', items=[Age, Color]) class ExampleObject(Object): description = 'ex description f' properties = {'str': Auth} additional_properties = False example = {'str': 'ex str'} ExampleObjects = array('ex objects', items=ExampleObject,