class CheckEntry(Document): check_actor = StringField(required=True) check_action = StringField() status = StringField(required=True, enum=['PASS', 'FAIL']) summary = StringField(required=True) params = ArrayField(items=StringField(), unique_items=True, additional_items=False)
class Task(Document): class Options(object): title = 'Task' description = 'A task.' definition_id = 'task' name = StringField(required=True, min_length=5) type = StringField(required=True, enum=['TYPE_1', 'TYPE_2']) resources = ArrayField(DocumentField(Resource)) created_at = DateTimeField(name='created-at', required=True) author = DocumentField(User)
class User(Document): login = StringField() friends = ArrayField( Var({ 'db_role': db_role_friends_field, 'request_role': request_role_friends_field, }))
class Child(Base): class Options(object): definition_id = 'child' title = 'Child' b = StringField() c = DocumentField(RECURSIVE_REFERENCE_CONSTANT)
class Base(Document): class Options(object): inheritance_mode = ALL_OF definition_id = 'base' title = 'Base' a = StringField()
class Letters(Document): z = StringField() x = StringField() a = StringField() b = StringField() c = StringField() y = StringField() d = StringField() e = StringField()
def test_array_field(): f = ArrayField(items=()) with pytest.raises(SchemaGenerationException) as e: f.get_schema() assert list(e.value.steps) == [FieldStep(f), AttributeStep('items')] f = ArrayField(items=( Var({'role_x': StringField()}), Var({'role_x': IntField()}), )) role = 'role_y' with pytest.raises(SchemaGenerationException) as e: f.get_schema(role='role_y') assert list(e.value.steps) == [ FieldStep(f, role=role), AttributeStep('items', role=role) ] f = ArrayField(items=(None, None)) with pytest.raises(SchemaGenerationException) as e: f.get_schema() assert list( e.value.steps) == [FieldStep(f), AttributeStep('items'), ItemStep(0)] f = ArrayField(items=object()) with pytest.raises(SchemaGenerationException) as e: f.get_schema() assert list(e.value.steps) == [FieldStep(f), AttributeStep('items')] f = ArrayField(additional_items=object()) with pytest.raises(SchemaGenerationException) as e: f.get_schema() assert list( e.value.steps) == [FieldStep(f), AttributeStep('additional_items')] f = ArrayField(items=FieldStub()) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert e.message == FieldStub.ERROR_MESSAGE assert list(e.steps) == [FieldStep(f), AttributeStep('items')] f = ArrayField(items=(FieldStub(), )) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert e.message == FieldStub.ERROR_MESSAGE assert list(e.steps) == [FieldStep(f), AttributeStep('items'), ItemStep(0)] f = ArrayField(additional_items=FieldStub()) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert e.message == FieldStub.ERROR_MESSAGE assert list(e.steps) == [FieldStep(f), AttributeStep('additional_items')]
def test_error(): db_role_friends_field = ArrayField((StringField(), None)) request_role_friends_field = ArrayField(StringField()) class User(Document): login = StringField() friends = ArrayField( Var({ 'db_role': db_role_friends_field, 'request_role': request_role_friends_field, })) class Users(Document): users = ArrayField(DocumentField(User)) Users.get_schema() role = 'db_role' with pytest.raises(SchemaGenerationException) as e: Users.get_schema(role=role) e = e.value assert list(e.steps) == [ DocumentStep(Users, role=role), FieldStep(Users._backend, role=role), AttributeStep('properties', role=role), ItemStep('users', role=role), FieldStep(Users.users, role=role), AttributeStep('items', role=role), FieldStep(Users.users.items, role=role), DocumentStep(User, role=role), FieldStep(User._backend, role=role), AttributeStep('properties', role=role), ItemStep('friends', role=role), FieldStep(User.friends, role=role), AttributeStep('items', role=role), FieldStep(db_role_friends_field, role=role), AttributeStep('items', role=role), ItemStep(1, role=role) ] assert e.message == 'None is not resolvable' assert ("Steps: Users -> DocumentBackend.properties['users'] -> " "ArrayField.items -> DocumentField -> User -> " "DocumentBackend.properties['friends'] -> ArrayField.items -> " "ArrayField.items[1]") in str(e)
class CommonJobDocument(Document): name = StringField(description="Name of the job.") n_iterations = NumberField( minimum=0, multiple=1, description="Configure the number of time the job should be repeated before executing the next job.", ) reset_before_job_start = BooleanField( description="Configures the reset of LR1110 strategy for this job. If set to true, the LR1110 is reset before executing this job. If set to false, it is not reset before executing this job." )
def test_exceptions(): f_1 = StringField() f_2 = StringField() # test __eq__ and __ne__ assert FieldStep(f_1) == FieldStep(f_1) assert FieldStep(f_1, role='role_1') != FieldStep(f_1) assert FieldStep(f_1) != FieldStep(f_2) assert FieldStep(f_1) != AttributeStep('fields') assert not (FieldStep(f_1) == AttributeStep('fields')) # test __repr__ r = repr(FieldStep(f_1, role='role_1')) assert repr(f_1) in r assert 'role_1' in r message = 'Something went wrong' e = SchemaGenerationException(message) assert str(e) == message step = FieldStep(f_1) e.steps.appendleft(step) assert str(e) == '{0}\nSteps: {1}'.format(message, step)
class StringList(Document): value = ArrayField(items=StringField(), additional_items=False)
class TypeStringList(Document): value = ArrayField(items=StringField(), unique_items=True, additional_items=False)
class A(Document): class Options(object): id = 'http://example.com/schema/' id = StringField() b = DocumentField('B')
def test_dict_field(): f = DictField(properties={'a': object()}) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert 'not resolvable' in e.message assert list( e.steps) == [FieldStep(f), AttributeStep('properties'), ItemStep('a')] f = DictField(pattern_properties={'a.*': object()}) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert 'not resolvable' in e.message assert list(e.steps) == [ FieldStep(f), AttributeStep('pattern_properties'), ItemStep('a.*') ] f = DictField(additional_properties=object()) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert 'not a BaseField or a bool' in e.message assert list( e.steps) == [FieldStep(f), AttributeStep('additional_properties')] f = DictField(properties={'a': FieldStub()}) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert e.message == FieldStub.ERROR_MESSAGE assert list( e.steps) == [FieldStep(f), AttributeStep('properties'), ItemStep('a')] f = DictField(pattern_properties={'a.*': FieldStub()}) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert e.message == FieldStub.ERROR_MESSAGE assert list(e.steps) == [ FieldStep(f), AttributeStep('pattern_properties'), ItemStep('a.*') ] f = DictField(additional_properties=FieldStub()) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert e.message == FieldStub.ERROR_MESSAGE assert list( e.steps) == [FieldStep(f), AttributeStep('additional_properties')] for kwarg_value in (object(), Var({'role_x': object()})): for kwarg in ('properties', 'pattern_properties'): f = DictField(**{kwarg: kwarg_value}) with pytest.raises(SchemaGenerationException) as e: f.get_schema(role='role_x') e = e.value assert 'not a dict' in e.message assert list(e.steps) == [ FieldStep(f, role='role_x'), AttributeStep(kwarg, role='role_x') ] f = DictField(additional_properties=kwarg_value) with pytest.raises(SchemaGenerationException) as e: f.get_schema(role='role_x') e = e.value assert 'not a BaseField or a bool' in e.message assert list(e.steps) == [ FieldStep(f, role='role_x'), AttributeStep('additional_properties', role='role_x') ] f = DictField(pattern_properties={'((((': StringField()}) with pytest.raises(SchemaGenerationException) as e: f.get_schema() e = e.value assert 'unbalanced parenthesis' in e.message assert list(e.steps) == [FieldStep(f), AttributeStep('pattern_properties')]
class Message(Document): value = StringField()
class C(Document): name = StringField() c = DocumentField('C')
class Error(Document): context = StringField() value = StringField()
class D(Document): class Options(object): id = '#hash' id = StringField()
class BaseTypeString(Document): value = StringField()
class TypePortScan(Document): shallow_scan = BooleanField(required=False) port_range = StringField(required=False) force_nmap = BooleanField(required=False)
class Button(Base): class Options(object): definition_id = 'button' title = 'Button' on_click = StringField(required=True)
class Shape(Base): class Options(object): definition_id = 'shape' title = 'Shape' color = StringField(required=True)
class OptionalContainerName(Document): value = StringField( pattern='^([a-zA-Z0-9][a-zA-Z0-9_.-]+|)$', required=False, description='Valid container name. Same pattern used by Docker.')
class A(Document): name = StringField() b = DocumentField('B', as_ref=True)
class ContainerName(Document): value = StringField( pattern='^[a-zA-Z0-9][a-zA-Z0-9_.-]+$', required=True, description='Valid container name. Same pattern used by Docker.')
class X(Document): name = StringField()
class PortData(Document): name = StringField(required=True) product = StringField()
class X(Document): with Scope('role_1') as s_1: s_1.name = StringField() with Scope('role_2') as s_2: s_2.name = StringField()
class String(Document): value = StringField()
class CommonJobDocument(Document): name = StringField() n_iterations = NumberField(minimum=0, multiple=1) reset_before_job_start = BooleanField()