def convert_defaultval(cwattr, default): from decimal import Decimal import yams from cubicweb import Binary if default is None: return if isinstance(default, Binary): # partially migrated instance, try to be idempotent return default atype = cwattr.to_entity[0].name if atype == 'Boolean': # boolean attributes with default=False were stored as '' assert default in ('True', 'False', ''), repr(default) default = default == 'True' elif atype in ('Int', 'BigInt'): default = int(default) elif atype == 'Float': default = float(default) elif atype == 'Decimal': default = Decimal(default) elif atype in ('Date', 'Datetime', 'TZDatetime', 'Time'): try: # handle NOW and TODAY, keep them stored as strings yams.KEYWORD_MAP[atype][default.upper()] default = default.upper() except KeyError: # otherwise get an actual date or datetime default = yams.DATE_FACTORY_MAP[atype](default) else: assert atype == 'String', atype return Binary.zpickle(default)
def test_rdef2rql(self): self.assertListEqual([ ('INSERT CWAttribute X: X cardinality %(cardinality)s,X defaultval %(defaultval)s,' 'X description %(description)s,X formula %(formula)s,X fulltextindexed %(fulltextindexed)s,' 'X indexed %(indexed)s,X internationalizable %(internationalizable)s,' 'X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,' 'X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s', {'se': None, 'rt': None, 'oe': None, 'description': u'', 'internationalizable': True, 'fulltextindexed': False, 'ordernum': 3, 'defaultval': Binary.zpickle(u'text/plain'), 'indexed': False, 'formula': None, 'cardinality': u'?1'}), ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X ' 'WHERE CT eid %(ct)s, EDEF eid %(x)s', {'x': None, 'value': u'{"msg": null, "values": ["text/rest", "text/markdown", ' '"text/html", "text/plain"]}', 'ct': 'FormatConstraint_eid'}), ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X ' 'WHERE CT eid %(ct)s, EDEF eid %(x)s', {'x': None, 'value': u'{"max": 50, "min": null, "msg": null}', 'ct': 'SizeConstraint_eid'})], list(rdef2rql(schema['description_format'].rdefs[('CWRType', 'String')], cstrtypemap)))
def test_add_attribute_to_base_class(self): with self.admin_access.repo_cnx() as cnx: attreid = cnx.execute( 'INSERT CWAttribute X: X cardinality "11", X defaultval %(default)s, ' 'X indexed TRUE, X relation_type RT, X from_entity E, X to_entity F ' 'WHERE RT name "messageid", E name "BaseTransition", F name "String"', {'default': Binary.zpickle('noname')})[0][0] assert cnx.execute( 'SET X read_permission Y WHERE X eid %(x)s, Y name "managers"', {'x': attreid}) cnx.commit() self.schema.rebuild_infered_relations() self.assertIn('Transition', self.schema['messageid'].subjects()) self.assertIn('WorkflowTransition', self.schema['messageid'].subjects()) cnx.execute( 'Any X WHERE X is_instance_of BaseTransition, X messageid "hop"' )
def _rdef_values(rdef): amap = {'order': 'ordernum', 'default': 'defaultval'} values = {} extra = {} for prop in rdef.rproperty_defs(rdef.object): if prop in _IGNORED_PROPS: continue value = getattr(rdef, prop) if prop not in KNOWN_RPROPERTIES: extra[prop] = value continue # XXX type cast really necessary? if prop in ('indexed', 'fulltextindexed', 'internationalizable'): value = bool(value) elif prop == 'ordernum': value = int(value) if value is not None and prop == 'default': value = Binary.zpickle(value) values[amap.get(prop, prop)] = value if extra: values['extra_props'] = Binary(json.dumps(extra).encode('ascii')) relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] return relations, values
def test_gzpickle_roundtrip(self): old = (u'foo', b'bar', 42, {}) new = Binary.zpickle(old).unzpickle() self.assertEqual(old, new) self.assertIsNot(old, new)
def test_base(self): with self.admin_access.repo_cnx() as cnx: schema = self.repo.schema self.assertFalse(schema.has_entity('Societe2')) self.assertFalse(schema.has_entity('concerne2')) # schema should be update on insertion (after commit) eeid = cnx.execute('INSERT CWEType X: X name "Societe2", ' 'X description "", X final FALSE')[0][0] self._set_perms(cnx, eeid) cnx.execute( 'INSERT CWRType X: X name "concerne2", X description "", ' 'X final FALSE, X symmetric FALSE') self.assertFalse(schema.has_entity('Societe2')) self.assertFalse(schema.has_entity('concerne2')) # have to commit before adding definition relations cnx.commit() self.assertTrue(schema.has_entity('Societe2')) self.assertTrue(schema.has_relation('concerne2')) attreid = cnx.execute( 'INSERT CWAttribute X: X cardinality "11", ' 'X defaultval %(default)s, X indexed TRUE, ' 'X relation_type RT, X from_entity E, X to_entity F ' 'WHERE RT name "name", E name "Societe2", ' 'F name "String"', {'default': Binary.zpickle('noname')})[0][0] self._set_attr_perms(cnx, attreid) concerne2_rdef_eid = cnx.execute( 'INSERT CWRelation X: X cardinality "**", X relation_type RT, ' 'X from_entity E, X to_entity E ' 'WHERE RT name "concerne2", E name "Societe2"')[0][0] self._set_perms(cnx, concerne2_rdef_eid) self.assertNotIn('name', schema['Societe2'].subject_relations()) self.assertNotIn('concerne2', schema['Societe2'].subject_relations()) self.assertFalse(self.index_exists(cnx, 'Societe2', 'name')) cnx.commit() self.assertIn('name', schema['Societe2'].subject_relations()) self.assertIn('concerne2', schema['Societe2'].subject_relations()) self.assertTrue(self.index_exists(cnx, 'Societe2', 'name')) # now we should be able to insert and query Societe2 s2eid = cnx.execute('INSERT Societe2 X: X name "logilab"')[0][0] cnx.execute('Societe2 X WHERE X name "logilab"') cnx.execute('SET X concerne2 X WHERE X name "logilab"') rset = cnx.execute('Any X WHERE X concerne2 Y') self.assertEqual(rset.rows, [[s2eid]]) # check that when a relation definition is deleted, existing relations are deleted rdefeid = cnx.execute( 'INSERT CWRelation X: X cardinality "**", X relation_type RT, ' ' X from_entity E, X to_entity E ' 'WHERE RT name "concerne2", E name "CWUser"')[0][0] self._set_perms(cnx, rdefeid) cnx.commit() cnx.execute('DELETE CWRelation X WHERE X eid %(x)s', {'x': concerne2_rdef_eid}) cnx.commit() self.assertIn('concerne2', schema['CWUser'].subject_relations()) self.assertNotIn('concerne2', schema['Societe2'].subject_relations()) self.assertFalse(cnx.execute('Any X WHERE X concerne2 Y')) # schema should be cleaned on delete (after commit) cnx.execute('DELETE CWEType X WHERE X name "Societe2"') cnx.execute('DELETE CWRType X WHERE X name "concerne2"') self.assertTrue(self.index_exists(cnx, 'Societe2', 'name')) self.assertTrue(schema.has_entity('Societe2')) self.assertTrue(schema.has_relation('concerne2')) cnx.commit() self.assertFalse(self.index_exists(cnx, 'Societe2', 'name')) self.assertFalse(schema.has_entity('Societe2')) self.assertFalse(schema.has_entity('concerne2')) self.assertNotIn('concerne2', schema['CWUser'].subject_relations())