Commit c142e0cd authored by Sylvain's avatar Sylvain
Browse files

test fixes

parent 4138f7cbde42
"""defines classes used to build a schema
:organization: Logilab
:copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"
__docformat__ = "restructuredtext en"
__metaclass__ = type
......@@ -182,8 +187,6 @@ class Definition(object):
yield eschema.type
class metadefinition(type):
"""this metaclass builds the __relations__ attribute
of EntityType's subclasses
......@@ -275,6 +278,7 @@ class RelationBase(Definition):
cardinality = None
constraints = ()
symetric = False
inlined = False
def __init__(self, *args, **kwargs):
super(RelationBase, self).__init__(*args, **kwargs)
......
......@@ -3,7 +3,7 @@ relation definitions files or a direct python definition file)
:organization: Logilab
:copyright: 2004-2007 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:copyright: 2004-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
......@@ -54,6 +54,7 @@ class RelationFileReader(builder.FileReader):
if 'inline' in relation_def:
#print 'XXX inline is deprecated'
rdef.cardinality = '?*'
rdef.inlined = True
relation_def.remove('inline')
# is there some arbitrary constraint ?
if relation_def:
......
......@@ -4,7 +4,6 @@
:copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"
from copy import deepcopy
......@@ -521,8 +520,9 @@ class RelationSchema(ERSchema):
def __init__(self, schema=None, rdef=None, **kwargs):
if rdef is not None:
# if this relation is symetric
# if this relation is symetric/inlined
self.symetric = rdef.symetric
self.inlined = rdef.inlined
# if this relation is an attribute relation
self.final = False
# mapping to subject/object with schema as key
......
......@@ -26,7 +26,7 @@ def schema2sql(dbhelper, schema, skip_entities=(), skip_relations=()):
w(eschema2sql(dbhelper, eschema, skip_relations))
for rtype in sorted(schema.relations()):
rschema = schema.rschema(rtype)
if rschema.is_final() or rschema.physical_mode() == 'subjectinline':
if rschema.is_final() or rschema.inlined:
continue
w(rschema2sql(rschema))
return '\n'.join(output)
......@@ -45,7 +45,7 @@ def dropschema2sql(schema, skip_entities=(), skip_relations=()):
w(dropeschema2sql(eschema, skip_relations))
for rtype in sorted(schema.relations()):
rschema = schema.rschema(rtype)
if rschema.is_final() or rschema.physical_mode() == 'subjectinline':
if rschema.is_final() or rschema.inlined:
continue
w(droprschema2sql(rschema))
return '\n'.join(output)
......@@ -55,7 +55,7 @@ def eschema_attrs(eschema, skip_relations):
if not attrdef[0].type in skip_relations]
attrs += [(rschema, None)
for rschema in eschema.subject_relations()
if not rschema.final and rschema.physical_mode() == 'subjectinline']
if not rschema.final and rschema.inlined]
return attrs
def dropeschema2sql(eschema, skip_relations=()):
......@@ -145,8 +145,8 @@ CREATE TABLE %(table)s (
CONSTRAINT %(table)s_p_key PRIMARY KEY(eid_from, eid_to),
);
CREATE INDEX %(table)s_from_idx ON %(table)s (eid_from);
CREATE INDEX %(table)s_to_idx ON %(table)s (eid_to);"""
CREATE INDEX %(table)s_from_idx ON %(table)s(eid_from);
CREATE INDEX %(table)s_to_idx ON %(table)s(eid_to);"""
# CONSTRAINT %(table)s_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
# CONSTRAINT %(table)s_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
......@@ -174,7 +174,7 @@ def grant_schema(schema, user, set_owner=True, skip_entities=()):
w(grant_eschema(eschema, user, set_owner))
for rtype in sorted(schema.relations()):
rschema = schema.rschema(rtype)
if rschema.is_final() or rschema.physical_mode() == 'subjectinline':
if rschema.is_final() or rschema.inlined:
continue
w(grant_rschema(rschema, user, set_owner))
return '\n'.join(output)
......
......@@ -27,7 +27,7 @@ class initial_state(MetaUserRelationType):
"""indicate which state should be used by default when an entity using states
is created
"""
inlined = True
class Eetype(MetaEntityType):
"""define an entity type, used to build the application schema"""
......
......@@ -223,13 +223,10 @@ class EntitySchemaTC(BaseSchemaTC):
def test_subject_relations(self):
"""check subject relations a returned in the same order as in the
schema definition"""
rels = eperson.subject_relations()
rels = eperson.ordered_relations()
expected = ['nom', 'prenom', 'sexe', 'tel', 'fax', 'datenaiss',
'TEST', 'promo', 'travaille', 'evaluee', 'concerne']
self.assertEquals([r.type for r in rels], expected)
rels = [schem.type for schem in eperson.subject_relations()]
self.assertEquals(rels, ['nom', 'prenom', 'sexe', 'tel', 'fax', 'datenaiss',
'TEST', 'promo', 'travaille', 'evaluee', 'concerne'])
def test_object_relations(self):
"""check object relations a returned in the same order as in the
......@@ -404,7 +401,7 @@ class SchemaTC(BaseSchemaTC):
self.failIf(eperson is pschema['Person'])
self.failUnlessEqual(eperson, pschema['Person'])
self.failUnlessEqual('Person', pschema['Person'])
self.failUnlessEqual(eperson.subject_relations(), pschema['Person'].subject_relations())
self.failUnlessEqual(eperson.ordered_relations(), pschema['Person'].ordered_relations())
self.failUnlessEqual(eperson.object_relations(), pschema['Person'].object_relations())
......
"""
unit tests for module yams.schema2sql
Copyright Logilab 2002-2005, all rights reserved.
http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""unit tests for module yams.schema2sql
"""
from cStringIO import StringIO
from logilab.common.testlib import TestCase, unittest_main
from logilab.common.db import get_adv_func_helper
from logilab.common.adbh import get_adv_func_helper
from yams import SchemaLoader
from yams.schema2sql import schema2sql
......@@ -37,7 +33,7 @@ CREATE TABLE Affaire(
ref varchar(12),
inline_rel integer
);
CREATE INDEX affaire_inline_rel_idx ON Affaire (inline_rel);
CREATE INDEX affaire_inline_rel_idx ON Affaire(inline_rel);
CREATE TABLE Company(
name text
......@@ -63,8 +59,8 @@ CREATE TABLE Eetype(
final boolean,
initial_state integer
);
CREATE INDEX eetype_name_idx ON Eetype (name);
CREATE INDEX eetype_initial_state_idx ON Eetype (initial_state);
CREATE INDEX eetype_name_idx ON Eetype(name);
CREATE INDEX eetype_initial_state_idx ON Eetype(initial_state);
CREATE TABLE Employee(
);
......@@ -109,7 +105,7 @@ CREATE TABLE State(
name varchar(256) NOT NULL,
description text
);
CREATE INDEX state_name_idx ON State (name);
CREATE INDEX state_name_idx ON State(name);
CREATE TABLE pkginfo(
modname varchar(30) DEFAULT 'yo' NOT NULL,
......@@ -130,100 +126,82 @@ CREATE TABLE concerne_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT concerne_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT concerne_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT concerne_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX concerne_relation_from_idx ON concerne_relation (eid_from);
CREATE INDEX concerne_relation_to_idx ON concerne_relation (eid_to);
CREATE INDEX concerne_relation_from_idx ON concerne_relation(eid_from);
CREATE INDEX concerne_relation_to_idx ON concerne_relation(eid_to);
CREATE TABLE evaluee_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT evaluee_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT evaluee_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT evaluee_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX evaluee_relation_from_idx ON evaluee_relation (eid_from);
CREATE INDEX evaluee_relation_to_idx ON evaluee_relation (eid_to);
CREATE INDEX evaluee_relation_from_idx ON evaluee_relation(eid_from);
CREATE INDEX evaluee_relation_to_idx ON evaluee_relation(eid_to);
CREATE TABLE next_state_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT next_state_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT next_state_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT next_state_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX next_state_relation_from_idx ON next_state_relation (eid_from);
CREATE INDEX next_state_relation_to_idx ON next_state_relation (eid_to);
CREATE INDEX next_state_relation_from_idx ON next_state_relation(eid_from);
CREATE INDEX next_state_relation_to_idx ON next_state_relation(eid_to);
CREATE TABLE obj_wildcard_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT obj_wildcard_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT obj_wildcard_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT obj_wildcard_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX obj_wildcard_relation_from_idx ON obj_wildcard_relation (eid_from);
CREATE INDEX obj_wildcard_relation_to_idx ON obj_wildcard_relation (eid_to);
CREATE INDEX obj_wildcard_relation_from_idx ON obj_wildcard_relation(eid_from);
CREATE INDEX obj_wildcard_relation_to_idx ON obj_wildcard_relation(eid_to);
CREATE TABLE state_of_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT state_of_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT state_of_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT state_of_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX state_of_relation_from_idx ON state_of_relation (eid_from);
CREATE INDEX state_of_relation_to_idx ON state_of_relation (eid_to);
CREATE INDEX state_of_relation_from_idx ON state_of_relation(eid_from);
CREATE INDEX state_of_relation_to_idx ON state_of_relation(eid_to);
CREATE TABLE subj_wildcard_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT subj_wildcard_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT subj_wildcard_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT subj_wildcard_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX subj_wildcard_relation_from_idx ON subj_wildcard_relation (eid_from);
CREATE INDEX subj_wildcard_relation_to_idx ON subj_wildcard_relation (eid_to);
CREATE INDEX subj_wildcard_relation_from_idx ON subj_wildcard_relation(eid_from);
CREATE INDEX subj_wildcard_relation_to_idx ON subj_wildcard_relation(eid_to);
CREATE TABLE sym_rel_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT sym_rel_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT sym_rel_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT sym_rel_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX sym_rel_relation_from_idx ON sym_rel_relation (eid_from);
CREATE INDEX sym_rel_relation_to_idx ON sym_rel_relation (eid_to);
CREATE INDEX sym_rel_relation_from_idx ON sym_rel_relation(eid_from);
CREATE INDEX sym_rel_relation_to_idx ON sym_rel_relation(eid_to);
CREATE TABLE travaille_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT travaille_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT travaille_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT travaille_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX travaille_relation_from_idx ON travaille_relation (eid_from);
CREATE INDEX travaille_relation_to_idx ON travaille_relation (eid_to);
CREATE INDEX travaille_relation_from_idx ON travaille_relation(eid_from);
CREATE INDEX travaille_relation_to_idx ON travaille_relation(eid_to);
CREATE TABLE works_for_relation (
eid_from INTEGER NOT NULL,
eid_to INTEGER NOT NULL,
CONSTRAINT works_for_relation_p_key PRIMARY KEY(eid_from, eid_to),
CONSTRAINT works_for_relation_fkey1 FOREIGN KEY (eid_from) REFERENCES entities (eid) ON DELETE CASCADE,
CONSTRAINT works_for_relation_fkey2 FOREIGN KEY (eid_to) REFERENCES entities (eid) ON DELETE CASCADE
);
CREATE INDEX works_for_relation_from_idx ON works_for_relation (eid_from);
CREATE INDEX works_for_relation_to_idx ON works_for_relation (eid_to);
CREATE INDEX works_for_relation_from_idx ON works_for_relation(eid_from);
CREATE INDEX works_for_relation_to_idx ON works_for_relation(eid_to);
"""
class SQLSchemaTC(TestCase):
......@@ -231,7 +209,7 @@ class SQLSchemaTC(TestCase):
def test_known_values(self):
dbhelper = get_adv_func_helper('postgres')
output = schema2sql(dbhelper, schema)
self.assertTextEquals(output.strip(), EXPECTED_DATA_NO_DROP.strip())
self.assertTextEquals(EXPECTED_DATA_NO_DROP.strip(), output.strip())
if __name__ == '__main__':
......
"""unit tests for module yams.reader
Copyright Logilab 2003-2006, all rights reserved.
Copyright Logilab 2003-2008, all rights reserved.
"""
from logilab.common.testlib import TestCase, unittest_main
......@@ -8,6 +8,7 @@ from logilab.common.compat import sorted
from mx.DateTime import now, today, Date, DateTimeType, Time, DateTimeDeltaType
from yams import BadSchemaDefinition
from yams.schema import Schema, EntitySchema
from yams.reader import SchemaLoader, RelationFileReader
from yams.constraints import StaticVocabularyConstraint, SizeConstraint
......@@ -222,13 +223,13 @@ class SchemaLoaderTC(TestCase):
def test_physical_mode(self):
rschema = schema.rschema('evaluee')
self.assertEquals(rschema.physical_mode(), None)
self.assertEquals(rschema.inlined, False)
rschema = schema.rschema('state_of')
self.assertEquals(rschema.inlined, False)
rschema = schema.rschema('inline_rel')
self.assertEquals(rschema.physical_mode(), 'subjectinline')
self.assertEquals(rschema.inlined, True)
rschema = schema.rschema('initial_state')
self.assertEquals(rschema.physical_mode(), 'subjectinline')
rschema = schema.rschema('state_of')
self.assertEquals(rschema.physical_mode(), None)
self.assertEquals(rschema.inlined, True)
......@@ -362,5 +363,41 @@ class PySchemaTC(TestCase):
self.failUnless(isinstance(t2, DateTimeDeltaType))
class SchemaLoaderTC2(TestCase):
def test_broken_schema1(self):
SchemaLoader.main_schema_directory = 'brokenschema1'
ex = self.assertRaises(BadSchemaDefinition,
SchemaLoader().load, [DATADIR], 'Test', DummyDefaultHandler())
self.assertEquals(str(ex), '')
def test_broken_schema2(self):
SchemaLoader.main_schema_directory = 'brokenschema2'
ex = self.assertRaises(BadSchemaDefinition,
SchemaLoader().load, [DATADIR], 'Test', DummyDefaultHandler())
self.assertEquals(str(ex), '')
def test_broken_schema2(self):
SchemaLoader.main_schema_directory = 'brokenschema2'
ex = self.assertRaises(BadSchemaDefinition,
SchemaLoader().load, [DATADIR], 'Test', DummyDefaultHandler())
self.assertEquals(str(ex), '')
def test_schema(self):
SchemaLoader.main_schema_directory = 'schema2'
schema = SchemaLoader().load([DATADIR], 'Test', DummyDefaultHandler())
rel = schema['rel']
self.assertEquals(rel.rproperty('Anentity', 'Anentity', 'composite'),
'subject')
self.assertEquals(rel.rproperty('Anotherentity', 'Anentity', 'composite'),
'subject')
self.assertEquals(rel.rproperty('Anentity', 'Anentity', 'cardinality'),
'1*')
self.assertEquals(rel.rproperty('Anotherentity', 'Anentity', 'cardinality'),
'1*')
self.assertEquals(rel.symetric, True)
self.assertEquals(rel.inlined, True)
self.assertEquals(rel.meta, False)
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