From 7cb68a41ac6c639cc4818f21cab801b57ca20253 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 1 Apr 2013 13:29:32 +0200 Subject: [PATCH] config: Improve validate error messages and fix handling of non-string choices. --- mopidy/utils/config.py | 7 ++++--- tests/utils/config_test.py | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/mopidy/utils/config.py b/mopidy/utils/config.py index a34fccb6..2fcdfd94 100644 --- a/mopidy/utils/config.py +++ b/mopidy/utils/config.py @@ -4,19 +4,20 @@ from __future__ import unicode_literals def validate_choice(value, choices): """Choice validation, normally called in config value's validate().""" if choices is not None and value not in choices : - raise ValueError('must be one of %s.' % ', '.join(choices)) + names = ', '.join(repr(c) for c in choices) + raise ValueError('%r must be one of %s.' % (value, names)) def validate_minimum(value, minimum): """Minimum validation, normally called in config value's validate().""" if minimum is not None and value < minimum: - raise ValueError('must be larger than %s.' % minimum) + raise ValueError('%r must be larger than %r.' % (value, minimum)) def validate_maximum(value, maximum): """Maximum validation, normally called in config value's validate().""" if maximum is not None and value > maximum: - raise ValueError('must be smaller than %s.' % maximum) + raise ValueError('%r must be smaller than %r.' % (value, maximum)) class ConfigValue(object): diff --git a/tests/utils/config_test.py b/tests/utils/config_test.py index 28436b5c..5345e5a4 100644 --- a/tests/utils/config_test.py +++ b/tests/utils/config_test.py @@ -11,6 +11,7 @@ class ValidateChoiceTest(unittest.TestCase): def test_valid_value_passes(self): config.validate_choice('foo', ['foo', 'bar', 'baz']) + config.validate_choice(1, [1, 2, 3]) def test_empty_choices_fails(self): with self.assertRaises(ValueError): @@ -19,6 +20,8 @@ class ValidateChoiceTest(unittest.TestCase): def test_invalid_value_fails(self): with self.assertRaises(ValueError): config.validate_choice('foobar', ['foo', 'bar', 'baz']) + with self.assertRaises(ValueError): + config.validate_choice(5, [1, 2, 3]) class ValidateMinimumTest(unittest.TestCase): @@ -95,7 +98,6 @@ class StringTest(unittest.TestCase): def test_deserialize_enforces_choices(self): value = config.String(choices=['foo', 'bar', 'baz']) - self.assertEqual('foo', value.deserialize('foo')) with self.assertRaises(ValueError): value.deserialize('foobar')