Ejemplo n.º 1
0
def test_global_state():
    """A Field can store its data in a global dict so that it can be recreated later with the same underlying data."""
    ExecutionContext().install()
    state_dict = {}
    a = Field()
    a.bind('x', a)

    a.input_status = EmptyStub()
    a.validation_error = EmptyStub()
    a.user_input = EmptyStub()
    a.parsed_input = EmptyStub()

    a.activate_global_field_data_store(state_dict)

    a.initial_value = EmptyStub()

    b = Field()
    b.bind('x', b)

    assert a.initial_value is not b.initial_value
    assert a.input_status is not b.input_status
    assert a.validation_error is not b.validation_error
    assert a.user_input is not b.user_input
    assert a.parsed_input is not b.parsed_input

    b.activate_global_field_data_store(state_dict)

    assert a.initial_value is b.initial_value
    assert a.input_status is b.input_status
    assert a.validation_error is b.validation_error
    assert a.user_input is b.user_input
    assert a.parsed_input is b.parsed_input
Ejemplo n.º 2
0
def test_namespaces():
    ExecutionContext().install()
    state_dict = {}
    a = Field()
    a.bind('x', a)

    # Case: namespaces change the name of the Field
    b = a.in_namespace('deeper')

    assert a.name == 'x'
    assert b.name == 'deeper-x'

    # Case: namespaces can be nested
    c = b.in_namespace('even')
    assert c.name == 'even-deeper-x'

    # Case: a Field *in* different namespace, but made from another share the same data
    a.initial_value = EmptyStub()
    a.input_status = EmptyStub()
    a.validation_error = EmptyStub()
    a.user_input = EmptyStub()
    a.parsed_input = EmptyStub()

    assert a.initial_value is b.initial_value is c.initial_value
    assert a.input_status is b.input_status is c.input_status
    assert a.validation_error is b.validation_error is c.validation_error
    assert a.user_input is b.user_input is c.user_input
    assert a.parsed_input is b.parsed_input is c.parsed_input
Ejemplo n.º 3
0
def test_getting_modified_copy(fixture):
    """It is possible to get a modified copy of an existing field if you want to link it with
       different constraints on a different input"""

    other_constraint = ValidationConstraint('Other error')
    other_constraint.name = 'other'
    field = Field()
    field.add_validation_constraint(other_constraint)
    model_object = EmptyStub()
    field.bind('field_name', model_object)

    # Getting a copy
    new_field = field.copy()
    assert new_field is not field
    assert new_field.name == field.name
    assert new_field.storage_object == field.storage_object
    assert new_field.default == field.default
    assert new_field.label == field.label
    copied_other_constraint = new_field.get_validation_constraint_named(other_constraint.name)
    assert copied_other_constraint.field is new_field
    new_validation_constraints = [i.__class__ for i in new_field.validation_constraints]
    old_validation_constraints = [i.__class__ for i in field.validation_constraints]
    assert new_validation_constraints == old_validation_constraints
    assert new_field.validation_constraints != field.validation_constraints
    assert new_field.validation_constraints is not field.validation_constraints

    assert new_field.access_rights is not field.access_rights
    assert new_field.access_rights.readable is field.access_rights.readable
    assert new_field.access_rights.writable is field.access_rights.writable

    # Getting a required copy
    assert not field.required
    required_field = field.as_required(required_message='new required message')
    assert required_field.required
    required = required_field.get_validation_constraint_named(RequiredConstraint.name)
    assert required.error_message.template == 'new required message'

    # Getting copy that's not required
    field.make_required('')
    assert field.required
    optional_field = field.as_optional()
    assert not optional_field.required

    # Getting copy with a ValidationConstraint of certain type removed
    assert field.required 
    more_lax_field = field.without_validation_constraint(RequiredConstraint)
    assert not more_lax_field.required 

    # Getting copy with a new ValidationConstraint added
    field.make_optional()
    assert not field.required 
    more_strict_field = field.with_validation_constraint(RequiredConstraint())
    assert more_strict_field.required 

    # Getting copy with a new label
    assert field.label != 'new label'
    differently_labelled_field = field.with_label('new label')
    assert differently_labelled_field.label == 'new label'
Ejemplo n.º 4
0
 def field_metadata(self, fixture):
     """Fields provide metadata about their contents"""
     field = Field(default=2, required=True, label='A field')
     field.bind('fieldname', fixture.model_object)
     
     vassert( field.label == 'A field' )
     vassert( field.required )
     vassert( field.variable_name == 'fieldname' )  
     vassert( field.get_model_value() == 2 )
Ejemplo n.º 5
0
    def tailored_access_make_inputs_security_sensitive(self, fixture):
        """An Input is sensitive if explicitly set as sensitive, or if its Fields has non-defaulted
           mechanisms for determiing access rights."""

        form = Form(fixture.view, 'some_form')
        field = Field(default=3, readable=Allowed(True))
        field.bind('field_name', EmptyStub())
        input_widget = TextInput(form, field)

        vassert(input_widget.is_security_sensitive)
Ejemplo n.º 6
0
    def re_binding_behaviour_of_field_index(self, fixture):
        """FieldIndexes wont bind a field if it already is bound."""
        model_object1 = EmptyStub()
        model_object2 = EmptyStub()
        bound_field = Field()
        bound_field.bind('bound_field', model_object2)

        vassert( bound_field.is_bound )
        vassert( bound_field.bound_to is model_object2 )
        index = FieldIndex(model_object1)
        index.new_name_for_bound_field = bound_field
        vassert( index.new_name_for_bound_field.name is 'bound_field' )
        vassert( bound_field.bound_to is model_object2 )
Ejemplo n.º 7
0
def test_re_binding_behaviour_of_field_index(fixture):
    """FieldIndexes wont bind a field if it already is bound."""

    model_object1 = EmptyStub()
    model_object2 = EmptyStub()
    bound_field = Field()
    bound_field.bind('bound_field', model_object2)

    assert bound_field.is_bound
    assert bound_field.bound_to is model_object2
    index = FieldIndex(model_object1)
    index.new_name_for_bound_field = bound_field
    assert index.new_name_for_bound_field.name is 'bound_field'
    assert bound_field.bound_to is model_object2
Ejemplo n.º 8
0
    def basic_marshalling(self, fixture):
        field = Field()
        obj = EmptyStub()
        field.bind('value', obj)

        # From input
        field.from_input('abc')
        vassert( obj.value == 'abc' )

        # As input
        obj.value = 'def'
        vassert( field.as_input() == 'def' )

        # As input - corner case
        obj.value = ''
        vassert( field.as_input() == '' )
Ejemplo n.º 9
0
def test_basic_marshalling(fixture):

    field = Field()
    obj = EmptyStub()
    field.bind('value', obj)

    # From input
    field.from_input('abc')
    assert obj.value == 'abc'

    # As input
    obj.value = 'def'
    assert field.as_input() == 'def'

    # As input - corner case
    obj.value = ''
    assert field.as_input() == ''
Ejemplo n.º 10
0
def test_when_initial_value_is_read():
    """The initial_value of a Field is set upon first activate_global_field_data_store"""
    ExecutionContext().install()
    state_dict = {}
    a = Field()
    a.bind('x', a)
    a.x = 'initial value for a'

    assert not a.initial_value

    a.activate_global_field_data_store(state_dict)

    assert a.initial_value == a.x

    a.x = 'changed value'
    a.activate_global_field_data_store(state_dict)

    assert a.initial_value != a.x
Ejemplo n.º 11
0
def test_fields_have_access_rights(web_fixture):
    """Fields have access rights for reading and for writing. By default both reading and writing are allowed.
       This default can be changed by passing an Action (which returns a boolean) for each kind of
       access which will be called to determine whether that kind of access is allowed.
       """
    fixture = web_fixture

    def not_allowed(): return False

    # Case: the default
    field = Field()
    assert field.can_read()
    assert field.can_write()

    # Case: tailored rights
    field = Field(readable=Action(not_allowed), writable=Action(not_allowed))
    field.bind('field_name', fixture)
    assert not field.can_read()
    assert not field.can_write()
Ejemplo n.º 12
0
 def new_field(self):
     field = Field(default=3, readable=self.readable, writable=self.writable)
     field.bind('field_name', EmptyStub())
     return field