[constraints] fix FormatConstraint.failed_message

It's inherited from StaticVocabularyConstraint, and expects a 'values' attribute, so set that on init.

Closes #204923

authorJulien Cristau <julien.cristau@logilab.fr>
changeset14a1e4b971c5
branchstable
phasepublic
hiddenno
parent revision#3d71c3533c15 stable is 0.39
child revision#8d94c545673f fix date/time default deprecation warning
files modified by this revision
constraints.py
test/unittest_schema.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1391172933 -3600
# Fri Jan 31 13:55:33 2014 +0100
# Branch stable
# Node ID 14a1e4b971c5a9a3c30d0f63e7dd15ff32943a54
# Parent 3d71c3533c15726201201484a4f946e0927f2507
[constraints] fix FormatConstraint.failed_message

It's inherited from StaticVocabularyConstraint, and expects a 'values'
attribute, so set that on init.

Closes #204923

diff --git a/constraints.py b/constraints.py
@@ -369,11 +369,11 @@
1      regular_formats = (_('text/rest'),
2                         _('text/html'),
3                         _('text/plain'),
4                         )
5      def __init__(self):
6 -        pass
7 +        self.values = self.vocabulary()
8 
9      def check_consistency(self, subjschema, objschema, rdef):
10          if not objschema.final:
11              raise BadSchemaDefinition("unique constraint doesn't apply to non "
12                                        "final entity type")
diff --git a/test/unittest_schema.py b/test/unittest_schema.py
@@ -30,11 +30,12 @@
13  from yams.buildobjs import (register_base_types, make_type, _add_relation,
14                              EntityType, RelationType, RelationDefinition)
15  from yams.schema import Schema, RelationDefinitionSchema
16  from yams.interfaces import IVocabularyConstraint
17  from yams.constraints import (BASE_CHECKERS, SizeConstraint, RegexpConstraint,
18 -                              StaticVocabularyConstraint, IntervalBoundConstraint)
19 +                              StaticVocabularyConstraint, IntervalBoundConstraint,
20 +                              FormatConstraint)
21  from yams.reader import SchemaLoader
22 
23 
24  # build a dummy schema ########################################################
25 
@@ -66,10 +67,11 @@
26              ('Person datenaiss Date'),
27              ('Person TEST Boolean'),
28              ('Person promo String'),
29              ('Person promo_enlarged String'),
30              ('Person promo_encoding String'),
31 +            ('Person promo_format String'),
32              # real relations
33              ('Person  travaille Societe'),
34              ('Person  evaluee   Note'),
35              ('Societe evaluee   Note'),
36              ('Person  concerne  Affaire'),
@@ -94,10 +96,11 @@
37          eperson.rdef('nom').constraints = [SizeConstraint(20, 10)]
38          eperson.rdef('prenom').constraints = [SizeConstraint(64)]
39          eperson.rdef('tel').constraints = [IntervalBoundConstraint(maxvalue=999999)]
40          eperson.rdef('fax').constraints = [IntervalBoundConstraint(minvalue=12, maxvalue=999999)]
41          eperson.rdef('promo').constraints = [StaticVocabularyConstraint( (u'bon', u'pasbon'))]
42 +        eperson.rdef('promo_format').constraints = [FormatConstraint()]
43 
44          estring = schema.eschema('String')
45          eint = schema.eschema('Int')
46          rconcerne = schema.rschema('concerne')
47          rnom = schema.rschema('nom')
@@ -119,10 +122,11 @@
48      ('Person', [('nom', 1), ('nom', u'tropcour'),
49                  ('nom', u'>10 mais  sup�rieur � < 20 , c\'est long'),
50                  ('sexe', u'F'), ('sexe', u'MorF'), ('sexe', 'F'),
51                  ('promo', 'bon'), ('promo', 'uyou'),
52                  ('promo', u' pas bon du tout'),
53 +                ('promo_format', u'text/something'),
54                  ('tel', 'notastring'),
55                  ('tel', 1000000),
56                  ('fax', 11),
57                  ('TEST', 'notaboolean'), #('TEST', 0), ('TEST', 1)]), #should we accept this ?
58                  ('TEST', 'true'), ('TEST', 'false')]),
@@ -244,11 +248,12 @@
59      def test_is_metadata(self):
60          self.assertEqual(eperson.is_metadata('promo'), None)
61          self.assertEqual(eperson.is_metadata('promo_enlarged'), None)
62          self.assertEqual(eperson.is_metadata('promo_encoding'), ('promo', 'encoding'))
63          self.assertEqual([(k.type, v)  for k, v in eperson.meta_attributes().items()],
64 -                          [('promo_encoding', ('encoding', 'promo'))])
65 +                          [('promo_encoding', ('encoding', 'promo')),
66 +                           ('promo_format', ('format', 'promo'))])
67 
68      def test_defaults(self):
69          self.assertEqual(list(eperson.defaults()), [])
70          self.assertRaises(StopIteration, estring.defaults().next)
71 
@@ -272,13 +277,13 @@
72 
73      def test_subject_relations(self):
74          """check subject relations a returned in the same order as in the
75          schema definition"""
76          rels = eperson.ordered_relations()
77 -        expected = ['nom', 'prenom', 'sexe', 'tel', 'fax', 'datenaiss',
78 -                    'TEST', 'promo', 'promo_enlarged', 'promo_encoding', 'travaille',
79 -                    'evaluee', 'concerne']
80 +        expected = ['nom', 'prenom', 'sexe', 'tel', 'fax', 'datenaiss', 'TEST',
81 +                    'promo', 'promo_enlarged', 'promo_encoding', 'promo_format',
82 +                    'travaille', 'evaluee', 'concerne']
83          self.assertEqual([r.type for r in rels], expected)
84 
85      def test_object_relations(self):
86          """check object relations a returned in the same order as in the
87          schema definition"""
@@ -410,11 +415,12 @@
88 
89      def test_schema_relations(self):
90          all_relations = ['TEST', 'concerne', 'travaille', 'evaluee',
91                           'date', 'type', 'sujet', 'ref', 'nom', 'prenom',
92                           'starton', 'sexe', 'promo', 'promo_enlarged',
93 -                         'promo_encoding', 'tel', 'fax', 'datenaiss']
94 +                         'promo_encoding', 'promo_format', 'tel', 'fax',
95 +                         'datenaiss']
96          all_relations.sort()
97          relations = schema.relations()
98          relations.sort()
99          self.assertEqual(relations, all_relations)
100