<s> from __future__ import absolute_import , unicode_literals <EOL> from bson . objectid import ObjectId <EOL> from mock import patch <EOL> from mongorest . collection import Collection <EOL> from mongorest . errors import * <EOL> from mongorest . testcase import TestCase <EOL> from mongorest . validator import Validator <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> class TestValidator ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . validator = Validator ( ) <EOL> def test_validate_type_objectid_sets_error_if_not_objectid ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : <NUM_LIT:1> } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> self . validator . errors , { '<STR_LIT:test>' : '<STR_LIT>' } <EOL> ) <EOL> def test_validate_type_objectid_does_not_set_error_if_type_is_correct ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : ObjectId ( ) } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' } } <EOL> self . assertTrue ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( self . validator . errors , { } ) <EOL> def test_validate_returns_true_if_no_errors_are_found ( self ) : <EOL> document = Collection ( ) <EOL> document . schema = { } <EOL> self . assertTrue ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( self . validator . errors , { } ) <EOL> def test_validate_returns_false_if_errors_are_found ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT:test>' } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> self . validator . errors , { '<STR_LIT:test>' : '<STR_LIT>' } <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_unknown_field_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT:1>' } ) <EOL> document . schema = { } <EOL> self . validator . allow_unknown = False <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { } , <EOL> errors = [ <EOL> UnknownFieldError ( collection = '<STR_LIT>' , field = '<STR_LIT:test>' ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_required_field_error ( self ) : <EOL> document = Collection ( { } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT>' : True } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT>' : True } <EOL> } , errors = [ <EOL> RequiredFieldError ( collection = '<STR_LIT>' , field = '<STR_LIT:test>' ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_read_only_field_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT:test>' } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT>' : True } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT>' : True } <EOL> } , errors = [ <EOL> ReadOnlyFieldError ( collection = '<STR_LIT>' , field = '<STR_LIT:test>' ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_field_type_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT:test>' } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> } , errors = [ <EOL> FieldTypeError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , <EOL> field_type = '<STR_LIT>' <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_regex_match_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT>' } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : '<STR_LIT>' } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> } , errors = [ <EOL> RegexMatchError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , <EOL> regex = '<STR_LIT>' <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_min_length_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT>' } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : <NUM_LIT:3> } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : <NUM_LIT:3> } <EOL> } , errors = [ <EOL> MinLengthError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , <EOL> min_length = <NUM_LIT:3> <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_max_length_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT>' } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : <NUM_LIT:3> } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : <NUM_LIT:3> } <EOL> } , errors = [ <EOL> MaxLengthError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , <EOL> max_length = <NUM_LIT:3> <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_length_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : [ '<STR_LIT:test>' , '<STR_LIT:test>' ] } ) <EOL> document . schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:list>' , '<STR_LIT>' : [ { '<STR_LIT:type>' : '<STR_LIT:string>' } ] } <EOL> } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:list>' , '<STR_LIT>' : [ { '<STR_LIT:type>' : '<STR_LIT:string>' } ] } <EOL> } , errors = [ <EOL> LengthError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , length = <NUM_LIT:1> <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_value_not_allowed_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : '<STR_LIT>' } ) <EOL> document . schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : [ '<STR_LIT:test>' ] } <EOL> } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : [ '<STR_LIT:test>' ] } <EOL> } , errors = [ <EOL> ValueNotAllowedError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , value = '<STR_LIT>' <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_values_not_allowed_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : [ '<STR_LIT>' , '<STR_LIT:test>' ] } ) <EOL> document . schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:list>' , '<STR_LIT>' : [ '<STR_LIT:test>' ] } <EOL> } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:list>' , '<STR_LIT>' : [ '<STR_LIT:test>' ] } <EOL> } , errors = [ <EOL> ValuesNotAllowedError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , <EOL> values = '<STR_LIT>' <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_min_value_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : <NUM_LIT:0> } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:10> } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:10> } <EOL> } , errors = [ <EOL> MinValueError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , <EOL> min_value = <NUM_LIT:10> <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_max_value_error ( self ) : <EOL> document = Collection ( { '<STR_LIT:test>' : <NUM_LIT:11> } ) <EOL> document . schema = { '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:10> } } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:10> } <EOL> } , errors = [ <EOL> MaxValueError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT:test>' , <EOL> max_value = <NUM_LIT:10> <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> def test_validate_sets_correct_errors_on_document_if_more_than_one_error ( self ) : <EOL> document = Collection ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> document . schema = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : True } , '<STR_LIT>' : { '<STR_LIT:type>' : [ '<STR_LIT:list>' , '<STR_LIT:string>' ] } , <EOL> } <EOL> self . assertFalse ( self . validator . validate_document ( document ) ) <EOL> self . assertEqual ( <EOL> document . errors , <EOL> DocumentValidationError ( <EOL> collection = '<STR_LIT>' , document = document . document , schema = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : True } , '<STR_LIT>' : { '<STR_LIT:type>' : [ '<STR_LIT:list>' , '<STR_LIT:string>' ] } <EOL> } , errors = [ <EOL> RequiredFieldError ( collection = '<STR_LIT>' , field = '<STR_LIT>' ) , <EOL> FieldTypeError ( <EOL> collection = '<STR_LIT>' , field = '<STR_LIT>' , <EOL> field_type = '<STR_LIT>' <EOL> ) <EOL> ] <EOL> ) <EOL> ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_flattened_errors_returns_flaten_call ( self , flatten ) : <EOL> with self . validator . flattened_errors : <EOL> flatten . assert_called_once_with ( { } , '<STR_LIT>' , '<STR_LIT:.>' ) <EOL> def test_flatten_returns_flattened_dict ( self ) : <EOL> flattened = self . validator . flatten ( { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:test>' } , '<STR_LIT>' : '<STR_LIT:test>' <EOL> } ) <EOL> self . assertEqual ( <EOL> flattened , { '<STR_LIT>' : '<STR_LIT:test>' , '<STR_LIT>' : '<STR_LIT:test>' } <EOL> ) <EOL> def test_get_field_schema_returns_schema_of_outer_field ( self ) : <EOL> self . validator . schema = { '<STR_LIT:test>' : { '<STR_LIT>' : True } } <EOL> self . assertEqual ( <EOL> self . validator . get_field_schema ( '<STR_LIT:test>' ) , { '<STR_LIT>' : True } <EOL> ) <EOL> def test_get_field_schema_returns_schema_of_inner_field ( self ) : <EOL> self . validator . schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : { '<STR_LIT:test>' : { '<STR_LIT>' : True } } } <EOL> } <EOL> self . assertEqual ( <EOL> self . validator . get_field_schema ( '<STR_LIT>' ) , { '<STR_LIT>' : True } <EOL> ) <EOL> def test_get_field_schema_returns_inner_schema_of_field ( self ) : <EOL> self . validator . schema = { <EOL> '<STR_LIT:test>' : { '<STR_LIT:type>' : '<STR_LIT:list>' , '<STR_LIT>' : { '<STR_LIT:type>' : '<STR_LIT>' } } <EOL> } <EOL> self . assertEqual ( <EOL> self . validator . get_field_schema ( '<STR_LIT>' ) , { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> ) </s>
