Changeset View
Changeset View
Standalone View
Standalone View
swh/model/tests/fields/test_compound.py
# Copyright (C) 2015 The Software Heritage developers | # Copyright (C) 2015 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import datetime | import datetime | ||||
import unittest | import unittest | ||||
from nose.tools import istest | from swh.model.exceptions import NON_FIELD_ERRORS, ValidationError | ||||
from swh.model.exceptions import ValidationError, NON_FIELD_ERRORS | |||||
from swh.model.fields import compound, simple | from swh.model.fields import compound, simple | ||||
class ValidateCompound(unittest.TestCase): | class ValidateCompound(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
def validate_always(model): | def validate_always(model): | ||||
return True | return True | ||||
Show All 38 Lines | def setUp(self): | ||||
self.test_value_missing_keys = {'int'} | self.test_value_missing_keys = {'int'} | ||||
self.test_value_wrong_type = 42 | self.test_value_wrong_type = 42 | ||||
self.present_keys = set(self.test_value) | self.present_keys = set(self.test_value) | ||||
self.missing_keys = {'missingkey1', 'missingkey2'} | self.missing_keys = {'missingkey1', 'missingkey2'} | ||||
@istest | def test_validate_any_key(self): | ||||
def validate_any_key(self): | |||||
self.assertTrue( | self.assertTrue( | ||||
compound.validate_any_key(self.test_value, self.present_keys)) | compound.validate_any_key(self.test_value, self.present_keys)) | ||||
self.assertTrue( | self.assertTrue( | ||||
compound.validate_any_key(self.test_value, | compound.validate_any_key(self.test_value, | ||||
self.present_keys | self.missing_keys)) | self.present_keys | self.missing_keys)) | ||||
@istest | def test_validate_any_key_missing(self): | ||||
def validate_any_key_missing(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_any_key(self.test_value, self.missing_keys) | compound.validate_any_key(self.test_value, self.missing_keys) | ||||
exc = cm.exception | exc = cm.exception | ||||
self.assertIsInstance(str(exc), str) | self.assertIsInstance(str(exc), str) | ||||
self.assertEqual(exc.code, 'missing-alternative-field') | self.assertEqual(exc.code, 'missing-alternative-field') | ||||
self.assertEqual(exc.params['missing_fields'], | self.assertEqual(exc.params['missing_fields'], | ||||
', '.join(sorted(self.missing_keys))) | ', '.join(sorted(self.missing_keys))) | ||||
@istest | def test_validate_all_keys(self): | ||||
def validate_all_keys(self): | |||||
self.assertTrue( | self.assertTrue( | ||||
compound.validate_all_keys(self.test_value, self.present_keys)) | compound.validate_all_keys(self.test_value, self.present_keys)) | ||||
@istest | def test_validate_all_keys_missing(self): | ||||
def validate_all_keys_missing(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_all_keys(self.test_value, self.missing_keys) | compound.validate_all_keys(self.test_value, self.missing_keys) | ||||
exc = cm.exception | exc = cm.exception | ||||
self.assertIsInstance(str(exc), str) | self.assertIsInstance(str(exc), str) | ||||
self.assertEqual(exc.code, 'missing-mandatory-field') | self.assertEqual(exc.code, 'missing-mandatory-field') | ||||
self.assertEqual(exc.params['missing_fields'], | self.assertEqual(exc.params['missing_fields'], | ||||
', '.join(sorted(self.missing_keys))) | ', '.join(sorted(self.missing_keys))) | ||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_all_keys(self.test_value, | compound.validate_all_keys(self.test_value, | ||||
self.present_keys | self.missing_keys) | self.present_keys | self.missing_keys) | ||||
exc = cm.exception | exc = cm.exception | ||||
self.assertIsInstance(str(exc), str) | self.assertIsInstance(str(exc), str) | ||||
self.assertEqual(exc.code, 'missing-mandatory-field') | self.assertEqual(exc.code, 'missing-mandatory-field') | ||||
self.assertEqual(exc.params['missing_fields'], | self.assertEqual(exc.params['missing_fields'], | ||||
', '.join(sorted(self.missing_keys))) | ', '.join(sorted(self.missing_keys))) | ||||
@istest | def test_validate_against_schema(self): | ||||
def validate_against_schema(self): | |||||
self.assertTrue( | self.assertTrue( | ||||
compound.validate_against_schema(self.test_model, self.test_schema, | compound.validate_against_schema(self.test_model, self.test_schema, | ||||
self.test_value)) | self.test_value)) | ||||
@istest | def test_validate_against_schema_wrong_type(self): | ||||
def validate_against_schema_wrong_type(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_against_schema(self.test_model, self.test_schema, | compound.validate_against_schema(self.test_model, self.test_schema, | ||||
self.test_value_wrong_type) | self.test_value_wrong_type) | ||||
exc = cm.exception | exc = cm.exception | ||||
self.assertIsInstance(str(exc), str) | self.assertIsInstance(str(exc), str) | ||||
self.assertEqual(exc.code, 'model-unexpected-type') | self.assertEqual(exc.code, 'model-unexpected-type') | ||||
self.assertEqual(exc.params['model'], self.test_model) | self.assertEqual(exc.params['model'], self.test_model) | ||||
self.assertEqual(exc.params['type'], | self.assertEqual(exc.params['type'], | ||||
self.test_value_wrong_type.__class__.__name__) | self.test_value_wrong_type.__class__.__name__) | ||||
@istest | def test_validate_against_schema_mandatory_keys(self): | ||||
def validate_against_schema_mandatory_keys(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_against_schema(self.test_model, self.test_schema, | compound.validate_against_schema(self.test_model, self.test_schema, | ||||
self.test_value_missing) | self.test_value_missing) | ||||
# The exception should be of the form: | # The exception should be of the form: | ||||
# ValidationError({ | # ValidationError({ | ||||
# 'mandatory_key1': [ValidationError('model-field-mandatory')], | # 'mandatory_key1': [ValidationError('model-field-mandatory')], | ||||
# 'mandatory_key2': [ValidationError('model-field-mandatory')], | # 'mandatory_key2': [ValidationError('model-field-mandatory')], | ||||
# }) | # }) | ||||
exc = cm.exception | exc = cm.exception | ||||
self.assertIsInstance(str(exc), str) | self.assertIsInstance(str(exc), str) | ||||
for key in self.test_value_missing_keys: | for key in self.test_value_missing_keys: | ||||
nested_key = exc.error_dict[key] | nested_key = exc.error_dict[key] | ||||
self.assertIsInstance(nested_key, list) | self.assertIsInstance(nested_key, list) | ||||
self.assertEqual(len(nested_key), 1) | self.assertEqual(len(nested_key), 1) | ||||
nested = nested_key[0] | nested = nested_key[0] | ||||
self.assertIsInstance(nested, ValidationError) | self.assertIsInstance(nested, ValidationError) | ||||
self.assertEqual(nested.code, 'model-field-mandatory') | self.assertEqual(nested.code, 'model-field-mandatory') | ||||
self.assertEqual(nested.params['field'], key) | self.assertEqual(nested.params['field'], key) | ||||
@istest | def test_validate_whole_schema_shortcut_previous_error(self): | ||||
def validate_against_schema_whole_schema_shortcut_previous_error(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_against_schema( | compound.validate_against_schema( | ||||
self.test_model, | self.test_model, | ||||
self.test_schema_shortcut, | self.test_schema_shortcut, | ||||
self.test_value_missing, | self.test_value_missing, | ||||
) | ) | ||||
exc = cm.exception | exc = cm.exception | ||||
self.assertIsInstance(str(exc), str) | self.assertIsInstance(str(exc), str) | ||||
self.assertNotIn(NON_FIELD_ERRORS, exc.error_dict) | self.assertNotIn(NON_FIELD_ERRORS, exc.error_dict) | ||||
@istest | def test_validate_whole_schema(self): | ||||
def validate_against_schema_whole_schema(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_against_schema( | compound.validate_against_schema( | ||||
self.test_model, | self.test_model, | ||||
self.test_schema_shortcut, | self.test_schema_shortcut, | ||||
self.test_value, | self.test_value, | ||||
) | ) | ||||
# The exception should be of the form: | # The exception should be of the form: | ||||
Show All 10 Lines | def test_validate_whole_schema(self): | ||||
self.assertEquals(len(non_field_errors), 1) | self.assertEquals(len(non_field_errors), 1) | ||||
nested = non_field_errors[0] | nested = non_field_errors[0] | ||||
self.assertIsInstance(nested, ValidationError) | self.assertIsInstance(nested, ValidationError) | ||||
self.assertEquals(nested.code, 'model-validation-failed') | self.assertEquals(nested.code, 'model-validation-failed') | ||||
self.assertEquals(nested.params['model'], self.test_model) | self.assertEquals(nested.params['model'], self.test_model) | ||||
self.assertEquals(nested.params['validator'], 'validate_never') | self.assertEquals(nested.params['validator'], 'validate_never') | ||||
@istest | def test_validate_against_schema_field_error(self): | ||||
def validate_against_schema_field_error(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_against_schema(self.test_model, self.test_schema, | compound.validate_against_schema(self.test_model, self.test_schema, | ||||
self.test_value_str_error) | self.test_value_str_error) | ||||
# The exception should be of the form: | # The exception should be of the form: | ||||
# ValidationError({ | # ValidationError({ | ||||
# 'str': [ValidationError('unexpected-type')], | # 'str': [ValidationError('unexpected-type')], | ||||
# }) | # }) | ||||
exc = cm.exception | exc = cm.exception | ||||
self.assertIsInstance(str(exc), str) | self.assertIsInstance(str(exc), str) | ||||
self.assertEquals(set(exc.error_dict.keys()), {'str'}) | self.assertEquals(set(exc.error_dict.keys()), {'str'}) | ||||
str_errors = exc.error_dict['str'] | str_errors = exc.error_dict['str'] | ||||
self.assertIsInstance(str_errors, list) | self.assertIsInstance(str_errors, list) | ||||
self.assertEquals(len(str_errors), 1) | self.assertEquals(len(str_errors), 1) | ||||
nested = str_errors[0] | nested = str_errors[0] | ||||
self.assertIsInstance(nested, ValidationError) | self.assertIsInstance(nested, ValidationError) | ||||
self.assertEquals(nested.code, 'unexpected-type') | self.assertEquals(nested.code, 'unexpected-type') | ||||
@istest | def test_validate_against_schema_field_failed(self): | ||||
def validate_against_schema_field_failed(self): | |||||
with self.assertRaises(ValidationError) as cm: | with self.assertRaises(ValidationError) as cm: | ||||
compound.validate_against_schema(self.test_model, | compound.validate_against_schema(self.test_model, | ||||
self.test_schema_field_failed, | self.test_schema_field_failed, | ||||
self.test_value) | self.test_value) | ||||
# The exception should be of the form: | # The exception should be of the form: | ||||
# ValidationError({ | # ValidationError({ | ||||
# 'int': [ValidationError('field-validation-failed')], | # 'int': [ValidationError('field-validation-failed')], | ||||
Show All 15 Lines |