Commit 998eb8c6 authored by "Sylvain ext:(%22)'s avatar "Sylvain ext:(%22)
Browse files

merge

......@@ -3,6 +3,7 @@ ChangeLog for yams
--
* fix _ to unicode so we can have unicode description in schema file
* automatically sets maxsize when some vocabulary is defined
2006-11-14 -- 0.9.2
* new "includefinal" optional argument to ESchema.relation_definitions
......
......@@ -88,6 +88,14 @@ class AbstractTypedAttribute(SubjectRelation):
vocabulary = kwargs.pop('vocabulary', None)
if vocabulary is not None:
add_constraint(kwargs, StaticVocabularyConstraint(vocabulary))
for constraint in kwargs.get('constraints', ()):
if isinstance(constraint, SizeConstraint):
break
if isinstance(constraint, StaticVocabularyConstraint):
maxsize = max(len(x) for x in constraint.values)
else:
if maxsize:
add_constraint(kwargs, SizeConstraint(max=maxsize))
unique = kwargs.pop('unique', None)
if unique:
add_constraint(kwargs, UniqueConstraint())
......
......@@ -29,6 +29,10 @@ KEYWORD_MAP = {'NOW' : now,
}
def rehash(dictionary):
# WARNING: don't replace this function by a simple dictionary copy
# this is necessary since the __hash__ method of objects
# contained in the original dictionary has changed and we
# want a fresh dictionary built using the new __hash__ method
res = {}
for key, val in dictionary.items():
res[key] = val
......
......@@ -8,6 +8,7 @@ from logilab.common.compat import sorted
from yams.schema import Schema, EntitySchema
from yams.reader import SchemaLoader, RelationFileReader
from yams.constraints import StaticVocabularyConstraint, SizeConstraint
import os.path as osp
......@@ -275,14 +276,14 @@ class SchemaLoaderTC(TestCase):
from yams import builder as B
class BasePerson(B.EntityType):
firstname = B.String()
lastname = B.String()
firstname = B.String(vocabulary=('logilab', 'caesium'), maxsize=10)
lastname = B.String(constraints=[StaticVocabularyConstraint(['logilab', 'caesium'])])
class Person(BasePerson):
email = B.String()
class Employee(Person):
company = B.String()
company = B.String(vocabulary=('logilab', 'caesium'))
class Foo(B.EntityType):
i = B.Int(required=True)
......@@ -307,7 +308,17 @@ class PySchemaTC(TestCase):
['Int', 'Float', 'Datetime'])
self.assertEquals(foo.relations[0].cardinality, '11')
self.assertEquals(foo.relations[1].cardinality, '?1')
def test_maxsize(self):
bp = BasePerson()
def maxsize(e):
for e in e.constraints:
if isinstance(e, SizeConstraint):
return e.max
self.assertEquals(maxsize(bp.relations[0]), 10)
self.assertEquals(maxsize(bp.relations[1]), 7)
emp = Employee()
self.assertEquals(maxsize(emp.relations[3]), 7)
if __name__ == '__main__':
unittest_main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment